From 90b5811156079aa000dbf297a85d2da7121f2a59 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Wed, 17 Sep 2014 14:27:53 -0700 Subject: Begin moving some of the common code to a shared base To enable the addition of a new lockfile impl that has some of the similar functionality as the existing impls in pylockfile we need to break out what will be shared and what will not be shared by the existing impls and the soon to be added new impl. Change-Id: I3fddfb05b78657ebed1449f0389004061e7e7082 --- lockfile/__init__.py | 87 +++++++++++++++++++++++++++++----------------------- 1 file 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) -- cgit v1.2.1