From 8081b4adf54fff3566e3ff174bc56cf2f5aa35f6 Mon Sep 17 00:00:00 2001 From: Marcel Robitaille Date: Fri, 11 Dec 2020 00:54:51 -0500 Subject: [PATCH] Option to ignore missing files update and compare --- dotdrop/comparator.py | 7 +++++-- dotdrop/dotdrop.py | 6 ++++-- dotdrop/options.py | 8 +++++--- dotdrop/updater.py | 9 +++++++-- dotdrop/utils.py | 6 +++++- tests/helpers.py | 1 + 6 files changed, 27 insertions(+), 10 deletions(-) diff --git a/dotdrop/comparator.py b/dotdrop/comparator.py index 634a489..6159afb 100644 --- a/dotdrop/comparator.py +++ b/dotdrop/comparator.py @@ -16,7 +16,7 @@ from dotdrop.utils import must_ignore, uniq_list, diff, \ class Comparator: - def __init__(self, diff_cmd='', debug=False): + def __init__(self, diff_cmd='', debug=False, ignore_missing_in_dotdrop=False): """constructor @diff_cmd: diff command to use @debug: enable debug @@ -24,6 +24,7 @@ class Comparator: self.diff_cmd = diff_cmd self.debug = debug self.log = Logger() + self.ignore_missing_in_dotdrop = ignore_missing_in_dotdrop def compare(self, left, right, ignore=[]): """diff left (dotdrop dotfile) and right (deployed file)""" @@ -111,7 +112,9 @@ class Comparator: if must_ignore([os.path.join(right, i)], ignore, debug=self.debug): continue - ret.append('=> \"{}\" does not exist in dotdrop\n'.format(i)) + + if not self.ignore_missing_in_dotdrop: + ret.append('=> \"{}\" does not exist in dotdrop\n'.format(i)) # same left and right but different type funny = comp.common_funny diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index 47b9679..49c79e0 100644 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -80,7 +80,8 @@ def _dotfile_update(o, path, key=False): updater = Updater(o.dotpath, o.variables, o.conf, dry=o.dry, safe=o.safe, debug=o.debug, ignore=o.update_ignore, - showpatch=o.update_showpatch) + showpatch=o.update_showpatch, + ignore_missing_in_dotdrop=o.ignore_missing_in_dotdrop) if key: return updater.update_key(path) return updater.update_path(path) @@ -97,7 +98,8 @@ def _dotfile_compare(o, dotfile, tmp): workdir=o.workdir, debug=o.debug, backup_suffix=o.install_backup_suffix, diff_cmd=o.diff_command) - comp = Comparator(diff_cmd=o.diff_command, debug=o.debug) + comp = Comparator(diff_cmd=o.diff_command, debug=o.debug, + ignore_missing_in_dotdrop=o.ignore_missing_in_dotdrop) # add dotfile variables newvars = dotfile.get_dotfile_variables() diff --git a/dotdrop/options.py b/dotdrop/options.py index 93edd7f..d05cbd8 100644 --- a/dotdrop/options.py +++ b/dotdrop/options.py @@ -43,7 +43,7 @@ OPT_LINK = { LinkTypes.LINK.name.lower(): LinkTypes.LINK, LinkTypes.LINK_CHILDREN.name.lower(): LinkTypes.LINK_CHILDREN} -BANNER = """ _ _ _ +BANNER = r""" _ _ _ __| | ___ | |_ __| |_ __ ___ _ __ / _` |/ _ \| __/ _` | '__/ _ \| '_ | \__,_|\___/ \__\__,_|_| \___/| .__/ v{} @@ -57,9 +57,9 @@ Usage: [-w ] [...] dotdrop import [-Vbdfm] [-c ] [-p ] [-s ] [-l ] [-i ...] ... - dotdrop compare [-LVb] [-c ] [-p ] + dotdrop compare [-LVbz] [-c ] [-p ] [-w ] [-C ...] [-i ...] - dotdrop update [-VbfdkP] [-c ] [-p ] + dotdrop update [-VbfdkPz] [-c ] [-p ] [-w ] [-i ...] [...] dotdrop remove [-Vbfdk] [-c ] [-p ] [...] dotdrop files [-VbTG] [-c ] [-p ] @@ -73,6 +73,7 @@ Options: -b --no-banner Do not display the banner. -c --cfg= Path to the config. -C --file= Path of dotfile to compare. + -z --ignore-missing Ignore files in installed folders that are missing. -d --dry Dry run. -D --showdiff Show a diff before overwriting. -f --force Do not ask user confirmation for anything. @@ -123,6 +124,7 @@ class Options(AttrMonitor): self.log = Logger() self.debug = self.args['--verbose'] or ENV_DEBUG in os.environ self.dry = self.args['--dry'] + self.ignore_missing_in_dotdrop = self.args['--ignore-missing'] if ENV_NODEBUG in os.environ: # force disabling debugs self.debug = False diff --git a/dotdrop/updater.py b/dotdrop/updater.py index 11eda8b..0a36c2b 100644 --- a/dotdrop/updater.py +++ b/dotdrop/updater.py @@ -25,7 +25,8 @@ class Updater: def __init__(self, dotpath, variables, conf, dry=False, safe=True, debug=False, - ignore=[], showpatch=False): + ignore=[], showpatch=False, + ignore_missing_in_dotdrop=False): """constructor @dotpath: path where dotfiles are stored @variables: dictionary of variables for the templates @@ -44,6 +45,7 @@ class Updater: self.debug = debug self.ignore = ignore self.showpatch = showpatch + self.ignore_missing_in_dotdrop = ignore_missing_in_dotdrop self.templater = Templategen(variables=self.variables, base=self.dotpath, debug=self.debug) @@ -403,7 +405,10 @@ class Updater: return True def _ignore(self, paths): - if must_ignore(paths, self.ignores, debug=self.debug): + if must_ignore( + paths, self.ignores, debug=self.debug, + ignore_missing_in_dotdrop=self.ignore_missing_in_dotdrop, + ): if self.debug: self.log.dbg('ignoring update for {}'.format(paths)) return True diff --git a/dotdrop/utils.py b/dotdrop/utils.py index 9189cda..c6a3a49 100644 --- a/dotdrop/utils.py +++ b/dotdrop/utils.py @@ -198,8 +198,12 @@ def strip_home(path): return path -def must_ignore(paths, ignores, debug=False): +def must_ignore(paths, ignores, debug=False, ignore_missing_in_dotdrop=False): """return true if any paths in list matches any ignore patterns""" + if ignore_missing_in_dotdrop and \ + len(paths) >= 2 and not os.path.exists(paths[1]): + return True + if not ignores: return False if debug: diff --git a/tests/helpers.py b/tests/helpers.py index d080e9e..c29fe62 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -135,6 +135,7 @@ def _fake_args(): args['--file-only'] = False args['--workers'] = 1 args['--preserve-mode'] = False + args['--ignore-missing'] = False # cmds args['profiles'] = False args['files'] = False