From abeb34454c5dd7662b96ff6eadc3a9a88f215b6a Mon Sep 17 00:00:00 2001 From: grdddj Date: Thu, 30 Mar 2023 12:48:34 +0200 Subject: [PATCH] feat(python): use DebugLinkRecordScreen.refresh_index when restarting emulator During each restart, Emulator will transfer the original screen-recording directory to the new emulator/client and will change the refresh_index so that older screenshots are not being overwritten by new ones. [no changelog] --- python/src/trezorlib/_internal/emulator.py | 12 +++++++++++- python/src/trezorlib/debuglink.py | 15 +++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/python/src/trezorlib/_internal/emulator.py b/python/src/trezorlib/_internal/emulator.py index f1c3ed9fd..4f6d56f8e 100644 --- a/python/src/trezorlib/_internal/emulator.py +++ b/python/src/trezorlib/_internal/emulator.py @@ -82,6 +82,9 @@ class Emulator: self.auto_interact = auto_interact self.extra_args = list(extra_args) + # To save all screenshots properly in one directory between restarts + self.restart_amount = 0 + @property def client(self) -> TrezorClientDebugLink: """So that type-checkers do not see `client` as `Optional`. @@ -112,7 +115,7 @@ class Emulator: if transport._ping(): break if self.process.poll() is not None: - raise RuntimeError("Emulator proces died") + raise RuntimeError("Emulator process died") elapsed = time.monotonic() - start if elapsed >= timeout: @@ -200,8 +203,15 @@ class Emulator: self.process = None def restart(self) -> None: + # preserving the recording directory between restarts + self.restart_amount += 1 + prev_screenshot_dir = self.client.debug.screenshot_recording_dir self.stop() self.start() + if prev_screenshot_dir: + self.client.debug.start_recording( + prev_screenshot_dir, refresh_index=self.restart_amount + ) def __enter__(self) -> "Emulator": return self diff --git a/python/src/trezorlib/debuglink.py b/python/src/trezorlib/debuglink.py index d57e258f6..8a8841ab0 100644 --- a/python/src/trezorlib/debuglink.py +++ b/python/src/trezorlib/debuglink.py @@ -195,6 +195,9 @@ class DebugLink: # To be set by TrezorClientDebugLink (is not known during creation time) self.model: Optional[str] = None + # Where screenshots are being saved + self.screenshot_recording_dir: Optional[str] = None + # For T1 screenshotting functionality in DebugUI self.t1_take_screenshots = False self.t1_screenshot_directory: Optional[Path] = None @@ -411,16 +414,24 @@ class DebugLink: def reseed(self, value: int) -> protobuf.MessageType: return self._call(messages.DebugLinkReseedRandom(value=value)) - def start_recording(self, directory: str) -> None: + def start_recording( + self, directory: str, refresh_index: Optional[int] = None + ) -> None: + self.screenshot_recording_dir = directory # Different recording logic between TT and T1 if self.model == "T": - self._call(messages.DebugLinkRecordScreen(target_directory=directory)) + self._call( + messages.DebugLinkRecordScreen( + target_directory=directory, refresh_index=refresh_index + ) + ) else: self.t1_screenshot_directory = Path(directory) self.t1_screenshot_counter = 0 self.t1_take_screenshots = True def stop_recording(self) -> None: + self.screenshot_recording_dir = None # Different recording logic between TT and T1 if self.model == "T": self._call(messages.DebugLinkRecordScreen(target_directory=None))