|
1 | 1 | import json
|
2 | 2 | import logging
|
3 | 3 | import os
|
| 4 | +from dataclasses import dataclass, field |
4 | 5 | from optparse import Values
|
5 |
| -from typing import Dict, List |
| 6 | +from typing import Any, Dict, List, Optional, Union |
| 7 | + |
| 8 | +from pip._vendor.packaging.requirements import Requirement |
6 | 9 |
|
7 | 10 | from pip._internal.cli import cmdoptions
|
8 | 11 | from pip._internal.cli.cmdoptions import make_target_python
|
9 | 12 | from pip._internal.cli.req_command import RequirementCommand, with_cleanup
|
10 | 13 | from pip._internal.cli.status_codes import SUCCESS
|
| 14 | +from pip._internal.models.link import Link |
11 | 15 | from pip._internal.req.req_tracker import get_requirement_tracker
|
12 | 16 | from pip._internal.utils.misc import ensure_dir, normalize_path, write_output
|
13 | 17 | from pip._internal.utils.temp_dir import TempDirectory
|
14 | 18 |
|
15 | 19 | logger = logging.getLogger(__name__)
|
16 | 20 |
|
17 | 21 |
|
| 22 | +@dataclass |
| 23 | +class RequirementHash: |
| 24 | + hash_name: str |
| 25 | + hash_value: str |
| 26 | + |
| 27 | + @classmethod |
| 28 | + def from_dist_info_metadata( |
| 29 | + cls, |
| 30 | + dist_info_metadata: Optional[str], |
| 31 | + ) -> Optional["RequirementHash"]: |
| 32 | + if dist_info_metadata is None: |
| 33 | + return None |
| 34 | + if dist_info_metadata == "true": |
| 35 | + return None |
| 36 | + # FIXME: don't use private `_hash_re`! |
| 37 | + hash_match = Link._hash_re.match(dist_info_metadata) |
| 38 | + if hash_match is None: |
| 39 | + return None |
| 40 | + hash_name, hash_value = hash_match.groups() |
| 41 | + return cls(hash_name=hash_name, hash_value=hash_value) |
| 42 | + |
| 43 | + @classmethod |
| 44 | + def from_link(cls, link: Link) -> Optional["RequirementHash"]: |
| 45 | + if not link.is_hash_allowed: |
| 46 | + return None |
| 47 | + hash_name = link.hash_name |
| 48 | + hash_value = link.hash |
| 49 | + assert hash_name is not None |
| 50 | + assert hash_value is not None |
| 51 | + return cls(hash_name=hash_name, hash_value=hash_value) |
| 52 | + |
| 53 | + def as_json(self) -> Dict[str, str]: |
| 54 | + return { |
| 55 | + "hash_name": self.hash_name, |
| 56 | + "hash_value": self.hash_value, |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | +@dataclass |
| 61 | +class DistInfoMetadata: |
| 62 | + """???/From PEP 658""" |
| 63 | + |
| 64 | + metadata_url: str |
| 65 | + metadata_hash: Optional[RequirementHash] |
| 66 | + |
| 67 | + @classmethod |
| 68 | + def from_link(cls, link: Link) -> Optional["DistInfoMetadata"]: |
| 69 | + if link.dist_info_metadata is None: |
| 70 | + return None |
| 71 | + metadata_url = f"{link.url_without_fragment}.metadata" |
| 72 | + metadata_hash = RequirementHash.from_dist_info_metadata(link.dist_info_metadata) |
| 73 | + return cls(metadata_url=metadata_url, metadata_hash=metadata_hash) |
| 74 | + |
| 75 | + def as_json(self) -> Dict[str, Union[str, Optional[Dict[str, str]]]]: |
| 76 | + return { |
| 77 | + "metadata_url": self.metadata_url, |
| 78 | + "metadata_hash": ( |
| 79 | + self.metadata_hash.as_json() if self.metadata_hash else None |
| 80 | + ), |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | +@dataclass |
| 85 | +class RequirementDownloadInfo: |
| 86 | + req: Requirement |
| 87 | + url: str |
| 88 | + file_hash: Optional[RequirementHash] |
| 89 | + dist_info_metadata: Optional[DistInfoMetadata] |
| 90 | + |
| 91 | + @classmethod |
| 92 | + def from_req_and_link( |
| 93 | + cls, |
| 94 | + req: Requirement, |
| 95 | + link: Link, |
| 96 | + ) -> "RequirementDownloadInfo": |
| 97 | + return cls( |
| 98 | + req=req, |
| 99 | + url=link.url, |
| 100 | + file_hash=RequirementHash.from_link(link), |
| 101 | + dist_info_metadata=DistInfoMetadata.from_link(link), |
| 102 | + ) |
| 103 | + |
| 104 | + def as_json(self) -> Dict[str, Any]: |
| 105 | + return { |
| 106 | + "req": str(self.req), |
| 107 | + "url": self.url, |
| 108 | + "hash": self.file_hash and self.file_hash.as_json(), |
| 109 | + "dist_info_metadata": ( |
| 110 | + self.dist_info_metadata and self.dist_info_metadata.as_json() |
| 111 | + ), |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | +@dataclass |
| 116 | +class DownloadInfos: |
| 117 | + # python_version: Optional[Requirement] = None |
| 118 | + python_version: Optional[str] = None |
| 119 | + resolution: Dict[str, RequirementDownloadInfo] = field(default_factory=dict) |
| 120 | + |
| 121 | + def as_json(self) -> Dict[str, Any]: |
| 122 | + return { |
| 123 | + "python_version": self.python_version and str(self.python_version), |
| 124 | + "resolution": { |
| 125 | + name: info.as_json() for name, info in self.resolution.items() |
| 126 | + }, |
| 127 | + } |
| 128 | + |
| 129 | + |
18 | 130 | class DownloadCommand(RequirementCommand):
|
19 | 131 | """
|
20 | 132 | Download packages from:
|
@@ -149,24 +261,37 @@ def run(self, options: Values, args: List[str]) -> int:
|
149 | 261 | requirement_set = resolver.resolve(reqs, check_supported_wheels=True)
|
150 | 262 |
|
151 | 263 | downloaded: List[str] = []
|
152 |
| - download_infos: List[Dict[str, str]] = [] |
153 | 264 | for req in requirement_set.requirements.values():
|
| 265 | + # If this distribution was not already satisfied, that means we |
| 266 | + # downloaded it. |
154 | 267 | if req.satisfied_by is None:
|
155 |
| - assert req.name is not None |
156 |
| - assert req.link is not None |
157 |
| - download_infos.append( |
158 |
| - { |
159 |
| - "name": req.name, |
160 |
| - "url": req.link.url, |
161 |
| - } |
162 |
| - ) |
163 | 268 | preparer.save_linked_requirement(req)
|
| 269 | + assert req.name is not None |
164 | 270 | downloaded.append(req.name)
|
165 | 271 |
|
| 272 | + download_infos = DownloadInfos() |
| 273 | + assert requirement_set.candidates is not None |
| 274 | + for candidate in requirement_set.candidates.mapping.values(): |
| 275 | + # This will occur for the python version requirement, for example. |
| 276 | + if candidate.name not in requirement_set.requirements: |
| 277 | + # download_infos.python_version = candidate.as_requirement() |
| 278 | + download_infos.python_version = str(candidate) |
| 279 | + continue |
| 280 | + req = requirement_set.requirements[candidate.name] |
| 281 | + assert req.name is not None |
| 282 | + assert req.link is not None |
| 283 | + assert req.name not in download_infos.resolution |
| 284 | + download_infos.resolution[ |
| 285 | + req.name |
| 286 | + ] = RequirementDownloadInfo.from_req_and_link( |
| 287 | + req=candidate.as_requirement(), |
| 288 | + link=req.link, |
| 289 | + ) |
| 290 | + |
166 | 291 | if downloaded:
|
167 | 292 | write_output("Successfully downloaded %s", " ".join(downloaded))
|
168 | 293 | if options.print_download_urls:
|
169 | 294 | with open(options.print_download_urls, "w") as f:
|
170 |
| - json.dump(download_infos, f, indent=4) |
| 295 | + json.dump(download_infos.as_json(), f, indent=4) |
171 | 296 |
|
172 | 297 | return SUCCESS
|
0 commit comments