summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2014-12-04 19:25:27 +0000
committerGerrit Code Review <review@openstack.org>2014-12-04 19:25:28 +0000
commit777758cdf4520271370b3338b86b5c66f9b104f0 (patch)
tree1e33c80387e0d25b87819a3a5d7bea63be96f7c1
parent7056a48a16bae5ddc806fcd1683df12b880d5701 (diff)
parent90b5811156079aa000dbf297a85d2da7121f2a59 (diff)
downloadlockfile-777758cdf4520271370b3338b86b5c66f9b104f0.tar.gz
Merge "Begin moving some of the common code to a shared base"
-rw-r--r--lockfile/__init__.py87
1 files changed, 48 insertions, 39 deletions
diff --git a/lockfile/__init__.py b/lockfile/__init__.py
index d905af9..419d122 100644
--- a/lockfile/__init__.py
+++ b/lockfile/__init__.py
@@ -1,3 +1,5 @@
+# -*- coding: utf-8 -*-
+
"""
lockfile.py - Platform-independent advisory file locks.
@@ -154,14 +156,58 @@ class NotMyLock(UnlockError):
"""
pass
-class LockBase:
+class _SharedBase(object):
+ def __init__(self, path):
+ self.path = path
+
+ def acquire(self, timeout=None):
+ """
+ Acquire the lock.
+
+ * If timeout is omitted (or None), wait forever trying to lock the
+ file.
+
+ * If timeout > 0, try to acquire the lock for that many seconds. If
+ the lock period expires and the file is still locked, raise
+ LockTimeout.
+
+ * If timeout <= 0, raise AlreadyLocked immediately if the file is
+ already locked.
+ """
+ raise NotImplemented("implement in subclass")
+
+ def release(self):
+ """
+ Release the lock.
+
+ If the file is not locked, raise NotLocked.
+ """
+ raise NotImplemented("implement in subclass")
+
+ def __enter__(self):
+ """
+ Context manager support.
+ """
+ self.acquire()
+ return self
+
+ def __exit__(self, *_exc):
+ """
+ Context manager support.
+ """
+ self.release()
+
+ def __repr__(self):
+ return "<%s: %r>" % (self.__class__.__name__, self.path)
+
+class LockBase(_SharedBase):
"""Base class for platform-specific lock classes."""
def __init__(self, path, threaded=True, timeout=None):
"""
>>> lock = LockBase('somefile')
>>> lock = LockBase('somefile', threaded=False)
"""
- self.path = path
+ super(LockBase, self).__init__(path)
self.lock_file = os.path.abspath(path) + ".lock"
self.hostname = socket.gethostname()
self.pid = os.getpid()
@@ -189,30 +235,6 @@ class LockBase:
hash(self.path)))
self.timeout = timeout
- def acquire(self, timeout=None):
- """
- Acquire the lock.
-
- * If timeout is omitted (or None), wait forever trying to lock the
- file.
-
- * If timeout > 0, try to acquire the lock for that many seconds. If
- the lock period expires and the file is still locked, raise
- LockTimeout.
-
- * If timeout <= 0, raise AlreadyLocked immediately if the file is
- already locked.
- """
- raise NotImplemented("implement in subclass")
-
- def release(self):
- """
- Release the lock.
-
- If the file is not locked, raise NotLocked.
- """
- raise NotImplemented("implement in subclass")
-
def is_locked(self):
"""
Tell whether or not the file is locked.
@@ -231,19 +253,6 @@ class LockBase:
"""
raise NotImplemented("implement in subclass")
- def __enter__(self):
- """
- Context manager support.
- """
- self.acquire()
- return self
-
- def __exit__(self, *_exc):
- """
- Context manager support.
- """
- self.release()
-
def __repr__(self):
return "<%s: %r -- %r>" % (self.__class__.__name__, self.unique_name,
self.path)