Skip to content

Commit 3b83940

Browse files
committed
member could be function, and use filemanip.hash_infile
1 parent 92a8ce5 commit 3b83940

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed

nipype/interfaces/base/support.py

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
"""
1010
from __future__ import print_function, division, unicode_literals, absolute_import
11-
from builtins import range, object, open, str
11+
from builtins import range, object, str
1212

1313
import os
1414
from copy import deepcopy
@@ -18,7 +18,7 @@
1818

1919
from ... import logging
2020
from ...utils.misc import is_container
21-
from ...utils.filemanip import md5, to_str
21+
from ...utils.filemanip import md5, to_str, hash_infile
2222
iflogger = logging.getLogger('interface')
2323

2424

@@ -116,27 +116,6 @@ def __repr__(self):
116116
outstr.append(')')
117117
return ''.join(outstr)
118118

119-
def _hash_infile(self, adict, key):
120-
# Inject file hashes into adict[key]
121-
stuff = adict[key]
122-
if not is_container(stuff):
123-
stuff = [stuff]
124-
file_list = []
125-
for afile in stuff:
126-
if os.path.isfile(afile):
127-
md5obj = md5()
128-
with open(afile, 'rb') as fp:
129-
while True:
130-
data = fp.read(8192)
131-
if not data:
132-
break
133-
md5obj.update(data)
134-
md5hex = md5obj.hexdigest()
135-
else:
136-
md5hex = None
137-
file_list.append((afile, md5hex))
138-
return file_list
139-
140119
def _get_bunch_hash(self):
141120
"""Return a dictionary of our items with hashes for each file.
142121
@@ -181,7 +160,7 @@ def _get_bunch_hash(self):
181160
dict_withhash = self.dictcopy()
182161
dict_nofilename = self.dictcopy()
183162
for item in infile_list:
184-
dict_withhash[item] = self._hash_infile(dict_withhash, item)
163+
dict_withhash[item] = _hash_bunch_dict(dict_withhash, item)
185164
dict_nofilename[item] = [val[1] for val in dict_withhash[item]]
186165
# Sort the items of the dictionary, before hashing the string
187166
# representation so we get a predictable order of the
@@ -208,6 +187,15 @@ def __pretty__(self, p, cycle):
208187
p.end_group(6, ')')
209188

210189

190+
def _hash_bunch_dict(self, adict, key):
191+
"""Inject file hashes into adict[key]"""
192+
stuff = adict[key]
193+
if not is_container(stuff):
194+
stuff = [stuff]
195+
return [(afile, hash_infile(afile))
196+
for afile in stuff]
197+
198+
211199
class InterfaceResult(object):
212200
"""Object that contains the results of running a particular Interface.
213201

0 commit comments

Comments
 (0)