From dece710399e305a2d94a0786e71c56dcdba14430 Mon Sep 17 00:00:00 2001 From: Ben Klein Date: Sat, 27 Jan 2018 02:27:44 -0500 Subject: [PATCH 1/7] Implement globbing support --- dotbot/dispatcher.py | 3 ++- plugins/link.py | 56 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/dotbot/dispatcher.py b/dotbot/dispatcher.py index cc07435..d1a4f95 100644 --- a/dotbot/dispatcher.py +++ b/dotbot/dispatcher.py @@ -30,10 +30,11 @@ class Dispatcher(object): try: success &= plugin.handle(action, task[action]) handled = True - except Exception: + except Exception as err: self._log.error( 'An error was encountered while executing action %s' % action) + self._log.debug(err) if not handled: success = False self._log.error('Action %s not handled' % action) diff --git a/plugins/link.py b/plugins/link.py index 4b50320..b77b956 100644 --- a/plugins/link.py +++ b/plugins/link.py @@ -1,4 +1,5 @@ import os +import glob import shutil import dotbot @@ -27,26 +28,60 @@ class Link(dotbot.Plugin): force = defaults.get('force', False) relink = defaults.get('relink', False) create = defaults.get('create', False) + use_glob = defaults.get('use_glob', False) if isinstance(source, dict): # extended config relative = source.get('relative', relative) force = source.get('force', force) relink = source.get('relink', relink) create = source.get('create', create) + use_glob = source.get('use_glob', use_glob) path = self._default_source(destination, source.get('path')) else: path = self._default_source(destination, source) path = os.path.expandvars(os.path.expanduser(path)) - if not self._exists(os.path.join(self._context.base_directory(), path)): - success = False - self._log.warning('Nonexistent target %s -> %s' % - (destination, path)) - continue - if create: - success &= self._create(destination) - if force or relink: - success &= self._delete(path, destination, relative, force) - success &= self._link(path, destination, relative) + if use_glob: + self._log.debug("Globbing with path: " + str(path)) + glob_results = glob.glob(path) + if len(glob_results) is 0: + self._log.warning("Globbing couldn't find anything matching " + str(path)) + success = False + continue + glob_star_loc = path.find('*') + if glob_star_loc is -1 and destination[-1] is '/': + self._log.error("Ambiguous action requested.") + self._log.error("No wildcard in glob, directory use undefined: " + + destination + " -> " + str(glob_results)) + self._log.warning("Did you want to link the directory or into it?") + success = False + continue + elif glob_star_loc is -1 and len(glob_results) is 1: + # perform a normal link operation + if create: + success &= self._create(destination) + if force or relink: + success &= self._delete(path, destination, relative, force) + success &= self._link(path, destination, relative) + else: + self._log.lowinfo("Linking globbed items: " + str(glob_results)) + glob_base = path[:glob_star_loc] + for glob_full_item in glob_results: + glob_item = glob_full_item[len(glob_base):] + glob_link_destination = destination + glob_item + if create: + success &= self._create(glob_link_destination) + success &= self._link(glob_full_item, glob_link_destination, relative) + else: + if create: + success &= self._create(destination) + if not self._exists(os.path.join(self._context.base_directory(), path)): + success = False + self._log.warning('Nonexistent target %s -> %s' % + (destination, path)) + continue + if force or relink: + success &= self._delete(path, destination, relative, force) + success &= self._link(path, destination, relative) if success: self._log.info('All links have been set up') else: @@ -87,6 +122,7 @@ class Link(dotbot.Plugin): success = True parent = os.path.abspath(os.path.join(os.path.expanduser(path), os.pardir)) if not self._exists(parent): + self._log.debug("Try to create parent: " + str(parent)) try: os.makedirs(parent) except OSError: From 564d16fcd55f17e71012fd79bec39f2846fe484f Mon Sep 17 00:00:00 2001 From: Ben Klein Date: Sat, 27 Jan 2018 04:11:11 -0500 Subject: [PATCH 2/7] Allow force and relink on glob items --- plugins/link.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/link.py b/plugins/link.py index b77b956..d0f057e 100644 --- a/plugins/link.py +++ b/plugins/link.py @@ -63,13 +63,15 @@ class Link(dotbot.Plugin): success &= self._delete(path, destination, relative, force) success &= self._link(path, destination, relative) else: - self._log.lowinfo("Linking globbed items: " + str(glob_results)) + self._log.lowinfo("Globs from '" + path + "': " + str(glob_results)) glob_base = path[:glob_star_loc] for glob_full_item in glob_results: glob_item = glob_full_item[len(glob_base):] glob_link_destination = destination + glob_item if create: success &= self._create(glob_link_destination) + if force or relink: + success &= self._delete(glob_full_item, glob_link_destination, relative, force) success &= self._link(glob_full_item, glob_link_destination, relative) else: if create: From 7ebb601a10eb7fdcb533d4f4ca837d97d27f484a Mon Sep 17 00:00:00 2001 From: Ben Klein Date: Wed, 31 Jan 2018 18:32:00 -0500 Subject: [PATCH 3/7] Add use_globs to readme --- README.md | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1d6e938..e5a58b2 100644 --- a/README.md +++ b/README.md @@ -165,17 +165,22 @@ files if necessary. Environment variables in paths are automatically expanded. Link commands are specified as a dictionary mapping targets to source locations. Source locations are specified relative to the base directory (that -is specified when running the installer). Directory names should *not* contain -a trailing "/" character. +is specified when running the installer). If linking directories, *do not* include a trailing slash. Link commands support an (optional) extended configuration. In this type of configuration, instead of specifying source locations directly, targets are -mapped to extended configuration dictionaries. These dictionaries map `path` to -the source path, specify `create` as `true` if the parent directory should be -created if necessary, specify `relink` as `true` if incorrect symbolic links -should be automatically overwritten, specify `force` as `true` if the file or -directory should be forcibly linked, and specify `relative` as `true` if the -symbolic link should have a relative path. +mapped to extended configuration dictionaries. + +Available extended configuration parameters: + +| Link Option | Explanation | +| -- | -- | +| `path` | The target for the symlink, the same as in the shortcut syntax (default:null, automatic (see below)) | +| `create` | When true, create parent directories to the link as needed. (default:false) | +| `relink` | Removes the old target if it's a symlink (default:false) | +| `force` | Force removes the old target, file or folder, and forces a new link (default:false) | +| `relative` | Use a relative path when creating the symlink (default:false, absolute links) | +| `use_glob` | Treat a `*` character as a wildcard, and perform link operations on all of those matches (default:false) | #### Example @@ -207,6 +212,10 @@ the following three config files equivalent: ~/.zshrc: force: true path: zshrc + ~/.config/: + use_glob: true + path: config/* + relink: true ``` ```yaml @@ -217,6 +226,10 @@ the following three config files equivalent: relink: true ~/.zshrc: force: true + ~/.config/: + use_glob: true + path: config/* + relink: true ``` ```json @@ -230,6 +243,11 @@ the following three config files equivalent: }, "~/.zshrc": { "force": true + }, + "~/.config/": { + "use_glob": true, + "path": "config/*", + "relink": true } } } From 7d069b4ac8723be24010ac79c1185a231fa458f2 Mon Sep 17 00:00:00 2001 From: Anish Athalye Date: Fri, 6 Apr 2018 21:59:28 +0530 Subject: [PATCH 4/7] Rename 'use_glob' to 'glob' --- README.md | 8 ++++---- plugins/link.py | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e5a58b2..06d073b 100644 --- a/README.md +++ b/README.md @@ -180,7 +180,7 @@ Available extended configuration parameters: | `relink` | Removes the old target if it's a symlink (default:false) | | `force` | Force removes the old target, file or folder, and forces a new link (default:false) | | `relative` | Use a relative path when creating the symlink (default:false, absolute links) | -| `use_glob` | Treat a `*` character as a wildcard, and perform link operations on all of those matches (default:false) | +| `glob` | Treat a `*` character as a wildcard, and perform link operations on all of those matches (default:false) | #### Example @@ -213,7 +213,7 @@ the following three config files equivalent: force: true path: zshrc ~/.config/: - use_glob: true + glob: true path: config/* relink: true ``` @@ -227,7 +227,7 @@ the following three config files equivalent: ~/.zshrc: force: true ~/.config/: - use_glob: true + glob: true path: config/* relink: true ``` @@ -245,7 +245,7 @@ the following three config files equivalent: "force": true }, "~/.config/": { - "use_glob": true, + "glob": true, "path": "config/*", "relink": true } diff --git a/plugins/link.py b/plugins/link.py index d0f057e..64d2070 100644 --- a/plugins/link.py +++ b/plugins/link.py @@ -28,14 +28,14 @@ class Link(dotbot.Plugin): force = defaults.get('force', False) relink = defaults.get('relink', False) create = defaults.get('create', False) - use_glob = defaults.get('use_glob', False) + use_glob = defaults.get('glob', False) if isinstance(source, dict): # extended config relative = source.get('relative', relative) force = source.get('force', force) relink = source.get('relink', relink) create = source.get('create', create) - use_glob = source.get('use_glob', use_glob) + use_glob = source.get('glob', use_glob) path = self._default_source(destination, source.get('path')) else: path = self._default_source(destination, source) From 8d08e4b1adfd00092835b6aed0edecd222f49977 Mon Sep 17 00:00:00 2001 From: Anish Athalye Date: Fri, 6 Apr 2018 22:09:57 +0530 Subject: [PATCH 5/7] Add tests for globbing --- test/tests/link-glob-ambiguous.bash | 45 ++++++++++++++++++++++++++ test/tests/link-glob-multi-star.bash | 31 ++++++++++++++++++ test/tests/link-glob.bash | 47 ++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 test/tests/link-glob-ambiguous.bash create mode 100644 test/tests/link-glob-multi-star.bash create mode 100644 test/tests/link-glob.bash diff --git a/test/tests/link-glob-ambiguous.bash b/test/tests/link-glob-ambiguous.bash new file mode 100644 index 0000000..97d42cd --- /dev/null +++ b/test/tests/link-glob-ambiguous.bash @@ -0,0 +1,45 @@ +test_description='link glob ambiguous' +. '../test-lib.bash' + +test_expect_success 'setup' ' +mkdir ${DOTFILES}/bin +' + +test_expect_failure 'run 1' ' +run_dotbot < ${DOTFILES}/config/foo/a && +echo "banana" > ${DOTFILES}/config/bar/b && +echo "cherry" > ${DOTFILES}/config/bar/c +' + +test_expect_success 'run' ' +run_dotbot -v < ${DOTFILES}/bin/a && +echo "banana" > ${DOTFILES}/bin/b && +echo "cherry" > ${DOTFILES}/bin/c +' + +test_expect_success 'run 1' ' +run_dotbot -v < Date: Fri, 6 Apr 2018 22:26:16 +0530 Subject: [PATCH 6/7] Fix bug --- plugins/link.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/link.py b/plugins/link.py index 64d2070..5274e3b 100644 --- a/plugins/link.py +++ b/plugins/link.py @@ -67,7 +67,7 @@ class Link(dotbot.Plugin): glob_base = path[:glob_star_loc] for glob_full_item in glob_results: glob_item = glob_full_item[len(glob_base):] - glob_link_destination = destination + glob_item + glob_link_destination = os.path.join(destination, glob_item) if create: success &= self._create(glob_link_destination) if force or relink: From 972e80f18850d0cd45f22d2a4c14320f3cd204c6 Mon Sep 17 00:00:00 2001 From: Anish Athalye Date: Fri, 6 Apr 2018 22:37:52 +0530 Subject: [PATCH 7/7] Fix tests on Travis CI The tests were failing due to the '~/bin' directory already existing on the machine. This patch changes the tests to use the directory name 'foo'. --- test/tests/link-glob-ambiguous.bash | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/tests/link-glob-ambiguous.bash b/test/tests/link-glob-ambiguous.bash index 97d42cd..7e23348 100644 --- a/test/tests/link-glob-ambiguous.bash +++ b/test/tests/link-glob-ambiguous.bash @@ -2,44 +2,44 @@ test_description='link glob ambiguous' . '../test-lib.bash' test_expect_success 'setup' ' -mkdir ${DOTFILES}/bin +mkdir ${DOTFILES}/foo ' test_expect_failure 'run 1' ' run_dotbot <