1"""Prepares a distribution for installation
11from typing
import Dict, Iterable, List, Optional
18 DirectoryUrlHashUnsupported,
23 NetworkConnectionError,
24 PreviousBuildDirError,
34 HTTPRangeRequestUnsupported,
41 direct_url_for_editable,
60 req: InstallRequirement,
61 build_tracker: BuildTracker,
62 finder: PackageFinder,
63 build_isolation: bool,
64 check_build_deps: bool,
66 """Prepare a distribution for installation."""
67 abstract_dist = make_distribution_for_install_requirement(req)
70 finder, build_isolation, check_build_deps
77 assert vcs_backend
is not None
82 def __init__(self, path: str, content_type: Optional[str]) ->
None:
84 if content_type
is None:
93 download_dir: Optional[str] =
None,
94 hashes: Optional[Hashes] =
None,
96 temp_dir =
TempDirectory(kind=
"unpack", globally_managed=
True)
98 already_downloaded_path =
None
102 if already_downloaded_path:
103 from_path = already_downloaded_path
111 return File(from_path, content_type)
115 link: Link, download_dir: Optional[str] =
None, hashes: Optional[Hashes] =
None
117 """Get file and optionally check its hash."""
119 already_downloaded_path =
None
123 if already_downloaded_path:
124 from_path = already_downloaded_path
135 return File(from_path,
None)
141 download: Downloader,
143 download_dir: Optional[str] =
None,
144 hashes: Optional[Hashes] =
None,
146 """Unpack link into location, downloading if required.
148 :param hashes: A Hashes object, one of whose embedded hashes must match,
149 or HashMismatch will be raised. If the Hashes is empty, no matches are
150 required, and unhashable types of requirements (like VCS ones, which
151 would ordinarily raise HashUnsupported) are allowed.
184 hashes: Optional[Hashes],
185 warn_on_hash_mismatch: bool =
True,
187 """Check download_dir for previously downloaded file with correct hash
188 If a correct file is found return its path else None
196 logger.info(
"File was already downloaded %s", download_path)
201 if warn_on_hash_mismatch:
203 "Previously-downloaded file %s has bad hash. Re-downloading.",
212 """Prepares a Requirement"""
217 download_dir: Optional[str],
219 build_isolation: bool,
220 check_build_deps: bool,
221 build_tracker: BuildTracker,
224 finder: PackageFinder,
225 require_hashes: bool,
229 legacy_resolver: bool,
267 self._downloaded: Dict[str, str] = {}
273 """Provide context for the requirement being prepared."""
275 message =
"Processing %s"
278 message =
"Collecting %s"
279 information = str(
req.req or req)
289 information += f
" (from {comes_from})"
300 self, req: InstallRequirement, parallel_builds: bool
302 """Ensure source_dir of a linked InstallRequirement."""
318 parallel_builds=parallel_builds,
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. "
370 req: InstallRequirement,
371 ) -> Optional[BaseDistribution]:
374 "Metadata-only fetching is not used in the legacy resolver",
379 "Metadata-only fetching is not used as hash checking is required",
389 req: InstallRequirement,
390 ) -> Optional[BaseDistribution]:
391 """Fetch metadata from the data-dist-info-metadata attribute, if possible."""
394 if metadata_link
is None:
398 "Obtaining dependency information for %s from %s",
409 metadata_contents =
f.read()
411 metadata_dist = get_metadata_distribution(
431 ) -> Optional[BaseDistribution]:
432 """Fetch metadata using lazy wheel, if possible."""
438 "Lazy wheel is not used as %r does not point to a remote wheel",
446 "Obtaining dependency information from %s %s",
452 return dist_from_wheel_url(name, url, self.
_session)
453 except HTTPRangeRequestUnsupported:
459 partially_downloaded_reqs: Iterable[InstallRequirement],
460 parallel_builds: bool =
False,
462 """Download any requirements which were only fetched by metadata."""
465 temp_dir =
TempDirectory(kind=
"unpack", globally_managed=
True).path
470 links_to_fully_download: Dict[Link, InstallRequirement] = {}
471 for req
in partially_downloaded_reqs:
473 links_to_fully_download[
req.link] = req
479 for link, (filepath, _)
in batch_download:
480 logger.debug(
"Downloading link %s to %s", link, filepath)
481 req = links_to_fully_download[link]
499 for req
in partially_downloaded_reqs:
503 self, req: InstallRequirement, parallel_builds: bool =
False
504 ) -> BaseDistribution:
505 """Prepare a requirement to be obtained from req.link."""
527 if file_path
is not None:
533 if metadata_dist
is not None:
541 self, reqs: Iterable[InstallRequirement], parallel_builds: bool =
False
543 """Prepare linked requirements more, if needed."""
550 if file_path
is not None:
556 partially_downloaded_reqs: List[InstallRequirement] = []
566 partially_downloaded_reqs,
567 parallel_builds=parallel_builds,
571 self, req: InstallRequirement, parallel_builds: bool
572 ) -> BaseDistribution:
596 "The hashes of the source archive found in cache entry "
597 "don't match, ignoring cached built wheel "
598 "and re-downloading source."
607 elif link.url not in self._downloaded:
617 except NetworkConnectionError
as exc:
619 "Could not install requirement {} because of HTTP "
620 "error {} for URL {}".format(req, exc, link)
623 file_path = self._downloaded[
link.url]
626 local_file =
File(file_path, content_type=
None)
673 "Not copying link to destination directory "
674 "since it is a directory: %s",
685 download_path = display_path(download_location)
690 req: InstallRequirement,
691 ) -> BaseDistribution:
692 """Prepare an editable requirement."""
693 assert req.editable,
"cannot prepare a non-editable req as editable"
700 "The editable requirement {} cannot be installed when "
701 "requiring hashes, because there is no single file to "
723 req: InstallRequirement,
725 ) -> BaseDistribution:
726 """Prepare an already-installed requirement."""
728 assert skip_reason
is not None, (
729 "did not get skip reason skipped but req.satisfied_by "
738 "Since it is already installed, we are trusting this "
739 "package without checking its hash. To ensure a "
740 "completely repeatable environment, install into an "
None __init__(self, str path, Optional[str] content_type)
BaseDistribution prepare_installed_requirement(self, InstallRequirement req, str skip_reason)
None _log_preparing_link(self, InstallRequirement req)
Optional[BaseDistribution] _fetch_metadata_using_link_data_attr(self, InstallRequirement req)
_previous_requirement_header
BaseDistribution prepare_editable_requirement(self, InstallRequirement req)
BaseDistribution _prepare_linked_requirement(self, InstallRequirement req, bool parallel_builds)
Optional[BaseDistribution] _fetch_metadata_using_lazy_wheel(self, Link link)
None _ensure_link_req_src_dir(self, InstallRequirement req, bool parallel_builds)
None prepare_linked_requirements_more(self, Iterable[InstallRequirement] reqs, bool parallel_builds=False)
Hashes _get_linked_req_hashes(self, InstallRequirement req)
None __init__(self, str build_dir, Optional[str] download_dir, str src_dir, bool build_isolation, bool check_build_deps, BuildTracker build_tracker, PipSession session, str progress_bar, PackageFinder finder, bool require_hashes, bool use_user_site, bool lazy_wheel, int verbosity, bool legacy_resolver)
None save_linked_requirement(self, InstallRequirement req)
Optional[BaseDistribution] _fetch_metadata_only(self, InstallRequirement req)
BaseDistribution prepare_linked_requirement(self, InstallRequirement req, bool parallel_builds=False)
None _complete_partial_requirements(self, Iterable[InstallRequirement] partially_downloaded_reqs, bool parallel_builds=False)
File get_http_url(Link link, Downloader download, Optional[str] download_dir=None, Optional[Hashes] hashes=None)
Optional[File] unpack_url(Link link, str location, Downloader download, int verbosity, Optional[str] download_dir=None, Optional[Hashes] hashes=None)
File get_file_url(Link link, Optional[str] download_dir=None, Optional[Hashes] hashes=None)
BaseDistribution _get_prepared_distribution(InstallRequirement req, BuildTracker build_tracker, PackageFinder finder, bool build_isolation, bool check_build_deps)
Optional[str] _check_download_dir(Link link, str download_dir, Optional[Hashes] hashes, bool warn_on_hash_mismatch=True)
None unpack_vcs_link(Link link, str location, int verbosity)