|
| 1 | +import filelock |
1 | 2 | import os
|
2 | 3 | import os.path as op
|
3 | 4 | import logging
|
|
32 | 33 | compress_dicoms
|
33 | 34 | )
|
34 | 35 |
|
| 36 | +LOCKFILE = 'heudiconv.lock' |
35 | 37 | lgr = logging.getLogger(__name__)
|
36 | 38 |
|
37 | 39 |
|
@@ -210,14 +212,23 @@ def prep_conversion(sid, dicoms, outdir, heuristic, converter, anon_sid,
|
210 | 212 | clear_temp_dicoms(item_dicoms)
|
211 | 213 |
|
212 | 214 | 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', {})) |
221 | 232 |
|
222 | 233 |
|
223 | 234 | def convert(items, converter, scaninfo_suffix, custom_callable, with_prov,
|
|
0 commit comments