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