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/clean.py | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) (limited to 'dotbot/plugins/clean.py') diff --git a/dotbot/plugins/clean.py b/dotbot/plugins/clean.py index c8f3a03..212fb07 100644 --- a/dotbot/plugins/clean.py +++ b/dotbot/plugins/clean.py @@ -9,7 +9,7 @@ class Clean(dotbot.Plugin): _directive = 'clean' - def handle(self, directive, targets): + def handle(self, directive, targets, dry_run): success = True defaults = self._context.defaults().get(self._directive, {}) for target in targets: @@ -18,14 +18,22 @@ class Clean(dotbot.Plugin): if isinstance(targets, dict) and isinstance(targets[target], dict): force = targets[target].get('force', force) recursive = targets[target].get('recursive', recursive) - success &= self._clean(target, force, recursive) - if success: - self._log.verbose('All targets have been cleaned') + success &= self._clean(target, force, recurseve, dry_run) + + if dry_run: + if success: + self._log.verbose('clean/dry run: nothing to do') + else: + self._log.verbose('clean/dry run: some targets are dirty') else: - self._log.error('Some targets were not successfully cleaned') + if success: + self._log.verbose('All targets have been cleaned') + else: + self._log.error('Some targets were not successfully cleaned') + return success - def _clean(self, target, force, recursive): + def _clean(self, target, force, recursive, dry_run): ''' Cleans all the broken symbolic links in target if they point to a subdirectory of the base directory or if forced to clean. @@ -33,21 +41,28 @@ class Clean(dotbot.Plugin): if not os.path.isdir(os.path.expandvars(os.path.expanduser(target))): self._log.debug('Ignoring nonexistent directory %s' % target) return True + + ret = True for item in os.listdir(os.path.expandvars(os.path.expanduser(target))): path = os.path.join(os.path.expandvars(os.path.expanduser(target)), item) if recursive and os.path.isdir(path): # isdir implies not islink -- we don't want to descend into # symlinked directories. okay to do a recursive call here # because depth should be fairly limited - self._clean(path, force, recursive) + ret &= self._clean(path, force, recursive) if not os.path.exists(path) and os.path.islink(path): points_at = os.path.join(os.path.dirname(path), os.readlink(path)) if self._in_directory(path, self._context.base_directory()) or force: self._log.info('Removing invalid link %s -> %s' % (path, points_at)) - os.remove(path) + + if dry_run: + ret = False + else: + os.remove(path) else: self._log.verbose('Link %s -> %s not removed.' % (path, points_at)) - return True + + return ret def _in_directory(self, path, directory): ''' -- cgit v1.2.3