-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsystem.py
More file actions
86 lines (71 loc) · 2.45 KB
/
system.py
File metadata and controls
86 lines (71 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import os
import platform
import socket
import subprocess
import shutil
import sys
import contextlib
import typing
def get_cpu_info():
"""
Get CPU info
"""
model_name = ""
arch = ""
if shutil.which("lscpu"):
with contextlib.suppress(subprocess.CalledProcessError):
info = subprocess.check_output("lscpu").decode().strip()
for line in info.split("\n"):
if "Model name" in line:
model_name = line.split(":")[1].strip()
if "Architecture" in line:
arch = line.split(":")[1].strip()
# TODO: Try /proc/cpuinfo if process fails
arch = arch or platform.machine()
if not model_name and shutil.which("sysctl"):
with contextlib.suppress(subprocess.CalledProcessError):
info = (
subprocess.check_output(["sysctl", "machdep.cpu.brand_string"])
.decode()
.strip()
)
if "machdep.cpu.brand_string:" in info:
model_name = info.split("machdep.cpu.brand_string: ")[1]
return model_name, arch
def get_gpu_info():
"""
Get GPU info
"""
_gpu_info: dict[str, str] = {"name": "", "driver_version": ""}
if shutil.which("nvidia-smi"):
with contextlib.suppress(subprocess.CalledProcessError, IndexError):
output = subprocess.check_output(
["nvidia-smi", "--query-gpu=name,driver_version", "--format=csv"]
)
lines = output.split(b"\n")
tokens = lines[1].split(b", ")
_gpu_info["name"] = tokens[0].decode()
_gpu_info["driver_version"] = tokens[1].decode()
return _gpu_info
def get_system() -> dict[str, typing.Any]:
"""
Get system details
"""
cpu = get_cpu_info()
gpu = get_gpu_info()
system: dict[str, typing.Any] = {"cwd": os.getcwd()}
system["hostname"] = socket.gethostname()
system["pythonversion"] = (
f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}"
)
system["platform"] = {}
system["platform"]["system"] = platform.system()
system["platform"]["release"] = platform.release()
system["platform"]["version"] = platform.version()
system["cpu"] = {}
system["cpu"]["arch"] = cpu[1]
system["cpu"]["processor"] = cpu[0]
system["gpu"] = {}
system["gpu"]["name"] = gpu["name"]
system["gpu"]["driver"] = gpu["driver_version"]
return system