Skip to content

Commit b12e931

Browse files
authored
Merge pull request #348 from stilley2/feature/bids_filelock
Acquire a lock prior to top level bids file modification
2 parents dcc590d + 13be87a commit b12e931

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

heudiconv/convert.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import filelock
12
import os
23
import os.path as op
34
import logging
@@ -32,6 +33,7 @@
3233
compress_dicoms
3334
)
3435

36+
LOCKFILE = 'heudiconv.lock'
3537
lgr = logging.getLogger(__name__)
3638

3739

@@ -210,14 +212,23 @@ def prep_conversion(sid, dicoms, outdir, heuristic, converter, anon_sid,
210212
clear_temp_dicoms(item_dicoms)
211213

212214
if bids_options is not None and 'notop' not in bids_options:
213-
if seqinfo:
214-
keys = list(seqinfo)
215-
add_participant_record(anon_outdir,
216-
anon_sid,
217-
keys[0].patient_age,
218-
keys[0].patient_sex)
219-
populate_bids_templates(anon_outdir,
220-
getattr(heuristic, 'DEFAULT_FIELDS', {}))
215+
lockfile = op.join(anon_outdir, LOCKFILE)
216+
if op.exists(lockfile):
217+
lgr.warning("Existing lockfile found in {0} - waiting for the "
218+
"lock to be released. To set a timeout limit, set "
219+
"the HEUDICONV_FILELOCK_TIMEOUT environmental variable "
220+
"to a value in seconds. If this process hangs, it may "
221+
"require a manual deletion of the {0}.".format(lockfile))
222+
timeout = os.getenv("HEUDICONV_LOCKFILE_TIMEOUT", -1)
223+
with filelock.SoftFileLock(lockfile, timeout=timeout):
224+
if seqinfo:
225+
keys = list(seqinfo)
226+
add_participant_record(anon_outdir,
227+
anon_sid,
228+
keys[0].patient_age,
229+
keys[0].patient_sex)
230+
populate_bids_templates(anon_outdir,
231+
getattr(heuristic, 'DEFAULT_FIELDS', {}))
221232

222233

223234
def convert(items, converter, scaninfo_suffix, custom_callable, with_prov,

heudiconv/info.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
'pathlib',
3030
'dcmstack>=0.7',
3131
'etelemetry',
32+
'filelock>=3.0.12',
3233
]
3334

3435
TESTS_REQUIRES = [

0 commit comments

Comments
 (0)