make hardware_service works on linux

This commit is contained in:
Alex Bezdieniezhnykh
2025-05-31 17:47:58 +03:00
parent 93e081c93d
commit 06878e7656
2 changed files with 20 additions and 50 deletions
+2 -2
View File
@@ -60,7 +60,7 @@ class ApiClient:
print(f"Upload fail: {e}")
def load_bytes(self, filename, folder):
hardware = get_hardware_info()
hardware_str = get_hardware_info()
if self.token is None:
self.login()
@@ -73,7 +73,7 @@ class ApiClient:
payload = json.dumps(
{
"password": self.credentials.password,
"hardware": hardware.to_json_object(),
"hardware": hardware_str,
"fileName": filename
}, indent=4)
response = requests.post(url, data=payload, headers=headers, stream=True)
+18 -48
View File
@@ -1,65 +1,35 @@
import os
import subprocess
import psutil
class HardwareInfo:
def __init__(self, cpu, gpu, memory, mac_address):
self.cpu = cpu
self.gpu = gpu
self.memory = memory
self.mac_address = mac_address
def to_json_object(self):
return {
"CPU": self.cpu,
"GPU": self.gpu,
"MacAddress": self.mac_address,
"Memory": self.memory
}
def __str__(self):
return f'CPU: {self.cpu}. GPU: {self.gpu}. Memory: {self.memory}. MAC Address: {self.mac_address}'
def get_mac_address(interface="Ethernet"):
addresses = psutil.net_if_addrs()
for interface_name, interface_info in addresses.items():
if interface_name == interface:
for addr in interface_info:
if addr.family == psutil.AF_LINK:
return addr.address.replace('-', '')
return None
def get_hardware_info():
res = subprocess.check_output("ver", shell=True).decode('utf-8')
if "Microsoft Windows" in res:
is_windows = True
else:
is_windows = False
if is_windows:
if os.name == 'nt': # 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-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 && "
"lscpu | grep 'Model name:' | cut -d':' -f2 && "
"lspci | grep VGA | cut -d':' -f3 && "
"free -g | grep Mem: | awk '{print $2}' && \""
"free -k | awk '/^Mem:/ {print $2}' && "
"cat /sys/block/sda/device/vpd_pg80 2>/dev/null || cat /sys/block/sda/device/serial 2>/dev/null"
)
result = subprocess.check_output(os_command, shell=True).decode('utf-8')
lines = [line.strip() for line in result.splitlines() if line.strip()]
cpu = lines[0].replace("Name=", "").replace(" ", " ")
gpu = lines[1].replace("Name=", "").replace(" ", " ")
memory = lines[2].replace("TotalVisibleMemorySize=", "").replace(" ", " ")
mac_address = get_mac_address()
result = subprocess.check_output(os_command, shell=True).decode('utf-8', errors='ignore')
lines = [line.replace(" ", " ").replace("Name=", "").strip('\x00\x14 \t\n\r\v\f') for line in result.splitlines() if line.strip()]
return HardwareInfo(cpu, gpu, memory, mac_address)
cpu = lines[0]
gpu = lines[1]
# could be multiple gpus, that's why take memory and drive from the 2 last lines
len_lines = len(lines)
memory = lines[len_lines - 2].replace("TotalVisibleMemorySize=", "")
drive_serial = lines[len_lines - 1]
res = f'CPU: {cpu}. GPU: {gpu}. Memory: {memory}. DriveSerial: {drive_serial}'
return res