mirror of
1
0
Fork 0

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.
This commit is contained in:
Benjamin Motz 2019-03-13 10:55:29 +01:00
parent b062aeaf15
commit 8af95827ff
1 changed files with 34 additions and 6 deletions

View File

@ -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)