aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaurent Bachelier <laurent@bachelier.name>2010-03-28 20:19:25 +0200
committerLaurent Bachelier <laurent@bachelier.name>2010-03-28 20:19:25 +0200
commit2cf0e0de2fdf60fcf2bd1eb695cd5ab7f739da05 (patch)
tree7c797dc8d00cc53966841f26586cad5bc2d1130c
parentAdds a bit more documentation (diff)
downloadconfman-2cf0e0de2fdf60fcf2bd1eb695cd5ab7f739da05.tar.xz
Adds "Empty" action
The Empty action creates an empty file if it didn't exist already.
-rw-r--r--confman.py42
-rw-r--r--samples/conf4.p.py1
-rw-r--r--samples/emptyfile.empty0
3 files changed, 43 insertions, 0 deletions
diff --git a/confman.py b/confman.py
index 17c7c8f..db6c1c4 100644
--- a/confman.py
+++ b/confman.py
@@ -88,6 +88,38 @@ class SymlinkAction(Action):
print "Created new link: "+dest+" => "+source
+class EmptyAction(Action):
+ """
+ Ensures the destination file exists.
+ Creates an empty one if not.
+ """
+ matched = re.compile("\.empty$")
+
+ @classmethod
+ def matches(cls, filename):
+ if cls.matched.search(filename):
+ return cls.matched.sub("", filename)
+ return False
+
+ def check(self):
+ pass
+
+ def sync(self):
+ dest = self.dest_path()
+ # if the file does not exist
+ if not os.path.exists(dest):
+ # but it's a broken link
+ if os.path.islink(dest):
+ raise ActionException(self, "Destination is a broken link")
+ else:
+ self._makedirs()
+ open(dest, "w").close()
+ print "Created new empty file: "+dest
+
+ def __repr__(self):
+ return self.__class__.__name__+': EMPTY => '+self.dest
+
+
class CopyAction(Action):
pass #TODO
@@ -105,6 +137,11 @@ class SymlinkForwarder(Forwarder):
return SymlinkAction(parent.config, parent.relpath, self.filename, parent.dest)
+class EmptyForwarder(Forwarder):
+ def get_proxy(self, parent):
+ return EmptyAction(parent.config, parent.relpath, None, parent.dest)
+
+
class IgnoreForwarder(Forwarder):
def get_proxy(self, parent):
return None
@@ -127,6 +164,9 @@ class ProgrammableAction(Action):
def redirect(filename):
raise SymlinkForwarder("_"+filename)
+ def empty():
+ raise EmptyForwarder()
+
def ignore():
raise IgnoreForwarder()
@@ -134,6 +174,7 @@ class ProgrammableAction(Action):
{
"options": self.config.options,
"redirect": redirect,
+ "empty": empty,
"ignore": ignore,
}
@@ -186,6 +227,7 @@ class ConfigSource(object):
self.classes = [
ProgrammableAction,
IgnoreAction,
+ EmptyAction,
SymlinkAction,
]
diff --git a/samples/conf4.p.py b/samples/conf4.p.py
new file mode 100644
index 0000000..74c2212
--- /dev/null
+++ b/samples/conf4.p.py
@@ -0,0 +1 @@
+empty()
diff --git a/samples/emptyfile.empty b/samples/emptyfile.empty
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/samples/emptyfile.empty