summaryrefslogtreecommitdiffstats
path: root/debputy/plugins/numpy3.py
blob: 5cb9a85c80dd20a242b6d620ccb564b3c87ec092 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import functools
import os
from typing import Any, Tuple

from debputy.plugin.api import (
    DebputyPluginInitializer,
    BinaryCtrlAccessor,
    PackageProcessingContext,
)
from debputy.util import _error


def initialize(api: DebputyPluginInitializer) -> None:
    api.metadata_or_maintscript_detector(
        "numpy-depends",
        numpy3_versions,
        # Probably not necessary, but this is the most faithful conversion
        package_type=["deb", "udeb"],
    )


@functools.lru_cache
def _parse_numpy3_versions(versions_file: str) -> Tuple[str, str]:
    attributes = {}
    try:
        with open(versions_file, "rt", encoding="utf-8") as fd:
            for line in fd:
                if line.startswith("#") or line.isspace():
                    continue
                k, v = line.split()
                attributes[k] = v
    except FileNotFoundError:
        _error(
            f"Missing Build-Dependency on python3-numpy to ensure {versions_file}"
            " is present."
        )

    try:
        api_min_version = attributes["api-min-version"]
        abi_version = attributes["abi"]
    except KeyError as e:
        k = e.args[0]
        _error(f'Expected {versions_file} to contain the key "{k}"')
        assert False

    return api_min_version, abi_version


def numpy3_versions(
    _unused: Any,
    ctrl: BinaryCtrlAccessor,
    context: PackageProcessingContext,
) -> None:
    if context.binary_package.is_arch_all:
        dep = "python3-numpy"
    else:
        # Note we do not support --strict; codesearch.d.n suggests it is not used
        # anywhere and this saves us figuring out how to support it here.
        versions_file = os.environ.get("_NUMPY_TEST_PATH", "/usr/share/numpy3/versions")
        api_min_version, abi_version = _parse_numpy3_versions(versions_file)
        dep = f"python3-numpy (>= {api_min_version}), python3-numpy-abi{abi_version}"
    ctrl.substvars.add_dependency("python3:Depends", dep)