|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import json |
| 3 | +import sys |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | + |
| 7 | +def get_actual_files(): |
| 8 | + """Get all Python files from walter_modem folder (excluding .pyi type stubs).""" |
| 9 | + files = [] |
| 10 | + walter_modem_dir = Path("walter_modem") |
| 11 | + |
| 12 | + for py_file in sorted(walter_modem_dir.glob("*.py")): |
| 13 | + if py_file.is_file(): |
| 14 | + files.append(py_file.relative_to(Path("."))) |
| 15 | + |
| 16 | + for py_file in sorted((walter_modem_dir / "mixins").glob("*.py")): |
| 17 | + if py_file.is_file(): |
| 18 | + files.append(py_file.relative_to(Path("."))) |
| 19 | + |
| 20 | + return [str(f) for f in files] |
| 21 | + |
| 22 | + |
| 23 | +def get_github_urls(files): |
| 24 | + """Generate GitHub URLs for package.json format.""" |
| 25 | + return [[f, f"github:QuickSpot/walter-micropython/{f}"] for f in files] |
| 26 | + |
| 27 | + |
| 28 | +def validate_and_update_package_json(): |
| 29 | + """ |
| 30 | + Validates and syncs package.json with actual files in walter_modem folder. |
| 31 | + Returns: (changed: bool, added: list, removed: list) |
| 32 | + """ |
| 33 | + package_json_path = "package.json" |
| 34 | + |
| 35 | + with open(package_json_path, "r") as f: |
| 36 | + package_data = json.load(f) |
| 37 | + |
| 38 | + actual_files = get_actual_files() |
| 39 | + correct_urls = get_github_urls(actual_files) |
| 40 | + |
| 41 | + current_urls = package_data.get("urls", []) |
| 42 | + current_files = [url[0] for url in current_urls] |
| 43 | + |
| 44 | + actual_set = set(actual_files) |
| 45 | + current_set = set(current_files) |
| 46 | + added = sorted(actual_set - current_set) |
| 47 | + removed = sorted(current_set - actual_set) |
| 48 | + |
| 49 | + if added or removed: |
| 50 | + package_data["urls"] = correct_urls |
| 51 | + |
| 52 | + with open(package_json_path, "w") as f: |
| 53 | + f.write("{\n") |
| 54 | + f.write(' "urls": [\n') |
| 55 | + for i, url_pair in enumerate(correct_urls): |
| 56 | + f.write(f' {json.dumps(url_pair)}') |
| 57 | + f.write("," if i < len(correct_urls) - 1 else "") |
| 58 | + f.write("\n") |
| 59 | + f.write(" ],\n") |
| 60 | + f.write(f' "version": "{package_data["version"]}"\n') |
| 61 | + f.write("}\n") |
| 62 | + |
| 63 | + return True, added, removed |
| 64 | + |
| 65 | + return False, [], [] |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + try: |
| 70 | + changed, added, removed = validate_and_update_package_json() |
| 71 | + |
| 72 | + if changed: |
| 73 | + if added: |
| 74 | + print(f"Added files: {added}") |
| 75 | + if removed: |
| 76 | + print(f"Removed files: {removed}") |
| 77 | + print(f"Updated package.json") |
| 78 | + sys.exit(1) # Exit 1 to signal changes for GitHub Actions |
| 79 | + else: |
| 80 | + print("package.json is in sync") |
| 81 | + sys.exit(0) |
| 82 | + |
| 83 | + except Exception as e: |
| 84 | + print(f"Error: {e}", file=sys.stderr) |
| 85 | + sys.exit(2) |
0 commit comments