summaryrefslogtreecommitdiffstats
path: root/gitautodeploy/lock.py
diff options
context:
space:
mode:
authorVlad Ionescu <vlad@vladionescu.com>2016-05-25 15:07:01 -0700
committerVlad Ionescu <vlad@vladionescu.com>2016-05-25 15:07:01 -0700
commite707129cd9e427560cb8fa8884026fcbefbfa3a7 (patch)
tree2699b87f630f06b54b4ce5301a56371705a1543f /gitautodeploy/lock.py
parent6dc1656f4bfec511405d36973b68538aa95b26a7 (diff)
downloadGit-Auto-Deploy-e707129cd9e427560cb8fa8884026fcbefbfa3a7.zip
Git-Auto-Deploy-e707129cd9e427560cb8fa8884026fcbefbfa3a7.tar.gz
Git-Auto-Deploy-e707129cd9e427560cb8fa8884026fcbefbfa3a7.tar.bz2
Windows does not support all of *nix's signals, so check before...
Relying on them. The Python documentation mentions which signals are supported (emulated, really) on Windows. "On Windows, signal() can only be called with SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, or SIGTERM. A ValueError will be raised in any other case." From https://docs.python.org/3/library/signal.html#signal.signal
Diffstat (limited to 'gitautodeploy/lock.py')
-rw-r--r--gitautodeploy/lock.py35
1 files changed, 15 insertions, 20 deletions
diff --git a/gitautodeploy/lock.py b/gitautodeploy/lock.py
index b0b408b..f894bb3 100644
--- a/gitautodeploy/lock.py
+++ b/gitautodeploy/lock.py
@@ -1,12 +1,15 @@
+from lockfile import LockFile
+
class Lock():
"""Simple implementation of a mutex lock using the file systems. Works on
*nix systems."""
path = None
- _has_lock = False
+ lock = None
def __init__(self, path):
self.path = path
+ self.lock = LockFile(path)
def obtain(self):
import os
@@ -14,39 +17,31 @@ class Lock():
logger = logging.getLogger()
try:
- os.open(self.path, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
- self._has_lock = True
+ self.lock.acquire(0)
logger.debug("Successfully obtained lock: %s" % self.path)
- except OSError:
+ except AlreadyLocked:
return False
- else:
- return True
+
+ return True
def release(self):
import os
import logging
logger = logging.getLogger()
- if not self._has_lock:
+ if not self.has_lock():
raise Exception("Unable to release lock that is owned by another process")
- try:
- os.remove(self.path)
- logger.debug("Successfully released lock: %s" % self.path)
- finally:
- self._has_lock = False
+
+ self.lock.release()
+ logger.debug("Successfully released lock: %s" % self.path)
def has_lock(self):
- return self._has_lock
+ return self.lock.i_am_locking()
def clear(self):
import os
import logging
logger = logging.getLogger()
- try:
- os.remove(self.path)
- except OSError:
- pass
- finally:
- logger.debug("Successfully cleared lock: %s" % self.path)
- self._has_lock = False \ No newline at end of file
+ self.lock.break_lock()
+ logger.debug("Successfully cleared lock: %s" % self.path)