From a6cc47a5178d05437f87859396d3e8b37178f2b1 Mon Sep 17 00:00:00 2001 From: Japorized Date: Mon, 28 Aug 2017 11:46:12 +0800 Subject: [PATCH 1/3] File selection in compare function - This allows the user to choose which files do they wish to compare, using the --files arg - --files argument is comma delimited, and must match the keys in config.yaml --- dotdrop/dotdrop.py | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index 176a6f0..70c0a7d 100755 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -32,7 +32,7 @@ USAGE = """ Usage: dotdrop.py install [--profile=] [--cfg=] [(-f | --force)] [--nodiff] [--dry] - dotdrop.py compare [--profile=] [--cfg=] + dotdrop.py compare [--profile=] [--cfg=] [--files=] dotdrop.py list [--cfg=] dotdrop.py import [--profile=] [--cfg=] [(-l | --link)] [--dry] ... @@ -77,7 +77,7 @@ def install(opts, conf): return True -def compare(opts, conf, tmp): +def compare(opts, conf, tmp, focus=""): dotfiles = conf.get_dotfiles(opts['profile']) if dotfiles == []: LOG.err('no dotfiles defined for this profile (\"%s\")' % @@ -86,9 +86,33 @@ def compare(opts, conf, tmp): t = Templategen(base=opts['dotpath']) inst = Installer(create=opts['create'], backup=opts['backup'], dry=opts['dry'], base=opts['dotpath'], quiet=True) + + # Selected files or directory must be comma separated without whitespace. + # We will also make sure that the given arguments match the dotfile keys + # in the config file. If a key is not found, terminate the script and + # let the user know. + if focus is not None: + focus = focus.replace(" ", "") + selected = focus.split(",") + for selection in selected: + exist = False + for dotfile in dotfiles: + if selection == dotfile.key: + exist = True + break + if not exist: + LOG.log('One of the keys is not found. Please double check ' \ + 'your arguments.') + sys.exit(0) + else: + selected = [] + for dotfile in dotfiles: + selected.append(dotfile.key) + for dotfile in dotfiles: - LOG.log('diffing \"%s\" VS \"%s\"' % (dotfile.key, dotfile.dst)) - inst.compare(t, tmp, opts['profile'], dotfile.src, dotfile.dst) + if dotfile.key in selected: + LOG.log('diffing \"%s\" VS \"%s\"' % (dotfile.key, dotfile.dst)) + inst.compare(t, tmp, opts['profile'], dotfile.src, dotfile.dst) return len(dotfiles) > 0 @@ -159,6 +183,7 @@ if __name__ == '__main__': ret = True args = docopt(USAGE, version=VERSION) conf = Cfg(args['--cfg']) + focus = args['--files'] opts = conf.get_configs() opts['dry'] = args['--dry'] @@ -179,7 +204,7 @@ if __name__ == '__main__': elif args['compare']: tmp = utils.get_tmpdir() - if compare(opts, conf, tmp): + if compare(opts, conf, tmp, focus): LOG.log('generated temporary files available under %s' % (tmp)) else: os.rmdir(tmp) From 445a8186753bfa96698a3c2d31bce0d0d29142c2 Mon Sep 17 00:00:00 2001 From: Japorized Date: Mon, 28 Aug 2017 12:38:25 +0800 Subject: [PATCH 2/3] Update dotdrop.py Updating to conform to PEP8 --- dotdrop/dotdrop.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index 70c0a7d..1d19ae2 100755 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -101,7 +101,7 @@ def compare(opts, conf, tmp, focus=""): exist = True break if not exist: - LOG.log('One of the keys is not found. Please double check ' \ + LOG.log('One of the keys is not found. Please double check ' 'your arguments.') sys.exit(0) else: From a8c9c638b80612115b5d67e2497e1be5a94d578a Mon Sep 17 00:00:00 2001 From: Japorized Date: Mon, 28 Aug 2017 16:41:18 +0800 Subject: [PATCH 3/3] Changing arg from config keys to file dst --- dotdrop/dotdrop.py | 44 ++++++++++++++++---------------------------- 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index 70c0a7d..5323976 100755 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -32,7 +32,7 @@ USAGE = """ Usage: dotdrop.py install [--profile=] [--cfg=] [(-f | --force)] [--nodiff] [--dry] - dotdrop.py compare [--profile=] [--cfg=] [--files=] + dotdrop.py compare [--profile=] [--cfg=] [--files=] dotdrop.py list [--cfg=] dotdrop.py import [--profile=] [--cfg=] [(-l | --link)] [--dry] ... @@ -42,6 +42,7 @@ Usage: Options: --profile= Specify the profile to use [default: %s]. --cfg= Path to the config [default: %s/config.yaml]. + --files= Comma separated list of file to compare. --dry Dry run. --nodiff Do not diff when installing [default: False]. -l --link Import and link [default: False]. @@ -77,7 +78,7 @@ def install(opts, conf): return True -def compare(opts, conf, tmp, focus=""): +def compare(opts, conf, tmp, focus=None): dotfiles = conf.get_dotfiles(opts['profile']) if dotfiles == []: LOG.err('no dotfiles defined for this profile (\"%s\")' % @@ -87,32 +88,20 @@ def compare(opts, conf, tmp, focus=""): inst = Installer(create=opts['create'], backup=opts['backup'], dry=opts['dry'], base=opts['dotpath'], quiet=True) - # Selected files or directory must be comma separated without whitespace. - # We will also make sure that the given arguments match the dotfile keys - # in the config file. If a key is not found, terminate the script and - # let the user know. - if focus is not None: - focus = focus.replace(" ", "") - selected = focus.split(",") - for selection in selected: - exist = False - for dotfile in dotfiles: - if selection == dotfile.key: - exist = True - break - if not exist: - LOG.log('One of the keys is not found. Please double check ' \ - 'your arguments.') - sys.exit(0) - else: + # compare only specific files + selected = dotfiles + if focus: selected = [] - for dotfile in dotfiles: - selected.append(dotfile.key) + for selection in focus.replace(' ', '').split(','): + df = next((x for x in dotfiles if x.dst == selection), None) + if df: + selected.append(df) + else: + LOG.err('no dotfile matches \"%s\"' % (selection)) - for dotfile in dotfiles: - if dotfile.key in selected: - LOG.log('diffing \"%s\" VS \"%s\"' % (dotfile.key, dotfile.dst)) - inst.compare(t, tmp, opts['profile'], dotfile.src, dotfile.dst) + for dotfile in selected: + LOG.log('diffing \"%s\" VS \"%s\"' % (dotfile.key, dotfile.dst)) + inst.compare(t, tmp, opts['profile'], dotfile.src, dotfile.dst) return len(dotfiles) > 0 @@ -183,7 +172,6 @@ if __name__ == '__main__': ret = True args = docopt(USAGE, version=VERSION) conf = Cfg(args['--cfg']) - focus = args['--files'] opts = conf.get_configs() opts['dry'] = args['--dry'] @@ -204,7 +192,7 @@ if __name__ == '__main__': elif args['compare']: tmp = utils.get_tmpdir() - if compare(opts, conf, tmp, focus): + if compare(opts, conf, tmp, args['--files']): LOG.log('generated temporary files available under %s' % (tmp)) else: os.rmdir(tmp)