summaryrefslogtreecommitdiffstats
path: root/gitautodeploy/lock.py
diff options
context:
space:
mode:
authorOliver Poignant <oliver@poignant.se>2016-03-06 21:05:20 +0100
committerOliver Poignant <oliver@poignant.se>2016-03-06 21:05:20 +0100
commit3f7a830cdadc82f10bad26b060f2701ed08b34a9 (patch)
treee62af59711f37b88e64b6155aaa57904c3f3e7c7 /gitautodeploy/lock.py
parentec1f2a28c902855aa470f8523eea33f8aed6a2b5 (diff)
downloadGit-Auto-Deploy-3f7a830cdadc82f10bad26b060f2701ed08b34a9.zip
Git-Auto-Deploy-3f7a830cdadc82f10bad26b060f2701ed08b34a9.tar.gz
Git-Auto-Deploy-3f7a830cdadc82f10bad26b060f2701ed08b34a9.tar.bz2
Moved files around into new project structure
Diffstat (limited to 'gitautodeploy/lock.py')
-rw-r--r--gitautodeploy/lock.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/gitautodeploy/lock.py b/gitautodeploy/lock.py
new file mode 100644
index 0000000..8bd8b32
--- /dev/null
+++ b/gitautodeploy/lock.py
@@ -0,0 +1,52 @@
+class Lock():
+ """Simple implementation of a mutex lock using the file systems. Works on
+ *nix systems."""
+
+ path = None
+ _has_lock = False
+
+ def __init__(self, path):
+ self.path = path
+
+ def obtain(self):
+ import os
+ import logging
+ logger = logging.getLogger()
+
+ try:
+ os.open(self.path, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
+ self._has_lock = True
+ logger.info("Successfully obtained lock: %s" % self.path)
+ except OSError:
+ return False
+ else:
+ return True
+
+ def release(self):
+ import os
+ import logging
+ logger = logging.getLogger()
+
+ if not self._has_lock:
+ raise Exception("Unable to release lock that is owned by another process")
+ try:
+ os.remove(self.path)
+ logger.info("Successfully released lock: %s" % self.path)
+ finally:
+ self._has_lock = False
+
+ def has_lock(self):
+ return self._has_lock
+
+ def clear(self):
+ import os
+ import logging
+ logger = logging.getLogger()
+
+ try:
+ os.remove(self.path)
+ except OSError:
+ pass
+ finally:
+ logger.info("Successfully cleared lock: %s" % self.path)
+ self._has_lock = False \ No newline at end of file