From 9caf540e16efea36afffd027bc1399b92068c85d Mon Sep 17 00:00:00 2001 From: obrusvit Date: Thu, 5 Dec 2024 22:48:28 +0100 Subject: [PATCH] refactor(core): remove trezorui2 module - also remove "check" scripts which seem outdated [no changelog] --- core/embed/projects/firmware/mpconfigport.h | 1 - core/embed/projects/unix/mpconfigport.h | 1 - core/embed/rust/librust.h | 1 - core/embed/rust/librust_qstr.h | 1 - .../embed/rust/src/ui/model_mercury/layout.rs | 11 +- core/embed/rust/src/ui/model_tr/layout.rs | 11 +- core/embed/rust/src/ui/model_tt/layout.rs | 10 +- core/embed/upymod/rustmods.c | 4 - core/mocks/generated/trezorui2.pyi | 7 - core/src/trezor/ui/__init__.py | 1 - .../src/trezor/ui/layouts/mercury/__init__.py | 1 - .../src/trezor/ui/layouts/mercury/recovery.py | 1 - core/src/trezor/ui/layouts/tr/__init__.py | 1 - core/src/trezor/ui/layouts/tr/recovery.py | 1 - core/src/trezor/ui/layouts/tt/__init__.py | 1 - core/src/trezor/ui/layouts/tt/recovery.py | 1 - core/tools/README.md | 9 - core/tools/rust_api_check.py | 246 ------------------ core/tools/rust_api_models_unification.py | 85 ------ 19 files changed, 3 insertions(+), 391 deletions(-) delete mode 100644 core/mocks/generated/trezorui2.pyi delete mode 100755 core/tools/rust_api_check.py delete mode 100755 core/tools/rust_api_models_unification.py diff --git a/core/embed/projects/firmware/mpconfigport.h b/core/embed/projects/firmware/mpconfigport.h index fedc2d4e04..89b4a67233 100644 --- a/core/embed/projects/firmware/mpconfigport.h +++ b/core/embed/projects/firmware/mpconfigport.h @@ -164,7 +164,6 @@ #define MICROPY_PY_TREZORUTILS (1) #define MICROPY_PY_TREZORPROTO (1) #define MICROPY_PY_TREZORTRANSLATE (1) -#define MICROPY_PY_TREZORUI2 (1) #define MICROPY_PY_TREZORUI_API (1) #ifdef SYSTEM_VIEW diff --git a/core/embed/projects/unix/mpconfigport.h b/core/embed/projects/unix/mpconfigport.h index a1cd4c600e..a2b7463946 100644 --- a/core/embed/projects/unix/mpconfigport.h +++ b/core/embed/projects/unix/mpconfigport.h @@ -213,7 +213,6 @@ extern const struct _mp_print_t mp_stderr_print; #define MICROPY_PY_TREZORUTILS (1) #define MICROPY_PY_TREZORPROTO (1) #define MICROPY_PY_TREZORTRANSLATE (1) -#define MICROPY_PY_TREZORUI2 (1) #define MICROPY_PY_TREZORUI_API (1) #define MP_STATE_PORT MP_STATE_VM diff --git a/core/embed/rust/librust.h b/core/embed/rust/librust.h index d6432e7803..2ad5eb0db9 100644 --- a/core/embed/rust/librust.h +++ b/core/embed/rust/librust.h @@ -8,7 +8,6 @@ mp_obj_t protobuf_debug_msg_def_type(); #endif extern mp_obj_module_t mp_module_trezorproto; -extern mp_obj_module_t mp_module_trezorui2; extern mp_obj_module_t mp_module_trezorui_api; extern mp_obj_module_t mp_module_trezortranslate; diff --git a/core/embed/rust/librust_qstr.h b/core/embed/rust/librust_qstr.h index 6a83449f47..8f3073a319 100644 --- a/core/embed/rust/librust_qstr.h +++ b/core/embed/rust/librust_qstr.h @@ -683,7 +683,6 @@ static void _librust_qstrs(void) { MP_QSTR_touch_event; MP_QSTR_trace; MP_QSTR_trezorproto; - MP_QSTR_trezorui2; MP_QSTR_trezorui_api; MP_QSTR_tutorial; MP_QSTR_tutorial__continue; diff --git a/core/embed/rust/src/ui/model_mercury/layout.rs b/core/embed/rust/src/ui/model_mercury/layout.rs index 3b2069fd3e..25d7f045ab 100644 --- a/core/embed/rust/src/ui/model_mercury/layout.rs +++ b/core/embed/rust/src/ui/model_mercury/layout.rs @@ -8,7 +8,7 @@ use super::component::{ }; use crate::{ error::Error, - micropython::{macros::obj_module, module::Module, obj::Obj, qstr::Qstr}, + micropython::obj::Obj, ui::{ component::{ text::paragraphs::{ParagraphSource, Paragraphs}, @@ -184,12 +184,3 @@ where unreachable!(); } } - -#[no_mangle] -pub static mp_module_trezorui2: Module = obj_module! { - /// from trezor import utils - /// from trezorui_api import * - /// - Qstr::MP_QSTR___name__ => Qstr::MP_QSTR_trezorui2.to_obj(), - -}; diff --git a/core/embed/rust/src/ui/model_tr/layout.rs b/core/embed/rust/src/ui/model_tr/layout.rs index 26c60ea783..fb620850eb 100644 --- a/core/embed/rust/src/ui/model_tr/layout.rs +++ b/core/embed/rust/src/ui/model_tr/layout.rs @@ -7,7 +7,7 @@ use super::component::{ }; use crate::{ error::Error, - micropython::{macros::obj_module, map::Map, module::Module, obj::Obj, qstr::Qstr}, + micropython::obj::Obj, ui::{ component::{ base::Component, @@ -200,12 +200,3 @@ impl ComponentMsgObj for super::component::bl_confirm::Confirm<'_> { } } } - -#[no_mangle] -pub static mp_module_trezorui2: Module = obj_module! { - /// from trezor import utils - /// from trezorui_api import * - /// - Qstr::MP_QSTR___name__ => Qstr::MP_QSTR_trezorui2.to_obj(), - -}; diff --git a/core/embed/rust/src/ui/model_tt/layout.rs b/core/embed/rust/src/ui/model_tt/layout.rs index a6e8681db2..0d814170aa 100644 --- a/core/embed/rust/src/ui/model_tt/layout.rs +++ b/core/embed/rust/src/ui/model_tt/layout.rs @@ -9,7 +9,7 @@ use super::component::{ }; use crate::{ error::Error, - micropython::{macros::obj_module, map::Map, module::Module, obj::Obj, qstr::Qstr, util}, + micropython::obj::Obj, strutil::TString, ui::{ component::{ @@ -284,11 +284,3 @@ impl ComponentMsgObj for super::component::bl_confirm::Confirm<'_> { } } } - -#[no_mangle] -pub static mp_module_trezorui2: Module = obj_module! { - /// from trezor import utils - /// from trezorui_api import * - /// - Qstr::MP_QSTR___name__ => Qstr::MP_QSTR_trezorui2.to_obj(), -}; diff --git a/core/embed/upymod/rustmods.c b/core/embed/upymod/rustmods.c index 939dd05558..4159cf9e57 100644 --- a/core/embed/upymod/rustmods.c +++ b/core/embed/upymod/rustmods.c @@ -20,10 +20,6 @@ #include "librust.h" #include "py/runtime.h" -#if MICROPY_PY_TREZORUI2 -MP_REGISTER_MODULE(MP_QSTR_trezorui2, mp_module_trezorui2); -#endif - #if MICROPY_PY_TREZORUI_API MP_REGISTER_MODULE(MP_QSTR_trezorui_api, mp_module_trezorui_api); #endif diff --git a/core/mocks/generated/trezorui2.pyi b/core/mocks/generated/trezorui2.pyi deleted file mode 100644 index acfe35b970..0000000000 --- a/core/mocks/generated/trezorui2.pyi +++ /dev/null @@ -1,7 +0,0 @@ -from typing import * -from trezor import utils -from trezorui_api import * -from trezor import utils -from trezorui_api import * -from trezor import utils -from trezorui_api import * diff --git a/core/src/trezor/ui/__init__.py b/core/src/trezor/ui/__init__.py index 5264b9d521..2bd3c3e912 100644 --- a/core/src/trezor/ui/__init__.py +++ b/core/src/trezor/ui/__init__.py @@ -4,7 +4,6 @@ from micropython import const from trezorui import Display from typing import TYPE_CHECKING -import trezorui2 from trezor import io, log, loop, utils, wire, workflow from trezor.messages import ButtonAck, ButtonRequest from trezor.wire import context diff --git a/core/src/trezor/ui/layouts/mercury/__init__.py b/core/src/trezor/ui/layouts/mercury/__init__.py index 94a035a1ee..54b1e49b26 100644 --- a/core/src/trezor/ui/layouts/mercury/__init__.py +++ b/core/src/trezor/ui/layouts/mercury/__init__.py @@ -1,6 +1,5 @@ from typing import TYPE_CHECKING -import trezorui2 import trezorui_api from trezor import TR, ui, utils from trezor.enums import ButtonRequestType diff --git a/core/src/trezor/ui/layouts/mercury/recovery.py b/core/src/trezor/ui/layouts/mercury/recovery.py index 21e1f1736b..371a635d03 100644 --- a/core/src/trezor/ui/layouts/mercury/recovery.py +++ b/core/src/trezor/ui/layouts/mercury/recovery.py @@ -1,6 +1,5 @@ from typing import TYPE_CHECKING -import trezorui2 import trezorui_api from trezor import TR from trezor.enums import ButtonRequestType, RecoveryType diff --git a/core/src/trezor/ui/layouts/tr/__init__.py b/core/src/trezor/ui/layouts/tr/__init__.py index 46ae05c742..4ee60928a8 100644 --- a/core/src/trezor/ui/layouts/tr/__init__.py +++ b/core/src/trezor/ui/layouts/tr/__init__.py @@ -1,6 +1,5 @@ from typing import TYPE_CHECKING -import trezorui2 import trezorui_api from trezor import TR, ui, utils from trezor.enums import ButtonRequestType diff --git a/core/src/trezor/ui/layouts/tr/recovery.py b/core/src/trezor/ui/layouts/tr/recovery.py index 36496b914e..00cbb04fc2 100644 --- a/core/src/trezor/ui/layouts/tr/recovery.py +++ b/core/src/trezor/ui/layouts/tr/recovery.py @@ -1,6 +1,5 @@ from typing import TYPE_CHECKING -import trezorui2 import trezorui_api from trezor import TR, ui from trezor.enums import ButtonRequestType, RecoveryType diff --git a/core/src/trezor/ui/layouts/tt/__init__.py b/core/src/trezor/ui/layouts/tt/__init__.py index 0b0b013ffc..bb3558f330 100644 --- a/core/src/trezor/ui/layouts/tt/__init__.py +++ b/core/src/trezor/ui/layouts/tt/__init__.py @@ -1,6 +1,5 @@ from typing import TYPE_CHECKING -import trezorui2 import trezorui_api from trezor import TR, ui, utils from trezor.enums import ButtonRequestType diff --git a/core/src/trezor/ui/layouts/tt/recovery.py b/core/src/trezor/ui/layouts/tt/recovery.py index 8f19c1d49d..a2528c6082 100644 --- a/core/src/trezor/ui/layouts/tt/recovery.py +++ b/core/src/trezor/ui/layouts/tt/recovery.py @@ -1,6 +1,5 @@ from typing import TYPE_CHECKING -import trezorui2 import trezorui_api from trezor import TR, ui from trezor.enums import ButtonRequestType diff --git a/core/tools/README.md b/core/tools/README.md index be1f263054..dbb998813a 100644 --- a/core/tools/README.md +++ b/core/tools/README.md @@ -119,12 +119,3 @@ Generate a CMakeLists.txt file for the core. ### `provision_device.py` Run the provisioning flow on a prodtest firmware, against a staging provisioning server. - -### `rust_api_checks.py` - -Check that the Rust UI API is consistent with the Python UI API. - -### `rust_api_models_unification.py` - -Checks the consistency of the Rust API between models. - diff --git a/core/tools/rust_api_check.py b/core/tools/rust_api_check.py deleted file mode 100755 index 072a9baa86..0000000000 --- a/core/tools/rust_api_check.py +++ /dev/null @@ -1,246 +0,0 @@ -#!/usr/bin/env python3 -""" -Compare the mock python API with the real Rust one. -""" -from __future__ import annotations - -import re -from pathlib import Path - -HERE = Path(__file__).parent -CORE_DIR = HERE.parent -TT_FILE = CORE_DIR / "embed/rust/src/ui/model_tt/layout.rs" -TR_FILE = CORE_DIR / "embed/rust/src/ui/model_tr/layout.rs" -ALL_FILES = [ - TT_FILE, - TR_FILE, -] -# These are functions not defined directly in layout.rs -RUST_EXTERNAL_FUNCTIONS = [ - "disable_animation", - "jpeg_info", - "jpeg_test", -] -# These functions do not directly proccess upy arguments -RUST_DELEGATING_FUNCTIONS = [ - "new_show_success", - "new_show_warning", - "new_show_error", - "new_show_info", -] - - -def _get_all_functions_with_definitions(file: Path) -> dict[str, str]: - all_functions: dict[str, str] = {} - - with open(file, "r") as f: - lines = f.readlines() - - func_name: str | None = None - definition_lines: list[str] = [] - for line in lines: - # Function name - func_match = re.search(r'^extern "C" fn (\w+)', line) - if func_match: - func_name = func_match.group(1) - continue - - if not func_name: - continue - - # Saving the line - definition_lines.append(line.rstrip()) - - # End of definition - save and reset - if line.startswith("}"): - if func_name: - all_functions[func_name] = "\n".join(definition_lines) - func_name = None - definition_lines = [] - continue - - return all_functions - - -def _get_all_qstr_code_types(file: Path) -> dict[str, dict[str, str]]: - all_function_defs = _get_all_functions_with_definitions(file) - - all_qstr_defs: dict[str, dict[str, str]] = {} - for func_name, func_def in all_function_defs.items(): - qstr_defs: dict[str, str] = {} - one_separated_line: list[str] = [] - for line in func_def.splitlines(): - one_separated_line.append(line.strip()) - if not line.endswith(";"): - continue - one_line = "".join(one_separated_line) - one_separated_line = [] - - qstr_match = re.search(r"MP_QSTR_(\w+)", one_line) - if not qstr_match: - continue - qstr_name = qstr_match.group(1) - - rust_match = re.search(r"let (\w+): (.*?) =", one_line) - if not rust_match: - raise ValueError("No Rust type found") - rust_type = rust_match.group(2) - - if rust_type == "Obj": - # Cannot get the exact type - upy_type = "Any" - else: - upy_type = rust_type - - # There could be a default value - default = None - if "unwrap_or_else" in one_line: - default_match = re.search(r"unwrap_or_else\(\|_?\|\s+(.*?)\)", one_line) - if default_match: - default = default_match.group(1) - else: - raise ValueError("No default value found") - elif "kwargs.get_or(" in one_line: - default_match = re.search( - r"kwargs.get_or\(Qstr::MP_QSTR_\w+, (.*?)\)", one_line - ) - if default_match: - default = default_match.group(1) - else: - raise ValueError("No default value found") - - option_match = re.match(r"Option<(.*)>", upy_type) - if option_match: - upy_type = option_match.group(1) - upy_type = f"{upy_type} | None" - - gc_match = re.match(r"Gc<(.*)>", upy_type) - if gc_match: - upy_type = gc_match.group(1) - - upy_type = upy_type.replace("StrBuffer", "str") - upy_type = upy_type.replace("List", "list") - if re.match(r"^[ui]\d+$", upy_type) or upy_type == "usize": - upy_type = "int" - - if default: - if "const_none" in default: - default = "None" - elif default in ("true", "false"): - default = default.capitalize() - elif ".into(" in default: - default = default.split(".into(")[0] - elif "StrBuffer::empty" in default: - default = '""' - - upy_type += f" = {default}" - - qstr_defs[qstr_name] = upy_type - - all_qstr_defs[func_name] = qstr_defs - - return all_qstr_defs - - -def _get_all_upy_types(file: Path) -> dict[str, dict[str, str]]: - all_functions: dict[str, dict[str, str]] = {} - - with open(file, "r") as f: - lines = f.readlines() - - func_name: str | None = None - definition_lines: list[str] = [] - for line in lines: - line = line.strip() - - # Function name - func_match = re.search(r"^/// def (\w+)\(", line) - if func_match: - func_name = func_match.group(1) - continue - - if not func_name: - continue - - # Saving the line - definition_lines.append(line) - - # End of definition - save and reset - if line.startswith("Qstr::MP_QSTR_"): - if func_name: - if f"new_{func_name}" in line: - func_name = f"new_{func_name}" - def_text = "\n".join(definition_lines) - var_names_and_types = re.findall(r"(\w+): ([^#]*?)[,\n]", def_text) - all_functions[func_name] = { - name: type for name, type in var_names_and_types - } - unused_args: list[str] = [] - for def_l in definition_lines: - if "# unused" in def_l: - unused_match = re.search(r"(\w+):", def_l) - if unused_match: - unused_args.append(unused_match.group(1)) - all_functions[func_name]["_unused_args"] = unused_args # type: ignore - func_name = None - definition_lines = [] - continue - - return all_functions - - -def check_file(file: Path) -> None: - all_rust_types = _get_all_qstr_code_types(file) - all_upy_types = _get_all_upy_types(file) - - # Find discrepancies between these types - for func_name, rust_types in all_rust_types.items(): - upy_types = all_upy_types.get(func_name) - if upy_types is None: - print(f"Missing upy function {func_name}") - continue - unused_args = upy_types.get("_unused_args", []) - for qstr_name, rust_type in rust_types.items(): - upy_type = upy_types.get(qstr_name) - if not upy_type: - print(f"Missing upy argument {qstr_name} in {func_name} ({rust_type})") - continue - if upy_type and qstr_name in unused_args: - print(f"Argument {qstr_name} marked as unused but used in {func_name}") - if rust_type != upy_type: - if rust_type == "Any": - continue - if rust_type == "list" and upy_type.startswith("list"): - continue - print(f"Discrepancy in {func_name} {qstr_name}:") - print(f" Rust: {rust_type}") - print(f" Upy: {upy_type}") - - # Find things missing in Rust - for func_name, upy_types in all_upy_types.items(): - rust_types = all_rust_types.get(func_name) - if rust_types is None: - if func_name in RUST_EXTERNAL_FUNCTIONS: - continue - print(f"Missing Rust function {func_name}") - continue - unused_args = upy_types.get("_unused_args", []) - for qstr_name, upy_type in upy_types.items(): - if qstr_name == "_unused_args": - continue - rust_type = rust_types.get(qstr_name) - if not rust_type and qstr_name not in unused_args: - if func_name in RUST_DELEGATING_FUNCTIONS: - continue - print(f"Not parsing argument {qstr_name} in {func_name}") - continue - - -def main() -> None: - for file in ALL_FILES: - print(f"Checking {file}") - check_file(file) - - -if __name__ == "__main__": - main() diff --git a/core/tools/rust_api_models_unification.py b/core/tools/rust_api_models_unification.py deleted file mode 100755 index 0289631a63..0000000000 --- a/core/tools/rust_api_models_unification.py +++ /dev/null @@ -1,85 +0,0 @@ -#!/usr/bin/env python3 -""" -Check the consistency of the Rust API between models. -""" -from __future__ import annotations - -import re -from collections import defaultdict -from pathlib import Path - -HERE = Path(__file__).parent -CORE_DIR = HERE.parent -MOCK_FILE = CORE_DIR / "mocks/generated/trezorui2.pyi" - - -def _get_all_functions_with_definitions() -> dict[str, dict[str, str]]: - all_functions: dict[str, dict[str, str]] = defaultdict(dict) - - with open(MOCK_FILE, "r") as f: - lines = f.readlines() - - model: str | None = None - func_name: str | None = None - definition_lines: list[str] = [] - for line in lines: - # Model line, e.g. "# rust/src/ui/model_tt/layout.rs" - model_match = re.search(r"^# rust/src/ui/model_(\w+)/", line) - if model_match: - model = model_match.group(1).upper() - continue - - if model is None: - continue - - # Function name - func_match = re.search(r"^def\s+(\w+)", line) - if func_match: - func_name = func_match.group(1) - - # Getting rid of comments before saving the definition - line = line.split("#")[0].rstrip() - definition_lines.append(line) - - # End of definition - save and reset - if line.endswith(":"): - if func_name and model: - all_functions[func_name][model] = "\n".join(definition_lines) - func_name = None - model = None - definition_lines = [] - continue - - return all_functions - - -def main() -> None: - all_functions = _get_all_functions_with_definitions() - - # Show only those in one model - print(f"{40 * '/'}\nONLY ONE MODEL FUNCTIONS\n{40 * '/'}\n") - only_one_model_amount = 0 - for func_name, func_defs in all_functions.items(): - if len(func_defs) == 1: - print(f"{list(func_defs.keys())[0]} - {func_name}") - only_one_model_amount += 1 - - # Show those in both models, with comparing the definitions - print(f"\n{40 * '/'}\nDIFFERENT IMPLEMENTATIONS\n{40 * '/'}\n") - diff_impl_amount = 0 - for func_name, func_defs in all_functions.items(): - if len(func_defs) == 2: - models = list(func_defs.keys()) - if func_defs[models[0]] != func_defs[models[1]]: - print(func_name) - for model in func_defs.keys(): - print(model + " - " + func_defs[model]) - print("\n" + "-" * 40 + "\n") - diff_impl_amount += 1 - - print(f"Total only one model functions: {only_one_model_amount}") - print(f"Total different implementations: {diff_impl_amount}") - - -if __name__ == "__main__": - main()