|
29 | 29 |
|
30 | 30 |
|
31 | 31 | def get_cpu_model(): |
32 | | - if platform.system() == "Windows": |
| 32 | + system = platform.system() |
| 33 | + if system == "Windows": |
33 | 34 | return platform.processor() |
34 | | - elif platform.system() == "Darwin": |
| 35 | + elif system == "Darwin": |
35 | 36 | os.environ['PATH'] = os.environ['PATH'] + os.pathsep + '/usr/sbin' |
36 | | - command = "sysctl -n machdep.cpu.brand_string" |
37 | | - return subprocess.check_output(command).strip() |
38 | | - elif platform.system() == "Linux": |
39 | | - command = "cat /proc/cpuinfo" |
40 | | - output = subprocess.check_output(command, shell=True).decode().strip() |
41 | | - for line in output.split("\n"): |
42 | | - if line.startswith("model name"): |
43 | | - return line.split(':', 1)[1].strip() |
| 37 | + command = ["sysctl", "-n", "machdep.cpu.brand_string"] |
| 38 | + return subprocess.check_output(command, text=True).strip() |
| 39 | + elif system == "Linux": |
| 40 | + output = subprocess.check_output(["lscpu"], text=True) |
| 41 | + model_name = None |
| 42 | + architecture = None |
| 43 | + for line in output.splitlines(): |
| 44 | + if "Model name:" in line: |
| 45 | + model_name = line.split(":", 1)[1].strip() |
| 46 | + elif "Architecture:" in line: |
| 47 | + architecture = line.split(":", 1)[1].strip() |
| 48 | + # Prefer model_name if available; fallback to architecture |
| 49 | + return model_name or architecture or "unknown_cpu" |
44 | 50 | return "unknown_cpu" |
45 | 51 |
|
46 | 52 |
|
|
0 commit comments