mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-09 14:04:17 +00:00
implement relative upignore/cmpignore for #149 and some import refactoring
This commit is contained in:
@@ -10,7 +10,7 @@ import filecmp
|
||||
|
||||
# local imports
|
||||
from dotdrop.logger import Logger
|
||||
import dotdrop.utils as utils
|
||||
from dotdrop.utils import must_ignore, uniq_list, diff
|
||||
|
||||
|
||||
class Comparator:
|
||||
@@ -43,7 +43,7 @@ class Comparator:
|
||||
"""compare a file"""
|
||||
if self.debug:
|
||||
self.log.dbg('compare file {} with {}'.format(left, right))
|
||||
if utils.must_ignore([left, right], ignore, debug=self.debug):
|
||||
if must_ignore([left, right], ignore, debug=self.debug):
|
||||
if self.debug:
|
||||
self.log.dbg('ignoring diff {} and {}'.format(left, right))
|
||||
return ''
|
||||
@@ -55,7 +55,7 @@ class Comparator:
|
||||
self.log.dbg('compare directory {} with {}'.format(left, right))
|
||||
if not os.path.exists(right):
|
||||
return ''
|
||||
if utils.must_ignore([left, right], ignore, debug=self.debug):
|
||||
if must_ignore([left, right], ignore, debug=self.debug):
|
||||
if self.debug:
|
||||
self.log.dbg('ignoring diff {} and {}'.format(left, right))
|
||||
return ''
|
||||
@@ -68,15 +68,15 @@ class Comparator:
|
||||
|
||||
# handle files only in deployed dir
|
||||
for i in comp.left_only:
|
||||
if utils.must_ignore([os.path.join(left, i)],
|
||||
ignore, debug=self.debug):
|
||||
if must_ignore([os.path.join(left, i)],
|
||||
ignore, debug=self.debug):
|
||||
continue
|
||||
ret.append('=> \"{}\" does not exist on local\n'.format(i))
|
||||
|
||||
# handle files only in dotpath dir
|
||||
for i in comp.right_only:
|
||||
if utils.must_ignore([os.path.join(right, i)],
|
||||
ignore, debug=self.debug):
|
||||
if must_ignore([os.path.join(right, i)],
|
||||
ignore, debug=self.debug):
|
||||
continue
|
||||
ret.append('=> \"{}\" does not exist in dotdrop\n'.format(i))
|
||||
|
||||
@@ -85,8 +85,8 @@ class Comparator:
|
||||
for i in funny:
|
||||
lfile = os.path.join(left, i)
|
||||
rfile = os.path.join(right, i)
|
||||
if utils.must_ignore([lfile, rfile],
|
||||
ignore, debug=self.debug):
|
||||
if must_ignore([lfile, rfile],
|
||||
ignore, debug=self.debug):
|
||||
continue
|
||||
short = os.path.basename(lfile)
|
||||
# file vs dir
|
||||
@@ -95,12 +95,12 @@ class Comparator:
|
||||
# content is different
|
||||
funny = comp.diff_files
|
||||
funny.extend(comp.funny_files)
|
||||
funny = utils.uniq_list(funny)
|
||||
funny = uniq_list(funny)
|
||||
for i in funny:
|
||||
lfile = os.path.join(left, i)
|
||||
rfile = os.path.join(right, i)
|
||||
if utils.must_ignore([lfile, rfile],
|
||||
ignore, debug=self.debug):
|
||||
if must_ignore([lfile, rfile],
|
||||
ignore, debug=self.debug):
|
||||
continue
|
||||
diff = self._diff(lfile, rfile, header=True)
|
||||
ret.append(diff)
|
||||
@@ -115,9 +115,9 @@ class Comparator:
|
||||
|
||||
def _diff(self, left, right, header=False):
|
||||
"""diff using the unix tool diff"""
|
||||
diff = utils.diff(left, right, raw=False,
|
||||
opts=self.diffopts, debug=self.debug)
|
||||
d = diff(left, right, raw=False,
|
||||
opts=self.diffopts, debug=self.debug)
|
||||
if header:
|
||||
lshort = os.path.basename(left)
|
||||
diff = '=> diff \"{}\":\n{}'.format(lshort, diff)
|
||||
return diff
|
||||
d = '=> diff \"{}\":\n{}'.format(lshort, diff)
|
||||
return d
|
||||
|
||||
@@ -15,7 +15,8 @@ from dotdrop.templategen import Templategen
|
||||
from dotdrop.installer import Installer
|
||||
from dotdrop.updater import Updater
|
||||
from dotdrop.comparator import Comparator
|
||||
from dotdrop.utils import get_tmpdir, remove, strip_home, run, uniq_list
|
||||
from dotdrop.utils import get_tmpdir, remove, strip_home, \
|
||||
run, uniq_list, patch_ignores
|
||||
from dotdrop.linktypes import LinkTypes
|
||||
from dotdrop.exceptions import YamlException
|
||||
|
||||
@@ -225,6 +226,7 @@ def cmd_compare(o, tmp):
|
||||
same = False
|
||||
continue
|
||||
ignores = list(set(o.compare_ignore + dotfile.cmpignore))
|
||||
ignores = patch_ignores(ignores, dotfile.src)
|
||||
diff = comp.compare(insttmp, dotfile.dst, ignore=ignores)
|
||||
if tmpsrc:
|
||||
# clean tmp transformed dotfile if any
|
||||
|
||||
@@ -12,7 +12,8 @@ import filecmp
|
||||
# local imports
|
||||
from dotdrop.logger import Logger
|
||||
from dotdrop.templategen import Templategen
|
||||
import dotdrop.utils as utils
|
||||
from dotdrop.utils import patch_ignores, remove, get_unique_tmp_name, \
|
||||
write_to_tmpfile, must_ignore
|
||||
|
||||
|
||||
TILD = '~'
|
||||
@@ -76,7 +77,8 @@ class Updater:
|
||||
"""update dotfile from file pointed by path"""
|
||||
ret = False
|
||||
new_path = None
|
||||
self.ignores = list(set(self.ignore + dotfile.upignore))
|
||||
ignores = list(set(self.ignore + dotfile.upignore))
|
||||
self.ignores = patch_ignores(ignores, dotfile.dst)
|
||||
if self.debug:
|
||||
self.log.dbg('ignore pattern(s): {}'.format(self.ignores))
|
||||
|
||||
@@ -98,7 +100,7 @@ class Updater:
|
||||
ret = self._handle_file(path, dtpath)
|
||||
# clean temporary files
|
||||
if new_path and os.path.exists(new_path):
|
||||
utils.remove(new_path)
|
||||
remove(new_path)
|
||||
return ret
|
||||
|
||||
def _apply_trans_w(self, path, dotfile):
|
||||
@@ -108,12 +110,12 @@ class Updater:
|
||||
return path
|
||||
if self.debug:
|
||||
self.log.dbg('executing write transformation {}'.format(trans))
|
||||
tmp = utils.get_unique_tmp_name()
|
||||
tmp = get_unique_tmp_name()
|
||||
if not trans.transform(path, tmp):
|
||||
msg = 'transformation \"{}\" failed for {}'
|
||||
self.log.err(msg.format(trans.key, dotfile.key))
|
||||
if os.path.exists(tmp):
|
||||
utils.remove(tmp)
|
||||
remove(tmp)
|
||||
return None
|
||||
return tmp
|
||||
|
||||
@@ -128,7 +130,7 @@ class Updater:
|
||||
def _show_patch(self, fpath, tpath):
|
||||
"""provide a way to manually patch the template"""
|
||||
content = self._resolve_template(tpath)
|
||||
tmp = utils.write_to_tmpfile(content)
|
||||
tmp = write_to_tmpfile(content)
|
||||
cmds = ['diff', '-u', tmp, fpath, '|', 'patch', tpath]
|
||||
self.log.warn('try patching with: \"{}\"'.format(' '.join(cmds)))
|
||||
return False
|
||||
@@ -231,7 +233,7 @@ class Updater:
|
||||
self.log.dbg('rm -r {}'.format(old))
|
||||
if not self._confirm_rm_r(old):
|
||||
continue
|
||||
utils.remove(old)
|
||||
remove(old)
|
||||
self.log.sub('\"{}\" dir removed'.format(old))
|
||||
|
||||
# handle files diff
|
||||
@@ -283,7 +285,7 @@ class Updater:
|
||||
continue
|
||||
if self.debug:
|
||||
self.log.dbg('rm {}'.format(new))
|
||||
utils.remove(new)
|
||||
remove(new)
|
||||
self.log.sub('\"{}\" removed'.format(new))
|
||||
|
||||
# Recursively decent into common subdirectories.
|
||||
@@ -308,7 +310,7 @@ class Updater:
|
||||
return True
|
||||
|
||||
def _ignore(self, paths):
|
||||
if utils.must_ignore(paths, self.ignores, debug=self.debug):
|
||||
if must_ignore(paths, self.ignores, debug=self.debug):
|
||||
if self.debug:
|
||||
self.log.dbg('ignoring update for {}'.format(paths))
|
||||
return True
|
||||
|
||||
@@ -17,6 +17,7 @@ from shutil import rmtree
|
||||
from dotdrop.logger import Logger
|
||||
|
||||
LOG = Logger()
|
||||
STAR = '*'
|
||||
|
||||
|
||||
def run(cmd, raw=True, debug=False, checkerr=False):
|
||||
@@ -139,3 +140,21 @@ def uniq_list(a_list):
|
||||
if a not in new:
|
||||
new.append(a)
|
||||
return new
|
||||
|
||||
|
||||
def patch_ignores(ignores, prefix):
|
||||
"""allow relative ignore pattern"""
|
||||
new = []
|
||||
for ignore in ignores:
|
||||
if STAR in ignore:
|
||||
# is glob
|
||||
new.append(ignore)
|
||||
continue
|
||||
if os.path.isabs(ignore):
|
||||
# is absolute
|
||||
new.append(ignore)
|
||||
continue
|
||||
# patch upignore
|
||||
path = os.path.join(prefix, ignore)
|
||||
new.append(path)
|
||||
return new
|
||||
|
||||
Reference in New Issue
Block a user