@@ -25,21 +25,39 @@ def get(template, **kwargs):
2525 out_file = [Path (p ) for p in TF_LAYOUT .get (
2626 template = template , return_type = 'file' , ** kwargs )]
2727
28- # Try plain URL fetch first
29- for filepath in [p for p in out_file
30- if p .is_file () and p .stat ().st_size == 0 ]:
31- _s3_get (filepath )
32-
33- if TF_USE_DATALAD :
34- for filepath in [p for p in out_file if not p .is_file ()]:
28+ # Try DataLad first
29+ dl_missing = [p for p in out_file if not p .is_file ()]
30+ if TF_USE_DATALAD and dl_missing :
31+ for filepath in dl_missing :
3532 _datalad_get (filepath )
33+ dl_missing .remove (filepath )
34+
35+ # Fall-back to S3 if some files are still missing
36+ s3_missing = [p for p in out_file
37+ if p .is_file () and p .stat ().st_size == 0 ]
38+ for filepath in s3_missing + dl_missing :
39+ _s3_get (filepath )
3640
37- not_fetched = [p for p in out_file
41+ not_fetched = [str ( p ) for p in out_file
3842 if not p .is_file () or p .stat ().st_size == 0 ]
3943
40- if any (not_fetched ):
41- raise RuntimeError (
42- "Could not fetch template files: %s" % ', ' .join (not_fetched ))
44+ if not_fetched :
45+ msg = "Could not fetch template files: %s." % ', ' .join (not_fetched )
46+ if dl_missing and not TF_USE_DATALAD :
47+ msg += """\
48+ The $TEMPLATEFLOW_HOME folder %s seems to contain an initiated DataLad \
49+ dataset, but the environment variable $TEMPLATEFLOW_USE_DATALAD is not \
50+ set or set to one of (false, off, 0). Please set $TEMPLATEFLOW_USE_DATALAD \
51+ on (possible values: true, on, 1).""" % TF_LAYOUT .root
52+
53+ if s3_missing and TF_USE_DATALAD :
54+ msg += """\
55+ The $TEMPLATEFLOW_HOME folder %s seems to contain an plain \
56+ dataset, but the environment variable $TEMPLATEFLOW_USE_DATALAD is \
57+ set to one of (true, on, 1). Please set $TEMPLATEFLOW_USE_DATALAD \
58+ off (possible values: false, off, 0).""" % TF_LAYOUT .root
59+
60+ raise RuntimeError (msg )
4361
4462 if len (out_file ) == 1 :
4563 return out_file [0 ]
@@ -115,6 +133,9 @@ def _s3_get(filepath):
115133 total_size = int (r .headers .get ('content-length' , 0 ))
116134 block_size = 1024
117135 wrote = 0
136+ if not filepath .is_file ():
137+ filepath .unlink ()
138+
118139 with filepath .open ('wb' ) as f :
119140 for data in tqdm (r .iter_content (block_size ),
120141 total = ceil (total_size // block_size ),
0 commit comments