mcconf.py 文件源码

python
阅读 34 收藏 0 点赞 0 评论 0

项目:mcconf 作者: ManyThreads 项目源码 文件源码
def install(self, tgtdir, tmplenv):
        """install the file into the target directory."""
        srcfile = os.path.abspath(self.srcfile)
        tgtfile = os.path.abspath(os.path.join(tgtdir, self.dstfile))

        logging.debug('installing file %s to %s mode %s from module %s',
                          srcfile, tgtfile, self.installMode, self.module)
        if not os.path.isfile(srcfile):
            logging.warning('file %s is missing or not regular file, provided by %s from %s',
                            self.srcfile, self.module, self.module.modulefile)

        if not os.path.exists(os.path.dirname(tgtfile)):
            os.makedirs(os.path.dirname(tgtfile))
        # TODO ideally we should never overwrite files, reconfig run should delete previously installed files
        if os.path.exists(tgtfile) or os.path.islink(tgtfile):
            os.unlink(tgtfile)

        if self.installMode=='link':
            os.symlink(os.path.relpath(srcfile, os.path.dirname(tgtfile)), tgtfile)
        elif self.installMode=='hardlink':
            os.link(srcfile, tgtfile)
        elif self.installMode=='cinclude':
            with open(tgtfile, 'w') as f:
                f.write('#include "'+os.path.relpath(srcfile, tgtfile)+'"\n')
        elif self.installMode=='mako':
            with open(tgtfile, 'w') as f:
                tmpl = mako.template.Template(filename=self.srcfile,
                         imports=['import os'])
                ctx = mako.runtime.Context(f, **tmplenv)
                tmpl.render_context(ctx)
            shutil.copymode(srcfile, tgtfile)
        else: # copy the file
            shutil.copy2(srcfile, tgtfile)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号