|
| 1 | +import dataclasses |
| 2 | +from dataclasses import dataclass |
| 3 | +from typing import Any, Dict, Iterable, List, Literal, Self, Tuple |
| 4 | + |
| 5 | +from pip._vendor.typing_extensions import Optional |
| 6 | + |
| 7 | +from pip._internal.models.direct_url import ArchiveInfo, DirInfo, VcsInfo |
| 8 | +from pip._internal.models.link import Link |
| 9 | +from pip._internal.req.req_install import InstallRequirement |
| 10 | +from pip._internal.utils.urls import url_to_path |
| 11 | + |
| 12 | + |
| 13 | +def _toml_dict_factory(data: Iterable[Tuple[str, Any]]) -> Dict[str, Any]: |
| 14 | + return {key.replace("_", "-"): value for key, value in data if value is not None} |
| 15 | + |
| 16 | + |
| 17 | +@dataclass |
| 18 | +class PackageVcs: |
| 19 | + type: str |
| 20 | + url: Optional[str] |
| 21 | + # (not supported) path: Optional[str] |
| 22 | + requested_revision: Optional[str] |
| 23 | + commit_id: str |
| 24 | + subdirectory: Optional[str] |
| 25 | + |
| 26 | + |
| 27 | +@dataclass |
| 28 | +class PackageDirectory: |
| 29 | + path: str |
| 30 | + editable: Optional[bool] |
| 31 | + subdirectory: Optional[str] |
| 32 | + |
| 33 | + |
| 34 | +@dataclass |
| 35 | +class PackageArchive: |
| 36 | + url: Optional[str] |
| 37 | + # (not supported) path: Optional[str] |
| 38 | + # (not supported) size: Optional[int] |
| 39 | + hashes: Dict[str, str] |
| 40 | + subdirectory: Optional[str] |
| 41 | + |
| 42 | + |
| 43 | +@dataclass |
| 44 | +class PackageSdist: |
| 45 | + name: str |
| 46 | + # (not supported) upload_time |
| 47 | + url: Optional[str] |
| 48 | + # (not supported) path: Optional[str] |
| 49 | + # (not supported) size: Optional[int] |
| 50 | + hashes: Dict[str, str] |
| 51 | + |
| 52 | + |
| 53 | +@dataclass |
| 54 | +class PackageWheel: |
| 55 | + name: str |
| 56 | + # (not supported) upload_time |
| 57 | + url: Optional[str] |
| 58 | + # (not supported) path: Optional[str] |
| 59 | + # (not supported) size: Optional[int] |
| 60 | + hashes: Dict[str, str] |
| 61 | + |
| 62 | + |
| 63 | +@dataclass |
| 64 | +class Package: |
| 65 | + name: str |
| 66 | + version: Optional[str] = None |
| 67 | + # (not supported) marker: Optional[str] |
| 68 | + # (not supported) requires_python: Optional[str] |
| 69 | + # (not supported) dependencies |
| 70 | + direct: Optional[bool] = None |
| 71 | + vcs: Optional[PackageVcs] = None |
| 72 | + directory: Optional[PackageDirectory] = None |
| 73 | + archive: Optional[PackageArchive] = None |
| 74 | + # (not supported) index: Optional[str] |
| 75 | + sdist: Optional[PackageSdist] = None |
| 76 | + wheels: Optional[List[PackageWheel]] = None |
| 77 | + # (not supported) attestation_identities |
| 78 | + # (not supported) tool |
| 79 | + |
| 80 | + @classmethod |
| 81 | + def from_install_requirement(cls, ireq: InstallRequirement) -> Self: |
| 82 | + assert ireq.name |
| 83 | + dist = ireq.get_dist() |
| 84 | + download_info = ireq.download_info |
| 85 | + assert download_info |
| 86 | + package = cls( |
| 87 | + name=dist.canonical_name, |
| 88 | + version=str(dist.version), |
| 89 | + ) |
| 90 | + package.direct = ireq.is_direct if ireq.is_direct else None |
| 91 | + if package.direct: |
| 92 | + if isinstance(download_info.info, VcsInfo): |
| 93 | + package.vcs = PackageVcs( |
| 94 | + type=download_info.info.vcs, |
| 95 | + url=download_info.url, |
| 96 | + requested_revision=download_info.info.requested_revision, |
| 97 | + commit_id=download_info.info.commit_id, |
| 98 | + subdirectory=download_info.subdirectory, |
| 99 | + ) |
| 100 | + elif isinstance(download_info.info, DirInfo): |
| 101 | + package.directory = PackageDirectory( |
| 102 | + path=url_to_path(download_info.url), |
| 103 | + editable=( |
| 104 | + download_info.info.editable |
| 105 | + if download_info.info.editable |
| 106 | + else None |
| 107 | + ), |
| 108 | + subdirectory=download_info.subdirectory, |
| 109 | + ) |
| 110 | + elif isinstance(download_info.info, ArchiveInfo): |
| 111 | + if not download_info.info.hashes: |
| 112 | + raise NotImplementedError() |
| 113 | + package.archive = PackageArchive( |
| 114 | + url=download_info.url, |
| 115 | + hashes=download_info.info.hashes, |
| 116 | + subdirectory=download_info.subdirectory, |
| 117 | + ) |
| 118 | + else: |
| 119 | + # should never happen |
| 120 | + raise NotImplementedError() |
| 121 | + else: |
| 122 | + if isinstance(download_info.info, ArchiveInfo): |
| 123 | + link = Link(download_info.url) |
| 124 | + if not download_info.info.hashes: |
| 125 | + raise NotImplementedError() |
| 126 | + if link.is_wheel: |
| 127 | + package.wheels = [ |
| 128 | + PackageWheel( |
| 129 | + name=link.filename, |
| 130 | + url=download_info.url, |
| 131 | + hashes=download_info.info.hashes, |
| 132 | + ) |
| 133 | + ] |
| 134 | + else: |
| 135 | + package.sdist = PackageSdist( |
| 136 | + name=link.filename, |
| 137 | + url=download_info.url, |
| 138 | + hashes=download_info.info.hashes, |
| 139 | + ) |
| 140 | + else: |
| 141 | + # should never happen |
| 142 | + raise NotImplementedError() |
| 143 | + return package |
| 144 | + |
| 145 | + |
| 146 | +@dataclass |
| 147 | +class Pylock: |
| 148 | + lock_version: Literal["1.0"] = "1.0" |
| 149 | + # (not supported) environments |
| 150 | + # (not supported) requires_python |
| 151 | + created_by: str = "pip" |
| 152 | + packages: List[Package] = dataclasses.field(default_factory=list) |
| 153 | + # (not supported) tool |
| 154 | + |
| 155 | + def to_dict(self) -> Dict[str, Any]: |
| 156 | + return dataclasses.asdict(self, dict_factory=_toml_dict_factory) |
| 157 | + |
| 158 | + @classmethod |
| 159 | + def from_install_requirements( |
| 160 | + cls, install_requirements: Iterable[InstallRequirement] |
| 161 | + ) -> Self: |
| 162 | + return cls( |
| 163 | + packages=sorted( |
| 164 | + ( |
| 165 | + Package.from_install_requirement(ireq) |
| 166 | + for ireq in install_requirements |
| 167 | + ), |
| 168 | + key=lambda p: p.name, |
| 169 | + ) |
| 170 | + ) |
0 commit comments