|
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, LinkHash |
11 | 15 | from pip._internal.req.req_tracker import get_requirement_tracker
|
| 16 | +from pip._internal.resolution.base import RequirementSetWithCandidates |
12 | 17 | from pip._internal.utils.misc import ensure_dir, normalize_path, write_output
|
13 | 18 | from pip._internal.utils.temp_dir import TempDirectory
|
14 | 19 |
|
15 | 20 | logger = logging.getLogger(__name__)
|
16 | 21 |
|
17 | 22 |
|
| 23 | +@dataclass(frozen=True) |
| 24 | +class DistInfoMetadata: |
| 25 | + """???/From PEP 658""" |
| 26 | + |
| 27 | + metadata_url: str |
| 28 | + metadata_hash: Optional[LinkHash] |
| 29 | + |
| 30 | + @classmethod |
| 31 | + def from_link(cls, link: Link) -> Optional["DistInfoMetadata"]: |
| 32 | + if link.dist_info_metadata is None: |
| 33 | + return None |
| 34 | + |
| 35 | + metadata_url = f"{link.url_without_fragment}.metadata" |
| 36 | + if link.dist_info_metadata == "true": |
| 37 | + metadata_hash = None |
| 38 | + else: |
| 39 | + metadata_hash = LinkHash.split_hash_name_and_value(link.dist_info_metadata) |
| 40 | + |
| 41 | + return cls(metadata_url=metadata_url, metadata_hash=metadata_hash) |
| 42 | + |
| 43 | + def as_json(self) -> Dict[str, Union[str, Optional[Dict[str, str]]]]: |
| 44 | + return { |
| 45 | + "metadata_url": self.metadata_url, |
| 46 | + "metadata_hash": ( |
| 47 | + self.metadata_hash.as_json() if self.metadata_hash else None |
| 48 | + ), |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | +@dataclass(frozen=True) |
| 53 | +class RequirementDownloadInfo: |
| 54 | + req: Requirement |
| 55 | + url: str |
| 56 | + file_hash: Optional[LinkHash] |
| 57 | + dist_info_metadata: Optional[DistInfoMetadata] |
| 58 | + |
| 59 | + @classmethod |
| 60 | + def from_req_and_link( |
| 61 | + cls, |
| 62 | + req: Requirement, |
| 63 | + link: Link, |
| 64 | + ) -> "RequirementDownloadInfo": |
| 65 | + return cls( |
| 66 | + req=req, |
| 67 | + url=link.url, |
| 68 | + file_hash=link.get_link_hash(), |
| 69 | + dist_info_metadata=DistInfoMetadata.from_link(link), |
| 70 | + ) |
| 71 | + |
| 72 | + def as_json(self) -> Dict[str, Any]: |
| 73 | + return { |
| 74 | + "req": str(self.req), |
| 75 | + "url": self.url, |
| 76 | + "hash": self.file_hash and self.file_hash.as_json(), |
| 77 | + "dist_info_metadata": ( |
| 78 | + self.dist_info_metadata and self.dist_info_metadata.as_json() |
| 79 | + ), |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | +@dataclass |
| 84 | +class DownloadInfos: |
| 85 | + implicit_requirements: List[Requirement] = field(default_factory=list) |
| 86 | + resolution: Dict[str, RequirementDownloadInfo] = field(default_factory=dict) |
| 87 | + |
| 88 | + def as_json(self) -> Dict[str, Any]: |
| 89 | + return { |
| 90 | + "implicit_requirements": [str(req) for req in self.implicit_requirements], |
| 91 | + "resolution": { |
| 92 | + name: info.as_json() for name, info in self.resolution.items() |
| 93 | + }, |
| 94 | + } |
| 95 | + |
| 96 | + |
18 | 97 | class DownloadCommand(RequirementCommand):
|
19 | 98 | """
|
20 | 99 | Download packages from:
|
@@ -149,24 +228,46 @@ def run(self, options: Values, args: List[str]) -> int:
|
149 | 228 | requirement_set = resolver.resolve(reqs, check_supported_wheels=True)
|
150 | 229 |
|
151 | 230 | downloaded: List[str] = []
|
152 |
| - download_infos: List[Dict[str, str]] = [] |
153 | 231 | for req in requirement_set.requirements.values():
|
| 232 | + # If this distribution was not already satisfied, that means we |
| 233 | + # downloaded it. |
154 | 234 | 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 | 235 | preparer.save_linked_requirement(req)
|
| 236 | + assert req.name is not None |
164 | 237 | downloaded.append(req.name)
|
165 | 238 |
|
| 239 | + download_infos = DownloadInfos() |
| 240 | + if options.print_download_urls: |
| 241 | + if isinstance(requirement_set, RequirementSetWithCandidates): |
| 242 | + for candidate in requirement_set.candidates.mapping.values(): |
| 243 | + # This will occur for the python version requirement, for example. |
| 244 | + if candidate.name not in requirement_set.requirements: |
| 245 | + download_infos.implicit_requirements.append( |
| 246 | + candidate.as_serializable_requirement() |
| 247 | + ) |
| 248 | + continue |
| 249 | + req = requirement_set.requirements[candidate.name] |
| 250 | + assert req.name is not None |
| 251 | + assert req.link is not None |
| 252 | + assert req.name not in download_infos.resolution |
| 253 | + download_infos.resolution[ |
| 254 | + req.name |
| 255 | + ] = RequirementDownloadInfo.from_req_and_link( |
| 256 | + req=candidate.as_serializable_requirement(), |
| 257 | + link=req.link, |
| 258 | + ) |
| 259 | + else: |
| 260 | + logger.warning( |
| 261 | + "--print-download-urls is being used with the legacy resolver. " |
| 262 | + "The legacy resolver does not retain detailed dependency " |
| 263 | + "information, so all the fields in the output JSON file " |
| 264 | + "will be empty." |
| 265 | + ) |
| 266 | + |
166 | 267 | if downloaded:
|
167 | 268 | write_output("Successfully downloaded %s", " ".join(downloaded))
|
168 | 269 | if options.print_download_urls:
|
169 | 270 | with open(options.print_download_urls, "w") as f:
|
170 |
| - json.dump(download_infos, f, indent=4) |
| 271 | + json.dump(download_infos.as_json(), f, indent=4) |
171 | 272 |
|
172 | 273 | return SUCCESS
|
0 commit comments