mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 19:46:29 +00:00
rewrite inference and file loading to cython
Step 1: can compile
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
# cython: language_level=3
|
||||
import base64
|
||||
import subprocess
|
||||
from hashlib import sha384
|
||||
|
||||
cdef class HardwareInfo:
|
||||
cdef str cpu
|
||||
cdef str gpu
|
||||
cdef str memory
|
||||
cdef str mac_address
|
||||
cdef str hash
|
||||
|
||||
def __init__(self, str cpu, str gpu, str memory, str mac_address, str hw_hash):
|
||||
self.cpu = cpu
|
||||
self.gpu = gpu
|
||||
self.memory = memory
|
||||
self.mac_address = mac_address
|
||||
self.hash = hw_hash
|
||||
|
||||
def __str__(self):
|
||||
return f'CPU: {self.cpu}. GPU: {self.gpu}. Memory: {self.memory}. MAC Address: {self.mac_address}'
|
||||
|
||||
cdef class HardwareService:
|
||||
"""Handles hardware information retrieval and hash generation."""
|
||||
|
||||
def __init__(self):
|
||||
try:
|
||||
if subprocess.check_output("ver", shell=True).decode('utf-8').startswith("Microsoft"):
|
||||
self.is_windows = True
|
||||
else:
|
||||
self.is_windows = False
|
||||
except Exception:
|
||||
self.is_windows = False
|
||||
|
||||
cdef HardwareInfo get_hardware_info(self):
|
||||
if self.is_windows:
|
||||
os_command = (
|
||||
"wmic CPU get Name /Value && "
|
||||
"wmic path Win32_VideoController get Name /Value && "
|
||||
"wmic OS get TotalVisibleMemorySize /Value"
|
||||
)
|
||||
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}' && \""
|
||||
)
|
||||
# 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(" ", " ")
|
||||
cdef str memory = lines[2].replace("TotalVisibleMemorySize=", "").replace(" ", " ")
|
||||
|
||||
# Get MAC address
|
||||
if self.is_windows:
|
||||
mac_cmd = "getmac"
|
||||
else:
|
||||
mac_cmd = "cat /sys/class/net/*/address"
|
||||
cdef str mac_address = subprocess.check_output(mac_cmd, shell=True, text=True).splitlines()[0].strip()
|
||||
|
||||
cdef str full_hw_str = f'Azaion_{mac_address}_{cpu}_{gpu}'
|
||||
hw_hash = self.calc_hash(full_hw_str)
|
||||
return HardwareInfo(cpu, gpu, memory, mac_address, hw_hash)
|
||||
|
||||
cdef str calc_hash(self, str s):
|
||||
str_bytes = s.encode('utf-8')
|
||||
hash_bytes = sha384(str_bytes).digest()
|
||||
cdef str h = base64.b64encode(hash_bytes).decode('utf-8')
|
||||
return h
|
||||
Reference in New Issue
Block a user