import os import platform import subprocess import psutil cimport constants cdef str _CACHED_HW_INFO = None def _get_cpu(): try: with open("/proc/cpuinfo") as f: for line in f: if "model name" in line.lower(): return line.split(":")[1].strip() except OSError: pass cdef str p = platform.processor() if p: return p return platform.machine() def _get_gpu(): try: result = subprocess.run( ["lspci"], capture_output=True, text=True, timeout=5, ) for line in result.stdout.splitlines(): if "VGA" in line: parts = line.split(":") if len(parts) > 2: return parts[2].strip() return parts[-1].strip() except (OSError, subprocess.TimeoutExpired, FileNotFoundError): pass try: result = subprocess.run( ["system_profiler", "SPDisplaysDataType"], capture_output=True, text=True, timeout=5, ) for line in result.stdout.splitlines(): if "Chipset Model" in line: return line.split(":")[1].strip() except (OSError, subprocess.TimeoutExpired, FileNotFoundError): pass return "unknown" def _get_drive_serial(): try: for block in sorted(os.listdir("/sys/block")): for candidate in [ f"/sys/block/{block}/device/vpd_pg80", f"/sys/block/{block}/device/serial", f"/sys/block/{block}/serial", ]: try: with open(candidate, "rb") as f: serial = f.read().strip(b"\x00\x14 \t\n\r\v\f").decode("utf-8", errors="ignore") if serial: return serial except OSError: continue except OSError: pass try: result = subprocess.run( ["ioreg", "-rd1", "-c", "IOPlatformExpertDevice"], capture_output=True, text=True, timeout=5, ) for line in result.stdout.splitlines(): if "IOPlatformSerialNumber" in line: return line.split('"')[-2] except (OSError, subprocess.TimeoutExpired, FileNotFoundError): pass return "unknown" cdef class HardwareService: @staticmethod cdef str get_hardware_info(): global _CACHED_HW_INFO if _CACHED_HW_INFO is not None: constants.log("Using cached hardware info") return _CACHED_HW_INFO cdef str cpu = _get_cpu() cdef str gpu = _get_gpu() cdef str memory = str(psutil.virtual_memory().total // 1024) cdef str drive_serial = _get_drive_serial() cdef str res = f'CPU: {cpu}. GPU: {gpu}. Memory: {memory}. DriveSerial: {drive_serial}' constants.log(f'Gathered hardware: {res}') _CACHED_HW_INFO = res return res