Skip to content

Commit f932c55

Browse files
authored
Merge pull request #10 from templateflow/fix/datalad-related-error-msg
FIX: ``api.get`` - robuster fetcher algorithm (allows S3 download on DL repos) and better error messages
2 parents 899a16a + 422b2cd commit f932c55

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

.circleci/config.yml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
git config --global user.email "[email protected]"
4949
5050
- run:
51-
name: Run tests (w/o datalad)
51+
name: Run tests (w/o DataLad)
5252
environment:
5353
TEMPLATEFLOW_USE_DATALAD: 0
5454
TEMPLATEFLOW_HOME: "/tmp/data/templateflow"
@@ -69,7 +69,7 @@ jobs:
6969
pytest -vsx --doctest-modules /tmp/src/templateflow/templateflow
7070
7171
- run:
72-
name: Run tests (w/ datalad)
72+
name: Run tests (w/ DataLad)
7373
environment:
7474
TEMPLATEFLOW_USE_DATALAD: 1
7575
command: |
@@ -78,6 +78,20 @@ jobs:
7878
virtualenv venv
7979
pytest -vsx --doctest-modules /tmp/src/templateflow/templateflow
8080
81+
- run:
82+
name: Run tests (w/ DataLad, bypassed via S3)
83+
environment:
84+
TEMPLATEFLOW_USE_DATALAD: 1
85+
TEMPLATEFLOW_HOME: /home/circleci/.cache/templateflow-init
86+
command: |
87+
pyenv global 3.5.2
88+
virtualenv venv
89+
cd /tmp/src/templateflow
90+
pip install -e .
91+
python -c "from templateflow import api"
92+
export TEMPLATEFLOW_USE_DATALAD=0
93+
pytest -vsx --doctest-modules /tmp/src/templateflow/templateflow
94+
8195
- run:
8296
name: Test packaging
8397
command: |

templateflow/api.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)