Mercurial > repos > shellac > sam_consensus_v3
annotate env/lib/python3.9/site-packages/lockfile/pidlockfile.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
| author | shellac | 
|---|---|
| date | Mon, 22 Mar 2021 18:12:50 +0000 | 
| parents | |
| children | 
| rev | line source | 
|---|---|
| 
0
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
1 # -*- coding: utf-8 -*- | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
2 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
3 # pidlockfile.py | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
4 # | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
5 # Copyright © 2008–2009 Ben Finney <ben+python@benfinney.id.au> | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
6 # | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
7 # This is free software: you may copy, modify, and/or distribute this work | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
8 # under the terms of the Python Software Foundation License, version 2 or | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
9 # later as published by the Python Software Foundation. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
10 # No warranty expressed or implied. See the file LICENSE.PSF-2 for details. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
11 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
12 """ Lockfile behaviour implemented via Unix PID files. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
13 """ | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
14 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
15 from __future__ import absolute_import | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
16 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
17 import errno | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
18 import os | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
19 import time | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
20 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
21 from . import (LockBase, AlreadyLocked, LockFailed, NotLocked, NotMyLock, | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
22 LockTimeout) | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
23 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
24 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
25 class PIDLockFile(LockBase): | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
26 """ Lockfile implemented as a Unix PID file. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
27 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
28 The lock file is a normal file named by the attribute `path`. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
29 A lock's PID file contains a single line of text, containing | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
30 the process ID (PID) of the process that acquired the lock. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
31 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
32 >>> lock = PIDLockFile('somefile') | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
33 >>> lock = PIDLockFile('somefile') | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
34 """ | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
35 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
36 def __init__(self, path, threaded=False, timeout=None): | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
37 # pid lockfiles don't support threaded operation, so always force | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
38 # False as the threaded arg. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
39 LockBase.__init__(self, path, False, timeout) | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
40 self.unique_name = self.path | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
41 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
42 def read_pid(self): | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
43 """ Get the PID from the lock file. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
44 """ | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
45 return read_pid_from_pidfile(self.path) | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
46 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
47 def is_locked(self): | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
48 """ Test if the lock is currently held. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
49 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
50 The lock is held if the PID file for this lock exists. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
51 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
52 """ | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
53 return os.path.exists(self.path) | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
54 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
55 def i_am_locking(self): | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
56 """ Test if the lock is held by the current process. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
57 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
58 Returns ``True`` if the current process ID matches the | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
59 number stored in the PID file. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
60 """ | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
61 return self.is_locked() and os.getpid() == self.read_pid() | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
62 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
63 def acquire(self, timeout=None): | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
64 """ Acquire the lock. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
65 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
66 Creates the PID file for this lock, or raises an error if | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
67 the lock could not be acquired. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
68 """ | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
69 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
70 timeout = timeout if timeout is not None else self.timeout | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
71 end_time = time.time() | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
72 if timeout is not None and timeout > 0: | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
73 end_time += timeout | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
74 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
75 while True: | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
76 try: | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
77 write_pid_to_pidfile(self.path) | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
78 except OSError as exc: | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
79 if exc.errno == errno.EEXIST: | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
80 # The lock creation failed. Maybe sleep a bit. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
81 if time.time() > end_time: | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
82 if timeout is not None and timeout > 0: | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
83 raise LockTimeout("Timeout waiting to acquire" | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
84 " lock for %s" % | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
85 self.path) | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
86 else: | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
87 raise AlreadyLocked("%s is already locked" % | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
88 self.path) | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
89 time.sleep(timeout is not None and timeout / 10 or 0.1) | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
90 else: | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
91 raise LockFailed("failed to create %s" % self.path) | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
92 else: | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
93 return | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
94 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
95 def release(self): | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
96 """ Release the lock. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
97 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
98 Removes the PID file to release the lock, or raises an | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
99 error if the current process does not hold the lock. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
100 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
101 """ | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
102 if not self.is_locked(): | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
103 raise NotLocked("%s is not locked" % self.path) | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
104 if not self.i_am_locking(): | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
105 raise NotMyLock("%s is locked, but not by me" % self.path) | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
106 remove_existing_pidfile(self.path) | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
107 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
108 def break_lock(self): | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
109 """ Break an existing lock. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
110 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
111 Removes the PID file if it already exists, otherwise does | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
112 nothing. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
113 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
114 """ | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
115 remove_existing_pidfile(self.path) | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
116 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
117 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
118 def read_pid_from_pidfile(pidfile_path): | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
119 """ Read the PID recorded in the named PID file. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
120 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
121 Read and return the numeric PID recorded as text in the named | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
122 PID file. If the PID file cannot be read, or if the content is | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
123 not a valid PID, return ``None``. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
124 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
125 """ | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
126 pid = None | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
127 try: | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
128 pidfile = open(pidfile_path, 'r') | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
129 except IOError: | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
130 pass | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
131 else: | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
132 # According to the FHS 2.3 section on PID files in /var/run: | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
133 # | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
134 # The file must consist of the process identifier in | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
135 # ASCII-encoded decimal, followed by a newline character. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
136 # | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
137 # Programs that read PID files should be somewhat flexible | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
138 # in what they accept; i.e., they should ignore extra | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
139 # whitespace, leading zeroes, absence of the trailing | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
140 # newline, or additional lines in the PID file. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
141 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
142 line = pidfile.readline().strip() | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
143 try: | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
144 pid = int(line) | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
145 except ValueError: | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
146 pass | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
147 pidfile.close() | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
148 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
149 return pid | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
150 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
151 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
152 def write_pid_to_pidfile(pidfile_path): | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
153 """ Write the PID in the named PID file. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
154 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
155 Get the numeric process ID (“PID”) of the current process | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
156 and write it to the named file as a line of text. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
157 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
158 """ | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
159 open_flags = (os.O_CREAT | os.O_EXCL | os.O_WRONLY) | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
160 open_mode = 0o644 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
161 pidfile_fd = os.open(pidfile_path, open_flags, open_mode) | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
162 pidfile = os.fdopen(pidfile_fd, 'w') | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
163 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
164 # According to the FHS 2.3 section on PID files in /var/run: | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
165 # | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
166 # The file must consist of the process identifier in | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
167 # ASCII-encoded decimal, followed by a newline character. For | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
168 # example, if crond was process number 25, /var/run/crond.pid | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
169 # would contain three characters: two, five, and newline. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
170 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
171 pid = os.getpid() | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
172 pidfile.write("%s\n" % pid) | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
173 pidfile.close() | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
174 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
175 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
176 def remove_existing_pidfile(pidfile_path): | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
177 """ Remove the named PID file if it exists. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
178 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
179 Removing a PID file that doesn't already exist puts us in the | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
180 desired state, so we ignore the condition if the file does not | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
181 exist. | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
182 | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
183 """ | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
184 try: | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
185 os.remove(pidfile_path) | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
186 except OSError as exc: | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
187 if exc.errno == errno.ENOENT: | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
188 pass | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
189 else: | 
| 
 
4f3585e2f14b
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
 
shellac 
parents:  
diff
changeset
 | 
190 raise | 
