From 347ff1c6d79e72d271951540d5c09ee56de7799b Mon Sep 17 00:00:00 2001 From: grdddj Date: Mon, 14 Aug 2023 09:33:55 +0200 Subject: [PATCH] chore(python): add ruff linter as a dependency [no changelog] --- ci/shell.nix | 1 + core/embed/firmware/bootloader_hashes.py | 8 ++++++-- core/prof/prof.py | 6 +++--- core/tools/alloc.py | 2 +- core/tools/hid-bridge/logger.py | 2 +- pyproject.toml | 8 ++++++++ 6 files changed, 20 insertions(+), 7 deletions(-) diff --git a/ci/shell.nix b/ci/shell.nix index eaa0fb0ad..514beb917 100644 --- a/ci/shell.nix +++ b/ci/shell.nix @@ -98,6 +98,7 @@ stdenvNoCC.mkDerivation ({ poetry protobuf3_19 pyright + ruff (mkBinOnlyWrapper rustNightly) wget zlib diff --git a/core/embed/firmware/bootloader_hashes.py b/core/embed/firmware/bootloader_hashes.py index 1dd79bdf2..4afca3c67 100755 --- a/core/embed/firmware/bootloader_hashes.py +++ b/core/embed/firmware/bootloader_hashes.py @@ -29,7 +29,7 @@ def aligned_digest(data: bytes, padding: bytes) -> bytes: (unwritten NOR-flash byte) or 0x00 (explicitly cleared byte). """ if len(data) > ALIGNED_SIZE: - raise ValueError(fn, "too big") + raise ValueError("too big") assert len(padding) == 1 digest_data = data + padding * (ALIGNED_SIZE - len(data)) @@ -55,6 +55,7 @@ def bootloader_str(file: Path) -> str: data = file.read_bytes() suffix = file.stem[len("bootloader_") :].upper() + bytes_00 = to_uint_array(aligned_digest(data, b"\x00")) bytes_ff = to_uint_array(aligned_digest(data, b"\xff")) @@ -84,7 +85,10 @@ def main(): # write bootloader definitions for file in BOOTLOADERS.glob("bootloader*.bin"): - bl_check_new.append(bootloader_str(file)) + try: + bl_check_new.append(bootloader_str(file)) + except ValueError: + raise ValueError(f"Data too big in {file}") # consume up to auto-end for line in f: diff --git a/core/prof/prof.py b/core/prof/prof.py index 2ddf29939..32bd90966 100644 --- a/core/prof/prof.py +++ b/core/prof/prof.py @@ -16,7 +16,7 @@ class Coverage: self.__files = {} def line_tick(self, filename, lineno): - if not filename in self.__files: + if filename not in self.__files: self.__files[filename] = set() self.__files[filename].add(lineno) @@ -120,7 +120,7 @@ def atexit(): sys.atexit(atexit) global __prof__ -if not "__prof__" in globals(): +if "__prof__" not in globals(): if getenv("TREZOR_MEMPERF") == "1": __prof__ = AllocCounter() else: @@ -131,4 +131,4 @@ sys.settrace(trace_handler) if isinstance(__prof__, AllocCounter): __prof__.last_alloc_count = micropython.alloc_count() -import main +import main # noqa: F401 (imported but unused) diff --git a/core/tools/alloc.py b/core/tools/alloc.py index 32ccc7972..4e5559ee3 100755 --- a/core/tools/alloc.py +++ b/core/tools/alloc.py @@ -10,7 +10,7 @@ from typing_extensions import TypedDict import click from dominate import document -from dominate.tags import * +from dominate.tags import meta, style, h3, table, thead, tr, th, tbody, td, a from dominate.util import raw HERE = Path(__file__).resolve().parent diff --git a/core/tools/hid-bridge/logger.py b/core/tools/hid-bridge/logger.py index d6d6732d4..09394f76f 100644 --- a/core/tools/hid-bridge/logger.py +++ b/core/tools/hid-bridge/logger.py @@ -9,7 +9,7 @@ def __get_timestamp(): def __log_message(message): - if log_timestamps == True: + if log_timestamps is True: print(f"{__get_timestamp()}\t{message}") else: print(message) diff --git a/pyproject.toml b/pyproject.toml index 698bb75bc..2dbb6ca05 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -102,3 +102,11 @@ extra_standard_library = [ ] known_first_party = ["trezorlib", "apps", "coin_info", "marketcap", "ui_tests", "gitlab"] known_third_party = ["trezor", "storage"] + +[tool.ruff] +ignore = [ + "E501", # line too long + "E402", # module level import not at top of file + "E741", # ambiguous variable name +] +exclude = ["tests", "mocks"]