From e6e29f20a9c9c992e0740a9abf216d05fcbecdb7 Mon Sep 17 00:00:00 2001 From: Danny McClanahan <1305167+cosmicexplorer@users.noreply.github.com> Date: Sun, 3 Sep 2023 09:12:36 -0400 Subject: [PATCH] do not cache metadata lookups for git or file links --- src/pip/_internal/operations/prepare.py | 34 ++++++++++++------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/pip/_internal/operations/prepare.py b/src/pip/_internal/operations/prepare.py index dbb51f8b454..b56699af34f 100644 --- a/src/pip/_internal/operations/prepare.py +++ b/src/pip/_internal/operations/prepare.py @@ -14,7 +14,6 @@ from typing import Any, Dict, Iterable, List, Optional from pip._vendor.packaging.utils import canonicalize_name -from pip._vendor.requests.exceptions import InvalidSchema from pip._internal.cache import LinkMetadataCache from pip._internal.distributions import make_distribution_for_install_requirement @@ -428,11 +427,22 @@ def _fetch_metadata_only( self._cache_metadata(req.link, computed_metadata) return computed_metadata - def _fetch_cached_metadata( - self, - link: Link, - ) -> Optional[BaseDistribution]: + def _should_cache_metadata(self, link: Link) -> bool: if self._metadata_cache is None: + return False + + # Some types of links may host changing content we don't want to cache. + if link.scheme == "git": + logger.debug("not attempting to cache metadata from git url %s", link) + return False + if link.scheme == "file": + logger.debug("not attempting to cache metadata from local file %s", link) + return False + + return True + + def _fetch_cached_metadata(self, link: Link) -> Optional[BaseDistribution]: + if not self._should_cache_metadata(link): return None try: cached_path = self._metadata_cache.cache_path(link) @@ -459,7 +469,7 @@ def _cache_metadata( link: Link, metadata_dist: BaseDistribution, ) -> None: - if self._metadata_cache is None: + if not self._should_cache_metadata(link): return try: cached_path = self._metadata_cache.cache_path(link) @@ -564,17 +574,7 @@ def _complete_partial_requirements( links_to_fully_download: Dict[Link, InstallRequirement] = {} for req in partially_downloaded_reqs: assert req.link - # If this is e.g. a git url, we don't know how to handle that in the - # BatchDownloader, so leave it for self._prepare_linked_requirement() at the - # end of this method, which knows how to handle any URL. - can_simply_download = True - try: - # This will raise InvalidSchema if our Session can't download it. - self._session.get_adapter(req.link.url) - except InvalidSchema: - can_simply_download = False - if can_simply_download: - links_to_fully_download[req.link] = req + links_to_fully_download[req.link] = req batch_download = self._batch_download( links_to_fully_download.keys(),