diff options
author | Anish Athalye <me@anishathalye.com> | 2015-05-02 22:27:05 -0400 |
---|---|---|
committer | Anish Athalye <me@anishathalye.com> | 2015-05-02 22:30:14 -0400 |
commit | 3725d216849f474ef627c708ac77c3cd15053fc4 (patch) | |
tree | 359fa4866cc79d04452e009e63cf5aa7a608e443 /dotbot | |
parent | db8364490da392b4225844ddfc32e5fa69aa3821 (diff) |
Add functionality to overwrite broken links
This commit adds an option to the extended configuration syntax for
linking files and directories. The relink option is a safe alternative
to forcibly linking that only removes broken symbolic links, so it
cannot result in data loss.
Diffstat (limited to 'dotbot')
-rw-r--r-- | dotbot/executor/linker.py | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/dotbot/executor/linker.py b/dotbot/executor/linker.py index 9821fe7..f0e94bc 100644 --- a/dotbot/executor/linker.py +++ b/dotbot/executor/linker.py @@ -23,11 +23,14 @@ class Linker(Executor): # extended config path = source['path'] force = source.get('force', False) + relink = source.get('relink', False) create = source.get('create', False) if create: success &= self._create(destination) if force: - success &= self._delete(path, destination) + success &= self._delete(path, destination, force=True) + elif relink: + success &= self._delete(path, destination, force=False) else: path = source success &= self._link(path, destination) @@ -71,24 +74,30 @@ class Linker(Executor): self._log.lowinfo('Creating directory %s' % parent) return success - def _delete(self, source, path): + def _delete(self, source, path, force): success = True source = os.path.join(self._base_directory, source) if ((self._is_link(path) and self._link_destination(path) != source) or (self._exists(path) and not self._is_link(path))): fullpath = os.path.expanduser(path) + removed = False try: if os.path.islink(fullpath): os.unlink(fullpath) - elif os.path.isdir(fullpath): - shutil.rmtree(fullpath) - else: - os.remove(fullpath) + removed = True + 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: - self._log.lowinfo('Removing %s' % path) + if removed: + self._log.lowinfo('Removing %s' % path) return success def _link(self, source, link_name): |