Skip to content
This repository was archived by the owner on Jan 23, 2026. It is now read-only.

Commit 4cb91c5

Browse files
authored
Merge pull request #348 from jumpstarter-dev/uboot-even-better
UbootConsole: Reuse pexpect instance
2 parents 5b20349 + 176ff46 commit 4cb91c5

2 files changed

Lines changed: 48 additions & 36 deletions

File tree

packages/jumpstarter-driver-uboot/jumpstarter_driver_uboot/client.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from contextlib import contextmanager
12
from functools import cached_property
23

34
import pexpect
@@ -11,6 +12,7 @@ class UbootConsoleClient(CompositeClient):
1112
def prompt(self) -> str:
1213
return self.call("get_prompt")
1314

15+
@contextmanager
1416
def reboot_to_console(self) -> None:
1517
"""
1618
Reboot to U-Boot console
@@ -22,6 +24,7 @@ def reboot_to_console(self) -> None:
2224
self.power.cycle()
2325

2426
self.logger.info("Waiting for U-Boot prompt...")
27+
2528
with self.serial.pexpect() as p:
2629
for _ in range(100): # TODO: configurable retries
2730
try:
@@ -30,21 +33,30 @@ def reboot_to_console(self) -> None:
3033
except pexpect.TIMEOUT:
3134
continue
3235

33-
return
34-
35-
raise RuntimeError("Failed to get U-Boot prompt")
36-
37-
def run_command(self, cmd: str, timeout: int = 60) -> bytes:
38-
self.logger.info(f"Running command: {cmd}")
39-
with self.serial.pexpect() as p:
40-
p.sendline("")
41-
p.expect_exact(self.prompt, timeout=timeout)
42-
p.sendline(cmd)
43-
p.expect_exact(self.prompt, timeout=timeout)
44-
return p.before
36+
break
37+
else:
38+
raise RuntimeError("Failed to get U-Boot prompt")
39+
40+
self.p = p
41+
try:
42+
yield
43+
finally:
44+
delattr(self, "p")
45+
46+
def run_command(self, cmd: str, timeout: int = 60, *, _internal_log=True) -> bytes:
47+
if _internal_log:
48+
self.logger.info(f"Running command: {cmd}")
49+
if not hasattr(self, "p"):
50+
raise RuntimeError("Not in a reboot_to_console context")
51+
self.p.sendline("")
52+
self.p.expect_exact(self.prompt, timeout=timeout)
53+
self.p.sendline(cmd)
54+
self.p.expect_exact(self.prompt, timeout=timeout)
55+
return self.p.before
4556

4657
def run_command_checked(self, cmd: str, timeout: int = 60, check=True) -> list[str]:
47-
output = self.run_command("{}; echo $?".format(cmd))
58+
self.logger.info(f"Running command checked: {cmd}")
59+
output = self.run_command("{}; echo $?".format(cmd), _internal_log=False)
4860
parsed = output.strip().decode().splitlines()
4961

5062
if len(parsed) < 2:

packages/jumpstarter-driver-uboot/jumpstarter_driver_uboot/driver_test.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,33 +46,33 @@ def test_driver_uboot_console(uboot_image):
4646
root.qemu.flasher.flash(uboot_image, partition="bios")
4747

4848
uboot = root.uboot
49-
uboot.reboot_to_console()
5049

51-
assert uboot.run_command_checked("version") == [
52-
"U-Boot 2024.10 (Oct 11 2024 - 00:00:00 +0000)",
53-
"",
54-
]
50+
with uboot.reboot_to_console():
51+
assert uboot.run_command_checked("version") == [
52+
"U-Boot 2024.10 (Oct 11 2024 - 00:00:00 +0000)",
53+
"",
54+
]
5555

56-
print(uboot.setup_dhcp())
56+
print(uboot.setup_dhcp())
5757

58-
uboot.set_env_dict(
59-
{
60-
"foo": "bar",
61-
"baz": "qux",
62-
}
63-
)
58+
uboot.set_env_dict(
59+
{
60+
"foo": "bar",
61+
"baz": "qux",
62+
}
63+
)
6464

65-
assert uboot.get_env("foo") == "bar"
66-
assert uboot.get_env("baz") == "qux"
65+
assert uboot.get_env("foo") == "bar"
66+
assert uboot.get_env("baz") == "qux"
6767

68-
uboot.set_env_dict(
69-
{
70-
"foo": "qux",
71-
"baz": None,
72-
}
73-
)
68+
uboot.set_env_dict(
69+
{
70+
"foo": "qux",
71+
"baz": None,
72+
}
73+
)
7474

75-
assert uboot.get_env("foo") == "qux"
76-
assert uboot.get_env("baz") is None
75+
assert uboot.get_env("foo") == "qux"
76+
assert uboot.get_env("baz") is None
7777

78-
root.qemu.power.off()
78+
root.qemu.power.off()

0 commit comments

Comments
 (0)