Skip to content

Commit

Permalink
hash from link
Browse files Browse the repository at this point in the history
  • Loading branch information
wimglenn committed Oct 24, 2024
1 parent aa2d7a4 commit ea3108b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
36 changes: 26 additions & 10 deletions johnnydep/lib.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import hashlib
import io
import json
import os
import re
import subprocess
import sys
Expand Down Expand Up @@ -187,9 +186,7 @@ def license(self):

@cached_property
def versions_available(self):
finder = _get_package_finder()
matches = finder.find_matches(self.project_name, allow_prereleases=True)
versions = [p.version for p in matches][::-1]
versions = _get_versions(self.project_name)
if self._local_path is not None:
raw_version = self._local_path.name.split("-")[1]
local_version = canonicalize_version(raw_version)
Expand Down Expand Up @@ -263,17 +260,22 @@ def pinned(self):
def download_link(self):
if self._local_path is not None:
return f"file://{self._local_path}"
package_finder = _get_package_finder()
return package_finder.find_best_match(self.req, allow_prereleases=True).best.link.url
best = _get_link(self.req)
if best is not None:
return best.link.url

@property
def checksum(self):
if self._local_path is not None:
return "md5=" + hashlib.md5(self._local_path.read_bytes()).hexdigest()
link = self.download_link
best = _get_link(self.req)
if best.link.hashes:
for hash in "md5", "sha256":
if hash in best.link.hashes:
return f"{hash}={best.link.hashes[hash]}"

Check warning on line 275 in johnnydep/lib.py

View check run for this annotation

Codecov / codecov/patch

johnnydep/lib.py#L275

Added line #L275 was not covered by tests
f = io.BytesIO()
download_dist(
url=link,
url=best.link.url,
f=f,
index_url=config.index_url,
extra_index_url=config.extra_index_url,
Expand Down Expand Up @@ -577,14 +579,28 @@ def _get_package_finder():
return package_finder


@lru_cache(maxsize=None)
def _get_versions(req):
finder = _get_package_finder()
matches = finder.find_matches(req, allow_prereleases=True)
versions = [p.version for p in matches][::-1]
return versions


@lru_cache(maxsize=None)
def _get_link(req):
package_finder = _get_package_finder()
best = package_finder.find_best_match(req, allow_prereleases=True).best
return best


@lru_cache(maxsize=None)
def _get_info(dist_name):
log = logger.bind(dist_name=dist_name)
tmpdir = mkdtemp()
log.debug("created scratch", tmpdir=tmpdir)
try:
package_finder = _get_package_finder()
best = package_finder.find_best_match(dist_name, allow_prereleases=True).best
best = _get_link(dist_name)
if best is None:
raise JohnnyError(f"Package not found {dist_name!r}")
dist_path = Path(tmpdir) / best.link.filename
Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
@pytest.fixture(autouse=True)
def expire_caches():
lib._get_info.cache_clear()
lib._get_link.cache_clear()
lib._get_versions.cache_clear()


@pytest.fixture(autouse=True)
Expand Down

0 comments on commit ea3108b

Please sign in to comment.