Skip to content
Snippets Groups Projects
Commit 5cb213f3 authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

ENH: New lockdir utility function

parent 4b84d157
No related branches found
No related tags found
1 merge request!3BIP configuration system
import shutil
from fsl.wrappers.wrapperutils import cmdwrapper
installed_sienax = shutil.which('bb_sienax')
if installed_sienax:
@cmdwrapper
def bb_sienax(input, output=None, **kwargs):
return ['bb_sienax', input, output]
else:
def bb_sienax(input, output=None, **kwargs):
from . import sienax
sienax.bb_sienax(input, output)
#!/usr/bin/env python
"""The bip.utils package contains a range of miscellaneous utilities. """
import contextlib
import logging
import os.path as op
import os
import time
log = logging.getLogger(__name__)
@contextlib.contextmanager
def lockdir(dirname, delay=5):
"""Lock a directory for exclusive access.
Primitive mechanism by which concurrent access to a directory can be
prevented. Attempts to create a semaphore file in the directory, but waits
if that file already exists. Removes the file when finished.
"""
lockfile = op.join(dirname, '.fsl_ci.lockdir')
while True:
try:
log.debug(f'Attempting to lock %s for exclusive access.', dirname)
fd = os.open(lockfile, os.O_CREAT | os.O_EXCL | os.O_RDWR)
break
except OSError as e:
if e.errno != errno.EEXIST:
raise e
log.debug('%s is already locked - trying again '
'in %s seconds ...', dirname, delay)
time.sleep(10)
log.debug('Exclusive access acquired for %s ...', dirname)
try:
yield
finally:
log.debug('Relinquishing lock on %s', dirname)
os.close( fd)
os.unlink(lockfile)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment