aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaurent Bachelier <laurent@bachelier.name>2013-12-19 13:09:17 +0100
committerLaurent Bachelier <laurent@bachelier.name>2013-12-19 13:09:17 +0100
commitf15100ac700911d1f7abf02ef3c9958df96f0bfe (patch)
tree850f95858029ebbf7dec2f65cf94f87d4b866f8e
parentNot necessarily (diff)
downloadconfman-f15100ac700911d1f7abf02ef3c9958df96f0bfe.tar.xz
Support overwriting file target when symlink has same contents
-rw-r--r--confman.py15
1 files changed, 14 insertions, 1 deletions
diff --git a/confman.py b/confman.py
index 499b4bf..8eebdd4 100644
--- a/confman.py
+++ b/confman.py
@@ -87,10 +87,19 @@ class Action(object):
class SymlinkAction(Action):
+ FORCE_SAME = True
+ "Force replace destination file by symlink if source has same contents"
+
@classmethod
def matches(cls, filename):
return filename
+ def same_contents(self):
+ source = self.source_path()
+ dest = self.dest_path()
+ with open(source) as s, open(dest) as d:
+ return s.read() == d.read()
+
def check(self):
source = self.source_path()
if not osp.exists(source):
@@ -98,7 +107,7 @@ class SymlinkAction(Action):
dest = self.dest_path()
if osp.lexists(dest):
- if not osp.islink(dest):
+ if not osp.islink(dest) and not (self.FORCE_SAME and self.same_contents()):
resolve = "diff %s %s\nrm -vi %s" % (osp.abspath(source), osp.abspath(dest), osp.abspath(dest))
raise ActionException(self,
"Destination exists and is not a link",
@@ -116,6 +125,10 @@ class SymlinkAction(Action):
print "Link target altered"
else:
return
+ # if the destination is not a link, but has same contents as source
+ elif self.FORCE_SAME and self.same_contents():
+ os.unlink(dest)
+ print "Link target was a file with same contents"
else:
self._makedirs()