Skip to content

Commit 3bc612a

Browse files
committed
robuster fetch algorithm (allows S3 download on a DataLad repo)
1 parent b777b7b commit 3bc612a

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

templateflow/api.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,23 @@ 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
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:
32+
_datalad_get(filepath)
33+
dl_missing.remove(filepath)
34+
35+
# Fall-back to S3 if some files are still missing
2936
s3_missing = [p for p in out_file
3037
if p.is_file() and p.stat().st_size == 0]
31-
for filepath in s3_missing:
38+
for filepath in s3_missing + dl_missing:
3239
_s3_get(filepath)
3340

34-
dl_missing = None
35-
if TF_USE_DATALAD:
36-
dl_missing = [p for p in out_file if not p.is_file()]
37-
for filepath in dl_missing:
38-
_datalad_get(filepath)
39-
40-
not_fetched = [p for p in out_file
41+
not_fetched = [str(p) for p in out_file
4142
if not p.is_file() or p.stat().st_size == 0]
4243

43-
if any(not_fetched):
44+
if not_fetched:
4445
msg = "Could not fetch template files: %s." % ', '.join(not_fetched)
4546
if dl_missing and not TF_USE_DATALAD:
4647
msg += """\
@@ -132,6 +133,9 @@ def _s3_get(filepath):
132133
total_size = int(r.headers.get('content-length', 0))
133134
block_size = 1024
134135
wrote = 0
136+
if not filepath.is_file():
137+
filepath.unlink()
138+
135139
with filepath.open('wb') as f:
136140
for data in tqdm(r.iter_content(block_size),
137141
total=ceil(total_size // block_size),

0 commit comments

Comments
 (0)