Fix WheelCache.get in presence of legacy cache keys

This commit is contained in:
Stéphane Bidoul (ACSONE) 2019-12-15 18:20:03 +01:00
parent 80bfba3302
commit 36ff884673
No known key found for this signature in database
GPG Key ID: BCAB2555446B5B92
3 changed files with 24 additions and 17 deletions

1
news/7490.trivial Normal file
View File

@ -0,0 +1 @@
Fix unrelease bug from #7319.

View File

@ -138,11 +138,15 @@ class Cache(object):
candidates = []
path = self.get_path_for_link(link)
if os.path.isdir(path):
candidates.extend(os.listdir(path))
for candidate in os.listdir(path):
candidates.append((candidate, os.path.join(path, candidate)))
# TODO remove legacy path lookup in pip>=21
legacy_path = self.get_path_for_link_legacy(link)
if os.path.isdir(legacy_path):
candidates.extend(os.listdir(legacy_path))
for candidate in os.listdir(legacy_path):
candidates.append(
(candidate, os.path.join(legacy_path, candidate))
)
return candidates
def get_path_for_link_legacy(self, link):
@ -167,13 +171,6 @@ class Cache(object):
"""
raise NotImplementedError()
def _link_for_candidate(self, link, candidate):
# type: (Link, str) -> Link
root = self.get_path_for_link(link)
path = os.path.join(root, candidate)
return Link(path_to_url(path))
def cleanup(self):
# type: () -> None
pass
@ -228,7 +225,9 @@ class SimpleWheelCache(Cache):
return link
canonical_package_name = canonicalize_name(package_name)
for wheel_name in self._get_candidates(link, canonical_package_name):
for wheel_name, wheel_path in self._get_candidates(
link, canonical_package_name
):
try:
wheel = Wheel(wheel_name)
except InvalidWheelFilename:
@ -245,13 +244,17 @@ class SimpleWheelCache(Cache):
# Built for a different python/arch/etc
continue
candidates.append(
(wheel.support_index_min(supported_tags), wheel_name)
(
wheel.support_index_min(supported_tags),
wheel_name,
wheel_path,
)
)
if not candidates:
return link
return self._link_for_candidate(link, min(candidates)[1])
return Link(path_to_url(min(candidates)[2]))
class EphemWheelCache(SimpleWheelCache):

View File

@ -58,7 +58,7 @@ def test_cache_hash():
def test_get_path_for_link_legacy(tmpdir):
"""
Test that an existing cache entry that was created with the legacy hashing
mechanism is used.
mechanism is returned by WheelCache._get_candidates().
"""
wc = WheelCache(tmpdir, FormatControl())
link = Link("https://g.c/o/r")
@ -66,13 +66,16 @@ def test_get_path_for_link_legacy(tmpdir):
legacy_path = wc.get_path_for_link_legacy(link)
assert path != legacy_path
ensure_dir(path)
with open(os.path.join(path, "test-pyz-none-any.whl"), "w"):
with open(os.path.join(path, "test-1.0.0-pyz-none-any.whl"), "w"):
pass
ensure_dir(legacy_path)
with open(os.path.join(legacy_path, "test-pyx-none-any.whl"), "w"):
with open(os.path.join(legacy_path, "test-1.0.0-pyx-none-any.whl"), "w"):
pass
expected_candidates = {"test-pyx-none-any.whl", "test-pyz-none-any.whl"}
assert set(wc._get_candidates(link, "test")) == expected_candidates
expected_candidates = {
"test-1.0.0-pyx-none-any.whl", "test-1.0.0-pyz-none-any.whl"
}
candidates = {c[0] for c in wc._get_candidates(link, "test")}
assert candidates == expected_candidates
def test_get_with_legacy_entry_only(tmpdir):