diff options
author | Anish Athalye <me@anishathalye.com> | 2014-10-22 13:27:34 -0400 |
---|---|---|
committer | Anish Athalye <me@anishathalye.com> | 2014-10-22 14:42:23 -0400 |
commit | 13c925be87ddf946773689ec51fd0251f795a9c4 (patch) | |
tree | 31b01f49632867d099bec50a0894d6ffa55aabe8 /dotbot | |
parent | 58e4fb50b176c1a7126f8ccfcd5ecbd71b46860f (diff) |
Add functionality to forcibly link items
This commit introduces an extended configuration syntax for linking
files and directories. Currently, this syntax allows for specifying
items to be forcibly linked, overwriting existing files or directories
if necessary.
The extended configuration syntax was proposed by Travers McInerney
<travers@mcinerney.me>.
Diffstat (limited to 'dotbot')
-rw-r--r-- | dotbot/executor/linker.py | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/dotbot/executor/linker.py b/dotbot/executor/linker.py index 60c76b5..2d14ecc 100644 --- a/dotbot/executor/linker.py +++ b/dotbot/executor/linker.py @@ -1,4 +1,4 @@ -import os +import os, shutil from . import Executor class Linker(Executor): @@ -19,7 +19,15 @@ class Linker(Executor): def _process_links(self, links): success = True for destination, source in links.items(): - success &= self._link(source, destination) + if isinstance(source, dict): + # extended config + path = source['path'] + force = source.get('force', False) + if force: + success &= self._delete(destination) + else: + path = source + success &= self._link(path, destination) if success: self._log.info('All links have been set up') else: @@ -47,6 +55,22 @@ class Linker(Executor): path = os.path.expanduser(path) return os.path.exists(path) + def _delete(self, path): + success = True + if self._exists(path) and not self._is_link(path): + fullpath = os.path.expanduser(path) + try: + if os.path.isdir(fullpath): + shutil.rmtree(fullpath) + else: + os.remove(fullpath) + except OSError: + self._log.warning('Failed to remove %s' % path) + success = False + else: + self._log.lowinfo('Removing %s' % path) + return success + def _link(self, source, link_name): ''' Links link_name to source. |