|
| 1 | +# Copyright (C) 2023, CERN |
| 2 | +# This software is distributed under the terms of the MIT |
| 3 | +# licence, copied verbatim in the file "LICENSE". |
| 4 | +# In applying this license, CERN does not waive the privileges and immunities |
| 5 | +# granted to it by virtue of its status as Intergovernmental Organization |
| 6 | +# or submit itself to any jurisdiction. |
| 7 | + |
| 8 | +import dataclasses |
| 9 | + |
| 10 | +from packaging.utils import parse_wheel_filename |
| 11 | +from packaging.version import Version |
| 12 | +from simple_repository import model |
| 13 | + |
| 14 | + |
| 15 | +@dataclasses.dataclass(frozen=True) |
| 16 | +class CompatibilityMatrixModel: |
| 17 | + matrix: dict[tuple[str, str], model.File] |
| 18 | + py_and_abi_names: tuple[str, ...] |
| 19 | + platform_names: tuple[str, ...] |
| 20 | + |
| 21 | + |
| 22 | +def compatibility_matrix( |
| 23 | + files: tuple[model.File, ...], |
| 24 | +) -> CompatibilityMatrixModel: |
| 25 | + """ |
| 26 | + Look at the given files, and compute a compatibility matrix. |
| 27 | +
|
| 28 | + """ |
| 29 | + compat_matrix: dict[tuple[str, str], model.File] = {} |
| 30 | + # Track the py_abi_names seen, and store a sort key for those names. |
| 31 | + py_abi_names = {} |
| 32 | + # Track the platform_names (we sort by name). |
| 33 | + platform_names = set() |
| 34 | + |
| 35 | + interpreted_py_abi_tags: dict[tuple[str, str], InterpretedPyAndABITag] = {} |
| 36 | + |
| 37 | + for file in files: |
| 38 | + if not file.filename.lower().endswith('.whl'): |
| 39 | + continue |
| 40 | + _, _, _, tags = parse_wheel_filename(file.filename) |
| 41 | + |
| 42 | + # Ensure that the tags have a consistent sort order. From |
| 43 | + # packaging they come as a frozenset, so no such upstream guarantee is provided. |
| 44 | + sorted_tags = sorted(tags, key=lambda tag: (tag.platform, tag.abi, tag.interpreter)) |
| 45 | + |
| 46 | + for tag in sorted_tags: |
| 47 | + inter_abi_key = (tag.interpreter, tag.abi) |
| 48 | + if inter_abi_key not in interpreted_py_abi_tags: |
| 49 | + interpreted_py_abi_tags[inter_abi_key] = interpret_py_and_abi_tag(tag.interpreter, tag.abi) |
| 50 | + |
| 51 | + tag_interp = interpreted_py_abi_tags[inter_abi_key] |
| 52 | + compat_matrix[(tag_interp.nice_name, tag.platform)] = file |
| 53 | + |
| 54 | + # Track the seen tags, and define a sort order. |
| 55 | + py_abi_names[tag_interp.nice_name] = ( |
| 56 | + tag_interp.python_implementation, |
| 57 | + tag_interp.python_version, |
| 58 | + tag_interp.nice_name, |
| 59 | + ) |
| 60 | + platform_names.add(tag.platform) |
| 61 | + |
| 62 | + r_plat_names = tuple(sorted(platform_names)) |
| 63 | + r_py_abi_names = tuple(sorted(py_abi_names, key=py_abi_names.__getitem__)) |
| 64 | + |
| 65 | + return CompatibilityMatrixModel(compat_matrix, r_py_abi_names, r_plat_names) |
| 66 | + |
| 67 | + |
| 68 | +# https://packaging.python.org/en/latest/specifications/platform-compatibility-tags/#python-tag |
| 69 | +py_tag_implementations = { |
| 70 | + 'py': 'Python', |
| 71 | + 'cp': 'CPython', |
| 72 | + 'ip': 'IronPython', |
| 73 | + 'pp': 'PyPy', |
| 74 | + 'jy': 'Jython', |
| 75 | +} |
| 76 | + |
| 77 | + |
| 78 | +@dataclasses.dataclass(frozen=True) |
| 79 | +class InterpretedPyAndABITag: |
| 80 | + nice_name: str |
| 81 | + python_implementation: str | None = None |
| 82 | + python_version: Version | None = None |
| 83 | + |
| 84 | + |
| 85 | +def interpret_py_and_abi_tag(py_tag: str, abi_tag: str) -> InterpretedPyAndABITag: |
| 86 | + if py_tag[:2] in py_tag_implementations: |
| 87 | + py_impl, version_nodot = py_tag[:2], py_tag[2:] |
| 88 | + py_impl = py_tag_implementations.get(py_impl, py_impl) |
| 89 | + if '_' in version_nodot: |
| 90 | + py_version = Version('.'.join(version_nodot.split('_'))) |
| 91 | + elif len(version_nodot) == 1: |
| 92 | + # e.g. Pure python wheels |
| 93 | + py_version = Version(version_nodot) |
| 94 | + else: |
| 95 | + py_version = Version(f'{version_nodot[0]}.{version_nodot[1:]}') |
| 96 | + |
| 97 | + if abi_tag.startswith(py_tag): |
| 98 | + abi_tag_flags = abi_tag[len(py_tag):] |
| 99 | + if 'd' in abi_tag_flags: |
| 100 | + abi_tag_flags = abi_tag_flags.replace('d', '') |
| 101 | + py_impl += ' (debug)' |
| 102 | + if 'u' in abi_tag_flags: |
| 103 | + abi_tag_flags = abi_tag_flags.replace('u', '') |
| 104 | + # A python 2 concept. |
| 105 | + py_impl += ' (wide)' |
| 106 | + if 'm' in abi_tag_flags: |
| 107 | + abi_tag_flags = abi_tag_flags.replace('m', '') |
| 108 | + pass |
| 109 | + if abi_tag_flags: |
| 110 | + py_impl += f' (additional flags: {abi_tag_flags})' |
| 111 | + return InterpretedPyAndABITag(f'{py_impl} {py_version}', py_impl, py_version) |
| 112 | + elif abi_tag.startswith('pypy') and py_impl == 'PyPy': |
| 113 | + abi = abi_tag.split('_')[1] |
| 114 | + return InterpretedPyAndABITag(f'{py_impl} {py_version} ({abi})', py_impl, py_version) |
| 115 | + elif abi_tag == 'abi3': |
| 116 | + # Example PyQt6 |
| 117 | + return InterpretedPyAndABITag(f'{py_impl} >={py_version} (abi3)', py_impl, py_version) |
| 118 | + elif abi_tag == 'none': |
| 119 | + # Seen with pydantic-core 2.11.0 |
| 120 | + return InterpretedPyAndABITag(f'{py_impl} {py_version}', py_impl, py_version) |
| 121 | + else: |
| 122 | + return InterpretedPyAndABITag(f'{py_impl} {py_version} ({abi_tag})', py_impl, py_version) |
| 123 | + |
| 124 | + return InterpretedPyAndABITag(f'{py_tag} ({abi_tag})') |
0 commit comments