1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-09 05:59:15 +00:00
This commit is contained in:
deadc0de6
2021-04-30 20:01:02 +02:00
parent 5db5d51fb2
commit cb71bf299f
7 changed files with 532 additions and 480 deletions

View File

@@ -3,6 +3,7 @@ author: deadc0de6 (https://github.com/deadc0de6)
Copyright (c) 2017, deadc0de6 Copyright (c) 2017, deadc0de6
""" """
# pylint: disable=C0415
import sys import sys

View File

@@ -36,6 +36,7 @@ from dotdrop.exceptions import YamlException, UndefinedException
class CfgYaml: class CfgYaml:
"""yaml config file parser"""
# global entries # global entries
key_settings = Settings.key_yaml key_settings = Settings.key_yaml
@@ -437,7 +438,6 @@ class CfgYaml:
self._log.warn(warn) self._log.warn(warn)
self._dirty = False self._dirty = False
self.cfg_updated = False
return True return True
def dump(self): def dump(self):
@@ -464,20 +464,20 @@ class CfgYaml:
self._check_minversion(minversion) self._check_minversion(minversion)
# normalize paths # normalize paths
p = self._norm_path(settings[self.key_settings_dotpath]) paths = self._norm_path(settings[self.key_settings_dotpath])
settings[self.key_settings_dotpath] = p settings[self.key_settings_dotpath] = paths
p = self._norm_path(settings[self.key_settings_workdir]) paths = self._norm_path(settings[self.key_settings_workdir])
settings[self.key_settings_workdir] = p settings[self.key_settings_workdir] = paths
p = [ paths = [
self._norm_path(p) self._norm_path(path)
for p in settings[Settings.key_filter_file] for path in settings[Settings.key_filter_file]
] ]
settings[Settings.key_filter_file] = p settings[Settings.key_filter_file] = paths
p = [ paths = [
self._norm_path(p) self._norm_path(path)
for p in settings[Settings.key_func_file] for path in settings[Settings.key_func_file]
] ]
settings[Settings.key_func_file] = p settings[Settings.key_func_file] = paths
if self._debug: if self._debug:
self._debug_dict('settings block:', settings) self._debug_dict('settings block:', settings)
return settings return settings
@@ -1166,11 +1166,11 @@ class CfgYaml:
new = [] new = []
if not entries: if not entries:
return new return new
for e in entries: for entry in entries:
et = self._template_item(e) newe = self._template_item(entry)
if self._debug and e != et: if self._debug and entry != newe:
self._dbg('resolved: {} -> {}'.format(e, et)) self._dbg('resolved: {} -> {}'.format(entry, newe))
new.append(et) new.append(newe)
return new return new
def _template_dict(self, entries): def _template_dict(self, entries):
@@ -1178,11 +1178,11 @@ class CfgYaml:
new = {} new = {}
if not entries: if not entries:
return new return new
for k, v in entries.items(): for k, val in entries.items():
vt = self._template_item(v) newv = self._template_item(val)
if self._debug and v != vt: if self._debug and val != newv:
self._dbg('resolved: {} -> {}'.format(v, vt)) self._dbg('resolved: {} -> {}'.format(val, newv))
new[k] = vt new[k] = newv
return new return new
def _template_dotfiles_entries(self): def _template_dotfiles_entries(self):
@@ -1226,9 +1226,9 @@ class CfgYaml:
if self.key_all not in pdfs: if self.key_all not in pdfs:
# take a subset of the dotfiles # take a subset of the dotfiles
newdotfiles = {} newdotfiles = {}
for k, v in dotfiles.items(): for k, val in dotfiles.items():
if k in pdfs: if k in pdfs:
newdotfiles[k] = v newdotfiles[k] = val
dotfiles = newdotfiles dotfiles = newdotfiles
for dotfile in dotfiles.values(): for dotfile in dotfiles.values():
@@ -1246,26 +1246,29 @@ class CfgYaml:
var = self._enrich_vars(variables, self._profile) var = self._enrich_vars(variables, self._profile)
# use a separated templategen to handle variables # use a separated templategen to handle variables
# resolved outside the main config # resolved outside the main config
t = Templategen(variables=var, func_files = self.settings[Settings.key_func_file]
func_file=self.settings[Settings.key_func_file], filter_files = self.settings[Settings.key_filter_file]
filter_file=self.settings[Settings.key_filter_file]) templ = Templategen(variables=var,
func_file=func_files,
filter_file=filter_files)
for k in variables.keys(): for k in variables.keys():
val = variables[k] val = variables[k]
while Templategen.var_is_template(val): while Templategen.var_is_template(val):
val = t.generate_string(val) val = templ.generate_string(val)
variables[k] = val variables[k] = val
t.update_variables(variables) templ.update_variables(variables)
if variables is self.variables: if variables is self.variables:
self._redefine_templater() self._redefine_templater()
def _get_profile_included_vars(self): def _get_profile_included_vars(self):
"""resolve profile included variables/dynvariables""" """resolve profile included variables/dynvariables"""
for k, v in self.profiles.items(): for _, val in self.profiles.items():
if self.key_profile_include in v and v[self.key_profile_include]: if self.key_profile_include in val and \
val[self.key_profile_include]:
new = [] new = []
for x in v[self.key_profile_include]: for entry in val[self.key_profile_include]:
new.append(self._tmpl.generate_string(x)) new.append(self._tmpl.generate_string(entry))
v[self.key_profile_include] = new val[self.key_profile_include] = new
# now get the included ones # now get the included ones
pro_var = self._get_profile_included_item(self.key_profile_variables) pro_var = self._get_profile_included_item(self.key_profile_variables)
@@ -1291,7 +1294,8 @@ class CfgYaml:
""" """
if not dic: if not dic:
return return
[dic.pop(k, None) for k in self._profilevarskeys] for k in self._profilevarskeys:
dic.pop(k, None)
def _parse_extended_import_path(self, path_entry): def _parse_extended_import_path(self, path_entry):
"""Parse an import path in a tuple (path, fatal_not_found).""" """Parse an import path in a tuple (path, fatal_not_found)."""
@@ -1369,7 +1373,8 @@ class CfgYaml:
processed_paths = (self._process_path(p) for p in paths) processed_paths = (self._process_path(p) for p in paths)
return list(chain.from_iterable(processed_paths)) return list(chain.from_iterable(processed_paths))
def _merge_dict(self, high, low): @classmethod
def _merge_dict(cls, high, low):
"""merge high and low dict""" """merge high and low dict"""
if not high: if not high:
high = {} high = {}
@@ -1377,7 +1382,8 @@ class CfgYaml:
low = {} low = {}
return {**low, **high} return {**low, **high}
def _get_entry(self, dic, key, mandatory=True): @classmethod
def _get_entry(cls, dic, key, mandatory=True):
"""return copy of entry from yaml dictionary""" """return copy of entry from yaml dictionary"""
if key not in dic: if key not in dic:
if mandatory: if mandatory:
@@ -1393,19 +1399,19 @@ class CfgYaml:
def _clear_none(self, dic): def _clear_none(self, dic):
"""recursively delete all none/empty values in a dictionary.""" """recursively delete all none/empty values in a dictionary."""
new = {} new = {}
for k, v in dic.items(): for k, val in dic.items():
if k == self.key_dotfile_src: if k == self.key_dotfile_src:
# allow empty dotfile src # allow empty dotfile src
new[k] = v new[k] = val
continue continue
if k == self.key_dotfile_dst: if k == self.key_dotfile_dst:
# allow empty dotfile dst # allow empty dotfile dst
new[k] = v new[k] = val
continue continue
newv = v newv = val
if isinstance(v, dict): if isinstance(val, dict):
# recursive travers dict # recursive travers dict
newv = self._clear_none(v) newv = self._clear_none(val)
if not newv: if not newv:
# no empty dict # no empty dict
continue continue
@@ -1418,7 +1424,8 @@ class CfgYaml:
new[k] = newv new[k] = newv
return new return new
def _is_glob(self, path): @classmethod
def _is_glob(cls, path):
"""Quick test if path is a glob.""" """Quick test if path is a glob."""
return '*' in path or '?' in path return '*' in path or '?' in path
@@ -1435,8 +1442,8 @@ class CfgYaml:
return path return path
path = os.path.expanduser(path) path = os.path.expanduser(path)
if not os.path.isabs(path): if not os.path.isabs(path):
d = os.path.dirname(self._path) dirn = os.path.dirname(self._path)
ret = os.path.join(d, path) ret = os.path.join(dirn, path)
if self._debug: if self._debug:
msg = 'normalizing relative to cfg: {} -> {}' msg = 'normalizing relative to cfg: {} -> {}'
self._dbg(msg.format(path, ret)) self._dbg(msg.format(path, ret))
@@ -1446,30 +1453,31 @@ class CfgYaml:
self._dbg('normalizing: {} -> {}'.format(path, ret)) self._dbg('normalizing: {} -> {}'.format(path, ret))
return ret return ret
def _shell_exec_dvars(self, dic, keys=[]): def _shell_exec_dvars(self, dic, keys=None):
"""shell execute dynvariables in-place""" """shell execute dynvariables in-place"""
if not keys: if not keys:
keys = dic.keys() keys = dic.keys()
for k in keys: for k in keys:
v = dic[k] val = dic[k]
ret, out = shell(v, debug=self._debug) ret, out = shell(val, debug=self._debug)
if not ret: if not ret:
err = 'var \"{}: {}\" failed: {}'.format(k, v, out) err = 'var \"{}: {}\" failed: {}'.format(k, val, out)
self._log.err(err) self._log.err(err)
raise YamlException(err) raise YamlException(err)
if self._debug: if self._debug:
self._dbg('{}: `{}` -> {}'.format(k, v, out)) self._dbg('{}: `{}` -> {}'.format(k, val, out))
dic[k] = out dic[k] = out
def _check_minversion(self, minversion): @classmethod
def _check_minversion(cls, minversion):
if not minversion: if not minversion:
return return
try: try:
cur = tuple([int(x) for x in VERSION.split('.')]) cur = ([int(x) for x in VERSION.split('.')])
cfg = tuple([int(x) for x in minversion.split('.')]) cfg = ([int(x) for x in minversion.split('.')])
except Exception: except Exception as exc:
err = 'bad version: \"{}\" VS \"{}\"'.format(VERSION, minversion) err = 'bad version: \"{}\" VS \"{}\"'.format(VERSION, minversion)
raise YamlException(err) raise YamlException(err) from exc
if cur < cfg: if cur < cfg:
err = 'current dotdrop version is too old for that config file.' err = 'current dotdrop version is too old for that config file.'
err += ' Please update.' err += ' Please update.'
@@ -1495,8 +1503,8 @@ class CfgYaml:
self._dbg('{}:'.format(title)) self._dbg('{}:'.format(title))
if not elems: if not elems:
return return
for k, v in elems.items(): for k, val in elems.items():
self._dbg('\t- \"{}\": {}'.format(k, v)) self._dbg('\t- \"{}\": {}'.format(k, val))
def _dbg(self, content): def _dbg(self, content):
pre = os.path.basename(self._path) pre = os.path.basename(self._path)

File diff suppressed because it is too large Load Diff

View File

@@ -12,3 +12,7 @@ class YamlException(Exception):
class UndefinedException(Exception): class UndefinedException(Exception):
"""exception in templating""" """exception in templating"""
class UnmetDependency(Exception):
"""unmet dependency"""

View File

@@ -17,6 +17,7 @@ from dotdrop.exceptions import UndefinedException
class Installer: class Installer:
"""dotfile installer"""
def __init__(self, base='.', create=True, backup=True, def __init__(self, base='.', create=True, backup=True,
dry=False, safe=False, workdir='~/.config/dotdrop', dry=False, safe=False, workdir='~/.config/dotdrop',
@@ -65,7 +66,7 @@ class Installer:
def install(self, templater, src, dst, linktype, def install(self, templater, src, dst, linktype,
actionexec=None, noempty=False, actionexec=None, noempty=False,
ignore=[], is_template=True, ignore=None, is_template=True,
chmod=None, force_chmod=False): chmod=None, force_chmod=False):
""" """
install src to dst install src to dst
@@ -93,7 +94,7 @@ class Installer:
return True, None return True, None
msg = 'installing \"{}\" to \"{}\" (link: {})' msg = 'installing \"{}\" to \"{}\" (link: {})'
self.log.dbg(msg.format(src, dst, str(linktype))) self.log.dbg(msg.format(src, dst, str(linktype)))
src, dst, cont, err = self._check_paths(src, dst, chmod) src, dst, cont, err = self._check_paths(src, dst)
if not cont: if not cont:
return self._log_install(cont, err) return self._log_install(cont, err)
@@ -108,10 +109,13 @@ class Installer:
# install to temporary dir # install to temporary dir
# and ignore any actions # and ignore any actions
if self.totemp: if self.totemp:
r, err, _ = self.install_to_temp(templater, self.totemp, ret, err, _ = self.install_to_temp(templater,
src, dst, is_template=is_template, self.totemp,
chmod=chmod, ignore=ignore) src, dst,
return self._log_install(r, err) is_template=is_template,
chmod=chmod,
ignore=ignore)
return self._log_install(ret, err)
isdir = os.path.isdir(src) isdir = os.path.isdir(src)
self.log.dbg('install {} to {}'.format(src, dst)) self.log.dbg('install {} to {}'.format(src, dst))
@@ -120,39 +124,37 @@ class Installer:
if linktype == LinkTypes.NOLINK: if linktype == LinkTypes.NOLINK:
# normal file # normal file
if isdir: if isdir:
r, err = self._copy_dir(templater, src, dst, ret, err = self._copy_dir(templater, src, dst,
actionexec=actionexec, actionexec=actionexec,
noempty=noempty, ignore=ignore, noempty=noempty, ignore=ignore,
is_template=is_template, is_template=is_template)
chmod=chmod)
else: else:
r, err = self._copy_file(templater, src, dst, ret, err = self._copy_file(templater, src, dst,
actionexec=actionexec, actionexec=actionexec,
noempty=noempty, ignore=ignore, noempty=noempty, ignore=ignore,
is_template=is_template, is_template=is_template)
chmod=chmod)
elif linktype == LinkTypes.LINK: elif linktype == LinkTypes.LINK:
# symlink # symlink
r, err = self._link(templater, src, dst, ret, err = self._link(templater, src, dst,
actionexec=actionexec, actionexec=actionexec,
is_template=is_template) is_template=is_template)
elif linktype == LinkTypes.LINK_CHILDREN: elif linktype == LinkTypes.LINK_CHILDREN:
# symlink direct children # symlink direct children
if not isdir: if not isdir:
msg = 'symlink children of {} to {}' msg = 'symlink children of {} to {}'
self.log.dbg(msg.format(src, dst)) self.log.dbg(msg.format(src, dst))
err = 'source dotfile is not a directory: {}'.format(src) err = 'source dotfile is not a directory: {}'.format(src)
r = False ret = False
else: else:
r, err = self._link_children(templater, src, dst, ret, err = self._link_children(templater, src, dst,
actionexec=actionexec, actionexec=actionexec,
is_template=is_template, is_template=is_template,
ignore=ignore) ignore=ignore)
self.log.dbg('before chmod: {} err:{}'.format(r, err)) self.log.dbg('before chmod: {} err:{}'.format(ret, err))
if self.dry: if self.dry:
return self._log_install(r, err) return self._log_install(ret, err)
# handle chmod # handle chmod
# - on success (r, not err) # - on success (r, not err)
@@ -160,7 +162,7 @@ class Installer:
# but not when # but not when
# - error (not r, err) # - error (not r, err)
# - aborted (not r, err) # - aborted (not r, err)
if (r or (not r and not err)): if (ret or (not ret and not err)):
if not chmod: if not chmod:
chmod = utils.get_file_perm(src) chmod = utils.get_file_perm(src)
dstperms = utils.get_file_perm(dst) dstperms = utils.get_file_perm(dst)
@@ -168,21 +170,21 @@ class Installer:
# apply mode # apply mode
msg = 'chmod {} to {:o}'.format(dst, chmod) msg = 'chmod {} to {:o}'.format(dst, chmod)
if not force_chmod and self.safe and not self.log.ask(msg): if not force_chmod and self.safe and not self.log.ask(msg):
r = False ret = False
err = 'aborted' err = 'aborted'
else: else:
if not self.comparing: if not self.comparing:
self.log.sub('chmod {} to {:o}'.format(dst, chmod)) self.log.sub('chmod {} to {:o}'.format(dst, chmod))
if utils.chmod(dst, chmod, debug=self.debug): if utils.chmod(dst, chmod, debug=self.debug):
r = True ret = True
else: else:
r = False ret = False
err = 'chmod failed' err = 'chmod failed'
return self._log_install(r, err) return self._log_install(ret, err)
def install_to_temp(self, templater, tmpdir, src, dst, def install_to_temp(self, templater, tmpdir, src, dst,
is_template=True, chmod=None, ignore=[]): is_template=True, chmod=None, ignore=None):
""" """
install a dotfile to a tempdir install a dotfile to a tempdir
@@ -198,9 +200,10 @@ class Installer:
- success, error-if-any, dotfile-installed-path - success, error-if-any, dotfile-installed-path
""" """
self.log.dbg('tmp install {} (defined dst: {})'.format(src, dst)) self.log.dbg('tmp install {} (defined dst: {})'.format(src, dst))
src, dst, cont, err = self._check_paths(src, dst, chmod) src, dst, cont, err = self._check_paths(src, dst)
if not cont: if not cont:
return self._log_install(cont, err) self._log_install(cont, err)
return cont, err, None
ret = False ret = False
tmpdst = '' tmpdst = ''
@@ -253,18 +256,18 @@ class Installer:
self.log.dbg('is a template') self.log.dbg('is a template')
self.log.dbg('install to {}'.format(self.workdir)) self.log.dbg('install to {}'.format(self.workdir))
tmp = self._pivot_path(dst, self.workdir, striphome=True) tmp = self._pivot_path(dst, self.workdir, striphome=True)
r, err = self.install(templater, src, tmp, ret, err = self.install(templater, src, tmp,
LinkTypes.NOLINK, LinkTypes.NOLINK,
actionexec=actionexec, actionexec=actionexec,
is_template=is_template) is_template=is_template)
if not r and not os.path.exists(tmp): if not ret and not os.path.exists(tmp):
return r, err return ret, err
src = tmp src = tmp
r, err = self._symlink(src, dst, actionexec=actionexec) ret, err = self._symlink(src, dst, actionexec=actionexec)
return r, err return ret, err
def _link_children(self, templater, src, dst, def _link_children(self, templater, src, dst,
actionexec=None, is_template=True, ignore=[]): actionexec=None, is_template=True, ignore=None):
""" """
install link:link_children install link:link_children
@@ -318,11 +321,11 @@ class Installer:
self.log.dbg('install to {} and symlink' self.log.dbg('install to {} and symlink'
.format(self.workdir)) .format(self.workdir))
tmp = self._pivot_path(subdst, self.workdir, striphome=True) tmp = self._pivot_path(subdst, self.workdir, striphome=True)
r, e = self.install(templater, subsrc, tmp, ret2, err2 = self.install(templater, subsrc, tmp,
LinkTypes.NOLINK, LinkTypes.NOLINK,
actionexec=actionexec, actionexec=actionexec,
is_template=is_template) is_template=is_template)
if not r and e and not os.path.exists(tmp): if not ret2 and err2 and not os.path.exists(tmp):
continue continue
subsrc = tmp subsrc = tmp
@@ -369,8 +372,8 @@ class Installer:
overwrite = True overwrite = True
try: try:
utils.removepath(dst) utils.removepath(dst)
except OSError as e: except OSError as exc:
err = 'something went wrong with {}: {}'.format(src, e) err = 'something went wrong with {}: {}'.format(src, exc)
return False, err return False, err
if self.dry: if self.dry:
self.log.dry('would link {} to {}'.format(dst, src)) self.log.dry('would link {} to {}'.format(dst, src))
@@ -379,9 +382,9 @@ class Installer:
if not self._create_dirs(base): if not self._create_dirs(base):
err = 'error creating directory for {}'.format(dst) err = 'error creating directory for {}'.format(dst)
return False, err return False, err
r, e = self._exec_pre_actions(actionexec) ret, err = self._exec_pre_actions(actionexec)
if not r: if not ret:
return False, e return False, err
# re-check in case action created the file # re-check in case action created the file
if os.path.lexists(dst): if os.path.lexists(dst):
msg = 'Remove "{}" for link creation?'.format(dst) msg = 'Remove "{}" for link creation?'.format(dst)
@@ -389,8 +392,8 @@ class Installer:
return False, 'aborted' return False, 'aborted'
try: try:
utils.removepath(dst) utils.removepath(dst)
except OSError as e: except OSError as exc:
err = 'something went wrong with {}: {}'.format(src, e) err = 'something went wrong with {}: {}'.format(src, exc)
return False, err return False, err
os.symlink(src, dst) os.symlink(src, dst)
if not self.comparing: if not self.comparing:
@@ -399,8 +402,7 @@ class Installer:
def _copy_file(self, templater, src, dst, def _copy_file(self, templater, src, dst,
actionexec=None, noempty=False, actionexec=None, noempty=False,
ignore=[], is_template=True, ignore=None, is_template=True):
chmod=None):
""" """
install src to dst when is a file install src to dst when is a file
@@ -441,8 +443,8 @@ class Installer:
saved = templater.add_tmp_vars(self._get_tmp_file_vars(src, dst)) saved = templater.add_tmp_vars(self._get_tmp_file_vars(src, dst))
try: try:
content = templater.generate(src) content = templater.generate(src)
except UndefinedException as e: except UndefinedException as exc:
return False, str(e) return False, str(exc)
finally: finally:
templater.restore_vars(saved) templater.restore_vars(saved)
# test is empty # test is empty
@@ -456,8 +458,7 @@ class Installer:
# write the file # write the file
ret, err = self._write(src, dst, ret, err = self._write(src, dst,
content=content, content=content,
actionexec=actionexec, actionexec=actionexec)
chmod=chmod)
if ret and not err: if ret and not err:
if not self.dry and not self.comparing: if not self.dry and not self.comparing:
self.log.sub('install {} to {}'.format(src, dst)) self.log.sub('install {} to {}'.format(src, dst))
@@ -465,7 +466,7 @@ class Installer:
def _copy_dir(self, templater, src, dst, def _copy_dir(self, templater, src, dst,
actionexec=None, noempty=False, actionexec=None, noempty=False,
ignore=[], is_template=True, chmod=None): ignore=None, is_template=True):
""" """
install src to dst when is a directory install src to dst when is a directory
@@ -486,17 +487,16 @@ class Installer:
# handle all files in dir # handle all files in dir
for entry in os.listdir(src): for entry in os.listdir(src):
f = os.path.join(src, entry) fpath = os.path.join(src, entry)
self.log.dbg('deploy sub from {}: {}'.format(dst, entry)) self.log.dbg('deploy sub from {}: {}'.format(dst, entry))
if not os.path.isdir(f): if not os.path.isdir(fpath):
# is file # is file
res, err = self._copy_file(templater, f, res, err = self._copy_file(templater, fpath,
os.path.join(dst, entry), os.path.join(dst, entry),
actionexec=actionexec, actionexec=actionexec,
noempty=noempty, noempty=noempty,
ignore=ignore, ignore=ignore,
is_template=is_template, is_template=is_template)
chmod=None)
if not res and err: if not res and err:
# error occured # error occured
ret = res, err ret = res, err
@@ -506,13 +506,12 @@ class Installer:
ret = True, None ret = True, None
else: else:
# is directory # is directory
res, err = self._copy_dir(templater, f, res, err = self._copy_dir(templater, fpath,
os.path.join(dst, entry), os.path.join(dst, entry),
actionexec=actionexec, actionexec=actionexec,
noempty=noempty, noempty=noempty,
ignore=ignore, ignore=ignore,
is_template=is_template, is_template=is_template)
chmod=None)
if not res and err: if not res and err:
# error occured # error occured
ret = res, err ret = res, err
@@ -522,8 +521,33 @@ class Installer:
ret = True, None ret = True, None
return ret return ret
@classmethod
def _write_content_to_file(cls, content, src, dst):
"""write content to file"""
if content:
# write content the file
try:
with open(dst, 'wb') as file:
file.write(content)
shutil.copymode(src, dst)
except NotADirectoryError as exc:
err = 'opening dest file: {}'.format(exc)
return False, err
except OSError as exc:
return False, str(exc)
except TypeError as exc:
return False, str(exc)
else:
# copy file
try:
shutil.copyfile(src, dst)
shutil.copymode(src, dst)
except OSError as exc:
return False, str(exc)
return True, None
def _write(self, src, dst, content=None, def _write(self, src, dst, content=None,
actionexec=None, chmod=None): actionexec=None):
""" """
copy dotfile / write content to file copy dotfile / write content to file
@@ -541,8 +565,8 @@ class Installer:
if os.path.lexists(dst): if os.path.lexists(dst):
try: try:
os.stat(dst) os.stat(dst)
except OSError as e: except OSError as exc:
if e.errno == errno.ENOENT: if exc.errno == errno.ENOENT:
# broken symlink # broken symlink
err = 'broken symlink {}'.format(dst) err = 'broken symlink {}'.format(dst)
return False, err return False, err
@@ -560,47 +584,38 @@ class Installer:
if not self.log.ask('Overwrite \"{}\"'.format(dst)): if not self.log.ask('Overwrite \"{}\"'.format(dst)):
return False, 'aborted' return False, 'aborted'
overwrite = True overwrite = True
if self.backup and os.path.lexists(dst): if self.backup and os.path.lexists(dst):
self._backup(dst) self._backup(dst)
# create hierarchy
base = os.path.dirname(dst) base = os.path.dirname(dst)
if not self._create_dirs(base): if not self._create_dirs(base):
err = 'creating directory for {}'.format(dst) err = 'creating directory for {}'.format(dst)
return False, err return False, err
r, e = self._exec_pre_actions(actionexec)
if not r: # execute pre actions
return False, e ret, err = self._exec_pre_actions(actionexec)
if not ret:
return False, err
self.log.dbg('install file to \"{}\"'.format(dst)) self.log.dbg('install file to \"{}\"'.format(dst))
# re-check in case action created the file # re-check in case action created the file
if self.safe and not overwrite and os.path.lexists(dst): if self.safe and not overwrite and \
if not self.log.ask('Overwrite \"{}\"'.format(dst)): os.path.lexists(dst) and \
self.log.warn('ignoring {}'.format(dst)) not self.log.ask('Overwrite \"{}\"'.format(dst)):
return False, 'aborted' self.log.warn('ignoring {}'.format(dst))
return False, 'aborted'
if content: # writing to file
# write content the file return self._write_content_to_file(content, src, dst)
try:
with open(dst, 'wb') as f:
f.write(content)
shutil.copymode(src, dst)
except NotADirectoryError as e:
err = 'opening dest file: {}'.format(e)
return False, err
except Exception as e:
return False, str(e)
else:
# copy file
try:
shutil.copyfile(src, dst)
shutil.copymode(src, dst)
except Exception as e:
return False, str(e)
return True, None
######################################################## ########################################################
# helpers # helpers
######################################################## ########################################################
def _get_tmp_file_vars(self, src, dst): @classmethod
def _get_tmp_file_vars(cls, src, dst):
tmp = {} tmp = {}
tmp['_dotfile_sub_abs_src'] = src tmp['_dotfile_sub_abs_src'] = src
tmp['_dotfile_sub_abs_dst'] = dst tmp['_dotfile_sub_abs_dst'] = dst
@@ -615,10 +630,10 @@ class Installer:
if content: if content:
tmp = utils.write_to_tmpfile(content) tmp = utils.write_to_tmpfile(content)
src = tmp src = tmp
r = utils.fastdiff(src, dst) ret = utils.fastdiff(src, dst)
if r: if ret:
self.log.dbg('content differ') self.log.dbg('content differ')
return r return ret
def _show_diff_before_write(self, src, dst, content=None): def _show_diff_before_write(self, src, dst, content=None):
""" """
@@ -691,7 +706,10 @@ class Installer:
return ret, err return ret, err
def _log_install(self, boolean, err): def _log_install(self, boolean, err):
"""log installation process""" """
log installation process
returns success, error-if-any
"""
if not self.debug: if not self.debug:
return boolean, err return boolean, err
if boolean: if boolean:
@@ -703,7 +721,7 @@ class Installer:
self.log.dbg('install: IGNORED') self.log.dbg('install: IGNORED')
return boolean, err return boolean, err
def _check_paths(self, src, dst, chmod): def _check_paths(self, src, dst):
""" """
check and normalize param check and normalize param
returns <src>, <dst>, <continue>, <error> returns <src>, <dst>, <continue>, <error>

View File

@@ -30,9 +30,10 @@ COMMENT_END = '@@#}'
class Templategen: class Templategen:
"""dotfile templater"""
def __init__(self, base='.', variables={}, def __init__(self, base='.', variables=None,
func_file=[], filter_file=[], debug=False): func_file=None, filter_file=None, debug=False):
"""constructor """constructor
@base: directory path where to search for templates @base: directory path where to search for templates
@variables: dictionary of variables for templates @variables: dictionary of variables for templates
@@ -69,13 +70,13 @@ class Templategen:
self.log.dbg('load global functions:') self.log.dbg('load global functions:')
self._load_funcs_to_dic(jhelpers, self.env.globals) self._load_funcs_to_dic(jhelpers, self.env.globals)
if func_file: if func_file:
for f in func_file: for ffile in func_file:
self.log.dbg('load custom functions from {}'.format(f)) self.log.dbg('load custom functions from {}'.format(ffile))
self._load_path_to_dic(f, self.env.globals) self._load_path_to_dic(ffile, self.env.globals)
if filter_file: if filter_file:
for f in filter_file: for ffile in filter_file:
self.log.dbg('load custom filters from {}'.format(f)) self.log.dbg('load custom filters from {}'.format(ffile))
self._load_path_to_dic(f, self.env.filters) self._load_path_to_dic(ffile, self.env.filters)
if self.debug: if self.debug:
self._debug_dict('template additional variables', variables) self._debug_dict('template additional variables', variables)
@@ -89,9 +90,9 @@ class Templategen:
return '' return ''
try: try:
return self._handle_file(src) return self._handle_file(src)
except UndefinedError as e: except UndefinedError as exc:
err = 'undefined variable: {}'.format(e.message) err = 'undefined variable: {}'.format(exc.message)
raise UndefinedException(err) raise UndefinedException(err) from exc
def generate_string(self, string): def generate_string(self, string):
""" """
@@ -103,11 +104,11 @@ class Templategen:
return '' return ''
try: try:
return self.env.from_string(string).render(self.variables) return self.env.from_string(string).render(self.variables)
except UndefinedError as e: except UndefinedError as exc:
err = 'undefined variable: {}'.format(e.message) err = 'undefined variable: {}'.format(exc.message)
raise UndefinedException(err) raise UndefinedException(err) from exc
def add_tmp_vars(self, newvars={}): def add_tmp_vars(self, newvars=None):
"""add vars to the globals, make sure to call restore_vars""" """add vars to the globals, make sure to call restore_vars"""
saved_variables = self.variables.copy() saved_variables = self.variables.copy()
if not newvars: if not newvars:
@@ -139,13 +140,15 @@ class Templategen:
self.log.dbg('load function \"{}\"'.format(name)) self.log.dbg('load function \"{}\"'.format(name))
dic[name] = func dic[name] = func
def _header(self, prepend=''): @classmethod
def _header(cls, prepend=''):
"""add a comment usually in the header of a dotfile""" """add a comment usually in the header of a dotfile"""
return '{}{}'.format(prepend, utils.header()) return '{}{}'.format(prepend, utils.header())
def _handle_file(self, src): def _handle_file(self, src):
"""generate the file content from template""" """generate the file content from template"""
try: try:
# pylint: disable=C0415
import magic import magic
filetype = magic.from_file(src, mime=True) filetype = magic.from_file(src, mime=True)
self.log.dbg('using \"magic\" for filetype identification') self.log.dbg('using \"magic\" for filetype identification')
@@ -162,7 +165,8 @@ class Templategen:
return self._handle_bin_file(src) return self._handle_bin_file(src)
return self._handle_text_file(src) return self._handle_text_file(src)
def _is_text(self, fileoutput): @classmethod
def _is_text(cls, fileoutput):
"""return if `file -b` output is ascii text""" """return if `file -b` output is ascii text"""
out = fileoutput.lower() out = fileoutput.lower()
if out.startswith('text'): if out.startswith('text'):
@@ -179,8 +183,8 @@ class Templategen:
path = os.path.normpath(path) path = os.path.normpath(path)
if not os.path.exists(path): if not os.path.exists(path):
raise TemplateNotFound(path) raise TemplateNotFound(path)
with open(path, 'r') as f: with open(path, 'r') as file:
content = f.read() content = file.read()
return content return content
def _handle_text_file(self, src): def _handle_text_file(self, src):
@@ -199,18 +203,19 @@ class Templategen:
# this is dirty # this is dirty
if not src.startswith(self.base): if not src.startswith(self.base):
src = os.path.join(self.base, src) src = os.path.join(self.base, src)
with open(src, 'rb') as f: with open(src, 'rb') as file:
content = f.read() content = file.read()
return content return content
def _read_bad_encoded_text(self, path): @classmethod
def _read_bad_encoded_text(cls, path):
"""decode non utf-8 data""" """decode non utf-8 data"""
with open(path, 'rb') as f: with open(path, 'rb') as file:
data = f.read() data = file.read()
return data.decode('utf-8', 'replace') return data.decode('utf-8', 'replace')
@staticmethod @staticmethod
def is_template(path, ignore=[]): def is_template(path, ignore=None):
"""recursively check if any file is a template within path""" """recursively check if any file is a template within path"""
path = os.path.expanduser(path) path = os.path.expanduser(path)
@@ -250,10 +255,11 @@ class Templategen:
markers = [BLOCK_START, VAR_START, COMMENT_START] markers = [BLOCK_START, VAR_START, COMMENT_START]
patterns = [re.compile(marker.encode()) for marker in markers] patterns = [re.compile(marker.encode()) for marker in markers]
try: try:
with io.open(path, "r", encoding="utf-8") as f: with io.open(path, "r", encoding="utf-8") as file:
m = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) mapf = mmap.mmap(file.fileno(), 0,
access=mmap.ACCESS_READ)
for pattern in patterns: for pattern in patterns:
if pattern.search(m): if pattern.search(mapf):
return True return True
except UnicodeDecodeError: except UnicodeDecodeError:
# is binary so surely no template # is binary so surely no template
@@ -267,5 +273,5 @@ class Templategen:
self.log.dbg('{}:'.format(title)) self.log.dbg('{}:'.format(title))
if not elems: if not elems:
return return
for k, v in elems.items(): for k, val in elems.items():
self.log.dbg(' - \"{}\": {}'.format(k, v)) self.log.dbg(' - \"{}\": {}'.format(k, val))

View File

@@ -18,6 +18,7 @@ from shutil import rmtree, which
# local import # local import
from dotdrop.logger import Logger from dotdrop.logger import Logger
from dotdrop.exceptions import UnmetDependency
LOG = Logger() LOG = Logger()
STAR = '*' STAR = '*'
@@ -316,7 +317,7 @@ def dependencies_met():
err = 'The tool \"{}\" was not found in the PATH!' err = 'The tool \"{}\" was not found in the PATH!'
for dep in deps: for dep in deps:
if not which(dep): if not which(dep):
raise Exception(err.format(dep)) raise UnmetDependency(err.format(dep))
# check python deps # check python deps
err = 'missing python module \"{}\"' err = 'missing python module \"{}\"'