(argv: list[str] | None = None)
| 52 | |
| 53 | |
| 54 | def main(argv: list[str] | None = None) -> int: |
| 55 | args = parse_args(argv) |
| 56 | if args.cwd: |
| 57 | root_build_dir = args.cwd / ".build" |
| 58 | else: |
| 59 | root_build_dir = Path(".build") |
| 60 | |
| 61 | # Support nested PlatformIO structure: .build/pio/<board> |
| 62 | nested_pio_dir = root_build_dir / "pio" |
| 63 | if nested_pio_dir.is_dir(): |
| 64 | root_build_dir = nested_pio_dir |
| 65 | |
| 66 | board_dirs = [d for d in root_build_dir.iterdir() if d.is_dir()] |
| 67 | if not board_dirs: |
| 68 | print(f"No board directories found in {root_build_dir.absolute()}") |
| 69 | return 1 |
| 70 | print("Available boards:") |
| 71 | for i, board_dir in enumerate(board_dirs): |
| 72 | print(f"[{i}]: {board_dir.name}") |
| 73 | which = ( |
| 74 | 0 |
| 75 | if args.first |
| 76 | else int(input("Enter the number of the board you want to inspect: ")) |
| 77 | ) |
| 78 | board_dir = board_dirs[which] |
| 79 | optimization_report = board_dir / "optimization_report.txt" |
| 80 | if not optimization_report.exists(): |
| 81 | print( |
| 82 | f"No optimization report found for board '{board_dir.name}'.\n" |
| 83 | f"Expected: {optimization_report}\n" |
| 84 | "The build may not have completed successfully." |
| 85 | ) |
| 86 | return 0 |
| 87 | |
| 88 | total = optimization_report.stat().st_size |
| 89 | max_bytes = max(args.max_bytes, 0) |
| 90 | |
| 91 | if total <= max_bytes or max_bytes == 0: |
| 92 | text = optimization_report.read_text(encoding="utf-8", errors="replace") |
| 93 | print(text) |
| 94 | return 0 |
| 95 | |
| 96 | # Tail the file: seek to the cut-off offset, read to end, then snap to the |
| 97 | # next line boundary so we don't print a truncated leading line. |
| 98 | with optimization_report.open("rb") as fh: |
| 99 | fh.seek(total - max_bytes) |
| 100 | tail_bytes = fh.read() |
| 101 | first_newline = tail_bytes.find(b"\n") |
| 102 | if 0 <= first_newline < len(tail_bytes) - 1: |
| 103 | tail_bytes = tail_bytes[first_newline + 1 :] |
| 104 | tail_text = tail_bytes.decode("utf-8", errors="replace") |
| 105 | |
| 106 | omitted = total - len(tail_bytes) |
| 107 | print( |
| 108 | f"[optimization_report.py] {optimization_report} is {total} bytes; " |
| 109 | f"showing last {len(tail_bytes)} bytes ({omitted} bytes elided to keep " |
| 110 | f"the GitHub Actions log under the runner's buffer limit)." |
| 111 | ) |
no test coverage detected