diff options
author | Anish Athalye <me@anishathalye.com> | 2016-02-14 22:39:48 -0500 |
---|---|---|
committer | Anish Athalye <me@anishathalye.com> | 2016-02-14 23:06:52 -0500 |
commit | daf8d82e02c2bcc476932790fa25076631c4c312 (patch) | |
tree | 1413dbbb3e810618e127a17ac3843ee9a8be653c /plugins | |
parent | c402396c58114640745dbba132856cfd8cd7f422 (diff) |
Add functionality to create relative links
This commit adds an option to the extended configuration syntax for
linking files and directories. Enabling the relative option makes it so
that symbolic links are created with relative paths instead of absolute
paths.
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/link.py | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/plugins/link.py b/plugins/link.py index 429158d..3bb5686 100644 --- a/plugins/link.py +++ b/plugins/link.py @@ -23,6 +23,7 @@ class Link(dotbot.Plugin): if isinstance(source, dict): # extended config path = source['path'] + relative = source.get('relative', False) force = source.get('force', False) relink = source.get('relink', False) create = source.get('create', False) @@ -33,8 +34,9 @@ class Link(dotbot.Plugin): elif relink: success &= self._delete(path, destination, force=False) else: + relative = False path = source - success &= self._link(path, destination) + success &= self._link(path, destination, relative) if success: self._log.info('All links have been set up') else: @@ -101,7 +103,7 @@ class Link(dotbot.Plugin): self._log.lowinfo('Removing %s' % path) return success - def _link(self, source, link_name): + def _link(self, source, link_name, relative): ''' Links link_name to source. @@ -115,7 +117,11 @@ class Link(dotbot.Plugin): (link_name, self._link_destination(link_name))) elif not self._exists(link_name) and self._exists(source): try: - os.symlink(source, os.path.expanduser(link_name)) + destination = os.path.expanduser(link_name) + if relative: + destination_dir = os.path.dirname(destination) + source = os.path.relpath(source, destination_dir) + os.symlink(source, destination) except OSError: self._log.warning('Linking failed %s -> %s' % (link_name, source)) else: |