mirror of https://github.com/openXC7/prjxray.git
prjxray/util.py: replaced fcntl by flufl.lock to allows using script via NFS
This commit is contained in:
parent
e87c79a156
commit
5a67845a11
|
|
@ -8,7 +8,7 @@
|
||||||
# https://opensource.org/licenses/ISC
|
# https://opensource.org/licenses/ISC
|
||||||
#
|
#
|
||||||
# SPDX-License-Identifier: ISC
|
# SPDX-License-Identifier: ISC
|
||||||
import fcntl
|
from flufl.lock import Lock
|
||||||
import math
|
import math
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
|
|
@ -31,10 +31,12 @@ class OpenSafeFile:
|
||||||
|
|
||||||
def __init__(self, name, mode="r", timeout=10):
|
def __init__(self, name, mode="r", timeout=10):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
self.lock_name = self.name + ".lock"
|
||||||
self.mode = mode
|
self.mode = mode
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
|
|
||||||
self.fd = None
|
self.fd = None
|
||||||
|
self.lock = None
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
self.fd = open(self.name, self.mode)
|
self.fd = open(self.name, self.mode)
|
||||||
|
|
@ -48,17 +50,16 @@ class OpenSafeFile:
|
||||||
def lock_file(self):
|
def lock_file(self):
|
||||||
assert self.fd is not None
|
assert self.fd is not None
|
||||||
try:
|
try:
|
||||||
signal.signal(signal.SIGALRM, timeout_handler)
|
self.lock = Lock(self.lock_name, lifetime=self.timeout)
|
||||||
signal.alarm(self.timeout)
|
self.lock.lock()
|
||||||
fcntl.flock(self.fd.fileno(), fcntl.LOCK_EX)
|
except TimeoutError as e:
|
||||||
signal.alarm(0)
|
|
||||||
except Exception as e:
|
|
||||||
print(f"{e}: {self.name}")
|
print(f"{e}: {self.name}")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
def unlock_file(self):
|
def unlock_file(self):
|
||||||
assert self.fd is not None
|
assert self.fd is not None
|
||||||
fcntl.flock(self.fd.fileno(), fcntl.LOCK_UN)
|
assert self.lock is not None
|
||||||
|
self.lock.unlock()
|
||||||
|
|
||||||
|
|
||||||
def get_db_root():
|
def get_db_root():
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
flufl.lock
|
||||||
intervaltree
|
intervaltree
|
||||||
junit-xml
|
junit-xml
|
||||||
numpy
|
numpy
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue