From 79c9c0747e876c0b97a4fb3fe91fa9825e462722 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sun, 15 Nov 2020 16:25:57 +0100 Subject: Add a dry run mode. --- dotbot/plugins/link.py | 50 +++++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 21 deletions(-) (limited to 'dotbot/plugins/link.py') diff --git a/dotbot/plugins/link.py b/dotbot/plugins/link.py index 411a0ef..5e2bd90 100644 --- a/dotbot/plugins/link.py +++ b/dotbot/plugins/link.py @@ -11,7 +11,7 @@ class Link(dotbot.Plugin): _directive = 'link' - def handle(self, directive, links): + def handle(self, directive, links, dry_run): success = True defaults = self._context.defaults().get('link', {}) for destination, source in links.items(): @@ -47,12 +47,20 @@ class Link(dotbot.Plugin): (destination, path)) continue if force or relink: - success &= self._delete(path, destination, relative, canonical_path, force) - success &= self._link(path, destination, relative, canonical_path, ignore_missing) - if success: - self._log.verbose('All links have been set up') + success &= self._delete(path, destination, relative, canonical_path, force, dry_run) + success &= self._link(path, destination, relative, canonical_path, ignore_missing, dry_run) + + if dry_run: + if success: + self._log.verbose('link/dry run: nothing to do') + else: + self._log.verbose('link/dry run: some targets are missing') else: - self._log.error('Some links were not successfully set up') + if success: + self._log.verbose('All links have been set up') + else: + self._log.error('Some links were not successfully set up') + return success def _test_success(self, command): @@ -91,7 +99,7 @@ class Link(dotbot.Plugin): path = os.path.expanduser(path) return os.path.exists(path) - def _delete(self, source, path, relative, canonical_path, force): + def _delete(self, source, path, relative, canonical_path, force, dry_run): success = True source = os.path.join(self._context.base_directory(canonical_path=canonical_path), source) fullpath = os.path.expanduser(path) @@ -99,7 +107,11 @@ class Link(dotbot.Plugin): source = self._relative_path(source, fullpath) if ((self._is_link(path) and self._link_destination(path) != source) or (self._exists(path) and not self._is_link(path))): - removed = False + self._log.info('Removing %s' % path) + + if dry_run: + return False + try: if os.path.islink(fullpath): os.unlink(fullpath) @@ -107,16 +119,12 @@ class Link(dotbot.Plugin): elif force: if os.path.isdir(fullpath): shutil.rmtree(fullpath) - removed = True else: os.remove(fullpath) - removed = True except OSError: self._log.warning('Failed to remove %s' % path) success = False - else: - if removed: - self._log.info('Removing %s' % path) + return success def _relative_path(self, source, destination): @@ -127,7 +135,7 @@ class Link(dotbot.Plugin): destination_dir = os.path.dirname(destination) return os.path.relpath(source, destination_dir) - def _link(self, source, link_name, relative, canonical_path, ignore_missing): + def _link(self, source, link_name, relative, canonical_path, ignore_missing, dry_run): ''' Links link_name to source. @@ -149,13 +157,13 @@ class Link(dotbot.Plugin): # directory, and if source is relative, it will be relative to the # destination directory elif not self._exists(link_name) and (ignore_missing or self._exists(absolute_source)): - try: - os.symlink(source, destination) - except OSError: - self._log.warning('Linking failed %s -> %s' % (link_name, source)) - else: - self._log.verbose('Creating link %s -> %s' % (link_name, source)) - success = True + self._log.verbose('Creating link %s -> %s' % (link_name, source)) + if not dry_run: + try: + os.symlink(source, destination) + success = True + except OSError: + self._log.warning('Linking failed %s -> %s' % (link_name, source)) elif self._exists(link_name) and not self._is_link(link_name): self._log.warning( '%s already exists but is a regular file or directory' % -- cgit v1.2.3