Skip to content

Commit

Permalink
do not cache metadata lookups for git or file links
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmicexplorer committed Sep 3, 2023
1 parent 0f20f62 commit 55f185a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/pip/_internal/network/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def _get_http_response_filename(resp: Response, link: Link) -> str:


def _http_get_download(session: PipSession, link: Link) -> Response:
target_url = link.url.split("#", 1)[0]
target_url = link.url_without_fragment
resp = session.get(target_url, headers=HEADERS, stream=True)
raise_for_status(resp)
return resp
Expand Down
34 changes: 17 additions & 17 deletions src/pip/_internal/operations/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.startswith("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)
Expand All @@ -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)
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 55f185a

Please sign in to comment.