nixos/test-driver: apply ruff check suggestions

This commit is contained in:
Nick Cao
2024-11-22 09:16:03 -05:00
parent 9069a281a7
commit b25360a7e5
3 changed files with 48 additions and 45 deletions

View File

@@ -12,10 +12,11 @@ import sys
import tempfile
import threading
import time
from collections.abc import Iterable
from contextlib import _GeneratorContextManager, nullcontext
from pathlib import Path
from queue import Queue
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple
from typing import Any, Callable, Optional
from test_driver.logger import AbstractLogger
@@ -91,7 +92,7 @@ def make_command(args: list) -> str:
def _perform_ocr_on_screenshot(
screenshot_path: str, model_ids: Iterable[int]
) -> List[str]:
) -> list[str]:
if shutil.which("tesseract") is None:
raise Exception("OCR requested but enableOCR is false")
@@ -260,7 +261,7 @@ class Machine:
# Store last serial console lines for use
# of wait_for_console_text
last_lines: Queue = Queue()
callbacks: List[Callable]
callbacks: list[Callable]
def __repr__(self) -> str:
return f"<Machine '{self.name}'>"
@@ -273,7 +274,7 @@ class Machine:
logger: AbstractLogger,
name: str = "machine",
keep_vm_state: bool = False,
callbacks: Optional[List[Callable]] = None,
callbacks: Optional[list[Callable]] = None,
) -> None:
self.out_dir = out_dir
self.tmp_dir = tmp_dir
@@ -314,7 +315,7 @@ class Machine:
def log_serial(self, msg: str) -> None:
self.logger.log_serial(msg, self.name)
def nested(self, msg: str, attrs: Dict[str, str] = {}) -> _GeneratorContextManager:
def nested(self, msg: str, attrs: dict[str, str] = {}) -> _GeneratorContextManager:
my_attrs = {"machine": self.name}
my_attrs.update(attrs)
return self.logger.nested(msg, my_attrs)
@@ -373,7 +374,7 @@ class Machine:
):
retry(check_active, timeout)
def get_unit_info(self, unit: str, user: Optional[str] = None) -> Dict[str, str]:
def get_unit_info(self, unit: str, user: Optional[str] = None) -> dict[str, str]:
status, lines = self.systemctl(f'--no-pager show "{unit}"', user)
if status != 0:
raise Exception(
@@ -384,7 +385,7 @@ class Machine:
line_pattern = re.compile(r"^([^=]+)=(.*)$")
def tuple_from_line(line: str) -> Tuple[str, str]:
def tuple_from_line(line: str) -> tuple[str, str]:
match = line_pattern.match(line)
assert match is not None
return match[1], match[2]
@@ -424,7 +425,7 @@ class Machine:
assert match[1] == property, invalid_output_message
return match[2]
def systemctl(self, q: str, user: Optional[str] = None) -> Tuple[int, str]:
def systemctl(self, q: str, user: Optional[str] = None) -> tuple[int, str]:
"""
Runs `systemctl` commands with optional support for
`systemctl --user`
@@ -481,7 +482,7 @@ class Machine:
check_return: bool = True,
check_output: bool = True,
timeout: Optional[int] = 900,
) -> Tuple[int, str]:
) -> tuple[int, str]:
"""
Execute a shell command, returning a list `(status, stdout)`.
@@ -798,10 +799,10 @@ class Machine:
with self.nested(f"waiting for TCP port {port} on {addr} to be closed"):
retry(port_is_closed, timeout)
def start_job(self, jobname: str, user: Optional[str] = None) -> Tuple[int, str]:
def start_job(self, jobname: str, user: Optional[str] = None) -> tuple[int, str]:
return self.systemctl(f"start {jobname}", user)
def stop_job(self, jobname: str, user: Optional[str] = None) -> Tuple[int, str]:
def stop_job(self, jobname: str, user: Optional[str] = None) -> tuple[int, str]:
return self.systemctl(f"stop {jobname}", user)
def wait_for_job(self, jobname: str) -> None:
@@ -942,13 +943,13 @@ class Machine:
"""Debugging: Dump the contents of the TTY<n>"""
self.execute(f"fold -w 80 /dev/vcs{tty} | systemd-cat")
def _get_screen_text_variants(self, model_ids: Iterable[int]) -> List[str]:
def _get_screen_text_variants(self, model_ids: Iterable[int]) -> list[str]:
with tempfile.TemporaryDirectory() as tmpdir:
screenshot_path = os.path.join(tmpdir, "ppm")
self.send_monitor_command(f"screendump {screenshot_path}")
return _perform_ocr_on_screenshot(screenshot_path, model_ids)
def get_screen_text_variants(self) -> List[str]:
def get_screen_text_variants(self) -> list[str]:
"""
Return a list of different interpretations of what is currently
visible on the machine's screen using optical character
@@ -1168,7 +1169,7 @@ class Machine:
with self.nested("waiting for the X11 server"):
retry(check_x, timeout)
def get_window_names(self) -> List[str]:
def get_window_names(self) -> list[str]:
return self.succeed(
r"xwininfo -root -tree | sed 's/.*0x[0-9a-f]* \"\([^\"]*\)\".*/\1/; t; d'"
).splitlines()