mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 22:56:29 +00:00
76 lines
3.0 KiB
Cython
76 lines
3.0 KiB
Cython
import subprocess
|
|
import pynvml
|
|
|
|
|
|
cdef class HardwareService:
|
|
"""Handles hardware information retrieval and hash generation."""
|
|
|
|
def __init__(self):
|
|
try:
|
|
res = subprocess.check_output("ver", shell=True).decode('utf-8')
|
|
if "Microsoft Windows" in res:
|
|
self.is_windows = True
|
|
else:
|
|
self.is_windows = False
|
|
except Exception:
|
|
print('Error during os type checking')
|
|
self.is_windows = False
|
|
|
|
@staticmethod
|
|
cdef has_nvidia_gpu():
|
|
try:
|
|
pynvml.nvmlInit()
|
|
device_count = pynvml.nvmlDeviceGetCount()
|
|
|
|
if device_count > 0:
|
|
print(f"Found NVIDIA GPU(s).")
|
|
return True
|
|
else:
|
|
print("No NVIDIA GPUs found by NVML.")
|
|
return False
|
|
|
|
except pynvml.NVMLError as error:
|
|
print(f"Failed to find NVIDIA GPU")
|
|
return False
|
|
finally:
|
|
try:
|
|
pynvml.nvmlShutdown()
|
|
except:
|
|
print('Failed to shutdown pynvml cause probably no NVidia GPU')
|
|
pass
|
|
|
|
cdef str get_hardware_info(self):
|
|
if self.is_windows:
|
|
os_command = (
|
|
"powershell -Command \""
|
|
"Get-CimInstance -ClassName Win32_Processor | Select-Object -ExpandProperty Name | Write-Output; "
|
|
"Get-CimInstance -ClassName Win32_VideoController | Select-Object -ExpandProperty Name | Write-Output; "
|
|
"Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -ExpandProperty TotalVisibleMemorySize | Write-Output; "
|
|
"(Get-Disk | Where-Object {$_.IsSystem -eq $true}).SerialNumber"
|
|
"\""
|
|
)
|
|
else:
|
|
os_command = (
|
|
"/bin/bash -c \" lscpu | grep 'Model name:' | cut -d':' -f2 && "
|
|
"lspci | grep VGA | cut -d':' -f3 && "
|
|
"free -g | grep Mem: | awk '{print $2}' && \""
|
|
"udevadm info --query=property --name=\"/dev/$(lsblk -no pkname \"$(findmnt -n -o SOURCE --target /)\")\" | grep -E 'ID_SERIAL=|ID_SERIAL_SHORT=' | cut -d'=' -f2- | head -n1 && "
|
|
)
|
|
# in case of subprocess error do:
|
|
# cdef bytes os_command_bytes = os_command.encode('utf-8')
|
|
# and use os_command_bytes
|
|
|
|
result = subprocess.check_output(os_command, shell=True).decode('utf-8')
|
|
lines = [line.strip() for line in result.splitlines() if line.strip()]
|
|
|
|
cdef str cpu = lines[0].replace("Name=", "").replace(" ", " ")
|
|
cdef str gpu = lines[1].replace("Name=", "").replace(" ", " ")
|
|
# could be multiple gpus
|
|
|
|
len_lines = len(lines)
|
|
cdef str memory = lines[len_lines-2].replace("TotalVisibleMemorySize=", "").replace(" ", " ")
|
|
cdef str drive_serial = lines[len_lines-1]
|
|
|
|
cdef str res = f'CPU: {cpu}. GPU: {gpu}. Memory: {memory}. DriveSerial: {drive_serial}'
|
|
return res
|