Skip to content

Commit 0e2a0db

Browse files
committed
Improve pip wheel wrt hash checking of cached built wheels
1 parent 40cd79d commit 0e2a0db

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

src/pip/_internal/operations/prepare.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,10 @@ def unpack_url(
179179

180180

181181
def _check_download_dir(
182-
link: Link, download_dir: str, hashes: Optional[Hashes]
182+
link: Link,
183+
download_dir: str,
184+
hashes: Optional[Hashes],
185+
warn_on_hash_mismatch: bool = True,
183186
) -> Optional[str]:
184187
"""Check download_dir for previously downloaded file with correct hash
185188
If a correct file is found return its path else None
@@ -195,10 +198,11 @@ def _check_download_dir(
195198
try:
196199
hashes.check_against_path(download_path)
197200
except HashMismatch:
198-
logger.warning(
199-
"Previously-downloaded file %s has bad hash. Re-downloading.",
200-
download_path,
201-
)
201+
if warn_on_hash_mismatch:
202+
logger.warning(
203+
"Previously-downloaded file %s has bad hash. Re-downloading.",
204+
download_path,
205+
)
202206
os.unlink(download_path)
203207
return None
204208
return download_path
@@ -485,7 +489,18 @@ def prepare_linked_requirement(
485489
file_path = None
486490
if self.download_dir is not None and req.link.is_wheel:
487491
hashes = self._get_linked_req_hashes(req)
488-
file_path = _check_download_dir(req.link, self.download_dir, hashes)
492+
file_path = _check_download_dir(
493+
req.link,
494+
self.download_dir,
495+
hashes,
496+
# When a locally built wheel has been found in cache, we don't warn
497+
# about re-downloading when the already downloaded wheel hash does
498+
# not match. This is because the hash must be checked against the
499+
# original link, not the cached link. It that case the already
500+
# downloaded file will be removed and re-fetched from cache (which
501+
# implies a hash check against the cache entry's origin.json).
502+
warn_on_hash_mismatch=not req.original_link_is_in_wheel_cache,
503+
)
489504

490505
if file_path is not None:
491506
# The file is already available, so mark it as downloaded

0 commit comments

Comments
 (0)