From 1f28c789f9377512291ddb15b883b4a9a8b8c1ec Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Wed, 13 Jun 2018 15:20:38 +0200 Subject: [PATCH] adding ability to compare transformed files --- dotdrop/dotdrop.py | 55 +++++++++++++++++++++++++++++--------------- dotdrop/installer.py | 11 +++++---- 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index 5a4c620..37051ee 100644 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -103,20 +103,8 @@ def install(opts, conf): src = dotfile.src tmp = None if dotfile.trans: - tmp = '{}.{}'.format(src, TRANS_SUFFIX) - err = False - for trans in dotfile.trans: - LOG.dbg('executing transformation {}'.format(trans)) - s = os.path.join(opts['dotpath'], src) - temp = os.path.join(opts['dotpath'], tmp) - if not trans.transform(s, temp): - msg = 'transformation \"{}\" failed for {}' - LOG.err(msg.format(trans.key, dotfile.key)) - err = True - break - if err: - if tmp and os.path.exists(tmp): - remove(tmp) + tmp = apply_trans(opts, dotfile) + if not tmp: continue src = tmp r = inst.install(t, opts['profile'], src, dotfile.dst) @@ -139,6 +127,28 @@ def install(opts, conf): return True +def apply_trans(opts, dotfile): + """apply the transformation to the dotfile + return None if fails and new source if succeed""" + src = dotfile.src + new_src = '{}.{}'.format(src, TRANS_SUFFIX) + err = False + for trans in dotfile.trans: + LOG.dbg('executing transformation {}'.format(trans)) + s = os.path.join(opts['dotpath'], src) + temp = os.path.join(opts['dotpath'], new_src) + if not trans.transform(s, temp): + msg = 'transformation \"{}\" failed for {}' + LOG.err(msg.format(trans.key, dotfile.key)) + err = True + break + if err: + if new_src and os.path.exists(new_src): + remove(new_src) + return None + return new_src + + def compare(opts, conf, tmp, focus=None): """compare dotfiles and return True if all identical""" dotfiles = conf.get_dotfiles(opts['profile']) @@ -169,13 +179,20 @@ def compare(opts, conf, tmp, focus=None): for dotfile in selected: LOG.dbg('comparing {}'.format(dotfile)) + src = dotfile.src + tmpsrc = None if dotfile.trans: - msg = 'ignore {} as it uses transformation(s)' - LOG.log(msg.format(dotfile.key)) - continue + tmpsrc = apply_trans(opts, dotfile) + if not tmpsrc: + continue + src = tmpsrc + # create a fake dotfile which is the result of the transformation same, diff = inst.compare(t, tmp, opts['profile'], - dotfile.src, dotfile.dst, - opts=opts['dopts']) + src, dotfile.dst, opts=opts['dopts']) + if tmpsrc: + tmpsrc = os.path.join(opts['dotpath'], tmpsrc) + if os.path.exists(tmpsrc): + remove(tmpsrc) if same: LOG.dbg('diffing \"{}\" VS \"{}\"'.format(dotfile.key, dotfile.dst)) diff --git a/dotdrop/installer.py b/dotdrop/installer.py index 1fe3f42..56d3801 100644 --- a/dotdrop/installer.py +++ b/dotdrop/installer.py @@ -183,6 +183,7 @@ class Installer: def compare(self, templater, tmpdir, profile, src, dst, opts=''): """compare a temporary generated dotfile with the local one""" + # saved some flags while comparing self.comparing = True retval = False, '' drysaved = self.dry @@ -191,16 +192,17 @@ class Installer: self.diff = False createsaved = self.create self.create = True + # normalize src and dst src = os.path.expanduser(src) dst = os.path.expanduser(dst) self.log.dbg('comparing {} and {}'.format(src, dst)) if not os.path.exists(dst): + # destination dotfile does not exist retval = False, '\"{}\" does not exist on local\n'.format(dst) else: - ret, tmpdst = self._install_to_temp(templater, - profile, - src, dst, - tmpdir) + # install the dotfile to a temp directory for comparing + ret, tmpdst = self._install_to_temp(templater, profile, + src, dst, tmpdir) if ret: self.log.dbg('diffing {} and {}'.format(tmpdst, dst)) diff = utils.diff(tmpdst, dst, raw=False, opts=opts) @@ -208,6 +210,7 @@ class Installer: retval = True, '' else: retval = False, diff + # reset flags self.dry = drysaved self.diff = diffsaved self.comparing = False