okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
66331f9bc
main
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 : 25return sys .platform .startswith ("win" ) 26 27 28def system ()-> str : 29"""`platform.system()`: "Windows", "Linux", "Darwin" ...""" 30return "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 35one inside a 32-bit process on 64-bit Windows, PROCESSOR_ARCHITECTURE the process's own (which is also what an x64 36program emulated on ARM64 sees: the engine that fits is the x64 one). `platform.uname()` itself read these first on 37Python 3.11 and before. Without them (a stripped environment) the interpreter's own build tag answers.""" 38for key in ("PROCESSOR_ARCHITEW6432" ,"PROCESSOR_ARCHITECTURE" ): 39value = os .environ .get (key ,"" ).strip () 40if value : 41return _WINDOWS_MACHINES .get (value .lower (),value ) 42try : 43import sysconfig 44tag = sysconfig .get_platform ().lower () 45except Exception :# noqa: BLE001 - a fact lookup never fails a start 46tag = "" 47if "arm64" in tag : 48return "ARM64" 49if "amd64" in tag or "x86_64" in tag : 50return "AMD64" 51return "x86" if tag in ("win32" ,"" )else tag 52 53 54def machine ()-> str : 55"""`platform.machine()`: "AMD64", "ARM64" on Windows; "x86_64", "arm64", "aarch64" elsewhere.""" 56return _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.""" 61if is_windows (): 62info = _windows_version () 63if info is None : 64return "" 65return "11" if info [0 ]== 10 and info [2 ]>= 22000 else str (info [0 ]) 66return platform .release () 67 68 69def version ()-> str : 70"""`platform.version()`: "10.0.19045" on Windows, the kernel version elsewhere.""" 71if is_windows (): 72info = _windows_version () 73return "" if info is None else f" { info [ 0 ] } . { info [ 1 ] } . { info [ 2 ] } " 74return platform .version () 75 76 77def _windows_version ()-> tuple [int ,int ,int ]| None : 78try : 79info = sys .getwindowsversion ()# type: ignore[attr-defined] 80return int (info .major ),int (info .minor ),int (info .build ) 81except Exception :# noqa: BLE001 82return None 83 84 85def processor ()-> str : 86"""`platform.processor()`: the CPU's description (PROCESSOR_IDENTIFIER on Windows; the empty string when unknown).""" 87if is_windows (): 88return os .environ .get ("PROCESSOR_IDENTIFIER" ,"" ).strip () 89return platform .processor () 90 91 92def node ()-> str : 93"""`platform.node()`: the computer's name.""" 94if is_windows (): 95try : 96return socket .gethostname () 97except OSError : 98return os .environ .get ("COMPUTERNAME" ,"" ) 99return 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`).""" 104if is_windows (): 105return f"Windows- { release () } - { version () } " 106return platform .platform ()