77import mimetypes
88import os
99import shutil
10- from typing import Dict , Iterable , List , Optional , Set
10+ from pathlib import Path
11+ from typing import Dict , Iterable , List , Optional
1112
1213from pip ._vendor .packaging .utils import canonicalize_name
1314
2021 InstallationError ,
2122 MetadataInconsistent ,
2223 NetworkConnectionError ,
23- PreviousBuildDirError ,
2424 VcsHashUnsupported ,
2525)
2626from pip ._internal .index .package_finder import PackageFinder
4747 display_path ,
4848 hash_file ,
4949 hide_url ,
50- is_installable_dir ,
5150)
5251from pip ._internal .utils .temp_dir import TempDirectory
5352from pip ._internal .utils .unpacking import unpack_file
@@ -319,21 +318,7 @@ def _ensure_link_req_src_dir(
319318 autodelete = True ,
320319 parallel_builds = parallel_builds ,
321320 )
322-
323- # If a checkout exists, it's unwise to keep going. version
324- # inconsistencies are logged later, but do not fail the
325- # installation.
326- # FIXME: this won't upgrade when there's an existing
327- # package unpacked in `req.source_dir`
328- # TODO: this check is now probably dead code
329- if is_installable_dir (req .source_dir ):
330- raise PreviousBuildDirError (
331- "pip can't proceed with requirements '{}' due to a"
332- "pre-existing build directory ({}). This is likely "
333- "due to a previous installation that failed . pip is "
334- "being responsible and not assuming it can delete this. "
335- "Please delete it and try again." .format (req , req .source_dir )
336- )
321+ req .ensure_pristine_source_checkout ()
337322
338323 def _get_linked_req_hashes (self , req : InstallRequirement ) -> Hashes :
339324 # By the time this is called, the requirement's link should have
@@ -474,8 +459,6 @@ def _complete_partial_requirements(
474459 assert req .link
475460 links_to_fully_download [req .link ] = req
476461
477- reqs_with_newly_unpacked_source_dirs : Set [Link ] = set ()
478-
479462 batch_download = self ._batch_download (
480463 links_to_fully_download .keys (),
481464 temp_dir ,
@@ -490,28 +473,17 @@ def _complete_partial_requirements(
490473 # _prepare_linked_requirement().
491474 self ._downloaded [req .link .url ] = filepath
492475
493- # If this is an sdist, we need to unpack it and set the .source_dir
494- # immediately after downloading, as _prepare_linked_requirement() assumes
495- # the req is either not downloaded at all, or both downloaded and
496- # unpacked. The downloading and unpacking is is typically done with
497- # unpack_url(), but we separate the downloading and unpacking steps here in
498- # order to use the BatchDownloader.
476+ # If this is an sdist, we need to unpack it after downloading, but the
477+ # .source_dir won't be set up until we are in _prepare_linked_requirement().
478+ # Add the downloaded archive to the install requirement to unpack after
479+ # preparing the source dir.
499480 if not req .is_wheel :
500- hashes = self ._get_linked_req_hashes (req )
501- assert filepath == _check_download_dir (req .link , temp_dir , hashes )
502- self ._ensure_link_req_src_dir (req , parallel_builds )
503- unpack_file (filepath , req .source_dir )
504- reqs_with_newly_unpacked_source_dirs .add (req .link )
481+ req .needs_unpacked_archive (Path (filepath ))
505482
506483 # This step is necessary to ensure all lazy wheels are processed
507484 # successfully by the 'download', 'wheel', and 'install' commands.
508485 for req in partially_downloaded_reqs :
509- self ._prepare_linked_requirement (
510- req ,
511- parallel_builds ,
512- source_dir_exists_already = req .link
513- in reqs_with_newly_unpacked_source_dirs ,
514- )
486+ self ._prepare_linked_requirement (req , parallel_builds )
515487
516488 def prepare_linked_requirement (
517489 self , req : InstallRequirement , parallel_builds : bool = False
@@ -582,10 +554,7 @@ def prepare_linked_requirements_more(
582554 )
583555
584556 def _prepare_linked_requirement (
585- self ,
586- req : InstallRequirement ,
587- parallel_builds : bool ,
588- source_dir_exists_already : bool = False ,
557+ self , req : InstallRequirement , parallel_builds : bool
589558 ) -> BaseDistribution :
590559 assert req .link
591560 link = req .link
@@ -617,8 +586,7 @@ def _prepare_linked_requirement(
617586 req .link = req .cached_wheel_source_link
618587 link = req .link
619588
620- if not source_dir_exists_already :
621- self ._ensure_link_req_src_dir (req , parallel_builds )
589+ self ._ensure_link_req_src_dir (req , parallel_builds )
622590
623591 if link .is_existing_dir ():
624592 local_file = None
0 commit comments