|
| 1 | +import re |
| 2 | +import sys |
| 3 | + |
| 4 | +from pathlib import Path |
| 5 | +from typing import Optional, TypedDict |
| 6 | +from urllib.parse import urldefrag |
| 7 | + |
| 8 | +from clikit.api.io.flags import VERY_VERBOSE |
| 9 | +from clikit.io import ConsoleIO |
| 10 | +from packaging.tags import compatible_tags, cpython_tags |
| 11 | +from poetry.core.packages import Dependency, Package, ProjectPackage, URLDependency |
| 12 | +from poetry.installation.chooser import Chooser |
| 13 | +from poetry.installation.operations import Install |
| 14 | +from poetry.installation.operations.uninstall import Uninstall |
| 15 | +from poetry.puzzle import Solver |
| 16 | +from poetry.repositories.pool import Pool |
| 17 | +from poetry.repositories.pypi_repository import PyPiRepository |
| 18 | +from poetry.repositories.repository import Repository |
| 19 | +from poetry.utils.env import Env |
| 20 | + |
| 21 | +from conda_lock.src_parser.pyproject_toml import get_lookup as get_forward_lookup |
| 22 | + |
| 23 | + |
| 24 | +class PlatformEnv(Env): |
| 25 | + def __init__(self, python_version, platform): |
| 26 | + super().__init__(path=Path(sys.prefix)) |
| 27 | + if platform == "linux-64": |
| 28 | + # FIXME: in principle these depend on the glibc in the conda env |
| 29 | + self._platforms = ["manylinux_2_17_x86_64", "manylinux2014_x86_64"] |
| 30 | + else: |
| 31 | + raise ValueError(f"Unsupported platform '{platform}'") |
| 32 | + self._python_version = tuple(map(int, python_version.split("."))) |
| 33 | + |
| 34 | + def get_supported_tags(self): |
| 35 | + """ |
| 36 | + Mimic the output of packaging.tags.sys_tags() on the given platform |
| 37 | + """ |
| 38 | + return list( |
| 39 | + cpython_tags(python_version=self._python_version, platforms=self._platforms) |
| 40 | + ) + list( |
| 41 | + compatible_tags( |
| 42 | + python_version=self._python_version, platforms=self._platforms |
| 43 | + ) |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +class PipRequirement(TypedDict): |
| 48 | + name: str |
| 49 | + version: Optional[str] |
| 50 | + url: str |
| 51 | + hashes: list[str] |
| 52 | + |
| 53 | + |
| 54 | +REQUIREMENT_PATTERN = re.compile( |
| 55 | + r""" |
| 56 | + ^ |
| 57 | + (?P<name>[a-zA-Z0-9_-]+) # package name |
| 58 | + (?:\[(?P<extras>(?:\s?[a-zA-Z0-9_-]+(?:\s?\,\s?)?)+)\])? # extras |
| 59 | + (?: |
| 60 | + (?: # a direct reference |
| 61 | + \s?@\s?(?P<url>.*) |
| 62 | + ) |
| 63 | + | |
| 64 | + (?: # one or more PEP440 version specifiers |
| 65 | + \s?(?P<constraint> |
| 66 | + (?:\s? |
| 67 | + (?: |
| 68 | + (?:=|[><~=!])?= |
| 69 | + | |
| 70 | + [<>] |
| 71 | + ) |
| 72 | + \s? |
| 73 | + (?: |
| 74 | + [A-Za-z0-9\.-_\*]+ # a version tuple, e.g. x.y.z |
| 75 | + (?:-[A-Za-z]+(?:\.[0-9]+)?)? # a post-release tag, e.g. -alpha.2 |
| 76 | + (?:\s?\,\s?)? |
| 77 | + ) |
| 78 | + )+ |
| 79 | + ) |
| 80 | + ) |
| 81 | + )? |
| 82 | + $ |
| 83 | + """, |
| 84 | + re.VERBOSE, |
| 85 | +) |
| 86 | + |
| 87 | + |
| 88 | +def parse_pip_requirement(requirement: str) -> Optional[dict[str, str]]: |
| 89 | + match = REQUIREMENT_PATTERN.match(requirement) |
| 90 | + if not match: |
| 91 | + return None |
| 92 | + return match.groupdict() |
| 93 | + |
| 94 | + |
| 95 | +def get_dependency(requirement: str) -> Dependency: |
| 96 | + parsed = parse_pip_requirement(requirement) |
| 97 | + if parsed is None: |
| 98 | + raise ValueError(f"Unknown pip requirement '{requirement}'") |
| 99 | + extras = re.split(r"\s?\,\s?", parsed["extras"]) if parsed["extras"] else None |
| 100 | + if parsed["url"]: |
| 101 | + return URLDependency(name=parsed["name"], url=parsed["url"], extras=extras) |
| 102 | + else: |
| 103 | + return Dependency( |
| 104 | + name=parsed["name"], constraint=parsed["constraint"] or "*", extras=extras |
| 105 | + ) |
| 106 | + |
| 107 | + |
| 108 | +PYPI_LOOKUP: Optional[dict] = None |
| 109 | + |
| 110 | + |
| 111 | +def get_lookup() -> dict: |
| 112 | + global PYPI_LOOKUP |
| 113 | + if PYPI_LOOKUP is None: |
| 114 | + PYPI_LOOKUP = { |
| 115 | + record["conda_name"]: record for record in get_forward_lookup().values() |
| 116 | + } |
| 117 | + return PYPI_LOOKUP |
| 118 | + |
| 119 | + |
| 120 | +def normalize_conda_name(name: str): |
| 121 | + return get_lookup().get(name, {"pypi_name": name})["pypi_name"] |
| 122 | + |
| 123 | + |
| 124 | +def solve_pypi( |
| 125 | + dependencies: list[str], |
| 126 | + conda_installed: list[tuple[str, str]], |
| 127 | + python_version: str, |
| 128 | + platform: str, |
| 129 | + verbose: bool = False, |
| 130 | +) -> list[PipRequirement]: |
| 131 | + dummy_package = ProjectPackage("_dummy_package_", "0.0.0") |
| 132 | + dummy_package.python_versions = f"=={python_version}" |
| 133 | + for spec in dependencies: |
| 134 | + dummy_package.add_dependency(get_dependency(spec)) |
| 135 | + |
| 136 | + pypi = PyPiRepository() |
| 137 | + pool = Pool(repositories=[pypi]) |
| 138 | + |
| 139 | + installed = Repository() |
| 140 | + locked = Repository() |
| 141 | + |
| 142 | + python_packages = dict() |
| 143 | + for name, version in conda_installed: |
| 144 | + pypi_name = normalize_conda_name(name) |
| 145 | + # Prefer the Python package when its name collides with the Conda package |
| 146 | + # for the underlying library, e.g. python-xxhash (pypi: xxhash) over xxhash |
| 147 | + # (pypi: no equivalent) |
| 148 | + if pypi_name not in python_packages or pypi_name != name: |
| 149 | + python_packages[pypi_name] = version |
| 150 | + for name, version in python_packages.items(): |
| 151 | + for repo in (locked, installed): |
| 152 | + repo.add_package(Package(name=name, version=version)) |
| 153 | + |
| 154 | + io = ConsoleIO() |
| 155 | + if verbose: |
| 156 | + io.set_verbosity(VERY_VERBOSE) |
| 157 | + s = Solver( |
| 158 | + dummy_package, |
| 159 | + pool=pool, |
| 160 | + installed=installed, |
| 161 | + locked=locked, |
| 162 | + io=io, |
| 163 | + ) |
| 164 | + result = s.solve(use_latest=dependencies) |
| 165 | + |
| 166 | + chooser = Chooser(pool, env=PlatformEnv(python_version, platform)) |
| 167 | + |
| 168 | + # Extract distributions from Poetry package plan, ignoring uninstalls |
| 169 | + # (usually: conda package with no pypi equivalent) and skipped ops |
| 170 | + # (already installed) |
| 171 | + requirements: list[PipRequirement] = [] |
| 172 | + for op in result: |
| 173 | + if not isinstance(op, Uninstall) and not op.skipped: |
| 174 | + # Take direct references verbatim |
| 175 | + if op.package.source_type == "url": |
| 176 | + url, fragment = urldefrag(op.package.source_url) |
| 177 | + requirements.append( |
| 178 | + { |
| 179 | + "name": op.package.name, |
| 180 | + "version": None, |
| 181 | + "url": url, |
| 182 | + "hashes": [fragment.replace("=", ":")], |
| 183 | + } |
| 184 | + ) |
| 185 | + # Choose the most specific distribution for the target |
| 186 | + else: |
| 187 | + link = chooser.choose_for(op.package) |
| 188 | + requirements.append( |
| 189 | + { |
| 190 | + "name": op.package.name, |
| 191 | + "version": str(op.package.version), |
| 192 | + "url": link.url_without_fragment, |
| 193 | + "hashes": [f"{link.hash_name}:{link.hash}"], |
| 194 | + } |
| 195 | + ) |
| 196 | + |
| 197 | + return requirements |
0 commit comments