Skip to content

Commit 8f20d4d

Browse files
committed
fixes for long-broken cache fallback when cache dir not findable
1 parent 9b99e08 commit 8f20d4d

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

astropy/utils/data.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -753,10 +753,9 @@ def download_file(remote_url, cache=False):
753753
cache = False
754754
missing_cache = True # indicates that the cache is missing to raise a warning later
755755

756-
_acquire_download_cache_lock()
757-
756+
if not missing_cache:
757+
_acquire_download_cache_lock()
758758
try:
759-
760759
if cache:
761760
with _open_shelve(urlmapfn, True) as url2hash:
762761
if str(remote_url) in url2hash:
@@ -789,17 +788,15 @@ def download_file(remote_url, cache=False):
789788
block = remote.read(DOWNLOAD_CACHE_BLOCK_SIZE())
790789

791790
if cache:
792-
793791
with _open_shelve(urlmapfn, True) as url2hash:
794792
local_path = os.path.join(dldir, hash.hexdigest())
795793
move(f.name, local_path)
796794
url2hash[str(remote_url)] = local_path
797-
798795
else:
799-
800796
local_path = f.name
801797
if missing_cache:
802-
msg = 'File downloaded to temp file due to lack of cache access.'
798+
msg = ('File downloaded to temporary location due to problem '
799+
'with cache directory and will not be cached.')
803800
warn(CacheMissingWarning(msg, local_path))
804801
if DELETE_TEMPORARY_DOWNLOADS_AT_EXIT():
805802
global _tempfilestodel
@@ -810,7 +807,7 @@ def download_file(remote_url, cache=False):
810807
e.reason.args = (e.reason.errno, e.reason.strerror)
811808
raise e
812809
finally:
813-
if cache:
810+
if cache and not missing_cache:
814811
_release_download_cache_lock()
815812

816813
return local_path

astropy/utils/tests/test_data.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,24 @@ def test_get_pkg_data_contents():
137137
@remote_data
138138
def test_data_noastropy_fallback(monkeypatch, recwarn):
139139
"""
140-
Tests to make sure configuration items fall back to their defaults when
141-
there's a problem accessing the astropy directory
140+
Tests to make sure the default behavior when the cache directory can't
141+
be located is correct
142142
"""
143143
from pytest import raises
144144
from .. import data
145145
from ...config import paths
146146

147+
# needed for testing the *real* lock at the end
148+
lockdir = os.path.join(_get_download_cache_locs()[0], 'lock')
149+
147150
#better yet, set the configuration to make sure the temp files are deleted
148151
data.DELETE_TEMPORARY_DOWNLOADS_AT_EXIT.set(True)
149152

150-
#make sure the config directory is not searched
153+
#make sure the config and cache directories are not searched
151154
monkeypatch.setenv('XDG_CONFIG_HOME', 'foo')
152155
monkeypatch.delenv('XDG_CONFIG_HOME')
156+
monkeypatch.setenv('XDG_CACHE_HOME', 'bar')
157+
monkeypatch.delenv('XDG_CACHE_HOME')
153158

154159
# make sure the _find_or_create_astropy_dir function fails as though the
155160
# astropy dir could not be accessed
@@ -162,7 +167,7 @@ def osraiser(dirnm, linkto):
162167
paths.get_cache_dir()
163168

164169
#first try with cache
165-
fnout = data.get_pkg_data_filename(TESTURL)
170+
fnout = data.download_file(TESTURL, cache=True)
166171
assert os.path.isfile(fnout)
167172

168173
assert len(recwarn.list) > 1
@@ -172,7 +177,7 @@ def osraiser(dirnm, linkto):
172177
assert w1.category == data.CacheMissingWarning
173178
assert 'Remote data cache could not be accessed' in w1.message.args[0]
174179
assert w2.category == data.CacheMissingWarning
175-
assert 'File downloaded to temp file' in w2.message.args[0]
180+
assert 'File downloaded to temporary location' in w2.message.args[0]
176181
assert fnout == w2.message.args[1]
177182

178183
#clearing the cache should be a no-up that doesn't affect fnout
@@ -192,13 +197,14 @@ def osraiser(dirnm, linkto):
192197
assert 'Not clearing data cache - cache inacessable' in str(w3.message)
193198

194199
#now try with no cache
195-
with data.get_pkg_data_fileobj(TESTURL, cache=False) as googlepage:
200+
fnnocache = data.download_file(TESTURL, cache=False)
201+
with open(fnnocache) as googlepage:
196202
assert googlepage.read().decode().find('oogle</title>') > -1
197203

198204
#no warnings should be raise in fileobj because cache is unnecessary
199205
assert len(recwarn.list) == 0
200206

201-
lockdir = os.path.join(_get_download_cache_locs()[0], 'lock')
207+
# lockdir determined above as the *real* lockdir, not the temp one
202208
assert not os.path.isdir(lockdir), 'Cache dir lock was not released!'
203209

204210

0 commit comments

Comments
 (0)