Skip to content

Commit 56a5bc9

Browse files
committed
fix: remove temp dicoms if not actually converting as well
1 parent d1765d0 commit 56a5bc9

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

heudiconv/convert.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import logging
44
import shutil
55

6-
from .utils import (read_config, load_json, save_json, write_config,
7-
TempDirs, safe_copyfile, treat_infofile, set_readonly)
6+
from .utils import (read_config, load_json, save_json, write_config, TempDirs,
7+
safe_copyfile, treat_infofile, set_readonly,
8+
clear_temp_dicoms)
89
from .bids import (convert_sid_bids, populate_bids_templates, save_scans_key,
910
tuneup_bids_json_files, add_participant_record)
1011
from .dicoms import (group_dicoms_into_seqinfos, embed_metadata_from_dicoms,
@@ -143,6 +144,10 @@ def prep_conversion(sid, dicoms, outdir, heuristic, converter, anon_sid,
143144
outdir=tdir,
144145
min_meta=min_meta,
145146
overwrite=overwrite,)
147+
148+
for item_dicoms in filegroup.values():
149+
clear_temp_dicoms(item_dicoms)
150+
146151
if bids:
147152
if seqinfo:
148153
keys = list(seqinfo)
@@ -256,14 +261,6 @@ def convert(items, converter, scaninfo_suffix, custom_callable, with_prov,
256261
if op.exists(outname):
257262
set_readonly(outname)
258263

259-
if items:
260-
common = op.commonprefix(item_dicoms)
261-
tmp = '/'.join(common.split(op.sep)[:3])
262-
if (op.dirname(tmp) == '/tmp'
263-
and op.basename(tmp).startswith('heudiconvDCM')):
264-
# clean up directory holding dicoms
265-
shutil.rmtree(tmp)
266-
267264
if custom_callable is not None:
268265
custom_callable(*item)
269266

heudiconv/utils.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
"""Utility objects and functions"""
22

33
import os
4-
import os.path as op
5-
from tempfile import mkdtemp
6-
from glob import glob
4+
import tempfile
75
import json
86
import re
97
import sys
108
import shutil
11-
from collections import namedtuple
129
import copy
1310
import logging
1411
import stat
12+
import os.path as op
13+
from pathlib import Path
14+
from collections import namedtuple
15+
from glob import glob
1516

1617
SeqInfo = namedtuple(
1718
'SeqInfo',
@@ -64,7 +65,7 @@ def __init__(self):
6465
self.lgr = logging.getLogger('tempdirs')
6566

6667
def __call__(self, prefix=None):
67-
tmpdir = mkdtemp(prefix=prefix)
68+
tmpdir = tempfile.mkdtemp(prefix=prefix)
6869
self.dirs.append(tmpdir)
6970
return tmpdir
7071

@@ -319,3 +320,16 @@ def is_readonly(path):
319320
perms = stat.S_IMODE(os.lstat(os.path.realpath(path)).st_mode)
320321
# should be true if anyone is allowed to write
321322
return not bool(perms & ALL_CAN_WRITE)
323+
324+
325+
def clear_temp_dicoms(item_dicoms):
326+
"""Ensures DICOM temporary directories are safely cleared"""
327+
try:
328+
tmp = Path(op.commonprefix(item_dicoms).parents[1])
329+
except IndexError:
330+
return
331+
if (op.dirname(tmp) == tempfile.gettempdir()
332+
and op.basename(tmp).startswith('heudiconvDCM')
333+
and op.exists(tmp)):
334+
# clean up directory holding dicoms
335+
shutil.rmtree(tmp)

0 commit comments

Comments
 (0)