diff --git a/src/poetry/installation/chooser.py b/src/poetry/installation/chooser.py index b484504ed9a..4601189d16d 100644 --- a/src/poetry/installation/chooser.py +++ b/src/poetry/installation/chooser.py @@ -8,6 +8,7 @@ from poetry.config.config import Config from poetry.config.config import PackageFilterPolicy +from poetry.repositories.http_repository import HTTPRepository from poetry.utils.wheel import Wheel @@ -103,6 +104,12 @@ def _get_links(self, package: Package) -> list[Link]: assert link.hash_name is not None h = link.hash_name + ":" + link.hash + if ( + h not in hashes + and link.hash_name not in ("sha256", "sha384", "sha512") + and isinstance(repository, HTTPRepository) + ): + h = repository.calculate_sha256(link) or h if h not in hashes: logger.debug( "Skipping %s as %s checksum does not match expected value", diff --git a/src/poetry/repositories/http_repository.py b/src/poetry/repositories/http_repository.py index 9427d72bfe7..ebf8b1a4d77 100644 --- a/src/poetry/repositories/http_repository.py +++ b/src/poetry/repositories/http_repository.py @@ -226,24 +226,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 self._cached_or_downloaded_file(link) as filepath: - known_hash = ( - getattr(hashlib, link.hash_name)() if link.hash_name else None - ) - required_hash = hashlib.sha256() - - chunksize = 4096 - with filepath.open("rb") as f: - while True: - chunk = f.read(chunksize) - if not chunk: - break - if known_hash: - known_hash.update(chunk) - required_hash.update(chunk) - - if not known_hash or known_hash.hexdigest() == link.hash: - file_hash = f"{required_hash.name}:{required_hash.hexdigest()}" + file_hash = self.calculate_sha256(link) or file_hash files.append({"file": link.filename, "hash": file_hash}) @@ -257,6 +240,25 @@ def _links_to_data(self, links: list[Link], data: PackageInfo) -> dict[str, Any] return data.asdict() + def calculate_sha256(self, link: Link) -> str | None: + with self._cached_or_downloaded_file(link) as filepath: + known_hash = getattr(hashlib, link.hash_name)() if link.hash_name else None + required_hash = hashlib.sha256() + + chunksize = 4096 + with filepath.open("rb") as f: + while True: + chunk = f.read(chunksize) + if not chunk: + break + if known_hash: + known_hash.update(chunk) + required_hash.update(chunk) + + if not known_hash or known_hash.hexdigest() == link.hash: + return f"{required_hash.name}:{required_hash.hexdigest()}" + return None + def _get_response(self, endpoint: str) -> requests.Response | None: url = self._url + endpoint try: