1.1 --- a/imiptools/filesys.py Sun Sep 06 23:20:02 2015 +0200
1.2 +++ b/imiptools/filesys.py Mon Sep 07 18:54:54 2015 +0200
1.3 @@ -22,7 +22,7 @@
1.4 import errno
1.5 from imiptools.config import DEFAULT_PERMISSIONS, DEFAULT_DIR_PERMISSIONS
1.6 from os.path import abspath, commonprefix, exists, join, split
1.7 -from os import chmod, makedirs, mkdir, rename, rmdir
1.8 +from os import chmod, getpid, makedirs, mkdir, rename, rmdir
1.9 from time import sleep, time
1.10
1.11 def check_dir(base, filename):
1.12 @@ -149,13 +149,34 @@
1.13 # This uses the directory creation method exploited by MoinMoin.util.lock.
1.14 # However, a simple single lock type mechanism is employed here.
1.15
1.16 - def get_lock_dir(self, *parts):
1.17 + def make_lock_dir(self, *parts):
1.18
1.19 - "Return the lock directory defined by the given 'parts'."
1.20 + "Make the lock directory defined by the given 'parts'."
1.21
1.22 parts = parts and list(parts) or []
1.23 parts.append(self.lock_name)
1.24 - return self.get_object_in_store(*parts)
1.25 + parts.append(str(getpid()))
1.26 + makedirs(self.get_object_in_store(*parts))
1.27 +
1.28 + def remove_lock_dir(self, *parts):
1.29 +
1.30 + "Remove the lock directory defined by the given 'parts'."
1.31 +
1.32 + parts = parts and list(parts) or []
1.33 + parts.append(self.lock_name)
1.34 + parts.append(str(getpid()))
1.35 + rmdir(self.get_object_in_store(*parts))
1.36 + parts.pop()
1.37 + rmdir(self.get_object_in_store(*parts))
1.38 +
1.39 + def owning_lock_dir(self, *parts):
1.40 +
1.41 + "Return whether this process owns the lock directory."
1.42 +
1.43 + parts = parts and list(parts) or []
1.44 + parts.append(self.lock_name)
1.45 + parts.append(str(getpid()))
1.46 + return exists(self.get_object_in_store(*parts))
1.47
1.48 def acquire_lock(self, timeout=None, *parts):
1.49
1.50 @@ -168,11 +189,13 @@
1.51
1.52 while not timeout or now - start < timeout:
1.53 try:
1.54 - mkdir(self.get_lock_dir(*parts))
1.55 + self.make_lock_dir(*parts)
1.56 break
1.57 except OSError, exc:
1.58 if exc.errno != errno.EEXIST:
1.59 raise
1.60 + elif self.owning_lock_dir(*parts):
1.61 + break
1.62 sleep(1)
1.63 now = time()
1.64
1.65 @@ -184,7 +207,7 @@
1.66 """
1.67
1.68 try:
1.69 - rmdir(self.get_lock_dir(*parts))
1.70 + self.remove_lock_dir(*parts)
1.71 except OSError, exc:
1.72 if exc.errno != errno.ENOENT:
1.73 raise