Skip to content

Commit f83d6b6

Browse files
fepitrepiotrbartman
authored andcommitted
Use Port for device and make mypy happy
Related to work being done in QubesOS/qubes-core-admin#627
1 parent 6013161 commit f83d6b6

File tree

4 files changed

+56
-35
lines changed

4 files changed

+56
-35
lines changed

.gitlab-ci.yml

+13
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,16 @@ include:
1111
project: QubesOS/qubes-continuous-integration
1212
- file: /r4.2/gitlab-vm.yml
1313
project: QubesOS/qubes-continuous-integration
14+
15+
mypy:
16+
stage: checks
17+
image: fedora:40
18+
tags:
19+
- docker
20+
before_script:
21+
- sudo dnf install -y python3-mypy python3-pip
22+
script:
23+
- mypy --install-types --non-interactive --ignore-missing-imports --junit-xml mypy.xml qubesusbproxy
24+
artifacts:
25+
reports:
26+
junit: mypy.xml

qubesusbproxy/core3ext.py

+22-14
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class DescriptionOverrider:
5858
def description(self):
5959
return self.name
6060

61-
class DeviceInfo(DescriptionOverrider, LegacyDeviceInfo):
61+
class DeviceInfo(DescriptionOverrider, LegacyDeviceInfo): # type: ignore
6262
def __init__(self, port):
6363
# not supported options in legacy code
6464
self.safe_chars = self.safe_chars.replace(" ", "")
@@ -78,12 +78,12 @@ def frontend_domain(self):
7878
return self.attachment
7979

8080
@dataclasses.dataclass
81-
class Port:
81+
class Port: # type: ignore
8282
backend_domain: Any
8383
port_id: Any
8484
devclass: Any
8585

86-
class DeviceInterface:
86+
class DeviceInterface: # type: ignore
8787
# pylint: disable=too-few-public-methods
8888
pass
8989

@@ -105,20 +105,24 @@ def get_assigned_devices(devices):
105105

106106
class USBDevice(DeviceInfo):
107107
# pylint: disable=too-few-public-methods
108-
def __init__(self, backend_domain, port_id):
108+
def __init__(self, port: qubes.device_protocol.Port):
109+
if port.devclass != "usb":
110+
raise qubes.exc.QubesValueError(
111+
f"Incompatible device class for input port: {port.devclass}"
112+
)
113+
109114
# the superclass can restrict the allowed characters
110115
self.safe_chars = (
111116
string.ascii_letters + string.digits + string.punctuation + " "
112117
)
113-
port = Port(
114-
backend_domain=backend_domain, port_id=port_id, devclass="usb"
115-
)
118+
119+
# init parent class
116120
super().__init__(port)
117121

118-
self._qdb_ident = port_id.replace(".", "_")
122+
self._qdb_ident = port.port_id.replace(".", "_")
119123
self._qdb_path = "/qubes-usb-devices/" + self._qdb_ident
120-
self._vendor_id = None
121-
self._product_id = None
124+
self._vendor_id: Optional[str] = None
125+
self._product_id: Optional[str] = None
122126

123127
@property
124128
def vendor(self) -> str:
@@ -410,10 +414,12 @@ def _load_usb_known_devices() -> Dict[str, Dict[str, Tuple[str, str]]]:
410414
# C class class_name
411415
# subclass subclass_name <-- single tab
412416
# prog-if prog-if_name <-- two tabs
413-
result = {}
417+
result: Dict[str, Dict] = {}
414418
with open(
415419
HWDATA_PATH + "/usb.ids", encoding="utf-8", errors="ignore"
416420
) as usb_ids:
421+
vendor_id: Optional[str] = None
422+
vendor_name: Optional[str] = None
417423
for line in usb_ids.readlines():
418424
line = line.rstrip()
419425
if line.startswith("#"):
@@ -431,6 +437,8 @@ def _load_usb_known_devices() -> Dict[str, Dict[str, Tuple[str, str]]]:
431437
if line.startswith("\t"):
432438
# save vendor, device pair
433439
device_id, _, device_name = line[1:].split(" ", 2)
440+
if vendor_id is None or vendor_name is None:
441+
continue
434442
result[vendor_id][device_id] = vendor_name, device_name
435443
else:
436444
# new vendor
@@ -589,7 +597,7 @@ def on_device_list_usb(self, vm, event):
589597
vm.log.warning("Invalid USB device name detected")
590598
continue
591599
port_id = untrusted_qdb_ident.replace("_", ".")
592-
yield USBDevice(vm, port_id)
600+
yield USBDevice(Port(vm, port_id, "usb"))
593601

594602
@qubes.ext.handler("device-get:usb")
595603
def on_device_get_usb(self, vm, event, port_id):
@@ -600,12 +608,12 @@ def on_device_get_usb(self, vm, event, port_id):
600608
if vm.untrusted_qdb.list(
601609
"/qubes-usb-devices/" + port_id.replace(".", "_")
602610
):
603-
yield USBDevice(vm, port_id)
611+
yield USBDevice(Port(vm, port_id, "usb"))
604612

605613
@staticmethod
606614
def get_all_devices(app):
607615
for vm in app.domains:
608-
if not vm.is_running():
616+
if not vm.is_running() or not hasattr(vm, "devices"):
609617
continue
610618

611619
for dev in vm.devices["usb"]:

qubesusbproxy/tests.py

+19-19
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ def added_assign_setup(attachment=None):
884884
def test_010_on_qdb_change_multiple_assignments_including_full(self):
885885
back, front = self.added_assign_setup()
886886

887-
exp_dev = qubesusbproxy.core3ext.USBDevice(back, "1-1")
887+
exp_dev = qubesusbproxy.core3ext.USBDevice(Port(back, "1-1", "usb"))
888888
full_assig = DeviceAssignment(
889889
VirtualDevice(exp_dev.port, exp_dev.device_id),
890890
mode="auto-attach",
@@ -907,7 +907,7 @@ def test_010_on_qdb_change_multiple_assignments_including_full(self):
907907
front.devices["usb"]._assigned.append(port_assign)
908908
front.devices["usb"]._assigned.append(full_assig)
909909
back.devices["usb"]._exposed.append(
910-
qubesusbproxy.core3ext.USBDevice(back, "1-1")
910+
qubesusbproxy.core3ext.USBDevice(Port(back, "1-1", "usb"))
911911
)
912912

913913
self.ext.attach_and_notify = Mock()
@@ -922,7 +922,7 @@ def test_010_on_qdb_change_multiple_assignments_including_full(self):
922922
def test_011_on_qdb_change_multiple_assignments_port_vs_dev(self):
923923
back, front = self.added_assign_setup()
924924

925-
exp_dev = qubesusbproxy.core3ext.USBDevice(back, "1-1")
925+
exp_dev = qubesusbproxy.core3ext.USBDevice(Port(back, "1-1", "usb"))
926926
port_assign = DeviceAssignment(
927927
VirtualDevice(exp_dev.port, "*"),
928928
mode="auto-attach",
@@ -939,7 +939,7 @@ def test_011_on_qdb_change_multiple_assignments_port_vs_dev(self):
939939
front.devices["usb"]._assigned.append(dev_assign)
940940
front.devices["usb"]._assigned.append(port_assign)
941941
back.devices["usb"]._exposed.append(
942-
qubesusbproxy.core3ext.USBDevice(back, "1-1")
942+
qubesusbproxy.core3ext.USBDevice(Port(back, "1-1", "usb"))
943943
)
944944

945945
self.ext.attach_and_notify = Mock()
@@ -954,7 +954,7 @@ def test_011_on_qdb_change_multiple_assignments_port_vs_dev(self):
954954
def test_012_on_qdb_change_multiple_assignments_dev(self):
955955
back, front = self.added_assign_setup()
956956

957-
exp_dev = qubesusbproxy.core3ext.USBDevice(back, "1-1")
957+
exp_dev = qubesusbproxy.core3ext.USBDevice(Port(back, "1-1", "usb"))
958958
port_assign = DeviceAssignment(
959959
VirtualDevice(Port(exp_dev.backend_domain, "1-2", "usb"), "*"),
960960
mode="auto-attach",
@@ -971,10 +971,10 @@ def test_012_on_qdb_change_multiple_assignments_dev(self):
971971
front.devices["usb"]._assigned.append(dev_assign)
972972
front.devices["usb"]._assigned.append(port_assign)
973973
back.devices["usb"]._exposed.append(
974-
qubesusbproxy.core3ext.USBDevice(back, "1-1")
974+
qubesusbproxy.core3ext.USBDevice(Port(back, "1-1", "usb"))
975975
)
976976
back.devices["usb"]._exposed.append(
977-
qubesusbproxy.core3ext.USBDevice(back, "1-2")
977+
qubesusbproxy.core3ext.USBDevice(Port(back, "1-2", "usb"))
978978
)
979979

980980
self.ext.attach_and_notify = Mock()
@@ -989,7 +989,7 @@ def test_012_on_qdb_change_multiple_assignments_dev(self):
989989
def test_013_on_qdb_change_two_fronts(self):
990990
back, front = self.added_assign_setup()
991991

992-
exp_dev = qubesusbproxy.core3ext.USBDevice(back, "1-1")
992+
exp_dev = qubesusbproxy.core3ext.USBDevice(Port(back, "1-1", "usb"))
993993
assmnt = DeviceAssignment(exp_dev, mode="auto-attach")
994994

995995
front.devices["usb"]._assigned.append(assmnt)
@@ -1010,7 +1010,7 @@ def test_013_on_qdb_change_two_fronts(self):
10101010
def test_014_failed_confirmation(self, socket):
10111011
back, front = self.added_assign_setup()
10121012

1013-
exp_dev = qubesusbproxy.core3ext.USBDevice(back, "1-1")
1013+
exp_dev = qubesusbproxy.core3ext.USBDevice(Port(back, "1-1", "usb"))
10141014
assmnt = DeviceAssignment(exp_dev, mode="auto-attach")
10151015

10161016
front.devices["usb"]._assigned.append(assmnt)
@@ -1034,7 +1034,7 @@ def test_014_failed_confirmation(self, socket):
10341034
def test_015_successful_confirmation(self, socket):
10351035
back, front = self.added_assign_setup()
10361036

1037-
exp_dev = qubesusbproxy.core3ext.USBDevice(back, "1-1")
1037+
exp_dev = qubesusbproxy.core3ext.USBDevice(Port(back, "1-1", "usb"))
10381038
assmnt = DeviceAssignment(exp_dev, mode="auto-attach")
10391039

10401040
front.devices["usb"]._assigned.append(assmnt)
@@ -1055,7 +1055,7 @@ def test_015_successful_confirmation(self, socket):
10551055
def test_016_on_qdb_change_ask(self):
10561056
back, front = self.added_assign_setup()
10571057

1058-
exp_dev = qubesusbproxy.core3ext.USBDevice(back, "1-1")
1058+
exp_dev = qubesusbproxy.core3ext.USBDevice(Port(back, "1-1", "usb"))
10591059
assmnt = DeviceAssignment(exp_dev, mode="ask-to-attach")
10601060

10611061
front.devices["usb"]._assigned.append(assmnt)
@@ -1070,7 +1070,7 @@ def test_016_on_qdb_change_ask(self):
10701070
def test_020_on_startup_multiple_assignments_including_full(self):
10711071
back, front = self.added_assign_setup()
10721072

1073-
exp_dev = qubesusbproxy.core3ext.USBDevice(back, "1-1")
1073+
exp_dev = qubesusbproxy.core3ext.USBDevice(Port(back, "1-1", "usb"))
10741074
full_assig = DeviceAssignment(
10751075
VirtualDevice(exp_dev.port, exp_dev.device_id),
10761076
mode="auto-attach",
@@ -1093,7 +1093,7 @@ def test_020_on_startup_multiple_assignments_including_full(self):
10931093
front.devices["usb"]._assigned.append(port_assign)
10941094
front.devices["usb"]._assigned.append(full_assig)
10951095
back.devices["usb"]._exposed.append(
1096-
qubesusbproxy.core3ext.USBDevice(back, "1-1")
1096+
qubesusbproxy.core3ext.USBDevice(Port(back, "1-1", "usb"))
10971097
)
10981098

10991099
self.ext.attach_and_notify = AsyncMock()
@@ -1106,7 +1106,7 @@ def test_020_on_startup_multiple_assignments_including_full(self):
11061106
def test_021_on_startup_multiple_assignments_port_vs_dev(self):
11071107
back, front = self.added_assign_setup()
11081108

1109-
exp_dev = qubesusbproxy.core3ext.USBDevice(back, "1-1")
1109+
exp_dev = qubesusbproxy.core3ext.USBDevice(Port(back, "1-1", "usb"))
11101110
port_assign = DeviceAssignment(
11111111
VirtualDevice(exp_dev.port, "*"),
11121112
mode="auto-attach",
@@ -1123,7 +1123,7 @@ def test_021_on_startup_multiple_assignments_port_vs_dev(self):
11231123
front.devices["usb"]._assigned.append(dev_assign)
11241124
front.devices["usb"]._assigned.append(port_assign)
11251125
back.devices["usb"]._exposed.append(
1126-
qubesusbproxy.core3ext.USBDevice(back, "1-1")
1126+
qubesusbproxy.core3ext.USBDevice(Port(back, "1-1", "usb"))
11271127
)
11281128

11291129
loop = asyncio.get_event_loop()
@@ -1136,7 +1136,7 @@ def test_021_on_startup_multiple_assignments_port_vs_dev(self):
11361136
def test_022_on_startup_multiple_assignments_dev(self):
11371137
back, front = self.added_assign_setup()
11381138

1139-
exp_dev = qubesusbproxy.core3ext.USBDevice(back, "1-1")
1139+
exp_dev = qubesusbproxy.core3ext.USBDevice(Port(back, "1-1", "usb"))
11401140
port_assign = DeviceAssignment(
11411141
VirtualDevice(Port(exp_dev.backend_domain, "1-2", "usb"), "*"),
11421142
mode="auto-attach",
@@ -1153,10 +1153,10 @@ def test_022_on_startup_multiple_assignments_dev(self):
11531153
front.devices["usb"]._assigned.append(dev_assign)
11541154
front.devices["usb"]._assigned.append(port_assign)
11551155
back.devices["usb"]._exposed.append(
1156-
qubesusbproxy.core3ext.USBDevice(back, "1-1")
1156+
qubesusbproxy.core3ext.USBDevice(Port(back, "1-1", "usb"))
11571157
)
11581158
back.devices["usb"]._exposed.append(
1159-
qubesusbproxy.core3ext.USBDevice(back, "1-2")
1159+
qubesusbproxy.core3ext.USBDevice(Port(back, "1-2", "usb"))
11601160
)
11611161

11621162
self.ext.attach_and_notify = AsyncMock()
@@ -1169,7 +1169,7 @@ def test_022_on_startup_multiple_assignments_dev(self):
11691169
def test_023_on_startup_already_attached(self):
11701170
back, front = self.added_assign_setup(attachment="sys-usb")
11711171

1172-
exp_dev = qubesusbproxy.core3ext.USBDevice(back, "1-1")
1172+
exp_dev = qubesusbproxy.core3ext.USBDevice(Port(back, "1-1", "usb"))
11731173
assmnt = DeviceAssignment(
11741174
VirtualDevice(exp_dev.port, exp_dev.device_id), mode="auto-attach"
11751175
)

qubesusbproxy/utils.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
import qubes
2525

26-
from typing import Type
26+
from typing import Type, Dict, Any
2727

2828
from qubes import device_protocol
2929
from qubes.device_protocol import VirtualDevice
@@ -75,7 +75,7 @@ def device_list_change(
7575

7676
ext.devices_cache[vm.name] = current_devices
7777

78-
to_attach = {}
78+
to_attach: Dict[str, Dict] = {}
7979
for front_vm in vm.app.domains:
8080
if not front_vm.is_running():
8181
continue

0 commit comments

Comments
 (0)