Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/galaxy/util/filelock.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 """Code obtained from https://github.com/dmfrey/FileLock. | |
| 2 | |
| 3 See full license at: | |
| 4 | |
| 5 https://github.com/dmfrey/FileLock/blob/master/LICENSE.txt | |
| 6 | |
| 7 """ | |
| 8 import errno | |
| 9 import os | |
| 10 import time | |
| 11 | |
| 12 | |
| 13 class FileLockException(Exception): | |
| 14 pass | |
| 15 | |
| 16 | |
| 17 class FileLock(object): | |
| 18 """ A file locking mechanism that has context-manager support so | |
| 19 you can use it in a with statement. This should be relatively cross | |
| 20 compatible as it doesn't rely on msvcrt or fcntl for the locking. | |
| 21 """ | |
| 22 | |
| 23 def __init__(self, file_name, timeout=10, delay=.05): | |
| 24 """ Prepare the file locker. Specify the file to lock and optionally | |
| 25 the maximum timeout and the delay between each attempt to lock. | |
| 26 """ | |
| 27 self.is_locked = False | |
| 28 full_path = os.path.abspath(file_name) | |
| 29 self.lockfile = "%s.lock" % full_path | |
| 30 self.file_name = full_path | |
| 31 self.timeout = timeout | |
| 32 self.delay = delay | |
| 33 | |
| 34 def acquire(self): | |
| 35 """ Acquire the lock, if possible. If the lock is in use, it check again | |
| 36 every `wait` seconds. It does this until it either gets the lock or | |
| 37 exceeds `timeout` number of seconds, in which case it throws | |
| 38 an exception. | |
| 39 """ | |
| 40 start_time = time.time() | |
| 41 while True: | |
| 42 try: | |
| 43 self.fd = os.open(self.lockfile, os.O_CREAT | os.O_EXCL | os.O_RDWR) | |
| 44 break | |
| 45 except OSError as e: | |
| 46 if e.errno != errno.EEXIST: | |
| 47 raise | |
| 48 if (time.time() - start_time) >= self.timeout: | |
| 49 raise FileLockException("Timeout occurred.") | |
| 50 time.sleep(self.delay) | |
| 51 self.is_locked = True | |
| 52 | |
| 53 def release(self): | |
| 54 """ Get rid of the lock by deleting the lockfile. | |
| 55 When working in a `with` statement, this gets automatically | |
| 56 called at the end. | |
| 57 """ | |
| 58 if self.is_locked: | |
| 59 os.close(self.fd) | |
| 60 os.unlink(self.lockfile) | |
| 61 self.is_locked = False | |
| 62 | |
| 63 def __enter__(self): | |
| 64 """ Activated when used in the with statement. | |
| 65 Should automatically acquire a lock to be used in the with block. | |
| 66 """ | |
| 67 if not self.is_locked: | |
| 68 self.acquire() | |
| 69 return self | |
| 70 | |
| 71 def __exit__(self, type, value, traceback): | |
| 72 """ Activated at the end of the with statement. | |
| 73 It automatically releases the lock if it isn't locked. | |
| 74 """ | |
| 75 if self.is_locked: | |
| 76 self.release() | |
| 77 | |
| 78 def __del__(self): | |
| 79 """ Make sure that the FileLock instance doesn't leave a lockfile | |
| 80 lying around. | |
| 81 """ | |
| 82 self.release() |
