|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import pathlib |
| 4 | +import sys |
| 5 | +import tomlkit |
| 6 | + |
| 7 | + |
| 8 | +def find_modules(top_dir, port_dir): |
| 9 | + """Find all available modules in shared-bindings and port bindings.""" |
| 10 | + modules = set() |
| 11 | + for module in sorted( |
| 12 | + list(top_dir.glob("shared-bindings/*")) + list(port_dir.glob("bindings/*")), |
| 13 | + key=lambda x: x.name, |
| 14 | + ): |
| 15 | + if not module.is_dir(): |
| 16 | + continue |
| 17 | + modules.add(module.name) |
| 18 | + return sorted(modules) |
| 19 | + |
| 20 | + |
| 21 | +def find_board_info_files(port_dir): |
| 22 | + """Find all autogen_board_info.toml files in the port directory.""" |
| 23 | + return list(port_dir.glob("boards/**/autogen_board_info.toml")) |
| 24 | + |
| 25 | + |
| 26 | +def update_board_info(board_info_path, available_modules): |
| 27 | + """Update board info file with new modules set to false.""" |
| 28 | + if not board_info_path.exists(): |
| 29 | + print(f"Error: Board info file {board_info_path} does not exist", file=sys.stderr) |
| 30 | + return False |
| 31 | + |
| 32 | + # Load existing board info |
| 33 | + with open(board_info_path, "r", encoding="utf-8") as f: |
| 34 | + board_info = tomlkit.load(f) |
| 35 | + |
| 36 | + # Get current modules |
| 37 | + current_modules = set(board_info.get("modules", {})) |
| 38 | + |
| 39 | + # Find new modules |
| 40 | + new_modules = set(available_modules) - current_modules |
| 41 | + if not new_modules: |
| 42 | + print( |
| 43 | + f"No new modules found for {board_info_path.relative_to(board_info_path.parents[3])}" |
| 44 | + ) |
| 45 | + return True |
| 46 | + |
| 47 | + # Add new modules as disabled in alphabetical order |
| 48 | + modules_table = board_info["modules"] |
| 49 | + # Get all modules (existing and new) and sort them |
| 50 | + all_modules = list(current_modules | new_modules) |
| 51 | + all_modules.sort() |
| 52 | + |
| 53 | + # Create a new table with sorted modules |
| 54 | + sorted_table = tomlkit.table() |
| 55 | + for module in all_modules: |
| 56 | + if module in modules_table: |
| 57 | + # TODO: Use modules_table.item once tomlkit is released with changes from January 2025 |
| 58 | + sorted_table[module] = modules_table._value.item(module) |
| 59 | + else: |
| 60 | + sorted_table[module] = tomlkit.item(False) |
| 61 | + |
| 62 | + # Replace the modules table with the sorted one |
| 63 | + board_info["modules"] = sorted_table |
| 64 | + |
| 65 | + # Write updated board info |
| 66 | + with open(board_info_path, "w", encoding="utf-8") as f: |
| 67 | + tomlkit.dump(board_info, f) |
| 68 | + |
| 69 | + print( |
| 70 | + f"Updated {board_info_path.relative_to(board_info_path.parents[3])} with {len(new_modules)} new modules:" |
| 71 | + ) |
| 72 | + for module in sorted(new_modules): |
| 73 | + print(f" - {module}") |
| 74 | + return True |
| 75 | + |
| 76 | + |
| 77 | +def main(): |
| 78 | + # Get repo paths |
| 79 | + script_dir = pathlib.Path(__file__).parent |
| 80 | + top_dir = script_dir.parents[2] # circuitpython root |
| 81 | + port_dir = script_dir.parent # zephyr-cp directory |
| 82 | + |
| 83 | + # Get available modules once |
| 84 | + available_modules = find_modules(top_dir, port_dir) |
| 85 | + |
| 86 | + # Update all board info files |
| 87 | + board_info_files = find_board_info_files(port_dir) |
| 88 | + if not board_info_files: |
| 89 | + print("No board info files found") |
| 90 | + sys.exit(1) |
| 91 | + |
| 92 | + success = True |
| 93 | + for board_info_path in board_info_files: |
| 94 | + if not update_board_info(board_info_path, available_modules): |
| 95 | + success = False |
| 96 | + |
| 97 | + sys.exit(0 if success else 1) |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + main() |
0 commit comments