Skip to content

Commit 09c6785

Browse files
committed
New resolver: Avoid polluting dest dir
Previously, during dependency resolution for `pip download -d <dir>` or `pip wheel -w <dir>`, distributions downloaded are always saved to <dir>, even for those are only used in backtracking and are not part of the returned requirement set.
1 parent ceaddcc commit 09c6785

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed

news/8827.bugfix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Avoid polluting the destination directory by resolution artifacts
2+
when the new resolver is used for ``pip download`` or ``pip wheel``.

src/pip/_internal/commands/download.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ def run(self, options, args):
137137
for req in requirement_set.requirements.values():
138138
if not req.editable and req.satisfied_by is None:
139139
assert req.name is not None
140+
preparer.save_linked_requirement(req)
140141
downloaded.append(req.name)
141142
if downloaded:
142143
write_output('Successfully downloaded %s', ' '.join(downloaded))

src/pip/_internal/commands/wheel.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from optparse import Values
2222
from typing import List
2323

24+
from pip._internal.req.req_install import InstallRequirement
2425

2526
logger = logging.getLogger(__name__)
2627

@@ -156,10 +157,12 @@ def run(self, options, args):
156157
reqs, check_supported_wheels=True
157158
)
158159

159-
reqs_to_build = [
160-
r for r in requirement_set.requirements.values()
161-
if should_build_for_wheel_command(r)
162-
]
160+
reqs_to_build = [] # type: List[InstallRequirement]
161+
for req in requirement_set.requirements.values():
162+
if req.is_wheel:
163+
preparer.save_linked_requirement(req, only_wheels=True)
164+
elif should_build_for_wheel_command(req):
165+
reqs_to_build.append(req)
163166

164167
# build wheels
165168
build_successes, build_failures = build(

src/pip/_internal/operations/prepare.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -515,24 +515,34 @@ def _prepare_linked_requirement(self, req, parallel_builds):
515515
dist = _get_prepared_distribution(
516516
req, self.req_tracker, self.finder, self.build_isolation,
517517
)
518-
519-
if self.download_dir is not None:
520-
if link.is_existing_dir():
521-
logger.info('Link is a directory, ignoring download_dir')
522-
elif local_file:
523-
download_location = os.path.join(
524-
self.download_dir, link.filename
525-
)
526-
if not os.path.exists(download_location):
527-
shutil.copy(local_file.path, download_location)
528-
download_path = display_path(download_location)
529-
logger.info('Saved %s', download_path)
530-
531-
if link.is_vcs:
532-
# Make a .zip of the source_dir we already created.
533-
req.archive(self.download_dir)
534518
return dist
535519

520+
def save_linked_requirement(self, req, only_wheels=False):
521+
# type: (InstallRequirement, bool) -> None
522+
assert self.download_dir is not None
523+
assert req.link
524+
link = req.link
525+
if link.is_vcs and not only_wheels:
526+
# Make a .zip of the source_dir we already created.
527+
req.archive(self.download_dir)
528+
return
529+
530+
if link.is_existing_dir():
531+
logger.debug(
532+
'Not copying link to destination directory '
533+
'since it is a directory: %s', link,
534+
)
535+
return
536+
if req.local_file_path is None:
537+
# No distribution was downloaded for this requirement.
538+
return
539+
540+
download_location = os.path.join(self.download_dir, link.filename)
541+
if not os.path.exists(download_location):
542+
shutil.copy(req.local_file_path, download_location)
543+
download_path = display_path(download_location)
544+
logger.info('Saved %s', download_path)
545+
536546
def prepare_editable_requirement(
537547
self,
538548
req, # type: InstallRequirement

0 commit comments

Comments
 (0)