ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
list_libs.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# SPDX-License-Identifier: MIT
3# Copyright (c) 2026 Brighton Sikarskie
4"""List first-party, shared, and vendored library descriptions."""
5
6import re
7import sys
8from pathlib import Path
9
10REPO_ROOT = Path(__file__).resolve().parents[2]
11DESCRIPTION_LIMIT = 80
12NAME_COLUMN_WIDTH = 30
13LIBRARY_PATTERNS = (
14 "libs/*",
15 "libs/third_party/*",
16 "apps/shared_libs/*",
17 "apps/shared_libs/third_party/*",
18)
19
20
21def _vendor_brief(lib_dir: Path) -> str:
22 """Read one vendored component description from its parent registry."""
23 readme_path = lib_dir.parent / "README.md"
24 if not readme_path.exists():
25 return ""
26 readme_text = readme_path.read_text(encoding="utf-8")
27 match = re.search(
28 rf"\|\s*`?{re.escape(lib_dir.name)}`?\s*\|\s*(.+?)\s*\|",
29 readme_text,
30 )
31 return match.group(1).strip() if match else ""
32
33
34def _readme_summary(text: str) -> str:
35 """Return the first prose line from a library README."""
36 excluded_prefixes = ("#", "=", "-", "[", "!", "<", "Copyright", "SPDX")
37 for source_line in text.splitlines():
38 line = source_line.strip()
39 if line and not line.startswith(excluded_prefixes):
40 suffix = "..." if len(line) > DESCRIPTION_LIMIT else ""
41 return line[:DESCRIPTION_LIMIT] + suffix
42 return ""
43
44
45def _brief(lib_dir: Path) -> str:
46 """Extract a library description from its header or README."""
47 if "third_party" in lib_dir.parts:
48 return _vendor_brief(lib_dir)
49
50 lib_name = lib_dir.name
51 header_candidates = [
52 lib_dir / "inc" / f"{lib_name}.h",
53 lib_dir / "inc" / f"{lib_name.replace('ra8_', '')}.h",
54 lib_dir / f"{lib_name}.h",
55 lib_dir / "README.md",
56 lib_dir / "README.txt",
57 lib_dir / "README",
58 *(lib_dir / "inc").glob("*.h"),
59 ]
60 for document in header_candidates:
61 if not document.exists():
62 continue
63 text = document.read_text(encoding="utf-8", errors="ignore")
64 match = re.search(r"@brief\s+([^\n]+)", text)
65 if match:
66 return match.group(1).strip()
67 if document.name.startswith("README"):
68 summary = _readme_summary(text)
69 if summary:
70 return summary
71 return ""
72
73
74def _library_dirs() -> list[Path]:
75 """Return every direct library component from the canonical roots."""
76 candidates = {path for pattern in LIBRARY_PATTERNS for path in REPO_ROOT.glob(pattern)}
77 return sorted(path for path in candidates if path.is_dir() and path.name != "third_party")
78
79
80def _categorized_entries(search_keyword: str | None) -> tuple[list[str], list[str], list[str]]:
81 """Build display rows for firmware, shared, and vendored libraries."""
82 first_party: list[str] = []
83 shared_party: list[str] = []
84 third_party: list[str] = []
85 for lib_dir in _library_dirs():
86 name = lib_dir.name
87 brief = _brief(lib_dir)
88 if (
89 search_keyword
90 and search_keyword not in name.lower()
91 and search_keyword not in brief.lower()
92 ):
93 continue
94 entry = f" - {name:<{NAME_COLUMN_WIDTH}} {brief}"
95 if "third_party" in lib_dir.parts:
96 third_party.append(entry)
97 elif "shared_libs" in lib_dir.parts:
98 shared_party.append(entry)
99 else:
100 first_party.append(entry)
101 return first_party, shared_party, third_party
102
103
104def _print_section(title: str, entries: list[str], *, visible: bool = True) -> None:
105 """Print one optional titled group."""
106 if not entries:
107 return
108 if visible:
109 print(f"\n{title}:")
110 for entry in entries:
111 print(entry)
112
113
114def main() -> None:
115 """List libraries, optionally filtered by a case-insensitive query."""
116 search_keyword = sys.argv[1].lower() if len(sys.argv) > 1 else None
117 first_party, shared_party, third_party = _categorized_entries(search_keyword)
118
119 if search_keyword:
120 print(f"SEARCH RESULTS IN LIBRARIES FOR '{search_keyword}':")
121 if not first_party and not third_party and not shared_party:
122 print(" (No matches found)")
123 else:
124 print("FIRMWARE LIBRARIES:")
125
126 for entry in first_party:
127 print(entry)
128 _print_section("SHARED LIBRARIES", shared_party, visible=search_keyword is None)
129 _print_section("THIRD-PARTY LIBRARIES", third_party, visible=search_keyword is None)
130
131
132if __name__ == "__main__":
133 main()
void main(void)
The application entry point Reset_Handler hands control to.
Definition main.c:298