mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-15 04:07:17 +00:00
Correct whitespaces and add context manager for file locking
This commit is contained in:
parent
25bd9aa0f3
commit
96c6c9b358
@ -7,7 +7,6 @@ from stat import S_IRUSR, S_IWUSR, S_IXUSR
|
|||||||
from tfw.internals.lazy import lazy_property
|
from tfw.internals.lazy import lazy_property
|
||||||
from tfw.internals.ref_counter import RefCounter
|
from tfw.internals.ref_counter import RefCounter
|
||||||
|
|
||||||
|
|
||||||
KEYFILE = join(gettempdir(), 'tfw-auth.key')
|
KEYFILE = join(gettempdir(), 'tfw-auth.key')
|
||||||
LOCKFILE = join(gettempdir(), 'tfw-auth.lock')
|
LOCKFILE = join(gettempdir(), 'tfw-auth.lock')
|
||||||
|
|
||||||
@ -45,4 +44,5 @@ class KeyManager:
|
|||||||
def _chmod_700_keyfile(self):
|
def _chmod_700_keyfile(self):
|
||||||
chmod(self.keyfile, S_IRUSR | S_IWUSR | S_IXUSR)
|
chmod(self.keyfile, S_IRUSR | S_IWUSR | S_IXUSR)
|
||||||
|
|
||||||
|
|
||||||
register(KeyManager.refcounter.teardown_instance)
|
register(KeyManager.refcounter.teardown_instance)
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from os import remove
|
from os import remove
|
||||||
|
from contextlib import contextmanager
|
||||||
from fcntl import flock, LOCK_EX, LOCK_UN
|
from fcntl import flock, LOCK_EX, LOCK_UN
|
||||||
|
|
||||||
|
|
||||||
@ -6,9 +7,14 @@ class RefCounter:
|
|||||||
def __init__(self, lockpath):
|
def __init__(self, lockpath):
|
||||||
self.lockpath = lockpath
|
self.lockpath = lockpath
|
||||||
self._lockfile = open(self.lockpath, 'a+')
|
self._lockfile = open(self.lockpath, 'a+')
|
||||||
|
with self.locked():
|
||||||
|
counter = self._read_counter()
|
||||||
|
self._write_counter(counter+1)
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def locked(self):
|
||||||
flock(self._lockfile, LOCK_EX)
|
flock(self._lockfile, LOCK_EX)
|
||||||
counter = self._read_counter()
|
yield
|
||||||
self._write_counter(counter+1)
|
|
||||||
flock(self._lockfile, LOCK_UN)
|
flock(self._lockfile, LOCK_UN)
|
||||||
|
|
||||||
def _read_counter(self):
|
def _read_counter(self):
|
||||||
@ -26,14 +32,13 @@ class RefCounter:
|
|||||||
self._lockfile.flush()
|
self._lockfile.flush()
|
||||||
|
|
||||||
def teardown_instance(self):
|
def teardown_instance(self):
|
||||||
flock(self._lockfile, LOCK_EX)
|
with self.locked():
|
||||||
counter = self._read_counter()
|
counter = self._read_counter()
|
||||||
if counter <= 1:
|
if counter <= 1:
|
||||||
remove(self.lockpath)
|
remove(self.lockpath)
|
||||||
self.deallocate()
|
self.deallocate()
|
||||||
else:
|
else:
|
||||||
self._write_counter(counter-1)
|
self._write_counter(counter-1)
|
||||||
flock(self._lockfile, LOCK_UN)
|
|
||||||
self._lockfile.close()
|
self._lockfile.close()
|
||||||
|
|
||||||
def deallocate(self):
|
def deallocate(self):
|
||||||
|
@ -39,6 +39,7 @@ def context():
|
|||||||
mkfifo(pipepath)
|
mkfifo(pipepath)
|
||||||
yield CounterContext(join(workdir, 'test.lock'), pipepath)
|
yield CounterContext(join(workdir, 'test.lock'), pipepath)
|
||||||
|
|
||||||
|
|
||||||
def test_increment_decrement(context):
|
def test_increment_decrement(context):
|
||||||
counter, processes = 0, []
|
counter, processes = 0, []
|
||||||
for _ in range(5):
|
for _ in range(5):
|
||||||
@ -53,6 +54,7 @@ def test_increment_decrement(context):
|
|||||||
proc.send_signal(SIGINT)
|
proc.send_signal(SIGINT)
|
||||||
proc.wait()
|
proc.wait()
|
||||||
|
|
||||||
|
|
||||||
def test_deallocate(context):
|
def test_deallocate(context):
|
||||||
state = False
|
state = False
|
||||||
def trigger():
|
def trigger():
|
||||||
|
Loading…
Reference in New Issue
Block a user