Skip to content

Commit 1f4f582

Browse files
authored
Merge pull request #91 from templateflow/enh/wipe-cache-feature
ENH: Add a ``wipe()`` utility to clear up the cache
2 parents 2fdc7de + afd1132 commit 1f4f582

1 file changed

Lines changed: 64 additions & 27 deletions

File tree

templateflow/conf/__init__.py

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,43 +19,52 @@
1919
TF_CACHED = True
2020

2121

22+
def _init_cache():
23+
global TF_HOME, TF_CACHED, TF_USE_DATALAD
24+
25+
if not TF_HOME.exists() or not list(TF_HOME.iterdir()):
26+
TF_CACHED = False
27+
warn(
28+
f"""\
29+
TemplateFlow: repository not found at <{TF_HOME}>. Populating a new TemplateFlow stub.
30+
If the path reported above is not the desired location for TemplateFlow, \
31+
please set the TEMPLATEFLOW_HOME environment variable.""",
32+
ResourceWarning,
33+
)
34+
if TF_USE_DATALAD:
35+
try:
36+
from datalad.api import install
37+
except ImportError:
38+
TF_USE_DATALAD = False
39+
else:
40+
TF_HOME.parent.mkdir(exist_ok=True, parents=True)
41+
install(path=str(TF_HOME), source=TF_GITHUB_SOURCE, recursive=True)
42+
43+
if not TF_USE_DATALAD:
44+
from ._s3 import update as _update_s3
45+
46+
_update_s3(TF_HOME, local=True, overwrite=True)
47+
48+
49+
_init_cache()
50+
51+
2252
def requires_layout(func):
2353
"""Decorate function to ensure ``TF_LAYOUT`` is correctly initiated."""
54+
2455
@wraps(func)
2556
def wrapper(*args, **kwargs):
2657
from templateflow.conf import TF_LAYOUT
2758

2859
if TF_LAYOUT is None:
2960
from bids import __version__
30-
raise RuntimeError(f"A layout with PyBIDS <{__version__}> could not be initiated")
31-
return func(*args, **kwargs)
32-
return wrapper
33-
3461

35-
if not TF_HOME.exists() or not list(TF_HOME.iterdir()):
36-
TF_CACHED = False
37-
warn(
38-
"""\
39-
TemplateFlow: repository not found at %s. Populating a new TemplateFlow stub.
40-
If the path reported above is not the desired location for TemplateFlow, \
41-
please set the TEMPLATEFLOW_HOME environment variable.\
42-
"""
43-
% TF_HOME,
44-
ResourceWarning,
45-
)
46-
if TF_USE_DATALAD:
47-
try:
48-
from datalad.api import install
49-
except ImportError:
50-
TF_USE_DATALAD = False
51-
else:
52-
TF_HOME.parent.mkdir(exist_ok=True, parents=True)
53-
install(path=str(TF_HOME), source=TF_GITHUB_SOURCE, recursive=True)
54-
55-
if not TF_USE_DATALAD:
56-
from ._s3 import update as _update_s3
62+
raise RuntimeError(
63+
f"A layout with PyBIDS <{__version__}> could not be initiated"
64+
)
65+
return func(*args, **kwargs)
5766

58-
_update_s3(TF_HOME, local=True, overwrite=True)
67+
return wrapper
5968

6069

6170
def update(local=False, overwrite=True, silent=False):
@@ -64,6 +73,7 @@ def update(local=False, overwrite=True, silent=False):
6473
success = True
6574
else:
6675
from ._s3 import update as _update_s3
76+
6777
success = _update_s3(TF_HOME, local=local, overwrite=overwrite, silent=silent)
6878

6979
# update Layout only if necessary
@@ -72,10 +82,37 @@ def update(local=False, overwrite=True, silent=False):
7282
# ensure the api uses the updated layout
7383
import importlib
7484
from .. import api
85+
7586
importlib.reload(api)
7687
return success
7788

7889

90+
def wipe():
91+
"""Clear the cache if functioning in S3 mode."""
92+
global TF_USE_DATALAD, TF_HOME
93+
94+
if TF_USE_DATALAD:
95+
print("TemplateFlow is configured in DataLad mode, wipe() has no effect")
96+
return
97+
98+
import importlib
99+
from shutil import rmtree
100+
from templateflow import api
101+
102+
def _onerror(func, path, excinfo):
103+
from pathlib import Path
104+
105+
if Path(path).exists():
106+
print(
107+
f"Warning: could not delete <{path}>, please clear the cache manually."
108+
)
109+
110+
rmtree(TF_HOME, onerror=_onerror)
111+
_init_cache()
112+
113+
importlib.reload(api)
114+
115+
79116
def setup_home(force=False):
80117
"""Initialize/update TF's home if necessary."""
81118
if not force and not TF_CACHED:

0 commit comments

Comments
 (0)