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)
评论列表
文章目录