Skip to content

Commit

Permalink
q-dev: comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrbartman committed Oct 14, 2024
1 parent 55dbba9 commit bead910
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
36 changes: 32 additions & 4 deletions qubesadmin/device_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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****")
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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]:
Expand Down
4 changes: 2 additions & 2 deletions qubesadmin/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 5 additions & 5 deletions qubesadmin/tests/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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[
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion qubesadmin/tools/qvm_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit bead910

Please sign in to comment.