Skip to content

Commit 80a9fcf

Browse files
committed
rf: Remove unnecessary global constants from _s3
1 parent 662b4bf commit 80a9fcf

File tree

3 files changed

+42
-34
lines changed

3 files changed

+42
-34
lines changed

templateflow/conf/_s3.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,46 +25,48 @@
2525
from pathlib import Path
2626
from tempfile import mkstemp
2727

28-
from templateflow.conf import TF_GET_TIMEOUT, load_data
28+
from acres import Loader
29+
30+
load_data = Loader(__spec__.parent)
2931

3032
TF_SKEL_URL = (
3133
'https://raw.githubusercontent.com/templateflow/python-client/'
3234
'{release}/templateflow/conf/templateflow-skel.{ext}'
3335
).format
34-
TF_SKEL_PATH = load_data('templateflow-skel.zip')
35-
TF_SKEL_MD5 = load_data.readable('templateflow-skel.md5').read_text()
3636

3737

38-
def update(dest, local=True, overwrite=True, silent=False):
38+
def update(dest, local=True, overwrite=True, silent=False, *, timeout: int):
3939
"""Update an S3-backed TEMPLATEFLOW_HOME repository."""
40-
skel_file = Path((_get_skeleton_file() if not local else None) or TF_SKEL_PATH)
40+
skel_zip = load_data('templateflow-skel.zip')
41+
skel_file = Path((_get_skeleton_file(timeout) if not local else None) or skel_zip)
4142

4243
retval = _update_skeleton(skel_file, dest, overwrite=overwrite, silent=silent)
43-
if skel_file != TF_SKEL_PATH:
44+
if skel_file != skel_zip:
4445
skel_file.unlink()
4546
return retval
4647

4748

48-
def _get_skeleton_file():
49+
def _get_skeleton_file(timeout: int):
4950
import requests
5051

5152
try:
5253
r = requests.get(
5354
TF_SKEL_URL(release='master', ext='md5'),
5455
allow_redirects=True,
55-
timeout=TF_GET_TIMEOUT,
56+
timeout=timeout,
5657
)
5758
except requests.exceptions.ConnectionError:
5859
return
5960

6061
if not r.ok:
6162
return
6263

63-
if r.content.decode().split()[0] != TF_SKEL_MD5:
64+
md5 = load_data.readable('templateflow-skel.md5').read_bytes()
65+
if r.content != md5:
6466
r = requests.get(
6567
TF_SKEL_URL(release='master', ext='zip'),
6668
allow_redirects=True,
67-
timeout=TF_GET_TIMEOUT,
69+
timeout=timeout,
6870
)
6971
if r.ok:
7072
from os import close

templateflow/conf/bids.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
#
2323
"""Extending pyBIDS for querying TemplateFlow."""
2424

25+
from acres import Loader
2526
from bids.layout import BIDSLayout, add_config_paths
2627

27-
from templateflow.conf import load_data
28+
load_data = Loader(__spec__.parent)
2829

2930
add_config_paths(templateflow=load_data('config.json'))
3031

templateflow/tests/test_s3.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,31 +44,36 @@ def test_get_skel_file(tmp_path, monkeypatch):
4444
# First execution, the S3 stub is created (or datalad install)
4545
reload(tfc)
4646

47-
local_md5 = tfc._s3.TF_SKEL_MD5
48-
monkeypatch.setattr(tfc._s3, 'TF_SKEL_MD5', 'invent')
49-
new_skel = tfc._s3._get_skeleton_file()
47+
md5content = b'anything'
48+
49+
def mock_get(*args, **kwargs):
50+
class MockResponse:
51+
status_code = 200
52+
ok = True
53+
content = md5content
54+
55+
return MockResponse()
56+
57+
monkeypatch.setattr(requests, 'get', mock_get)
58+
59+
# Mismatching the local MD5 causes an update
60+
new_skel = tfc._s3._get_skeleton_file(timeout=10)
5061
assert new_skel is not None
5162
assert Path(new_skel).exists()
52-
assert Path(new_skel).stat().st_size > 0
63+
assert Path(new_skel).read_bytes() == b'anything'
5364

54-
latest_md5 = (
55-
requests.get(
56-
tfc._s3.TF_SKEL_URL(release='master', ext='md5', allow_redirects=True), timeout=10
57-
)
58-
.content.decode()
59-
.split()[0]
60-
)
61-
monkeypatch.setattr(tfc._s3, 'TF_SKEL_MD5', latest_md5)
62-
assert tfc._s3._get_skeleton_file() is None
65+
md5content = tfc._s3.load_data.readable('templateflow-skel.md5').read_bytes()
66+
# Matching the local MD5 skips the update
67+
assert tfc._s3._get_skeleton_file(timeout=10) is None
6368

64-
monkeypatch.setattr(tfc._s3, 'TF_SKEL_MD5', local_md5)
69+
# Bad URL fails to update
6570
monkeypatch.setattr(tfc._s3, 'TF_SKEL_URL', 'http://weird/{release}/{ext}'.format)
66-
assert tfc._s3._get_skeleton_file() is None
71+
assert tfc._s3._get_skeleton_file(timeout=10) is None
6772

6873
monkeypatch.setattr(
6974
tfc._s3, 'TF_SKEL_URL', tfc._s3.TF_SKEL_URL(release='{release}', ext='{ext}z').format
7075
)
71-
assert tfc._s3._get_skeleton_file() is None
76+
assert tfc._s3._get_skeleton_file(timeout=10) is None
7277

7378

7479
def test_update_s3(tmp_path, monkeypatch):
@@ -78,19 +83,19 @@ def test_update_s3(tmp_path, monkeypatch):
7883
monkeypatch.setenv('TEMPLATEFLOW_USE_DATALAD', 'off')
7984
monkeypatch.setenv('TEMPLATEFLOW_HOME', str(newhome))
8085

81-
assert tfc._s3.update(newhome)
82-
assert not tfc._s3.update(newhome, overwrite=False)
86+
assert tfc._s3.update(newhome, timeout=10)
87+
assert not tfc._s3.update(newhome, overwrite=False, timeout=10)
8388
for p in (newhome / 'tpl-MNI152NLin6Sym').glob('*.nii.gz'):
8489
p.unlink()
85-
assert tfc._s3.update(newhome, overwrite=False)
90+
assert tfc._s3.update(newhome, overwrite=False, timeout=10)
8691

8792
# This should cover the remote zip file fetching
88-
monkeypatch.setattr(tfc._s3, 'TF_SKEL_MD5', 'invent')
89-
assert tfc._s3.update(newhome, local=False)
90-
assert not tfc._s3.update(newhome, local=False, overwrite=False)
93+
# monkeypatch.setattr(tfc._s3, 'TF_SKEL_MD5', 'invent')
94+
assert tfc._s3.update(newhome, local=False, timeout=10)
95+
assert not tfc._s3.update(newhome, local=False, overwrite=False, timeout=10)
9196
for p in (newhome / 'tpl-MNI152NLin6Sym').glob('*.nii.gz'):
9297
p.unlink()
93-
assert tfc._s3.update(newhome, local=False, overwrite=False)
98+
assert tfc._s3.update(newhome, local=False, overwrite=False, timeout=10)
9499

95100

96101
def test_s3_400_error(monkeypatch):

0 commit comments

Comments
 (0)