Skip to content

Commit

Permalink
Merge pull request #8 from templateflow/fix/datalad-default
Browse files Browse the repository at this point in the history
FIX: Require environment variable to use DataLad
  • Loading branch information
oesteban authored Mar 13, 2019
2 parents 8f27ab9 + 1bc9513 commit c2be322
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
11 changes: 9 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
pyenv global 3.5.2
virtualenv venv
pip install -r /tmp/src/templateflow/requirements.txt
pip install datalad
pip install "setuptools>=27.0" twine
- save_cache:
Expand All @@ -48,6 +49,9 @@ jobs:
- run:
name: Run tests (w/o datalad)
environment:
TEMPLATEFLOW_USE_DATALAD: 0
TEMPLATEFLOW_HOME: "/tmp/data/templateflow"
command: |
pyenv global 3.5.2
virtualenv venv
Expand All @@ -56,19 +60,22 @@ jobs:
- run:
name: Run tests (pulling from S3)
environment:
TEMPLATEFLOW_USE_DATALAD: 0
TEMPLATEFLOW_HOME: "/tmp/data/templateflow-S3"
command: |
pyenv global 3.5.2
virtualenv venv
export TEMPLATEFLOW_HOME="/tmp/data/templateflow-S3"
pytest -vsx --doctest-modules /tmp/src/templateflow/templateflow
- run:
name: Run tests (w/ datalad)
environment:
TEMPLATEFLOW_USE_DATALAD: 1
command: |
unset TEMPLATEFLOW_HOME
pyenv global 3.5.2
virtualenv venv
pip install datalad
pytest -vsx --doctest-modules /tmp/src/templateflow/templateflow
- run:
Expand Down
15 changes: 12 additions & 3 deletions templateflow/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
from pathlib import Path
from json import loads
from .conf import TF_LAYOUT, TF_S3_ROOT
from .conf import TF_LAYOUT, TF_S3_ROOT, TF_USE_DATALAD


def get(template, **kwargs):
Expand All @@ -25,12 +25,21 @@ def get(template, **kwargs):
out_file = [Path(p) for p in TF_LAYOUT.get(
template=template, return_type='file', **kwargs)]

# Try plain URL fetch first
for filepath in [p for p in out_file
if p.is_file() and p.stat().st_size == 0]:
_s3_get(filepath)

for filepath in [p for p in out_file if not p.is_file()]:
_datalad_get(filepath)
if TF_USE_DATALAD:
for filepath in [p for p in out_file if not p.is_file()]:
_datalad_get(filepath)

not_fetched = [p for p in out_file
if not p.is_file() or p.stat().st_size == 0]

if any(not_fetched):
raise RuntimeError(
"Could not fetch template files: %s" % ', '.join(not_fetched))

if len(out_file) == 1:
return out_file[0]
Expand Down
20 changes: 13 additions & 7 deletions templateflow/conf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,32 @@
TF_HOME = Path(getenv('TEMPLATEFLOW_HOME', str(TF_DEFAULT_HOME)))
TF_GITHUB_SOURCE = 'https://github.com/templateflow/templateflow.git'
TF_S3_ROOT = 'https://templateflow.s3.amazonaws.com'
TF_USE_DATALAD = getenv('TEMPLATEFLOW_USE_DATALAD', 'false').lower() in (
'true', 'on', '1')

_msg = """\
TemplateFlow: repository not found at %s. Populating a TemplateFlow stub.
If the path reported above is not the desired location for Templateflow, \
If the path reported above is not the desired location for TemplateFlow, \
please set the TEMPLATEFLOW_HOME environment variable.
""" % TF_HOME

if not TF_HOME.exists() or not list(TF_HOME.iterdir()):
warn(_msg, ResourceWarning)
try:
from datalad.api import install
except ImportError:
if TF_USE_DATALAD:
try:
from datalad.api import install
except ImportError:
TF_USE_DATALAD = False
else:
TF_HOME.parent.mkdir(exist_ok=True, parents=True)
install(path=str(TF_HOME), source=TF_GITHUB_SOURCE, recursive=True)

if not TF_USE_DATALAD:
from zipfile import ZipFile
TF_HOME.mkdir(exist_ok=True, parents=True)
with ZipFile(resource_filename('templateflow',
'conf/templateflow-skel.zip'), 'r') as zipref:
zipref.extractall(str(TF_HOME))
else:
TF_HOME.parent.mkdir(exist_ok=True, parents=True)
install(path=str(TF_HOME), source=TF_GITHUB_SOURCE, recursive=True)

TF_LAYOUT = Layout(
TF_HOME, validate=False, config='templateflow',
Expand Down

0 comments on commit c2be322

Please sign in to comment.