Coverage for /usr/local/lib/python3.11/dist-packages/pyrocko/squirrel/lock.py: 90%
21 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-06 06:59 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-06 06:59 +0000
1# http://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
6'''
7Directory locking utility.
8'''
10import os
11import logging
13op = os.path
14logger = logging.getLogger('psq.lock')
17class LockDir(object):
19 def __init__(self, path):
20 self._path = path
21 self._lockfile = op.join(path, '.buried')
23 def __enter__(self):
24 if op.exists(self._lockfile):
25 raise EnvironmentError('Directory "%s" is locked' % self._path)
27 with open(self._lockfile, 'wb') as f:
28 f.write(b'')
29 logger.debug('Locked directory "%s"', self._path)
30 return self
32 def __exit__(self, type, value, traceback):
33 try:
34 os.remove(self._lockfile)
35 logger.debug('Unlocked directory "%s"', self._path)
36 except FileNotFoundError:
37 logger.warning(
38 'Lockfile "%s" was removed unintentionally', self._lockfile)