mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-16 10:36:11 +00:00
adding ability to compare transformed files
This commit is contained in:
@@ -103,20 +103,8 @@ def install(opts, conf):
|
|||||||
src = dotfile.src
|
src = dotfile.src
|
||||||
tmp = None
|
tmp = None
|
||||||
if dotfile.trans:
|
if dotfile.trans:
|
||||||
tmp = '{}.{}'.format(src, TRANS_SUFFIX)
|
tmp = apply_trans(opts, dotfile)
|
||||||
err = False
|
if not tmp:
|
||||||
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)
|
|
||||||
continue
|
continue
|
||||||
src = tmp
|
src = tmp
|
||||||
r = inst.install(t, opts['profile'], src, dotfile.dst)
|
r = inst.install(t, opts['profile'], src, dotfile.dst)
|
||||||
@@ -139,6 +127,28 @@ def install(opts, conf):
|
|||||||
return True
|
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):
|
def compare(opts, conf, tmp, focus=None):
|
||||||
"""compare dotfiles and return True if all identical"""
|
"""compare dotfiles and return True if all identical"""
|
||||||
dotfiles = conf.get_dotfiles(opts['profile'])
|
dotfiles = conf.get_dotfiles(opts['profile'])
|
||||||
@@ -169,13 +179,20 @@ def compare(opts, conf, tmp, focus=None):
|
|||||||
|
|
||||||
for dotfile in selected:
|
for dotfile in selected:
|
||||||
LOG.dbg('comparing {}'.format(dotfile))
|
LOG.dbg('comparing {}'.format(dotfile))
|
||||||
|
src = dotfile.src
|
||||||
|
tmpsrc = None
|
||||||
if dotfile.trans:
|
if dotfile.trans:
|
||||||
msg = 'ignore {} as it uses transformation(s)'
|
tmpsrc = apply_trans(opts, dotfile)
|
||||||
LOG.log(msg.format(dotfile.key))
|
if not tmpsrc:
|
||||||
continue
|
continue
|
||||||
|
src = tmpsrc
|
||||||
|
# create a fake dotfile which is the result of the transformation
|
||||||
same, diff = inst.compare(t, tmp, opts['profile'],
|
same, diff = inst.compare(t, tmp, opts['profile'],
|
||||||
dotfile.src, dotfile.dst,
|
src, dotfile.dst, opts=opts['dopts'])
|
||||||
opts=opts['dopts'])
|
if tmpsrc:
|
||||||
|
tmpsrc = os.path.join(opts['dotpath'], tmpsrc)
|
||||||
|
if os.path.exists(tmpsrc):
|
||||||
|
remove(tmpsrc)
|
||||||
if same:
|
if same:
|
||||||
LOG.dbg('diffing \"{}\" VS \"{}\"'.format(dotfile.key,
|
LOG.dbg('diffing \"{}\" VS \"{}\"'.format(dotfile.key,
|
||||||
dotfile.dst))
|
dotfile.dst))
|
||||||
|
|||||||
@@ -183,6 +183,7 @@ class Installer:
|
|||||||
|
|
||||||
def compare(self, templater, tmpdir, profile, src, dst, opts=''):
|
def compare(self, templater, tmpdir, profile, src, dst, opts=''):
|
||||||
"""compare a temporary generated dotfile with the local one"""
|
"""compare a temporary generated dotfile with the local one"""
|
||||||
|
# saved some flags while comparing
|
||||||
self.comparing = True
|
self.comparing = True
|
||||||
retval = False, ''
|
retval = False, ''
|
||||||
drysaved = self.dry
|
drysaved = self.dry
|
||||||
@@ -191,16 +192,17 @@ class Installer:
|
|||||||
self.diff = False
|
self.diff = False
|
||||||
createsaved = self.create
|
createsaved = self.create
|
||||||
self.create = True
|
self.create = True
|
||||||
|
# normalize src and dst
|
||||||
src = os.path.expanduser(src)
|
src = os.path.expanduser(src)
|
||||||
dst = os.path.expanduser(dst)
|
dst = os.path.expanduser(dst)
|
||||||
self.log.dbg('comparing {} and {}'.format(src, dst))
|
self.log.dbg('comparing {} and {}'.format(src, dst))
|
||||||
if not os.path.exists(dst):
|
if not os.path.exists(dst):
|
||||||
|
# destination dotfile does not exist
|
||||||
retval = False, '\"{}\" does not exist on local\n'.format(dst)
|
retval = False, '\"{}\" does not exist on local\n'.format(dst)
|
||||||
else:
|
else:
|
||||||
ret, tmpdst = self._install_to_temp(templater,
|
# install the dotfile to a temp directory for comparing
|
||||||
profile,
|
ret, tmpdst = self._install_to_temp(templater, profile,
|
||||||
src, dst,
|
src, dst, tmpdir)
|
||||||
tmpdir)
|
|
||||||
if ret:
|
if ret:
|
||||||
self.log.dbg('diffing {} and {}'.format(tmpdst, dst))
|
self.log.dbg('diffing {} and {}'.format(tmpdst, dst))
|
||||||
diff = utils.diff(tmpdst, dst, raw=False, opts=opts)
|
diff = utils.diff(tmpdst, dst, raw=False, opts=opts)
|
||||||
@@ -208,6 +210,7 @@ class Installer:
|
|||||||
retval = True, ''
|
retval = True, ''
|
||||||
else:
|
else:
|
||||||
retval = False, diff
|
retval = False, diff
|
||||||
|
# reset flags
|
||||||
self.dry = drysaved
|
self.dry = drysaved
|
||||||
self.diff = diffsaved
|
self.diff = diffsaved
|
||||||
self.comparing = False
|
self.comparing = False
|
||||||
|
|||||||
Reference in New Issue
Block a user