Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/lockfile/symlinklockfile.py @ 1:56ad4e20f292 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
| author | guerler |
|---|---|
| date | Fri, 31 Jul 2020 00:32:28 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 0:d30785e31577 | 1:56ad4e20f292 |
|---|---|
| 1 from __future__ import absolute_import | |
| 2 | |
| 3 import os | |
| 4 import time | |
| 5 | |
| 6 from . import (LockBase, NotLocked, NotMyLock, LockTimeout, | |
| 7 AlreadyLocked) | |
| 8 | |
| 9 | |
| 10 class SymlinkLockFile(LockBase): | |
| 11 """Lock access to a file using symlink(2).""" | |
| 12 | |
| 13 def __init__(self, path, threaded=True, timeout=None): | |
| 14 # super(SymlinkLockFile).__init(...) | |
| 15 LockBase.__init__(self, path, threaded, timeout) | |
| 16 # split it back! | |
| 17 self.unique_name = os.path.split(self.unique_name)[1] | |
| 18 | |
| 19 def acquire(self, timeout=None): | |
| 20 # Hopefully unnecessary for symlink. | |
| 21 # try: | |
| 22 # open(self.unique_name, "wb").close() | |
| 23 # except IOError: | |
| 24 # raise LockFailed("failed to create %s" % self.unique_name) | |
| 25 timeout = timeout if timeout is not None else self.timeout | |
| 26 end_time = time.time() | |
| 27 if timeout is not None and timeout > 0: | |
| 28 end_time += timeout | |
| 29 | |
| 30 while True: | |
| 31 # Try and create a symbolic link to it. | |
| 32 try: | |
| 33 os.symlink(self.unique_name, self.lock_file) | |
| 34 except OSError: | |
| 35 # Link creation failed. Maybe we've double-locked? | |
| 36 if self.i_am_locking(): | |
| 37 # Linked to out unique name. Proceed. | |
| 38 return | |
| 39 else: | |
| 40 # Otherwise the lock creation failed. | |
| 41 if timeout is not None and time.time() > end_time: | |
| 42 if timeout > 0: | |
| 43 raise LockTimeout("Timeout waiting to acquire" | |
| 44 " lock for %s" % | |
| 45 self.path) | |
| 46 else: | |
| 47 raise AlreadyLocked("%s is already locked" % | |
| 48 self.path) | |
| 49 time.sleep(timeout / 10 if timeout is not None else 0.1) | |
| 50 else: | |
| 51 # Link creation succeeded. We're good to go. | |
| 52 return | |
| 53 | |
| 54 def release(self): | |
| 55 if not self.is_locked(): | |
| 56 raise NotLocked("%s is not locked" % self.path) | |
| 57 elif not self.i_am_locking(): | |
| 58 raise NotMyLock("%s is locked, but not by me" % self.path) | |
| 59 os.unlink(self.lock_file) | |
| 60 | |
| 61 def is_locked(self): | |
| 62 return os.path.islink(self.lock_file) | |
| 63 | |
| 64 def i_am_locking(self): | |
| 65 return (os.path.islink(self.lock_file) | |
| 66 and os.readlink(self.lock_file) == self.unique_name) | |
| 67 | |
| 68 def break_lock(self): | |
| 69 if os.path.islink(self.lock_file): # exists && link | |
| 70 os.unlink(self.lock_file) |
