|
import subprocess |
|
from json import dumps |
|
|
|
all_devices = subprocess.check_output(["lspci"], text=True).strip().split("\n") |
|
print("-" * 20) |
|
|
|
gpu_count = 0 |
|
gpu_list = list() |
|
for device in all_devices: |
|
if "VGA compatible controller" in device or "3D controller" in device: |
|
try: |
|
pci_id = device.split()[0] |
|
|
|
# Get detailed information for the GPU device |
|
detail_output = ( |
|
subprocess.check_output(["lspci", "-v", "-s", pci_id], text=True) |
|
.strip() |
|
.split("\n") |
|
) |
|
except subprocess.CalledProcessError: |
|
continue |
|
|
|
gpu_element = dict() |
|
|
|
vendor = "Unknown" |
|
product_name = "Unknown" |
|
|
|
# Get vendor and product name from the initial lspci line |
|
if "NVIDIA" in device: |
|
vendor = "NVIDIA" |
|
product_name = " ".join(device.split()[2:]) |
|
elif "AMD" in device or "Advanced Micro Devices" in device: |
|
vendor = "AMD" |
|
product_name = " ".join(device.split()[2:]) |
|
elif "Intel" in device: |
|
vendor = "Intel" |
|
product_name = device.split(":")[-1].strip() |
|
else: |
|
product_name = " ".join(device.split()[1:]) |
|
|
|
gpu_element["product"] = product_name |
|
gpu_element["vendor"] = vendor |
|
|
|
businfo = f"pci@{pci_id}" |
|
|
|
detail_info = dict() |
|
for detail_line in detail_output: |
|
if detail_line.strip() == "": |
|
continue |
|
try: |
|
key, value = detail_line.split(":", 1) |
|
detail_info[key.strip()] = value.strip() |
|
except ValueError: |
|
# Handle lines that do not contain key-value pairs |
|
detail_info["Info"] = ( |
|
gpu_element.get("Info", "") + detail_line.strip() + " " |
|
) |
|
|
|
# Adjusting for iGPU / dedicated GPU |
|
if "DeviceName" in detail_info: |
|
device_class = detail_info["DeviceName"] |
|
else: |
|
device_class = "dedicated" |
|
|
|
configuration = { |
|
"driver": detail_info["Kernel driver in use"], |
|
} |
|
gpu_element = { |
|
"id": f"GPU{gpu_count+1}", |
|
"class": device_class, |
|
"description": detail_info["Subsystem"], |
|
"product": product_name, |
|
"vendor": vendor, |
|
"physid": pci_id, |
|
"businfo": businfo, |
|
"configuration": configuration, |
|
} |
|
|
|
gpu_list.append(gpu_element) |
|
gpu_count += 1 |
|
|
|
|
|
print(f"Total GPUs found: {gpu_count}") |
|
print(dumps(gpu_list, indent=4)) |
this looked helpful.
Sorry if you already had it......