Skip to content

Commit

Permalink
refactor: unify cache or download logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ralbertazzi committed May 15, 2023
1 parent 81b5dce commit 9d68507
Showing 1 changed file with 17 additions and 34 deletions.
51 changes: 17 additions & 34 deletions src/poetry/repositories/http_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import functools
import hashlib
import os
import urllib
import urllib.parse

from collections import defaultdict
from contextlib import contextmanager
from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
from typing import Iterator

import requests

Expand Down Expand Up @@ -75,39 +74,28 @@ def authenticated_url(self) -> str:
def _download(self, url: str, dest: Path) -> None:
return download_file(url, dest, session=self.session)

def _get_info_from_wheel(self, url: str) -> PackageInfo:
from poetry.inspection.info import PackageInfo

wheel_name = urllib.parse.urlparse(url).path.rsplit("/")[-1]

filepath = self._authenticator.get_cached_file_for_url(url)
@contextmanager
def _cached_or_downloaded_file(self, link: Link) -> Iterator[Path]:
filepath = self._authenticator.get_cached_file_for_url(link.url)
if filepath:
return PackageInfo.from_wheel(filepath)
yield filepath
else:
self._log(f"Downloading: {link.url}", level="debug")
with temporary_directory() as temp_dir:
filepath = Path(temp_dir) / link.filename
self._download(link.url, filepath)
yield filepath

self._log(f"Downloading wheel: {wheel_name}", level="debug")
filename = os.path.basename(wheel_name)
with temporary_directory() as temp_dir:
filepath = Path(temp_dir) / filename
self._download(url, filepath)
def _get_info_from_wheel(self, url: str) -> PackageInfo:
from poetry.inspection.info import PackageInfo

with self._cached_or_downloaded_file(Link(url)) as filepath:
return PackageInfo.from_wheel(filepath)

def _get_info_from_sdist(self, url: str) -> PackageInfo:
from poetry.inspection.info import PackageInfo

sdist_name = urllib.parse.urlparse(url).path
sdist_name_log = sdist_name.rsplit("/")[-1]

filepath = self._authenticator.get_cached_file_for_url(url)
if filepath:
return PackageInfo.from_wheel(filepath)

self._log(f"Downloading sdist: {sdist_name_log}", level="debug")
filename = os.path.basename(sdist_name)
with temporary_directory() as temp_dir:
filepath = Path(temp_dir) / filename
self._download(url, filepath)

with self._cached_or_downloaded_file(Link(url)) as filepath:
return PackageInfo.from_sdist(filepath)

def _get_info_from_urls(self, urls: dict[str, list[str]]) -> PackageInfo:
Expand Down Expand Up @@ -242,12 +230,7 @@ def _links_to_data(self, links: list[Link], data: PackageInfo) -> dict[str, Any]
and link.hash_name not in ("sha256", "sha384", "sha512")
and hasattr(hashlib, link.hash_name)
):
with temporary_directory() as temp_dir:
filepath = self._authenticator.get_cached_file_for_url(link.url)
if not filepath:
filepath = Path(temp_dir) / link.filename
self._download(link.url, filepath)

with self._cached_or_downloaded_file(link) as filepath:
known_hash = (
getattr(hashlib, link.hash_name)() if link.hash_name else None
)
Expand Down

0 comments on commit 9d68507

Please sign in to comment.