From 8af95827ff06563a02f44af6c681e80267517aa9 Mon Sep 17 00:00:00 2001 From: Benjamin Motz Date: Wed, 13 Mar 2019 10:55:29 +0100 Subject: [PATCH] Implement creation of missing target files or directories If the target in the dotbot-repo doesn't exist, copy from the original. The links are then created afterwards. --- dotbot/plugins/link.py | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/dotbot/plugins/link.py b/dotbot/plugins/link.py index 2506237..ec43d89 100644 --- a/dotbot/plugins/link.py +++ b/dotbot/plugins/link.py @@ -80,13 +80,19 @@ class Link(dotbot.Plugin): success &= self._delete(glob_full_item, glob_link_destination, relative, force) success &= self._link(glob_full_item, glob_link_destination, relative) else: - if create: + if (not self._exists( + os.path.join(self._context.base_directory(), path)) + and not os.path.islink(destination)): + success &= self._create_target(path, destination) + if success: + self._log.info('Moved %s to nonexistent target %s' % + (destination, path)) + else: + self._log.warning('Nonexistent target %s -> %s' % + (destination, path)) + continue + elif 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) @@ -153,6 +159,28 @@ class Link(dotbot.Plugin): self._log.lowinfo('Creating directory %s' % parent) return success + def _create_target(self, target, link_name): + """Create non-existent target by moving the file or directory at link_name""" + success = True + copy_source = os.path.expanduser(link_name) + copy_destination = os.path.join(self._context.base_directory(), target) + parent = os.path.dirname(copy_destination) + if not self._exists(parent): + self._log.debug("Try to create parent: " + str(parent)) + try: + os.makedirs(parent) + except OSError: + self._log.warning('Failed to create directory %s' % parent) + success = False + else: + self._log.lowinfo('Creating directory %s' % parent) + try: + shutil.move(copy_source, copy_destination) + except Exception as ex: + self._log.warning("Couldn't move %s to %s:\n%s" % (link_name, target, ex)) + success = False + return success + def _delete(self, source, path, relative, force): success = True source = os.path.join(self._context.base_directory(), source)