diff options
author | Jon Bernard <jbernard@jbernard.io> | 2017-12-19 06:01:30 +0200 |
---|---|---|
committer | Jon Bernard <jbernard@jbernard.io> | 2017-12-19 06:01:30 +0200 |
commit | 645e59b2719f4564abb349dc1bda48d41cab5f42 (patch) | |
tree | 142dc6b2727d1fcad7d7d1664af36f2d1411b2d5 | |
parent | 4b190cf8f8508b56794138e4f419c017ff70ad30 (diff) | |
download | dotfiles-645e59b2719f4564abb349dc1bda48d41cab5f42.tar.gz dotfiles-645e59b2719f4564abb349dc1bda48d41cab5f42.tar.bz2 dotfiles-645e59b2719f4564abb349dc1bda48d41cab5f42.zip |
Add support for adding symlinks
The remaining operations are still to be done, this is only for the add
operation for the moment. It appears that nesting works correctly and
all unit tests are passing. Tests for symlinks still need to be done.
-rw-r--r-- | dotfiles/dotfile.py | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/dotfiles/dotfile.py b/dotfiles/dotfile.py index 99541c0..8c5b38e 100644 --- a/dotfiles/dotfile.py +++ b/dotfiles/dotfile.py @@ -39,10 +39,15 @@ class Dotfile(object): def _link(self, debug): """Create a symlink from name to target, no error checking.""" + source = self.name + target = self.target + if self.name.islink(): + source = self.target + target = self.name.realpath() if debug: - echo('LINK %s -> %s' % (self.name, self.target)) + echo('LINK %s -> %s' % (source, target)) else: - self.name.mksymlinkto(self.target, absolute=0) + source.mksymlinkto(target, absolute=0) def _unlink(self, debug): """Remove a symlink in the home directory, no error checking.""" @@ -55,6 +60,10 @@ class Dotfile(object): """A shorter, more readable name given a home directory.""" return homedir.bestrelpath(self.name) + def is_present(self): + """Is this dotfile present in the repository?""" + return self.name.islink() and (self.name.realpath() == self.target) + @property def state(self): """The current state of this dotfile.""" @@ -71,16 +80,16 @@ class Dotfile(object): def add(self, debug=False): """Move a dotfile to it's target and create a symlink.""" - if self.name.check(link=1): - # XXX: if the name is already a link, we *could* just move it. + if self.is_present(): raise IsSymlink(self.name) if self.target.check(exists=1): raise TargetExists(self.name) self._ensure_dirs(debug) - if debug: - echo('MOVE %s -> %s' % (self.name, self.target)) - else: - self.name.move(self.target) + if not self.name.islink(): + if debug: + echo('MOVE %s -> %s' % (self.name, self.target)) + else: + self.name.move(self.target) self._link(debug) def remove(self, debug=False): |