mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-05 13:48:48 +00:00
better provide template variables as global in templategen
This commit is contained in:
@@ -81,8 +81,8 @@ def install(opts, conf):
|
||||
msg = 'no dotfiles defined for this profile (\"{}\")'
|
||||
LOG.err(msg.format(opts['profile']))
|
||||
return False
|
||||
t = Templategen(base=opts['dotpath'], variables=opts['variables'],
|
||||
debug=opts['debug'])
|
||||
t = Templategen(opts['profile'], base=opts['dotpath'],
|
||||
variables=opts['variables'], debug=opts['debug'])
|
||||
inst = Installer(create=opts['create'], backup=opts['backup'],
|
||||
dry=opts['dry'], safe=opts['safe'], base=opts['dotpath'],
|
||||
diff=opts['installdiff'], debug=opts['debug'])
|
||||
@@ -108,7 +108,7 @@ def install(opts, conf):
|
||||
if not tmp:
|
||||
continue
|
||||
src = tmp
|
||||
r = inst.install(t, opts['profile'], src, dotfile.dst)
|
||||
r = inst.install(t, src, dotfile.dst)
|
||||
if tmp:
|
||||
tmp = os.path.join(opts['dotpath'], tmp)
|
||||
if os.path.exists(tmp):
|
||||
@@ -185,8 +185,8 @@ def compare(opts, conf, tmp, focus=None, ignore=[]):
|
||||
if len(selected) < 1:
|
||||
return False
|
||||
|
||||
t = Templategen(base=opts['dotpath'], variables=opts['variables'],
|
||||
debug=opts['debug'])
|
||||
t = Templategen(opts['profile'], base=opts['dotpath'],
|
||||
variables=opts['variables'], debug=opts['debug'])
|
||||
inst = Installer(create=opts['create'], backup=opts['backup'],
|
||||
dry=opts['dry'], base=opts['dotpath'],
|
||||
debug=opts['debug'])
|
||||
@@ -209,8 +209,7 @@ def compare(opts, conf, tmp, focus=None, ignore=[]):
|
||||
continue
|
||||
src = tmpsrc
|
||||
# install dotfile to temporary dir
|
||||
ret, insttmp = inst.install_to_temp(t, tmp, opts['profile'],
|
||||
src, dotfile.dst)
|
||||
ret, insttmp = inst.install_to_temp(t, tmp, src, dotfile.dst)
|
||||
if not ret:
|
||||
# failed to install to tmp
|
||||
continue
|
||||
|
||||
@@ -29,7 +29,7 @@ class Installer:
|
||||
self.comparing = False
|
||||
self.log = Logger()
|
||||
|
||||
def install(self, templater, profile, src, dst):
|
||||
def install(self, templater, src, dst):
|
||||
"""install the src to dst using a template"""
|
||||
src = os.path.join(self.base, os.path.expanduser(src))
|
||||
dst = os.path.join(self.base, os.path.expanduser(dst))
|
||||
@@ -40,8 +40,8 @@ class Installer:
|
||||
if self.debug:
|
||||
self.log.dbg('install {} to {}'.format(src, dst))
|
||||
if os.path.isdir(src):
|
||||
return self._handle_dir(templater, profile, src, dst)
|
||||
return self._handle_file(templater, profile, src, dst)
|
||||
return self._handle_dir(templater, src, dst)
|
||||
return self._handle_file(templater, src, dst)
|
||||
|
||||
def link(self, src, dst):
|
||||
"""set src as the link target of dst"""
|
||||
@@ -76,7 +76,7 @@ class Installer:
|
||||
self.log.sub('linked {} to {}'.format(dst, src))
|
||||
return [(src, dst)]
|
||||
|
||||
def _handle_file(self, templater, profile, src, dst):
|
||||
def _handle_file(self, templater, src, dst):
|
||||
"""install src to dst when is a file"""
|
||||
if self.debug:
|
||||
self.log.dbg('generate template for {}'.format(src))
|
||||
@@ -84,7 +84,7 @@ class Installer:
|
||||
# symlink loop
|
||||
self.log.err('dotfile points to itself: {}'.format(dst))
|
||||
return []
|
||||
content = templater.generate(src, profile)
|
||||
content = templater.generate(src)
|
||||
if content is None:
|
||||
self.log.err('generate from template \"{}\"'.format(src))
|
||||
return []
|
||||
@@ -106,7 +106,7 @@ class Installer:
|
||||
return [(src, dst)]
|
||||
return []
|
||||
|
||||
def _handle_dir(self, templater, profile, src, dst):
|
||||
def _handle_dir(self, templater, src, dst):
|
||||
"""install src to dst when is a directory"""
|
||||
ret = []
|
||||
self._create_dirs(dst)
|
||||
@@ -114,12 +114,10 @@ class Installer:
|
||||
for entry in os.listdir(src):
|
||||
f = os.path.join(src, entry)
|
||||
if not os.path.isdir(f):
|
||||
res = self._handle_file(
|
||||
templater, profile, f, os.path.join(dst, entry))
|
||||
res = self._handle_file(templater, f, os.path.join(dst, entry))
|
||||
ret.extend(res)
|
||||
else:
|
||||
res = self._handle_dir(
|
||||
templater, profile, f, os.path.join(dst, entry))
|
||||
res = self._handle_dir(templater, f, os.path.join(dst, entry))
|
||||
ret.extend(res)
|
||||
return ret
|
||||
|
||||
@@ -186,15 +184,15 @@ class Installer:
|
||||
self.log.log('backup {} to {}'.format(path, dst))
|
||||
os.rename(path, dst)
|
||||
|
||||
def _install_to_temp(self, templater, profile, src, dst, tmpdir):
|
||||
def _install_to_temp(self, templater, src, dst, tmpdir):
|
||||
"""install a dotfile to a tempdir for comparing"""
|
||||
sub = dst
|
||||
if dst[0] == os.sep:
|
||||
sub = dst[1:]
|
||||
tmpdst = os.path.join(tmpdir, sub)
|
||||
return self.install(templater, profile, src, tmpdst), tmpdst
|
||||
return self.install(templater, src, tmpdst), tmpdst
|
||||
|
||||
def install_to_temp(self, templater, tmpdir, profile, src, dst):
|
||||
def install_to_temp(self, templater, tmpdir, src, dst):
|
||||
"""compare a temporary generated dotfile with the local one"""
|
||||
ret = False
|
||||
tmpdst = ''
|
||||
@@ -212,8 +210,7 @@ class Installer:
|
||||
if self.debug:
|
||||
self.log.dbg('tmp install {} to {}'.format(src, dst))
|
||||
# install the dotfile to a temp directory for comparing
|
||||
ret, tmpdst = self._install_to_temp(templater, profile,
|
||||
src, dst, tmpdir)
|
||||
ret, tmpdst = self._install_to_temp(templater, src, dst, tmpdir)
|
||||
if self.debug:
|
||||
self.log.dbg('tmp installed in {}'.format(tmpdst))
|
||||
# reset flags
|
||||
|
||||
@@ -22,7 +22,7 @@ COMMENT_END = '@@#}'
|
||||
|
||||
class Templategen:
|
||||
|
||||
def __init__(self, base='.', variables={}, debug=False):
|
||||
def __init__(self, profile, base='.', variables={}, debug=False):
|
||||
self.base = base.rstrip(os.sep)
|
||||
self.debug = debug
|
||||
loader = FileSystemLoader(self.base)
|
||||
@@ -36,19 +36,21 @@ class Templategen:
|
||||
comment_start_string=COMMENT_START,
|
||||
comment_end_string=COMMENT_END)
|
||||
self.env.globals['header'] = self._header
|
||||
self.env.globals['env'] = os.environ
|
||||
self.env.globals['profile'] = profile
|
||||
self.env.globals.update(variables)
|
||||
self.log = Logger()
|
||||
|
||||
def generate(self, src, profile):
|
||||
def generate(self, src):
|
||||
if not os.path.exists(src):
|
||||
return ''
|
||||
return self._handle_file(src, profile)
|
||||
return self._handle_file(src)
|
||||
|
||||
def _header(self, prepend=''):
|
||||
"""add a comment usually in the header of a dotfile"""
|
||||
return '{}{}'.format(prepend, utils.header())
|
||||
|
||||
def _handle_file(self, src, profile):
|
||||
def _handle_file(self, src):
|
||||
"""generate the file content from template"""
|
||||
filetype = utils.run(['file', '-b', src], raw=False, debug=self.debug)
|
||||
filetype = filetype.strip()
|
||||
@@ -58,24 +60,24 @@ class Templategen:
|
||||
if self.debug:
|
||||
self.log.dbg('\"{}\" is text: {}'.format(src, istext))
|
||||
if not istext:
|
||||
return self._handle_bin_file(src, profile)
|
||||
return self._handle_text_file(src, profile)
|
||||
return self._handle_bin_file(src)
|
||||
return self._handle_text_file(src)
|
||||
|
||||
def _handle_text_file(self, src, profile):
|
||||
def _handle_text_file(self, src):
|
||||
"""write text to file"""
|
||||
template_rel_path = os.path.relpath(src, self.base)
|
||||
try:
|
||||
template = self.env.get_template(template_rel_path)
|
||||
content = template.render(profile=profile, env=os.environ)
|
||||
content = template.render()
|
||||
except UnicodeDecodeError:
|
||||
data = self._read_bad_encoded_text(src)
|
||||
template = self.env.from_string(data)
|
||||
content = template.render(profile=profile, env=os.environ)
|
||||
content = template.render()
|
||||
|
||||
content = content.encode('UTF-8')
|
||||
return content
|
||||
|
||||
def _handle_bin_file(self, src, profile):
|
||||
def _handle_bin_file(self, src):
|
||||
"""write binary to file"""
|
||||
# this is dirty
|
||||
if not src.startswith(self.base):
|
||||
|
||||
@@ -30,14 +30,14 @@ class TestCompare(unittest.TestCase):
|
||||
def compare(self, opts, conf, tmp, nbdotfiles):
|
||||
dotfiles = conf.get_dotfiles(opts['profile'])
|
||||
self.assertTrue(len(dotfiles) == nbdotfiles)
|
||||
t = Templategen(base=opts['dotpath'], debug=True)
|
||||
t = Templategen(opts['profile'], base=opts['dotpath'], debug=True)
|
||||
inst = Installer(create=opts['create'], backup=opts['backup'],
|
||||
dry=opts['dry'], base=opts['dotpath'], debug=True)
|
||||
comp = Comparator()
|
||||
results = {}
|
||||
for dotfile in dotfiles:
|
||||
ret, insttmp = inst.install_to_temp(t, tmp, opts['profile'],
|
||||
dotfile.src, dotfile.dst)
|
||||
ret, insttmp = inst.install_to_temp(t, tmp, dotfile.src,
|
||||
dotfile.dst)
|
||||
if not ret:
|
||||
results[path] = False
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user