Skip to content

Commit 9573c2b

Browse files
authored
Merge pull request #7476 from sbidoul/wheel-builder-disentangle-2-sbi
Some more trivial refactorings in WheelBuilder
2 parents fedde5f + 5109709 commit 9573c2b

File tree

2 files changed

+54
-33
lines changed

2 files changed

+54
-33
lines changed

src/pip/_internal/wheel_builder.py

+42-33
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
if MYPY_CHECK_RUNNING:
3434
from typing import (
35-
Any, Callable, Iterable, List, Optional, Pattern, Text, Union,
35+
Any, Callable, Iterable, List, Optional, Pattern, Text, Tuple,
3636
)
3737

3838
from pip._internal.cache import WheelCache
@@ -263,6 +263,37 @@ def _build_wheel_pep517(
263263
return os.path.join(tempd, wheel_name)
264264

265265

266+
def _collect_buildset(
267+
requirements, # type: Iterable[InstallRequirement]
268+
wheel_cache, # type: WheelCache
269+
check_binary_allowed, # type: BinaryAllowedPredicate
270+
need_wheel, # type: bool
271+
):
272+
# type: (...) -> List[Tuple[InstallRequirement, str]]
273+
"""Return the list of InstallRequirement that need to be built,
274+
with the persistent or temporary cache directory where the built
275+
wheel needs to be stored.
276+
"""
277+
buildset = []
278+
cache_available = bool(wheel_cache.cache_dir)
279+
for req in requirements:
280+
if not should_build(
281+
req,
282+
need_wheel=need_wheel,
283+
check_binary_allowed=check_binary_allowed,
284+
):
285+
continue
286+
if (
287+
cache_available and
288+
should_cache(req, check_binary_allowed)
289+
):
290+
cache_dir = wheel_cache.get_path_for_link(req.link)
291+
else:
292+
cache_dir = wheel_cache.get_ephem_path_for_link(req.link)
293+
buildset.append((req, cache_dir))
294+
return buildset
295+
296+
266297
def _always_true(_):
267298
# type: (Any) -> bool
268299
return True
@@ -292,8 +323,6 @@ def __init__(
292323
self.build_options = build_options or []
293324
self.global_options = global_options or []
294325
self.check_binary_allowed = check_binary_allowed
295-
# file names of built wheel names
296-
self.wheel_filenames = [] # type: List[Union[bytes, Text]]
297326

298327
def _build_one(
299328
self,
@@ -389,29 +418,12 @@ def build(
389418
(not should_unpack and self._wheel_dir)
390419
)
391420

392-
buildset = []
393-
cache_available = bool(self.wheel_cache.cache_dir)
394-
395-
for req in requirements:
396-
if not should_build(
397-
req,
398-
need_wheel=not should_unpack,
399-
check_binary_allowed=self.check_binary_allowed,
400-
):
401-
continue
402-
403-
if (
404-
cache_available and
405-
should_cache(req, self.check_binary_allowed)
406-
):
407-
output_dir = self.wheel_cache.get_path_for_link(req.link)
408-
else:
409-
output_dir = self.wheel_cache.get_ephem_path_for_link(
410-
req.link
411-
)
412-
413-
buildset.append((req, output_dir))
414-
421+
buildset = _collect_buildset(
422+
requirements,
423+
wheel_cache=self.wheel_cache,
424+
check_binary_allowed=self.check_binary_allowed,
425+
need_wheel=not should_unpack,
426+
)
415427
if not buildset:
416428
return []
417429

@@ -426,9 +438,9 @@ def build(
426438

427439
with indent_log():
428440
build_success, build_failure = [], []
429-
for req, output_dir in buildset:
441+
for req, cache_dir in buildset:
430442
try:
431-
ensure_dir(output_dir)
443+
ensure_dir(cache_dir)
432444
except OSError as e:
433445
logger.warning(
434446
"Building wheel for %s failed: %s",
@@ -437,7 +449,7 @@ def build(
437449
build_failure.append(req)
438450
continue
439451

440-
wheel_file = self._build_one(req, output_dir)
452+
wheel_file = self._build_one(req, cache_dir)
441453
if wheel_file:
442454
if should_unpack:
443455
# XXX: This is mildly duplicative with prepare_files,
@@ -467,10 +479,7 @@ def build(
467479
# copy from cache to target directory
468480
try:
469481
ensure_dir(self._wheel_dir)
470-
shutil.copy(
471-
os.path.join(output_dir, wheel_file),
472-
self._wheel_dir,
473-
)
482+
shutil.copy(wheel_file, self._wheel_dir)
474483
except OSError as e:
475484
logger.warning(
476485
"Building wheel for %s failed: %s",

tests/functional/test_wheel.py

+12
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@ def test_basic_pip_wheel_downloads_wheels(script, data):
9898
assert "Saved" in result.stdout, result.stdout
9999

100100

101+
def test_pip_wheel_build_relative_cachedir(script, data):
102+
"""
103+
Test 'pip wheel' builds and caches with a non-absolute cache directory.
104+
"""
105+
result = script.pip(
106+
'wheel', '--no-index', '-f', data.find_links,
107+
'--cache-dir', './cache',
108+
'simple==3.0',
109+
)
110+
assert result.returncode == 0
111+
112+
101113
def test_pip_wheel_builds_when_no_binary_set(script, data):
102114
data.packages.joinpath('simple-3.0-py2.py3-none-any.whl').touch()
103115
# Check that the wheel package is ignored

0 commit comments

Comments
 (0)