test(scripts): typing

This commit is contained in:
Maxim Prokhorov
2025-06-08 21:45:59 +03:00
parent 41c2103df0
commit fd7b36bebb
2 changed files with 17 additions and 16 deletions

View File

@@ -38,6 +38,7 @@ import subprocess
import sys
from collections import OrderedDict
from typing import Optional
__version__ = (0, 4)
@@ -83,7 +84,7 @@ DESCRIPTION = "ESPurna Memory Analyzer v{}".format(
# -------------------------------------------------------------------------------
def analyse_memory(elf_file, *, size_cmd=SIZE):
def analyze_memory(elf_file, *, size_cmd=SIZE):
proc = subprocess.Popen(
[size_cmd, "-A", elf_file.as_posix()],
stdout=subprocess.PIPE,
@@ -105,7 +106,7 @@ def analyse_memory(elf_file, *, size_cmd=SIZE):
return values
def make_firmware_paths(envname: str, *, build_path=BUILD_PATH, firmware="firmware"):
def make_firmware_paths(envname: str, *, build_path=BUILD_PATH, firmware="firmware") -> tuple[pathlib.Path, pathlib.Path]:
elf_path = build_path / envname / f"{firmware}.elf"
bin_path = build_path / envname / f"{firmware}.bin"
@@ -156,7 +157,7 @@ def get_available_modules(args) -> OrderedDict[str, int]:
return OrderedDict(modules)
def parse_commandline_args():
def parse_commandline_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description=DESCRIPTION, formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
@@ -305,7 +306,7 @@ class Analyzer:
"",
)
self.print_delimiters()
self.baseline = self.run({})
self.baseline = self.run()
self.print_values("Baseline", self.baseline)
def print_values(self, header, values):
@@ -332,7 +333,7 @@ class Analyzer:
values["size"] - self.baseline["size"],
)
def run(self, modules=None):
def run(self, modules: Optional[OrderedDict[str, int]] = None):
run(
self._envname,
modules or self.modules,
@@ -344,7 +345,7 @@ class Analyzer:
elf_path, bin_path = make_firmware_paths(
self._envname, build_path=self._build_path
)
values = analyse_memory(elf_path, size_cmd=self._size_cmd)
values = analyze_memory(elf_path, size_cmd=self._size_cmd)
free = 80 * 1024 - values[".data"] - values[".rodata"] - values[".bss"]
free = free + (16 - free % 16)

View File

@@ -39,22 +39,22 @@ CACHE_PATH = TEST_PATH / "cache"
BUILD_PATH = ROOT_PATH / ".pio" / "build"
def bold(string):
def bold(string: str):
return clr(Color.BOLD, string)
def format_configurations(configurations):
def format_configurations(configurations: list[pathlib.Path]):
return "\n".join(str(cfg) for cfg in configurations)
def pluralize(string, length):
def pluralize(string: str, length: int):
if length > 1:
return f"{string}s"
return string
def build_configurations(args, configurations):
def build_configurations(args: argparse.Namespace, configurations: list[pathlib.Path]):
cmd = ["platformio", "run"]
if args.silent:
cmd.extend(["-s"])
@@ -83,11 +83,11 @@ def build_configurations(args, configurations):
try:
subprocess.check_call(cmd, env=os_env)
except subprocess.CalledProcessError:
log.error("%s failed to build", bold(cfg))
log.error("%s failed to build", bold(str(cfg)))
if configurations:
log.info(
"%s %s left\n%s",
bold(len(configurations)),
bold(str(len(configurations))),
pluralize("configuration", len(configurations)),
format_configurations(configurations),
)
@@ -99,16 +99,16 @@ def build_configurations(args, configurations):
log.info(
"%s finished in %s, %s is %s bytes",
*(bold(x) for x in (cfg, diff, firmware_bin, firmware_bin.stat().st_size)),
*(bold(str(x)) for x in (cfg, diff, firmware_bin, firmware_bin.stat().st_size)),
)
build_time += diff
if build_time:
log.info("Done after %s", bold(build_time))
log.info("Done after %s", bold(str(build_time)))
def main(args):
def main(args: argparse.Namespace):
if not args.environment:
log.error("No environment selected")
return
@@ -142,7 +142,7 @@ def main(args):
log.info(
"Found %s %s\n%s",
bold(len(configurations)),
bold(str(len(configurations))),
pluralize("configuration", len(configurations)),
format_configurations(configurations),
)