Skip to content

Commit 613cc01

Browse files
committed
Include subdirectory URL fragment in the cache key
1 parent 6c04fef commit 613cc01

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

news/7333.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Include ``subdirectory`` URL fragments in cache keys.

src/pip/_internal/cache.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ def _get_cache_path_parts(self, link):
5858
key_parts = [link.url_without_fragment]
5959
if link.hash_name is not None and link.hash is not None:
6060
key_parts.append("=".join([link.hash_name, link.hash]))
61+
if link.subdirectory_fragment:
62+
key_parts.append(
63+
"=".join(["subdirectory", link.subdirectory_fragment])
64+
)
6165
key_url = "#".join(key_parts)
6266

6367
# Encode our key url with sha224, we'll use this because it has similar
@@ -168,11 +172,17 @@ def get(
168172
# type: (...) -> Link
169173
candidates = []
170174

175+
canonical_package_name = None
176+
if package_name:
177+
canonical_package_name = canonicalize_name(package_name)
171178
for wheel_name in self._get_candidates(link, package_name):
172179
try:
173180
wheel = Wheel(wheel_name)
174181
except InvalidWheelFilename:
175182
continue
183+
assert canonical_package_name
184+
if wheel.name != canonical_package_name:
185+
continue
176186
if not wheel.supported(supported_tags):
177187
# Built for a different python/arch/etc
178188
continue

tests/unit/test_cache.py

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,44 @@
1+
import os
2+
13
from pip._internal.cache import WheelCache
4+
from pip._internal.models.format_control import FormatControl
5+
from pip._internal.models.link import Link
26
from pip._internal.utils.compat import expanduser
7+
from pip._internal.utils.misc import ensure_dir
8+
9+
10+
def test_expands_path():
11+
wc = WheelCache("~/.foo/", None)
12+
assert wc.cache_dir == expanduser("~/.foo/")
13+
14+
15+
def test_falsey_path_none():
16+
wc = WheelCache(False, None)
17+
assert wc.cache_dir is None
318

419

5-
class TestWheelCache:
20+
def test_subdirectory_fragment():
21+
"""
22+
Test the subdirectory URL fragment is part of the cache key.
23+
"""
24+
wc = WheelCache("~/.foo/", None)
25+
link1 = Link("git+https://g.c/o/r#subdirectory=d1")
26+
link2 = Link("git+https://g.c/o/r#subdirectory=d2")
27+
assert wc.get_path_for_link(link1) != wc.get_path_for_link(link2)
628

7-
def test_expands_path(self):
8-
wc = WheelCache("~/.foo/", None)
9-
assert wc.cache_dir == expanduser("~/.foo/")
1029

11-
def test_falsey_path_none(self):
12-
wc = WheelCache(False, None)
13-
assert wc.cache_dir is None
30+
def test_wheel_name_filter(tmpdir):
31+
"""
32+
Test the wheel cache filters on wheel name when several wheels
33+
for different package are stored under the same cache directory.
34+
"""
35+
wc = WheelCache(tmpdir, FormatControl())
36+
link = Link("https://g.c/package.tar.gz")
37+
cache_path = wc.get_path_for_link(link)
38+
ensure_dir(cache_path)
39+
with open(os.path.join(cache_path, "package-1.0-py3-none-any.whl"), "w"):
40+
pass
41+
# package matches wheel name
42+
assert wc.get(link, "package", [("py3", "none", "any")]) is not link
43+
# package2 does not match wheel name
44+
assert wc.get(link, "package2", [("py3", "none", "any")]) is link

0 commit comments

Comments
 (0)