comparison env/lib/python3.7/site-packages/lockfile/mkdirlockfile.py @ 5:9b1c78e6ba9c draft default tip

"planemo upload commit 6c0a8142489327ece472c84e558c47da711a9142"
author shellac
date Mon, 01 Jun 2020 08:59:25 -0400
parents 79f47841a781
children
comparison
equal deleted inserted replaced
4:79f47841a781 5:9b1c78e6ba9c
1 from __future__ import absolute_import, division
2
3 import time
4 import os
5 import sys
6 import errno
7
8 from . import (LockBase, LockFailed, NotLocked, NotMyLock, LockTimeout,
9 AlreadyLocked)
10
11
12 class MkdirLockFile(LockBase):
13 """Lock file by creating a directory."""
14 def __init__(self, path, threaded=True, timeout=None):
15 """
16 >>> lock = MkdirLockFile('somefile')
17 >>> lock = MkdirLockFile('somefile', threaded=False)
18 """
19 LockBase.__init__(self, path, threaded, timeout)
20 # Lock file itself is a directory. Place the unique file name into
21 # it.
22 self.unique_name = os.path.join(self.lock_file,
23 "%s.%s%s" % (self.hostname,
24 self.tname,
25 self.pid))
26
27 def acquire(self, timeout=None):
28 timeout = timeout if timeout is not None else self.timeout
29 end_time = time.time()
30 if timeout is not None and timeout > 0:
31 end_time += timeout
32
33 if timeout is None:
34 wait = 0.1
35 else:
36 wait = max(0, timeout / 10)
37
38 while True:
39 try:
40 os.mkdir(self.lock_file)
41 except OSError:
42 err = sys.exc_info()[1]
43 if err.errno == errno.EEXIST:
44 # Already locked.
45 if os.path.exists(self.unique_name):
46 # Already locked by me.
47 return
48 if timeout is not None and time.time() > end_time:
49 if timeout > 0:
50 raise LockTimeout("Timeout waiting to acquire"
51 " lock for %s" %
52 self.path)
53 else:
54 # Someone else has the lock.
55 raise AlreadyLocked("%s is already locked" %
56 self.path)
57 time.sleep(wait)
58 else:
59 # Couldn't create the lock for some other reason
60 raise LockFailed("failed to create %s" % self.lock_file)
61 else:
62 open(self.unique_name, "wb").close()
63 return
64
65 def release(self):
66 if not self.is_locked():
67 raise NotLocked("%s is not locked" % self.path)
68 elif not os.path.exists(self.unique_name):
69 raise NotMyLock("%s is locked, but not by me" % self.path)
70 os.unlink(self.unique_name)
71 os.rmdir(self.lock_file)
72
73 def is_locked(self):
74 return os.path.exists(self.lock_file)
75
76 def i_am_locking(self):
77 return (self.is_locked() and
78 os.path.exists(self.unique_name))
79
80 def break_lock(self):
81 if os.path.exists(self.lock_file):
82 for name in os.listdir(self.lock_file):
83 os.unlink(os.path.join(self.lock_file, name))
84 os.rmdir(self.lock_file)