|
5 | 5 | import json
|
6 | 6 | import logging
|
7 | 7 | import os
|
| 8 | +from pathlib import Path |
8 | 9 | from typing import Any, Dict, List, Optional, Set
|
9 | 10 |
|
10 | 11 | from pip._vendor.packaging.tags import Tag, interpreter_name, interpreter_version
|
11 | 12 | from pip._vendor.packaging.utils import canonicalize_name
|
12 | 13 |
|
13 | 14 | from pip._internal.exceptions import InvalidWheelFilename
|
| 15 | +from pip._internal.models.direct_url import DirectUrl |
14 | 16 | from pip._internal.models.format_control import FormatControl
|
15 | 17 | from pip._internal.models.link import Link
|
16 | 18 | from pip._internal.models.wheel import Wheel
|
|
19 | 21 |
|
20 | 22 | logger = logging.getLogger(__name__)
|
21 | 23 |
|
| 24 | +ORIGIN_JSON_NAME = "origin.json" |
| 25 | + |
22 | 26 |
|
23 | 27 | def _hash_dict(d: Dict[str, str]) -> str:
|
24 | 28 | """Return a stable sha224 of a dictionary."""
|
@@ -204,6 +208,10 @@ def __init__(
|
204 | 208 | ):
|
205 | 209 | self.link = link
|
206 | 210 | self.persistent = persistent
|
| 211 | + self.origin: Optional[DirectUrl] = None |
| 212 | + origin_direct_url_path = Path(self.link.file_path).parent / ORIGIN_JSON_NAME |
| 213 | + if origin_direct_url_path.exists(): |
| 214 | + self.origin = DirectUrl.from_json(origin_direct_url_path.read_text()) |
207 | 215 |
|
208 | 216 |
|
209 | 217 | class WheelCache(Cache):
|
@@ -262,3 +270,20 @@ def get_cache_entry(
|
262 | 270 | return CacheEntry(retval, persistent=False)
|
263 | 271 |
|
264 | 272 | return None
|
| 273 | + |
| 274 | + @staticmethod |
| 275 | + def record_download_origin(cache_dir: str, download_info: DirectUrl) -> None: |
| 276 | + origin_path = Path(cache_dir) / ORIGIN_JSON_NAME |
| 277 | + if origin_path.is_file(): |
| 278 | + origin = DirectUrl.from_json(origin_path.read_text()) |
| 279 | + # TODO: use DirectUrl.equivalent when https://github.com/pypa/pip/pull/10564 |
| 280 | + # is merged. |
| 281 | + if origin.url != download_info.url: |
| 282 | + logger.warning( |
| 283 | + "Origin URL %s in cache entry %s does not match download URL %s. " |
| 284 | + "This is likely a pip bug or a cache corruption issue.", |
| 285 | + origin.url, |
| 286 | + cache_dir, |
| 287 | + download_info.url, |
| 288 | + ) |
| 289 | + origin_path.write_text(download_info.to_json()) |
0 commit comments