Skip to content

Commit 29a5b6a

Browse files
committed
Merge pull request #711 from satra/fix/remove_files
fix: cleaned up file removal to pay attention to related files
2 parents b4bc351 + daf9b6a commit 29a5b6a

File tree

7 files changed

+166
-103
lines changed

7 files changed

+166
-103
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ Next release
4848
* FIX: FILMGLS compatibility with FSL 5.0.5
4949
* FIX: Freesurfer recon-all resume now avoids setting inputs
5050

51+
* FIX: File removal from node respects file associations img/hdr/mat, BRIK/HEAD
52+
5153
Release 0.8.0 (May 8, 2013)
5254
===========================
5355

doc/users/install.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ SciPy_ 0.7 - 0.12
127127

128128
Enthought_ Traits_ 4.0.0 - 4.3.0
129129

130+
Dateutil 1.5 -
131+
130132
.. note::
131133

132134
Full distributions such as pythonxy_ or EPD_ provide the above packages,

nipype/pipeline/tests/test_utils.py

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77
from tempfile import mkdtemp
88
from shutil import rmtree
99

10-
from ...testing import (assert_equal, assert_true,
11-
assert_false)
10+
from ...testing import (assert_equal, assert_true, assert_false)
1211
import nipype.pipeline.engine as pe
1312
import nipype.interfaces.base as nib
1413
import nipype.interfaces.utility as niu
1514
from ... import config
16-
from ..utils import merge_dict
17-
15+
from ..utils import merge_dict, clean_working_directory
1816

1917
def test_identitynode_removal():
2018

@@ -46,6 +44,48 @@ def test_function(arg1, arg2, arg3):
4644
yield assert_equal, len(eg.nodes()), 8
4745

4846

47+
def test_clean_working_directory():
48+
class OutputSpec(nib.TraitedSpec):
49+
files = nib.traits.List(nib.File)
50+
others = nib.File()
51+
class InputSpec(nib.TraitedSpec):
52+
infile = nib.File()
53+
outputs = OutputSpec()
54+
inputs = InputSpec()
55+
56+
wd = mkdtemp()
57+
filenames = ['file.hdr', 'file.img', 'file.BRIK', 'file.HEAD',
58+
'_0x1234.json', 'foo.txt']
59+
outfiles = []
60+
for filename in filenames:
61+
outfile = os.path.join(wd, filename)
62+
with open(outfile, 'wt') as fp:
63+
fp.writelines('dummy')
64+
outfiles.append(outfile)
65+
outputs.files = outfiles[:4:2]
66+
outputs.others = outfiles[5]
67+
inputs.infile = outfiles[-1]
68+
needed_outputs = ['files']
69+
config.set_default_config()
70+
yield assert_true, os.path.exists(outfiles[5])
71+
config.set_default_config()
72+
config.set('execution', 'remove_unnecessary_outputs', False)
73+
out = clean_working_directory(outputs, wd, inputs, needed_outputs,
74+
deepcopy(config._sections))
75+
yield assert_true, os.path.exists(outfiles[5])
76+
yield assert_equal, out.others, outfiles[5]
77+
config.set('execution', 'remove_unnecessary_outputs', True)
78+
out = clean_working_directory(outputs, wd, inputs, needed_outputs,
79+
deepcopy(config._sections))
80+
yield assert_true, os.path.exists(outfiles[1])
81+
yield assert_true, os.path.exists(outfiles[3])
82+
yield assert_true, os.path.exists(outfiles[4])
83+
yield assert_false, os.path.exists(outfiles[5])
84+
yield assert_equal, out.others, nib.Undefined
85+
yield assert_equal, len(out.files), 2
86+
config.set_default_config()
87+
rmtree(wd)
88+
4989
def test_outputs_removal():
5090

5191
def test_function(arg1):
@@ -140,19 +180,12 @@ def test_function(arg1):
140180
file1 = os.path.join(os.getcwd(), 'file1.txt')
141181
file2 = os.path.join(os.getcwd(), 'file2.txt')
142182
file3 = os.path.join(os.getcwd(), 'file3.txt')
143-
fp = open(file1, 'wt')
144-
fp.write('%d' % arg1)
145-
fp.close()
146-
fp = open(file2, 'wt')
147-
fp.write('%d' % arg1)
148-
fp.close()
149-
fp = open(file3, 'wt')
150-
fp.write('%d' % arg1)
151-
fp.close()
183+
file4 = os.path.join(os.getcwd(), 'subdir', 'file1.txt')
184+
files = [file1, file2, file3, file4]
152185
os.mkdir("subdir")
153-
fp = open("subdir/file1.txt", 'wt')
154-
fp.write('%d' % arg1)
155-
fp.close()
186+
for filename in files:
187+
with open(filename, 'wt') as fp:
188+
fp.write('%d' % arg1)
156189
return file1, file2, os.path.join(os.getcwd(),"subdir")
157190

158191
def test_function2(in_file, arg):
@@ -161,15 +194,10 @@ def test_function2(in_file, arg):
161194
file1 = os.path.join(os.getcwd(), 'file1.txt')
162195
file2 = os.path.join(os.getcwd(), 'file2.txt')
163196
file3 = os.path.join(os.getcwd(), 'file3.txt')
164-
fp = open(file1, 'wt')
165-
fp.write('%d' % arg + in_arg)
166-
fp.close()
167-
fp = open(file2, 'wt')
168-
fp.write('%d' % arg + in_arg)
169-
fp.close()
170-
fp = open(file3, 'wt')
171-
fp.write('%d' % arg + in_arg)
172-
fp.close()
197+
files = [file1, file2, file3]
198+
for filename in files:
199+
with open(filename, 'wt') as fp:
200+
fp.write('%d' % arg + in_arg)
173201
return file1, file2, 1
174202

175203
def test_function3(arg):

nipype/pipeline/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import networkx as nx
2121

2222
from ..utils.filemanip import (fname_presuffix, FileNotFoundError,
23-
filename_to_list)
23+
filename_to_list, get_related_files)
2424
from ..utils.misc import create_function_from_source, str2bool
2525
from ..interfaces.base import (CommandLine, isdefined, Undefined, Bunch,
2626
InterfaceResult)
@@ -1006,6 +1006,10 @@ def clean_working_directory(outputs, cwd, inputs, needed_outputs, config,
10061006
needed_dirs.extend(filename_to_list(dirs2keep))
10071007
for extra in ['_nipype', '_report']:
10081008
needed_dirs.extend(glob(os.path.join(cwd, extra)))
1009+
temp = []
1010+
for filename in needed_files:
1011+
temp.extend(get_related_files(filename))
1012+
needed_files = temp
10091013
logger.debug('Needed files: %s' % (';'.join(needed_files)))
10101014
logger.debug('Needed dirs: %s' % (';'.join(needed_dirs)))
10111015
files2remove = []

0 commit comments

Comments
 (0)