-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenlock.py
More file actions
431 lines (363 loc) · 11.9 KB
/
openlock.py
File metadata and controls
431 lines (363 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
from __future__ import annotations
import atexit
import copy
import logging
import os
import platform
import subprocess
import sys
import tempfile
import threading
import time
import warnings
from pathlib import Path
from typing import Any
if sys.version_info >= (3, 11):
from typing import TypedDict, Unpack
__version__ = "1.2.1"
logger = logging.getLogger(__name__)
IS_WINDOWS = "windows" in platform.system().lower()
def _pid_valid_windows(pid: int, name: str) -> bool:
cmdlet = (
"(Get-CimInstance Win32_Process " "-Filter 'ProcessId = {}').CommandLine"
).format(pid)
cmd = [
"powershell",
cmdlet,
]
p = subprocess.run(
cmd,
stdout=subprocess.PIPE,
universal_newlines=True,
bufsize=1,
)
out = p.stdout.lower()
if name.lower() in out and "python" in out:
return True
return False
def _pid_valid_posix(pid: int, name: str) -> bool:
# for busybox these options are undocumented...
cmd = ["ps", "-f", str(pid)]
with subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
universal_newlines=True,
bufsize=1,
) as p:
assert p.stdout is not None
read_header = True
for line in iter(p.stdout.readline, ""):
line = line.lower()
line_ = line.split()
if len(line_) == 0:
continue
if "pid" in line_ and read_header:
# header
index = line_.index("pid")
read_header = False
continue
try:
pid_ = int(line_[index])
except ValueError:
continue
if name.lower() in line and "python" in line and pid == pid_:
return True
return False
def _pid_valid(pid: int, name: str) -> bool:
if IS_WINDOWS:
return _pid_valid_windows(pid, name)
else:
return _pid_valid_posix(pid, name)
class OpenLockException(Exception):
"""
Base exception raised by the openlock library.
"""
pass
class Timeout(OpenLockException):
"""
Raised when the waiting time for acquiring a
:py:class:`openlock.FileLock` has expired.
"""
pass
class InvalidRelease(OpenLockException):
"""
Raised when :py:meth:`openlock.FileLock.release` is called
on a lock we do not own.
"""
pass
class InvalidLockFile(OpenLockException):
"""
Raised when openlock is unable to create a
valid lock file in :py:meth:`openlock.FileLock.acquire`.
"""
pass
class InvalidOption(OpenLockException):
"""
Raised when :py:func:`openlock.set_defaults` is used to set
a non-existing option.
"""
pass
if sys.version_info >= (3, 11):
class _LockState(TypedDict, total=False):
state: str
reason: str
pid: int
name: str
class Defaults(TypedDict, total=False):
"""
Default options.
"""
race_delay: float
"""
delay before we check that we still have the lock file
"""
tries: int
"""
number of attempts to create a valid lock file
"""
retry_period: float
"""
delay before reattempting to acquire a lock
"""
_defaults: Defaults = {
"race_delay": 0.2,
"tries": 2,
"retry_period": 0.3,
}
def get_defaults() -> Defaults:
"""
Get defaults.
"""
return copy.copy(_defaults)
def set_defaults(**kw: Unpack[Defaults]) -> None:
"""
Set defaults.
:param kw: default parameters
:raises InvalidOption: raised when trying to set a non-existing
option
"""
dk = _defaults.keys()
for k in kw.keys():
if k not in dk:
raise InvalidOption(f"Invalid option: '{k}'")
_defaults.update(kw)
class FileLock:
"""
The lock constructor. An :py:class:`openlock.FileLock` object
supports the context manager protocol.
"""
lock_file: Path
"""
The `Path` object representing the lock file.
"""
timeout: float | None
"""
The value of the timeout parameter.
"""
__lock: threading.Lock
__acquired: bool
__retry_period: float
__race_delay: float
__tries: int
def __init__(
self,
lock_file: str | Path = "openlock.lock",
timeout: float | None = None,
) -> None:
"""
:param lock_file: the underlying file used for locking;
the calling process should have read/write access
:param timeout: the default for the corresponding argument of
:py:meth:`openlock.acquire`
"""
self.lock_file = Path(lock_file)
self.timeout = timeout
self.__lock = threading.Lock()
self.__acquired = False
self.__retry_period = _defaults["retry_period"]
self.__race_delay = _defaults["race_delay"]
self.__tries = _defaults["tries"]
logger.debug(f"{self} created")
def __lock_state(self, verify_pid_valid: bool = True) -> _LockState:
try:
with open(self.lock_file) as f:
s = f.readlines()
except FileNotFoundError:
return {
"state": "unlocked",
"reason": "file not found",
}
except Exception as e:
logger.exception(f"Error accessing '{self.lock_file}': {str(e)}")
raise
try:
pid = int(s[0])
name = s[1].strip()
except (ValueError, IndexError):
return {
"state": "unlocked",
"reason": "invalid lock file",
}
if not verify_pid_valid:
return {
"state": "locked",
"pid": pid,
"name": name,
}
else:
if not _pid_valid(pid, name):
retry = self.__lock_state(verify_pid_valid=False)
if retry["state"] == "locked" and (
retry["pid"] != pid or retry["name"] != name
):
logger.debug(
f"Lock file '{self.lock_file}' has changed "
f"from {{'pid': {pid}, 'name': '{name}'}} to {{'pid': "
f"{retry['pid']}, 'name': {repr(retry['name'])}}} "
)
return retry
else:
return {
"state": "unlocked",
"reason": "pid not valid",
"pid": pid,
"name": name,
}
return {"state": "locked", "pid": pid, "name": name}
def __remove_lock_file(self) -> None:
try:
os.remove(self.lock_file)
logger.debug(f"Lock file '{self.lock_file}' removed")
except OSError:
pass
def __create_lock_file(self, pid: int, name: str) -> bool:
if self.lock_file.exists():
return False
temp_file = tempfile.NamedTemporaryFile(
dir=os.path.dirname(self.lock_file), delete=False
)
temp_file.write(f"{pid}\n{name}\n".encode())
temp_file.close()
locked = True
# try linking, which is atomic, and will fail if the file exists
try:
os.link(temp_file.name, self.lock_file)
logger.debug(f"Lock file '{self.lock_file}' created")
except FileExistsError:
locked = False
except OSError as e:
logger.error(f"Error creating '{self.lock_file}': {str(e)}")
locked = False
# Remove the temporary file
os.remove(temp_file.name)
return locked
def __write_lock_file(self, pid: int, name: str) -> None:
temp_file = tempfile.NamedTemporaryFile(
dir=os.path.dirname(self.lock_file), delete=False
)
temp_file.write(f"{pid}\n{name}\n".encode())
temp_file.close()
os.replace(temp_file.name, self.lock_file)
def __acquire_once(self) -> None:
pid, name = os.getpid(), sys.argv[0]
name_ = name.split()
if len(name_) >= 1:
name = Path(name_[0]).stem
if self.__create_lock_file(pid, name):
logger.debug(f"{self} acquired")
self.__acquired = True
atexit.register(self.__remove_lock_file)
return
lock_state = self.__lock_state()
logger.debug(f"{self}: {lock_state}")
for _ in range(0, self.__tries):
if lock_state["state"] == "locked":
return
t = time.time()
self.__write_lock_file(pid, name)
tt = time.time()
logger.debug(
f"Lock file '{self.lock_file}' with contents {{'pid': {pid}, "
f"'name': '{name}'}} written in {tt-t:#.2g} seconds"
)
if tt - t >= (2 / 3) * self.__race_delay:
message = (
"Slow system detected!! Consider increasing the "
"'race_delay' parameter "
f"(current value: {self.__race_delay:#.2g}, used: {tt-t:#.2g})."
)
warnings.warn(message)
time.sleep(self.__race_delay)
lock_state = self.__lock_state(verify_pid_valid=False)
logger.debug(f"{self}: {lock_state}")
if lock_state["state"] == "locked":
if lock_state["pid"] == os.getpid():
logger.debug(f"{self} acquired")
self.__acquired = True
atexit.register(self.__remove_lock_file)
return
raise InvalidLockFile("Unable to obtain a valid lock file")
def acquire(self, timeout: float | None = None) -> None:
"""
Attempts to acquires the lock.
:param timeout: specifies the maximum waiting time in seconds
before a :py:exc:`Timeout` exception is raised
:raises Timeout: raised when the waiting time for acquiring
the lock has expired
:raises InvalidLockFile: raised when openlock is unable to create a
valid lock file
"""
if timeout is None:
timeout = self.timeout
start_time = time.time()
with self.__lock:
while True:
if not self.__acquired:
self.__acquire_once()
if self.__acquired:
break
now = time.time()
if timeout is not None and now - start_time >= timeout:
raise Timeout(f"Unable to acquire {self}")
time.sleep(self.__retry_period)
def release(self) -> None:
"""
Releases the lock.
:raises InvalidRelease: raised when we don't own the lock
"""
with self.__lock:
if not self.__acquired:
raise InvalidRelease(f"Attempt at releasing {self} which we do not own")
self.__acquired = False
self.__remove_lock_file()
atexit.unregister(self.__remove_lock_file)
logger.debug(f"{self} released")
def locked(self) -> bool:
"""
True if we hold the lock.
"""
with self.__lock:
if self.__acquired:
return True
lock_state = self.__lock_state()
return lock_state["state"] == "locked"
def getpid(self) -> int | None:
"""
The PID of the process that holds the lock, if any. Otherwise returns `None`.
"""
with self.__lock:
if self.__acquired:
return os.getpid()
lock_state = self.__lock_state()
if lock_state["state"] == "locked":
return lock_state["pid"]
else:
return None
def __enter__(self) -> FileLock:
self.acquire()
return self
def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
self.release()
def __str__(self) -> str:
return f"FileLock('{self.lock_file}')"
__repr__ = __str__