From bead910ebd5b7b281be92332bb76448a6c65dbe0 Mon Sep 17 00:00:00 2001 From: Piotr Bartman-Szwarc Date: Wed, 31 Jul 2024 14:03:30 +0200 Subject: [PATCH] q-dev: comparison --- qubesadmin/device_protocol.py | 36 ++++++++++++++++++++++++++++++---- qubesadmin/devices.py | 4 ++-- qubesadmin/tests/devices.py | 10 +++++----- qubesadmin/tools/qvm_device.py | 2 +- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/qubesadmin/device_protocol.py b/qubesadmin/device_protocol.py index f5f21def..449d0f83 100644 --- a/qubesadmin/device_protocol.py +++ b/qubesadmin/device_protocol.py @@ -263,8 +263,8 @@ class DeviceCategory(Enum): Mouse = ("u03**02", "p0902**") Printer = ("u07****",) Scanner = ("p0903**",) - # Multimedia = Audio, Video, Displays etc. Microphone = ("m******",) + # Multimedia = Audio, Video, Displays etc. Multimedia = ("u01****", "u0e****", "u06****", "u10****", "p03****", "p04****") Wireless = ("ue0****", "p0d****") @@ -369,7 +369,7 @@ def __eq__(self, other): def __str__(self): if self.devclass == "block": - return "Block device" + return "Block Device" if self.devclass in ("usb", "pci"): # try subclass first as in `lspci` result = self._load_classes(self.devclass).get( @@ -458,6 +458,32 @@ def __init__( self.data = kwargs + def __hash__(self): + return hash(self.port)# self.self_identity)) + + def __eq__(self, other): + if isinstance(other, DeviceInfo): + return ( + self.port == other.port + # and self.self_identity == other.self_identity + ) + else: + return super().__lt__(other) + + def __lt__(self, other): + if isinstance(other, DeviceInfo): + # return (self.port, self.self_identity) < \ + # (other.port, other.self_identity) + return self.port < other.port + else: + return super().__lt__(other) + + def __repr__(self): + return f"{self.port!r}"#:{self.self_identity}" + + def __str__(self): + return f"{self.port}"#:{self.self_identity}" + @property def port(self) -> Port: """ @@ -558,8 +584,10 @@ def description(self) -> str: else: vendor = "unknown vendor" - main_interface = str(self.interfaces[0]) - return f"{main_interface}: {vendor} {prod}" + cat = self.interfaces[0].category.name + if cat == "Other": + cat = str(self.interfaces[0]) + return f"{cat}: {vendor} {prod}" @property def interfaces(self) -> List[DeviceInterface]: diff --git a/qubesadmin/devices.py b/qubesadmin/devices.py index a79019f1..5ead87dd 100644 --- a/qubesadmin/devices.py +++ b/qubesadmin/devices.py @@ -173,8 +173,8 @@ def get_assigned_devices( assignments_str = self._vm.qubesd_call( None, 'admin.vm.device.{}.Assigned'.format(self._class)).decode() for assignment_str in assignments_str.splitlines(): - device, _, untrusted_rest = assignment_str.partition(' ') - backend_domain_name, ident = device.split('+', 1) + port, _, untrusted_rest = assignment_str.partition(' ') + backend_domain_name, ident = port.split('+', 1) backend_domain = self._vm.app.domains.get_blind(backend_domain_name) assignment = DeviceAssignment.deserialize( diff --git a/qubesadmin/tests/devices.py b/qubesadmin/tests/devices.py index fbd1ff5b..0a4662e9 100644 --- a/qubesadmin/tests/devices.py +++ b/qubesadmin/tests/devices.py @@ -49,7 +49,7 @@ def test_000_available(self): self.assertEqual( dev.description, '?******: unknown vendor unknown test device') self.assertEqual(dev.data, {}) - self.assertEqual(str(dev), 'test-vm:dev1') + self.assertEqual(str(dev.port), 'test-vm:dev1') self.assertAllCalled() def test_001_available_desc(self): @@ -64,7 +64,7 @@ def test_001_available_desc(self): self.assertEqual(dev.ident, 'dev1') self.assertEqual(dev.description, '?******: itl test-device') self.assertEqual(dev.data, {}) - self.assertEqual(str(dev), 'test-vm:dev1') + self.assertEqual(str(dev.port), 'test-vm:dev1') def test_002_available_options(self): self.app.expected_calls[ @@ -78,7 +78,7 @@ def test_002_available_options(self): self.assertEqual(dev.ident, 'dev1') self.assertEqual(dev.description, '?******: itl test-device') self.assertEqual(dev.data, {'ro': 'True', 'other': '123'}) - self.assertEqual(str(dev), 'test-vm:dev1') + self.assertEqual(str(dev.port), 'test-vm:dev1') self.assertAllCalled() def test_010_getitem(self): @@ -91,7 +91,7 @@ def test_010_getitem(self): self.assertEqual(dev.ident, 'dev1') self.assertEqual(dev.description, '?******: itl test-device') self.assertEqual(dev.data, {}) - self.assertEqual(str(dev), 'test-vm:dev1') + self.assertEqual(str(dev.port), 'test-vm:dev1') self.assertAllCalled() def test_011_getitem_missing(self): @@ -105,7 +105,7 @@ def test_011_getitem_missing(self): self.assertEqual(dev.description, '?******: unknown vendor unknown test device') self.assertEqual(dev.data, {}) - self.assertEqual(str(dev), 'test-vm:dev2') + self.assertEqual(str(dev.port), 'test-vm:dev2') self.assertAllCalled() def test_020_attach(self): diff --git a/qubesadmin/tools/qvm_device.py b/qubesadmin/tools/qvm_device.py index e2cc82e3..1e48a0da 100644 --- a/qubesadmin/tools/qvm_device.py +++ b/qubesadmin/tools/qvm_device.py @@ -135,7 +135,7 @@ def _load_frontends_info(vm, dev, devclass): try: for assignment in vm.devices[devclass].get_dedicated_devices(): - if dev != assignment: + if dev != assignment.device: continue if assignment.options: yield '{!s} ({})'.format(