Skip to content

Commit

Permalink
[GPU] Parses lspci to avoid classing 3D NAND devices as GPUs.
Browse files Browse the repository at this point in the history
Closes #149.
  • Loading branch information
ingrinder authored and HorlogeSkynet committed Apr 4, 2024
1 parent 0a970c0 commit d5fc44d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
10 changes: 6 additions & 4 deletions archey/entries/gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import platform
import re
from shlex import split
from subprocess import DEVNULL, CalledProcessError, check_output
from typing import List

Expand Down Expand Up @@ -31,7 +32,7 @@ def __init__(self, *args, **kwargs):
def _parse_lspci_output() -> List[str]:
"""Based on `lspci` output, return a list of video controllers names"""
try:
lspci_output = check_output("lspci", universal_newlines=True).splitlines()
lspci_output = check_output(["lspci", "-m"], universal_newlines=True).splitlines()
except (FileNotFoundError, CalledProcessError):
return []

Expand All @@ -40,10 +41,11 @@ def _parse_lspci_output() -> List[str]:
# We'll be looking for specific video controllers (in the below keys order).
for video_key in ("3D", "VGA", "Display"):
for pci_device in lspci_output:
# If a controller type match...
if video_key in pci_device:
pci_class, pci_vendor, pci_device = split(pci_device)[1:4]
# If a controller type matches the class...
if video_key in pci_class:
# ... adds its name to the final list.
gpus_list.append(pci_device.partition(": ")[2])
gpus_list.append(f"{pci_vendor} {pci_device}")

return gpus_list

Expand Down
37 changes: 23 additions & 14 deletions archey/test/entries/test_archey_gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,43 @@ class TestGPUEntry(unittest.TestCase, CustomAssertions):
side_effect=[
FileNotFoundError(),
"""\
XX:YY.H IDE interface: IIIIIIIIIIIIIIII
XX:YY.H SMBus: BBBBBBBBBBBBBBBB
XX:YY.H VGA compatible controller: GPU-MODEL-NAME
XX:YY.H Audio device: DDDDDDDDDDDDDDDD
XX:YY.H "IDE interface" "Manufacturer" "IIIIIIIIIIIIIIII"
XX:YY.H "SMBus" "Manufacturer" "BBBBBBBBBBBBBBBB"
XX:YY.H "VGA compatible controller" "GPU-Manufacturer" "GPU-MODEL-NAME"
XX:YY.H "Audio device" "Manufacturer" "DDDDDDDDDDDDDDDD"
""",
"""\
XX:YY.H IDE interface: IIIIIIIIIIIIIIII
XX:YY.H SMBus: BBBBBBBBBBBBBBBB
XX:YY.H VGA compatible controller: GPU-MODEL-NAME
XX:YY.H Display controller: ANOTHER-MATCHING-VIDEO-CONTROLLER
XX:YY.H Audio device: DDDDDDDDDDDDDDDD
XX:YY.H 3D controller: 3D GPU-MODEL-NAME TAKES ADVANTAGE
XX:YY.H "IDE interface" "Manufacturer" "IIIIIIIIIIIIIIII"
XX:YY.H "SMBus" "Manufacturer" "BBBBBBBBBBBBBBBB"
XX:YY.H "VGA compatible controller" "GPU-Manufacturer" "GPU-MODEL-NAME"
XX:YY.H "Display controller" "Another-GPU-Manufacturer" "ANOTHER-MATCHING-VIDEO-CONTROLLER"
XX:YY.H "Audio device" "Manufacturer" "DDDDDDDDDDDDDDDD"
XX:YY.H "3D controller" "3D-Manufacturer" "3D GPU-MODEL-NAME TAKES ADVANTAGE"
""",
"""\
XX:YY.H "IDE interface" "Manufacturer" "IIIIIIIIIIIIIIII"
XX:YY.H "SMBus" "Manufacturer" "BBBBBBBBBBBBBBBB"
XX:YY.H "VGA compatible controller" "GPU-Manufacturer" "GPU-MODEL-NAME"
XX:YY.H "Audio device" "Manufacturer" "DDDDDDDDDDDDDDDD"
XX:YY.H "Non-Volatile memory controller" "Sandisk Corp" "SanDisk Ultra 3D / WD Blue SN570 NVMe SSD"
""",
],
)
def test_parse_lspci_output(self, _):
"""Check `_parse_lspci_output` behavior"""
# pylint: disable=protected-access
self.assertListEmpty(GPU._parse_lspci_output())
self.assertListEqual(GPU._parse_lspci_output(), ["GPU-MODEL-NAME"])
self.assertListEqual(GPU._parse_lspci_output(), ["GPU-Manufacturer GPU-MODEL-NAME"])
self.assertListEqual(
GPU._parse_lspci_output(),
[
"3D GPU-MODEL-NAME TAKES ADVANTAGE",
"GPU-MODEL-NAME",
"ANOTHER-MATCHING-VIDEO-CONTROLLER",
"3D-Manufacturer 3D GPU-MODEL-NAME TAKES ADVANTAGE",
"GPU-Manufacturer GPU-MODEL-NAME",
"Another-GPU-Manufacturer ANOTHER-MATCHING-VIDEO-CONTROLLER",
],
)
# Ensure 3D nand flash is ignored; see issue #149
self.assertListEqual(GPU._parse_lspci_output(), ["GPU-Manufacturer GPU-MODEL-NAME"])
# pylint: enable=protected-access

@patch(
Expand Down

0 comments on commit d5fc44d

Please sign in to comment.