diff --git a/src/judas_client/client.py b/src/judas_client/client.py index d9c8bb7..8ec219f 100644 --- a/src/judas_client/client.py +++ b/src/judas_client/client.py @@ -6,7 +6,6 @@ from __future__ import annotations import logging as lg import os import platform -import re import subprocess import uuid from typing import Any @@ -53,7 +52,6 @@ class Client: on_message=self.handle_message, ) - self.logger.debug("Gathering initial telemetry data...") self.initial_telemetry: dict[str, Any] = ( self._gather_initial_telemetry() ) @@ -73,22 +71,50 @@ class Client: return mac_address def _get_cpu_model(self) -> str: + """Get the CPU model name. - if platform.system() == "Windows": - return platform.processor() - elif platform.system() == "Darwin": - os.environ["PATH"] = os.environ["PATH"] + os.pathsep + "/usr/sbin" - command = "sysctl -n machdep.cpu.brand_string" - return str(subprocess.check_output(command).strip()) - elif platform.system() == "Linux": - command = "cat /proc/cpuinfo" - all_info = ( - subprocess.check_output(command, shell=True).decode().strip() - ) - for line in all_info.split("\n"): - if "model name" in line: - return re.sub(".*model name.*:", "", line, 1).strip() - return "Unknown" + Returns: + str: The CPU model name, or "Unknown" if it cannot be determined. + """ + + system = platform.system() + + if system == "Windows": + try: + import winreg + + key = winreg.OpenKey( + winreg.HKEY_LOCAL_MACHINE, + r"HARDWARE\DESCRIPTION\System\CentralProcessor\0", + ) + model, _ = winreg.QueryValueEx(key, "ProcessorNameString") + winreg.CloseKey(key) + return model.strip() + except Exception: + return platform.processor() + + elif system == "Linux": + try: + with open("/proc/cpuinfo", "r") as f: + for line in f: + if "model name" in line: + return line.split(":")[1].strip() + except Exception: + return platform.processor() + + elif system == "Darwin": + try: + return ( + subprocess.check_output( + ["sysctl", "-n", "machdep.cpu.brand_string"] + ) + .decode() + .strip() + ) + except Exception: + return platform.processor() + + return platform.processor() def _gather_initial_telemetry(self) -> dict[str, Any]: """Gather initial telemetry data.""" @@ -110,10 +136,10 @@ class Client: "model": self._get_cpu_model(), "physical_cores": psutil.cpu_count(logical=False), "threads": psutil.cpu_count(logical=True), - "max_frequency_mhz": cpu_frequency.max + "max_frequency_mhz": round(cpu_frequency.max, 2) if cpu_frequency else "Unknown", - "min_frequency_mhz": cpu_frequency.min + "min_frequency_mhz": round(cpu_frequency.min, 2) if cpu_frequency else "Unknown", }