@@ -207,7 +207,8 @@ def hash_timestamp(afile):
207207
208208
209209def copyfile (originalfile , newfile , copy = False , create_new = False ,
210- hashmethod = None , use_hardlink = False ):
210+ hashmethod = None , use_hardlink = False ,
211+ copy_related_files = True ):
211212 """Copy or link ``originalfile`` to ``newfile``.
212213
213214 If ``use_hardlink`` is True, and the file can be hard-linked, then a
@@ -228,6 +229,9 @@ def copyfile(originalfile, newfile, copy=False, create_new=False,
228229 use_hardlink : Bool
229230 specifies whether to hard-link files, when able
230231 (Default=False), taking precedence over copy
232+ copy_related_files : Bool
233+ specifies whether to also operate on related files, as defined in
234+ ``related_filetype_sets``
231235
232236 Returns
233237 -------
@@ -320,30 +324,36 @@ def copyfile(originalfile, newfile, copy=False, create_new=False,
320324 fmlogger .warn (e .message )
321325
322326 # Associated files
323- _ , _ , this_type = split_filename (originalfile )
324- for type_set in related_filetype_sets :
325- if this_type in type_set :
326- for alt_type in type_set :
327- alt_ofile = originalfile [:- len (this_type )] + alt_type
328- alt_nfile = newfile [:- len (this_type )] + alt_type
329- if os .path .exists (alt_ofile ) and not os .path .exists (alt_nfile ):
330- copyfile (alt_ofile , alt_nfile , copy ,
331- hashmethod = hashmethod ,
332- use_hardlink = use_hardlink )
327+ if copy_related_files :
328+ related_file_pairs = (get_related_files (f , include_this_file = False )
329+ for f in (originalfile , newfile ))
330+ for alt_ofile , alt_nfile in zip (* related_file_pairs ):
331+ if os .path .exists (alt_ofile ):
332+ copyfile (alt_ofile , alt_nfile , copy , hashmethod = hashmethod ,
333+ use_hardlink = use_hardlink , copy_related_files = False )
333334
334335 return newfile
335336
336337
337- def get_related_files (filename ):
338- """Returns a list of related files for Nifti-Pair, Analyze (SPM) and AFNI
339- files
338+ def get_related_files (filename , include_this_file = True ):
339+ """Returns a list of related files, as defined in
340+ ``related_filetype_sets``, for a filename. (e.g., Nifti-Pair, Analyze (SPM)
341+ and AFNI files).
342+
343+ Parameters
344+ ----------
345+ filename : str
346+ File name to find related filetypes of.
347+ include_this_file : bool
348+ If true, output includes the input filename.
340349 """
341350 related_files = []
342- path , name , ext = split_filename (filename )
351+ path , name , this_type = split_filename (filename )
343352 for type_set in related_filetype_sets :
344- if ext in type_set :
345- for new_ext in type_set :
346- related_files .append (os .path .join (path , name + new_ext ))
353+ if this_type in type_set :
354+ for related_type in type_set :
355+ if include_this_file or related_type != this_type :
356+ related_files .append (os .path .join (path , name + related_type ))
347357 if not len (related_files ):
348358 related_files = [filename ]
349359 return related_files
0 commit comments