okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-028-app-p13b8dc8fa3

main
4.2 KiB93 linesraw
1"""The oldest NVIDIA driver the CUDA 12 engine package runs on, taken from the package's own metadata (#4247, #4188).
2
3Setup used to say "the CUDA 12 engine works from driver 528 (525 on Linux)", a number copied from NVIDIA's minor-version-compatibility table,
4while the signed feed's cuda12 packages (built with CUDA 12.9) carry `min_driver` 575.51 / 576.02 and `strata update` refuses them below
5that: a PC on driver 556 was told its engine would work and then told the feed had no engine for it. The floor is now read from
6the package that would run:
7
81. an installed cuda12 package of this platform (the components folder, setup's own engine-feed folder): its manifest's `min_driver`;
92. else the signed feed (best effort, a few seconds, never an error): the newest cuda12 package of this platform;
103. else FALLBACK, the figure the shipped packages carry today (packaging/ENGINE-COMPONENT.md).
11
12Nothing here refuses anything: the callers say the number, and say where it came from.
13"""
14from __future__ import annotations
15
16import threading
17from pathlib import Path
18
19#: What the cuda12 packages carry (packaging/ENGINE-COMPONENT.md: min_driver nvidia 575.51). Only used when no package or feed can be read.
20FALLBACK = "575.51"
21VARIANT = "cuda12"
22FEED_SECONDS = 10.0
23
24
25def major(version: str | None) -> int:
26    """The leading number of a driver version ("576.02" -> 576); 0 when there is none."""
27    try:
28        return int(str(version or "").strip().split(".")[0])
29    except ValueError:
30        return 0
31
32
33def _key(version: str) -> tuple:
34    return tuple(int(p) if p.isdigit() else 0 for p in str(version).replace("-", ".").split("."))
35
36
37def _floor_of(packages, platform: str, variant: str):
38    """The `min_driver` (nvidia) of the newest package of `variant` for `platform`: items with .version, .platform, .backend, .variant, .min_driver."""
39    best = None
40    for item in packages:
41        if item.backend != "cuda" or item.variant != variant or (platform and item.platform != platform):
42            continue
43        need = (item.min_driver or {}).get("nvidia")
44        if not need or not major(need):
45            continue
46        if best is None or _key(item.version) > _key(best.version):
47            best = item
48    return None if best is None else str(best.min_driver["nvidia"])
49
50
51def installed(roots, platform: str, variant: str = VARIANT) -> str | None:
52    """The floor of a cuda12 package already on this PC, from its manifest (no network)."""
53    from strata_control import engine_component as ec
54    found = []
55    for root in roots or ():
56        try:
57            components, _problems = ec.scan(Path(root))
58        except Exception:                                # noqa: BLE001 - a folder that cannot be read has no answer
59            continue
60        found.extend(components)
61    return _floor_of(found, platform, variant)
62
63
64def from_feed(source: str, platform: str, variant: str = VARIANT, *, seconds: float = FEED_SECONDS) -> str | None:
65    """The floor the signed feed gives a cuda12 package of this platform; None when the feed cannot be read in `seconds` (offline, no key,
66    a feed with no such package). Runs the read on a thread so a hung connection never holds setup up."""
67    answer: list = []
68
69    def read() -> None:
70        try:
71            from strata_control import feed as feedmod
72            from strata_control import signing
73            loaded = feedmod.fetch(source, signing.load_trusted_keys())
74            answer.append(_floor_of(loaded.engine, platform, variant))
75        except Exception:                                # noqa: BLE001 - the floor is a convenience, never a reason to stop
76            answer.append(None)
77
78    worker = threading.Thread(target=read, daemon=True)
79    worker.start()
80    worker.join(seconds)
81    return answer[0] if answer else None
82
83
84def cuda12_floor(platform: str, *, roots=(), feed_source: str | None = None) -> tuple[str, str]:
85    """(floor, where it came from): "576.02", "as the signed update feed says"."""
86    got = installed(roots, platform)
87    if got:
88        return got, "as the installed engine package says"
89    if feed_source:
90        got = from_feed(feed_source, platform)
91        if got:
92            return got, "as the signed update feed says"
93    return FALLBACK, "the figure the published CUDA 12 packages carry"