3131import shutil
3232import subprocess
3333import re
34+ import copy
3435import tempfile
3536from os .path import join , dirname
3637from warnings import warn
3738
3839import sqlite3
3940
4041from .. import config , logging
41- from ..utils .filemanip import copyfile , list_to_filename , filename_to_list
42+ from ..utils .filemanip import (
43+ copyfile , list_to_filename , filename_to_list ,
44+ get_related_files , related_filetype_sets )
4245from ..utils .misc import human_order_sorted , str2bool
4346from .base import (
4447 TraitedSpec , traits , Str , File , Directory , BaseInterface , InputMultiPath ,
@@ -2422,12 +2425,12 @@ def _get_files_over_ssh(self, template):
24222425 # Get all files in the dir, and filter for desired files
24232426 template_dir = os .path .dirname (template )
24242427 template_base = os .path .basename (template )
2425- filelist = sftp .listdir (template_dir )
2428+ every_file_in_dir = sftp .listdir (template_dir )
24262429 if self .inputs .template_expression == 'fnmatch' :
2427- outfiles = fnmatch .filter (filelist , template_base )
2430+ outfiles = fnmatch .filter (every_file_in_dir , template_base )
24282431 elif self .inputs .template_expression == 'regexp' :
24292432 regexp = re .compile (template_base )
2430- outfiles = list (filter (regexp .match , filelist ))
2433+ outfiles = list (filter (regexp .match , every_file_in_dir ))
24312434 else :
24322435 raise ValueError ('template_expression value invalid' )
24332436
@@ -2450,7 +2453,18 @@ def _get_files_over_ssh(self, template):
24502453
24512454 # actually download the files, if desired
24522455 if self .inputs .download_files :
2453- for f in outfiles :
2456+ files_to_download = copy .copy (outfiles ) # make sure new list!
2457+
2458+ # check to see if there are any related files to download
2459+ for file_to_download in files_to_download :
2460+ related_to_current = get_related_files (
2461+ file_to_download , include_this_file = False )
2462+ existing_related_not_downloading = [
2463+ f for f in related_to_current
2464+ if f in every_file_in_dir and f not in files_to_download ]
2465+ files_to_download .extend (existing_related_not_downloading )
2466+
2467+ for f in files_to_download :
24542468 try :
24552469 sftp .get (os .path .join (template_dir , f ), f )
24562470 except IOError :
0 commit comments