Skip to content

Commit

Permalink
New resolver: Avoid polluting dest dir
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
McSinyx committed Sep 4, 2020
1 parent 822d42b commit 4c4287a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
2 changes: 2 additions & 0 deletions news/8827.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Avoid polluting the destination directory by resolution artifacts
when the new resolver is used for ``pip download`` or ``pip wheel``.
1 change: 1 addition & 0 deletions src/pip/_internal/commands/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def run(self, options, args):

downloaded = [] # type: List[str]
for req in requirement_set.requirements.values():
preparer.save_linked_requirement(req)
if not req.editable and req.satisfied_by is None:
assert req.name is not None
downloaded.append(req.name)
Expand Down
12 changes: 8 additions & 4 deletions src/pip/_internal/commands/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from optparse import Values
from typing import List

from pip._internal.req.req_install import InstallRequirement


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -156,10 +158,12 @@ def run(self, options, args):
reqs, check_supported_wheels=True
)

reqs_to_build = [
r for r in requirement_set.requirements.values()
if should_build_for_wheel_command(r)
]
reqs_to_build = [] # type: List[InstallRequirement]
for req in requirement_set.requirements.values():
if req.is_wheel:
preparer.save_linked_requirement(req)
elif should_build_for_wheel_command(req):
reqs_to_build.append(req)

# build wheels
build_successes, build_failures = build(
Expand Down
39 changes: 22 additions & 17 deletions src/pip/_internal/operations/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ def _prepare_linked_requirement(self, req, parallel_builds):
# type: (InstallRequirement, bool) -> Distribution
assert req.link
link = req.link
download_dir = self._get_download_dir(link)
req.download_dir = download_dir = self._get_download_dir(link)

with indent_log():
self._ensure_link_req_src_dir(req, download_dir, parallel_builds)
Expand Down Expand Up @@ -548,24 +548,29 @@ def _prepare_linked_requirement(self, req, parallel_builds):
req, self.req_tracker, self.finder, self.build_isolation,
)

if download_dir:
if link.is_existing_dir():
logger.info('Link is a directory, ignoring download_dir')
elif local_file:
download_location = os.path.join(
download_dir, link.filename
)
if not os.path.exists(download_location):
shutil.copy(local_file.path, download_location)
download_path = display_path(download_location)
logger.info('Saved %s', download_path)

if self._download_should_save:
# Make a .zip of the source_dir we already created.
if link.is_vcs:
req.archive(self.download_dir)
return dist

def save_linked_requirement(self, req):
# type: (InstallRequirement) -> None
assert req.link
link = req.link
if self._download_should_save and link.is_vcs:
# Make a .zip of the source_dir we already created.
req.archive(self.download_dir)

assert req.download_dir is not None
if link.is_existing_dir():
logger.debug(
'Not copying link to destination directory '
'since it is a directory: %s', link,
)
elif req.local_file_path is not None:
download_location = os.path.join(req.download_dir, link.filename)
if not os.path.exists(download_location):
shutil.copy(req.local_file_path, download_location)
download_path = display_path(download_location)
logger.info('Saved %s', download_path)

def prepare_editable_requirement(
self,
req, # type: InstallRequirement
Expand Down
2 changes: 2 additions & 0 deletions src/pip/_internal/req/req_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ def __init__(
self.local_file_path = None # type: Optional[str]
if self.link and self.link.is_file:
self.local_file_path = self.link.file_path
# Destination for pip download/wheel
self.download_dir = None # type: Optional[str]

if extras:
self.extras = extras
Expand Down

0 comments on commit 4c4287a

Please sign in to comment.