Skip to content

Commit e860f2d

Browse files
fix pypa#11847 for sdists
1 parent 3dc5ac5 commit e860f2d

File tree

1 file changed

+17
-31
lines changed

1 file changed

+17
-31
lines changed

src/pip/_internal/operations/prepare.py

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
InstallationError,
2121
MetadataInconsistent,
2222
NetworkConnectionError,
23-
PreviousBuildDirError,
2423
VcsHashUnsupported,
2524
)
2625
from pip._internal.index.package_finder import PackageFinder
@@ -47,7 +46,6 @@
4746
display_path,
4847
hash_file,
4948
hide_url,
50-
is_installable_dir,
5149
)
5250
from pip._internal.utils.temp_dir import TempDirectory
5351
from pip._internal.utils.unpacking import unpack_file
@@ -305,7 +303,6 @@ def _ensure_link_req_src_dir(
305303
# We don't need to unpack wheels, so no need for a source
306304
# directory.
307305
return
308-
assert req.source_dir is None
309306
if req.link.is_existing_dir():
310307
# build local directories in-tree
311308
req.source_dir = req.link.file_path
@@ -318,21 +315,6 @@ def _ensure_link_req_src_dir(
318315
parallel_builds=parallel_builds,
319316
)
320317

321-
# If a checkout exists, it's unwise to keep going. version
322-
# inconsistencies are logged later, but do not fail the
323-
# installation.
324-
# FIXME: this won't upgrade when there's an existing
325-
# package unpacked in `req.source_dir`
326-
# TODO: this check is now probably dead code
327-
if is_installable_dir(req.source_dir):
328-
raise PreviousBuildDirError(
329-
"pip can't proceed with requirements '{}' due to a"
330-
"pre-existing build directory ({}). This is likely "
331-
"due to a previous installation that failed . pip is "
332-
"being responsible and not assuming it can delete this. "
333-
"Please delete it and try again.".format(req, req.source_dir)
334-
)
335-
336318
def _get_linked_req_hashes(self, req: InstallRequirement) -> Hashes:
337319
# By the time this is called, the requirement's link should have
338320
# been checked so we can tell what kind of requirements req is
@@ -479,20 +461,24 @@ def _complete_partial_requirements(
479461
for link, (filepath, _) in batch_download:
480462
logger.debug("Downloading link %s to %s", link, filepath)
481463
req = links_to_fully_download[link]
464+
# Record the downloaded file path so wheel reqs can extract a Distribution
465+
# in .get_dist().
482466
req.local_file_path = filepath
483-
# TODO: This needs fixing for sdists
484-
# This is an emergency fix for #11847, which reports that
485-
# distributions get downloaded twice when metadata is loaded
486-
# from a PEP 658 standalone metadata file. Setting _downloaded
487-
# fixes this for wheels, but breaks the sdist case (tests
488-
# test_download_metadata). As PyPI is currently only serving
489-
# metadata for wheels, this is not an immediate issue.
490-
# Fixing the problem properly looks like it will require a
491-
# complete refactoring of the `prepare_linked_requirements_more`
492-
# logic, and I haven't a clue where to start on that, so for now
493-
# I have fixed the issue *just* for wheels.
494-
if req.is_wheel:
495-
self._downloaded[req.link.url] = filepath
467+
# Record that the file is downloaded so we don't do it again in
468+
# _prepare_linked_requirement().
469+
self._downloaded[req.link.url] = filepath
470+
471+
# If this is an sdist, we need to unpack it and set the .source_dir
472+
# immediately after downloading, as _prepare_linked_requirement() assumes
473+
# the req is either not downloaded at all, or both downloaded and
474+
# unpacked. The downloading and unpacking is is typically done with
475+
# unpack_url(), but we separate the downloading and unpacking steps here in
476+
# order to use the BatchDownloader.
477+
if not req.is_wheel:
478+
hashes = self._get_linked_req_hashes(req)
479+
assert filepath == _check_download_dir(req.link, temp_dir, hashes)
480+
self._ensure_link_req_src_dir(req, parallel_builds)
481+
unpack_file(filepath, req.source_dir)
496482

497483
# This step is necessary to ensure all lazy wheels are processed
498484
# successfully by the 'download', 'wheel', and 'install' commands.

0 commit comments

Comments
 (0)