1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-04 15:39:43 +00:00

refactor ignore and debug logs

This commit is contained in:
deadc0de6
2021-10-11 16:44:59 +02:00
parent 6f19d7ef7b
commit e90f406ed2
4 changed files with 30 additions and 10 deletions

View File

@@ -139,7 +139,9 @@ def _dotfile_compare(opts, dotfile, tmp):
ignores = patch_ignores(ignores, dotfile.dst, debug=opts.debug)
insttmp = None
if dotfile.template and Templategen.is_template(src, ignore=ignores):
if dotfile.template and Templategen.is_template(src,
ignore=ignores,
debug=opts.debug):
# install dotfile to temporary dir for compare
ret, err, insttmp = inst.install_to_temp(templ, tmp, src, dotfile.dst,
is_template=True,

View File

@@ -27,6 +27,7 @@ VAR_START = '{{@@'
VAR_END = '@@}}'
COMMENT_START = '{#@@'
COMMENT_END = '@@#}'
LOG = Logger()
class Templategen:
@@ -215,26 +216,34 @@ class Templategen:
return data.decode('utf-8', 'replace')
@staticmethod
def is_template(path, ignore=None):
def is_template(path, ignore=None, debug=False):
"""recursively check if any file is a template within path"""
if debug:
LOG.dbg('is template: {}'.format(path), force=True)
path = os.path.expanduser(path)
if utils.must_ignore([path], ignore, debug=False):
return False
if not os.path.exists(path):
# does not exist
return False
if utils.must_ignore([path], ignore, debug=debug):
# must be ignored
return False
if os.path.isfile(path):
# is file
return Templategen._is_template(path, ignore=ignore)
return Templategen._is_template(path, ignore=ignore, debug=debug)
# is a directory
for entry in os.listdir(path):
fpath = os.path.join(path, entry)
if not os.path.isfile(fpath):
# recursively explore directory
if Templategen.is_template(fpath, ignore=ignore):
if Templategen.is_template(fpath, ignore=ignore, debug=debug):
return True
else:
# check if file is a template
if Templategen._is_template(fpath, ignore=ignore):
if Templategen._is_template(fpath, ignore=ignore, debug=debug):
return True
return False
@@ -244,9 +253,9 @@ class Templategen:
return VAR_START in str(string)
@staticmethod
def _is_template(path, ignore):
def _is_template(path, ignore, debug=False):
"""test if file pointed by path is a template"""
if utils.must_ignore([path], ignore, debug=False):
if utils.must_ignore([path], ignore, debug=debug):
return False
if not os.path.isfile(path):
return False

View File

@@ -160,7 +160,8 @@ class Updater:
return tmp
def _is_template(self, path):
if not Templategen.is_template(path, ignore=self.ignores):
if not Templategen.is_template(path, ignore=self.ignores,
debug=self.debug):
self.log.dbg('{} is NO template'.format(path))
return False
self.log.warn('{} uses template, update manually'.format(path))

View File

@@ -221,6 +221,8 @@ def strip_home(path):
def must_ignore(paths, ignores, debug=False):
"""return true if any paths in list matches any ignore patterns"""
if debug:
LOG.dbg('must_ignore called', force=True)
if not ignores:
return False
if debug:
@@ -237,10 +239,14 @@ def must_ignore(paths, ignores, debug=False):
LOG.dbg('ignore \"{}\" match: {}'.format(i, path),
force=True)
ignore_matches.append(path)
# Then remove any matches that actually shouldn't be ignored
for nign in ignored_negative:
# Each of these will start with an '!' so we need to remove that
nign = nign[1:]
if debug:
msg = 'trying to match :\"{}\" with non-ignore-pattern:\"{}\"'
LOG.dbg(msg.format(path, nign))
if fnmatch.fnmatch(path, nign):
if debug:
msg = 'negative ignore \"{}\" match: {}'.format(nign, path)
@@ -256,6 +262,8 @@ def must_ignore(paths, ignores, debug=False):
'previous ignore pattern.'.format(nign)
)
if ignore_matches:
if debug:
LOG.dbg('ignoring {}'.format(paths), force=True)
return True
if debug:
LOG.dbg('NOT ignoring {}'.format(paths), force=True)