okami.mom/strata

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

Niko1221Merge f-0213-life (final 576d06553) into f-0213-int66331f9bc

main
4.2 KiB106 linesraw
1"""The OS and machine facts Control needs, answered without asking WMI on Windows (#4975, #5185).
2
3`platform.system()`, `.machine()`, `.release()`, `.version()`, `.processor()` and `.node()` all go through
4`platform.uname()`. On Windows with Python 3.12 that calls `win32_ver()` and `_get_machine_win32()`, which query WMI
5(`platform._wmi_query`) with no timeout: a stuck `winmgmt` service froze every Control command that resolves the engine
6component ("Strata Control did not answer within 60 s (models list)"), although the interpreter, the environment and the
7registry know the same facts. On Windows these functions read only those. Everywhere else they are the `platform` calls
8(an `os.uname()` there, not a service), looked up at call time so a test can replace them.
9
10Pure and tiny on purpose, like targets.py: the start path imports it.
11"""
12
13from __future__ import annotations
14
15import os
16import socket
17import sys
18import platform
19
20_WINDOWS_MACHINES = {"amd64": "AMD64", "x86_64": "AMD64", "arm64": "ARM64", "aarch64": "ARM64", "x86": "x86", "i386": "x86",
21                     "i686": "x86", "ia64": "ia64"}
22
23
24def is_windows() -> bool:
25    return sys.platform.startswith("win")
26
27
28def system() -> str:
29    """`platform.system()`: "Windows", "Linux", "Darwin" ..."""
30    return "Windows" if is_windows() else platform.system()
31
32
33def _windows_machine() -> str:
34    """Windows names its architecture in the environment the loader gives every process: PROCESSOR_ARCHITEW6432 is the real
35    one inside a 32-bit process on 64-bit Windows, PROCESSOR_ARCHITECTURE the process's own (which is also what an x64
36    program emulated on ARM64 sees: the engine that fits is the x64 one). `platform.uname()` itself read these first on
37    Python 3.11 and before. Without them (a stripped environment) the interpreter's own build tag answers."""
38    for key in ("PROCESSOR_ARCHITEW6432", "PROCESSOR_ARCHITECTURE"):
39        value = os.environ.get(key, "").strip()
40        if value:
41            return _WINDOWS_MACHINES.get(value.lower(), value)
42    try:
43        import sysconfig
44        tag = sysconfig.get_platform().lower()
45    except Exception:                                    # noqa: BLE001 - a fact lookup never fails a start
46        tag = ""
47    if "arm64" in tag:
48        return "ARM64"
49    if "amd64" in tag or "x86_64" in tag:
50        return "AMD64"
51    return "x86" if tag in ("win32", "") else tag
52
53
54def machine() -> str:
55    """`platform.machine()`: "AMD64", "ARM64" on Windows; "x86_64", "arm64", "aarch64" elsewhere."""
56    return _windows_machine() if is_windows() else platform.machine()
57
58
59def release() -> str:
60    """`platform.release()`: "10" or "11" on Windows (11 from build 22000), the kernel release elsewhere."""
61    if is_windows():
62        info = _windows_version()
63        if info is None:
64            return ""
65        return "11" if info[0] == 10 and info[2] >= 22000 else str(info[0])
66    return platform.release()
67
68
69def version() -> str:
70    """`platform.version()`: "10.0.19045" on Windows, the kernel version elsewhere."""
71    if is_windows():
72        info = _windows_version()
73        return "" if info is None else f"{info[0]}.{info[1]}.{info[2]}"
74    return platform.version()
75
76
77def _windows_version() -> tuple[int, int, int] | None:
78    try:
79        info = sys.getwindowsversion()                   # type: ignore[attr-defined]
80        return int(info.major), int(info.minor), int(info.build)
81    except Exception:                                    # noqa: BLE001
82        return None
83
84
85def processor() -> str:
86    """`platform.processor()`: the CPU's description (PROCESSOR_IDENTIFIER on Windows; the empty string when unknown)."""
87    if is_windows():
88        return os.environ.get("PROCESSOR_IDENTIFIER", "").strip()
89    return platform.processor()
90
91
92def node() -> str:
93    """`platform.node()`: the computer's name."""
94    if is_windows():
95        try:
96            return socket.gethostname()
97        except OSError:
98            return os.environ.get("COMPUTERNAME", "")
99    return platform.node()
100
101
102def platform_string() -> str:
103    """A one-line description of the OS for reports (`platform.platform()` asks the same questions as `uname`)."""
104    if is_windows():
105        return f"Windows-{release()}-{version()}"
106    return platform.platform()