Skip to content

Commit c195307

Browse files
committed
ENH better handling of temp directory for downloading subjects
1 parent d942fc0 commit c195307

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

cortex/tests/test_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ def test_download_subject():
1010
assert "fsaverage" not in cortex.db.subjects
1111
cortex.utils.download_subject(subject_id='fsaverage')
1212
assert "fsaverage" in cortex.db.subjects
13+
# test that downloading it again works
14+
cortex.utils.download_subject(subject_id='fsaverage', download_again=True)

cortex/utils.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,25 +1079,28 @@ def download_subject(subject_id='fsaverage', url=None, pycortex_store=None,
10791079
"the subject again.".format(subject_id))
10801080
return
10811081
# Map codes to URLs; more coming eventually
1082-
id_to_url = dict(fsaverage='https://ndownloader.figshare.com/files/17827577?private_link=4871247dce31e188e758',
1083-
)
1082+
id_to_url = dict(
1083+
fsaverage='https://ndownloader.figshare.com/files/17827577?private_link=4871247dce31e188e758',
1084+
)
10841085
if url is None:
1085-
if not subject_id in id_to_url:
1086+
if subject_id not in id_to_url:
10861087
raise ValueError('Unknown subject_id!')
10871088
url = id_to_url[subject_id]
1088-
print("Downloading from: {}".format(url))
1089-
# Download to temp dir
1090-
tmp_dir = tempfile.gettempdir()
1091-
wget.download(url, tmp_dir)
1092-
print('Downloaded subject {} to {}'.format(subject_id, tmp_dir))
1093-
# Un-tar to pycortex store
1089+
# Setup pycortex store location
10941090
if pycortex_store is None:
10951091
# Default location is current filestore in cortex.db
10961092
pycortex_store = db.filestore
1097-
pycortex_store = os.path.expanduser(pycortex_store)
1098-
with tarfile.open(os.path.join(tmp_dir, subject_id + '.tar.gz'), "r:gz") as tar:
1099-
print("Extracting subject {} to {}".format(subject_id, pycortex_store))
1100-
tar.extractall(path=pycortex_store)
1093+
pycortex_store = os.path.abspath(os.path.expanduser(pycortex_store))
1094+
# Download to temp dir
1095+
print("Downloading from: {}".format(url))
1096+
with tempfile.TemporaryDirectory() as tmp_dir:
1097+
print('Downloading subject {} to {}'.format(subject_id, tmp_dir))
1098+
wget.download(url, tmp_dir)
1099+
print('Done downloading')
1100+
# Un-tar to pycortex store
1101+
with tarfile.open(os.path.join(tmp_dir, subject_id + '.tar.gz'), "r:gz") as tar:
1102+
print("Extracting subject {} to {}".format(subject_id, pycortex_store))
1103+
tar.extractall(path=pycortex_store)
11011104

11021105
# reload all subjects from the filestore
11031106
db.reload_subjects()

0 commit comments

Comments
 (0)