Python
Windows machine facts: no console window, readable processor name
Rohit C Prasad149b8ec6d3f7
diff --git a/coworker/providers/local_machine.py b/coworker/providers/local_machine.pyindex c6553036..989f226a 100644--- a/coworker/providers/local_machine.py+++ b/coworker/providers/local_machine.py@@ -80,13 +80,7 @@ def nvidia_gpu_memory_bytes() -> Optional[int]:exe = shutil.which("nvidia-smi")if not exe:return None- try:- out = subprocess.run(- [exe, "--query-gpu=memory.total", "--format=csv,noheader,nounits"],- capture_output=True, text=True, timeout=5, check=False,- ).stdout- except (OSError, subprocess.SubprocessError):- return None+ out = _run([exe, "--query-gpu=memory.total", "--format=csv,noheader,nounits"])best = 0for line in out.splitlines():try:@@ -140,13 +134,35 @@ def _read(path: str) -> str:return ""+# Windows: a console program started from the desktop app would flash a black window.+_NO_WINDOW: dict[str, Any] = {"creationflags": 0x08000000} if sys.platform == "win32" else {} # CREATE_NO_WINDOW++def _run(args: list[str]) -> str:try:- return subprocess.run(args, capture_output=True, text=True, timeout=5, check=False).stdout.strip()+ return subprocess.run(+ args, capture_output=True, text=True, timeout=5, check=False, **_NO_WINDOW+ ).stdout.strip()except (OSError, subprocess.SubprocessError):return ""+def _windows_processor() -> str:+ """The marketing name Windows keeps in the registry ("Intel(R) Core(TM) i7-12700H");+ platform.processor() gives "Intel64 Family 6 Model 154 Stepping 3, GenuineIntel"."""+ try:+ import winreg++ key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"HARDWARE\DESCRIPTION\System\CentralProcessor\0")+ try:+ value, _ = winreg.QueryValueEx(key, "ProcessorNameString")+ finally:+ winreg.CloseKey(key)+ return " ".join(str(value).split())+ except Exception:+ return ""++def _nvidia_gpu_name() -> str:exe = shutil.which("nvidia-smi")if not exe:@@ -182,6 +198,8 @@ def _static_facts() -> dict[str, Any]:elif re.search(r"Jetson|Tegra|Orin|Thor", product, re.IGNORECASE):kind = "jetson"graphics = product.strip() + ", shares the memory"+ elif sys.platform == "win32":+ processor = _windows_processor() or platform.processor() or ""else:processor = platform.processor() or ""if kind == "cpu":diff --git a/tests/test_local_machine_windows.py b/tests/test_local_machine_windows.pynew file mode 100644index 00000000..7751c6ee--- /dev/null+++ b/tests/test_local_machine_windows.py@@ -0,0 +1,41 @@+"""Machine facts on Windows: no console window flashes, and a readable processor name."""++from __future__ import annotations++import subprocess+import sys+import types++from coworker.providers import local_machine+++def test_console_programs_run_without_a_window_on_windows(monkeypatch):+ seen = {}++ def fake_run(args, **kwargs):+ seen.update(kwargs)+ return subprocess.CompletedProcess(args, 0, stdout="24564\n", stderr="")++ monkeypatch.setattr(local_machine, "_NO_WINDOW", {"creationflags": 0x08000000})+ monkeypatch.setattr(local_machine.subprocess, "run", fake_run)+ monkeypatch.setattr(local_machine.shutil, "which", lambda name: "C:/nvidia-smi.exe")+ assert local_machine.nvidia_gpu_memory_bytes() == 24564 * 1024 * 1024+ assert seen["creationflags"] == 0x08000000+++def test_no_window_flag_only_on_windows():+ expected = {"creationflags": 0x08000000} if sys.platform == "win32" else {}+ assert local_machine._NO_WINDOW == expected+++def test_windows_processor_name_comes_from_the_registry(monkeypatch):+ fake = types.SimpleNamespace(+ HKEY_LOCAL_MACHINE=object(),+ OpenKey=lambda root, path: ("key", path),+ QueryValueEx=lambda key, name: ("Intel(R) Core(TM) i7-12700H ", 1),+ CloseKey=lambda key: None,+ )+ monkeypatch.setitem(sys.modules, "winreg", fake)+ assert local_machine._windows_processor() == "Intel(R) Core(TM) i7-12700H"+ monkeypatch.setitem(sys.modules, "winreg", None) # not Windows: import fails+ assert local_machine._windows_processor() == ""