Skip to content

Commit a8395f0

Browse files
committed
Make pip wheel cache what it built
Just like pip install.
1 parent 799596e commit a8395f0

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

news/6852.feature

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Cache wheels that ``pip wheel`` built locally, matching what
2+
``pip install`` does. This particularly helps performance in workflows where
3+
``pip wheel`` is used for `building before installing
4+
<https://pip.pypa.io/en/stable/user_guide/#installing-from-local-packages>`_.

src/pip/_internal/wheel.py

+23-12
Original file line numberDiff line numberDiff line change
@@ -1124,19 +1124,15 @@ def build(
11241124
):
11251125
continue
11261126

1127-
# Determine where the wheel should go.
1128-
if should_unpack:
1129-
if (
1130-
cache_available and
1131-
should_cache(req, self.check_binary_allowed)
1132-
):
1133-
output_dir = self.wheel_cache.get_path_for_link(req.link)
1134-
else:
1135-
output_dir = self.wheel_cache.get_ephem_path_for_link(
1136-
req.link
1137-
)
1127+
if (
1128+
cache_available and
1129+
should_cache(req, self.check_binary_allowed)
1130+
):
1131+
output_dir = self.wheel_cache.get_path_for_link(req.link)
11381132
else:
1139-
output_dir = self._wheel_dir
1133+
output_dir = self.wheel_cache.get_ephem_path_for_link(
1134+
req.link
1135+
)
11401136

11411137
buildset.append((req, output_dir))
11421138

@@ -1202,6 +1198,21 @@ def build(
12021198
assert req.link.is_wheel
12031199
# extract the wheel into the dir
12041200
unpack_file(req.link.file_path, req.source_dir)
1201+
else:
1202+
# copy from cache to target durectory
1203+
try:
1204+
ensure_dir(self._wheel_dir)
1205+
shutil.copy(
1206+
os.path.join(output_dir, wheel_file),
1207+
self._wheel_dir,
1208+
)
1209+
except OSError as e:
1210+
logger.warning(
1211+
"Building wheel for %s failed: %s",
1212+
req.name, e,
1213+
)
1214+
build_failure.append(req)
1215+
continue
12051216
else:
12061217
build_failure.append(req)
12071218

tests/functional/test_wheel.py

+24
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,30 @@ def test_pip_wheel_success(script, data):
6262
assert "Successfully built simple" in result.stdout, result.stdout
6363

6464

65+
def test_pip_wheel_build_cache(script, data):
66+
"""
67+
Test 'pip wheel' builds and caches.
68+
"""
69+
result = script.pip(
70+
'wheel', '--no-index', '-f', data.find_links,
71+
'simple==3.0',
72+
)
73+
wheel_file_name = 'simple-3.0-py%s-none-any.whl' % pyversion[0]
74+
wheel_file_path = script.scratch / wheel_file_name
75+
assert wheel_file_path in result.files_created, result.stdout
76+
assert "Successfully built simple" in result.stdout, result.stdout
77+
# remove target file
78+
(script.scratch_path / wheel_file_name).unlink()
79+
# pip wheel again and test that no build occurs since
80+
# we get the wheel from cache
81+
result = script.pip(
82+
'wheel', '--no-index', '-f', data.find_links,
83+
'simple==3.0',
84+
)
85+
assert wheel_file_path in result.files_created, result.stdout
86+
assert "Successfully built simple" not in result.stdout, result.stdout
87+
88+
6589
def test_basic_pip_wheel_downloads_wheels(script, data):
6690
"""
6791
Test 'pip wheel' downloads wheels

0 commit comments

Comments
 (0)