|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import argparse |
| 3 | +import sys |
| 4 | + |
| 5 | +import yaml |
| 6 | + |
| 7 | + |
| 8 | +def main(): |
| 9 | + parser = argparse.ArgumentParser( |
| 10 | + description="Merge base and overlay .repos files with overlay taking precedence." |
| 11 | + ) |
| 12 | + parser.add_argument("--base", required=True, help="Path to the base .repos file") |
| 13 | + parser.add_argument("--overlay", required=True, help="Path to the overlay .repos file") |
| 14 | + parser.add_argument( |
| 15 | + "--output", |
| 16 | + default="combined.repos", |
| 17 | + help="Path for the combined output file (default: combined.repos)", |
| 18 | + ) |
| 19 | + args = parser.parse_args() |
| 20 | + |
| 21 | + try: |
| 22 | + with open(args.base, "r") as bf: |
| 23 | + base_data = yaml.safe_load(bf) |
| 24 | + except Exception as e: |
| 25 | + sys.exit(f"Error reading base file '{args.base}': {e}") |
| 26 | + |
| 27 | + try: |
| 28 | + with open(args.overlay, "r") as of: |
| 29 | + overlay_data = yaml.safe_load(of) |
| 30 | + except Exception as e: |
| 31 | + sys.exit(f"Error reading overlay file '{args.overlay}': {e}") |
| 32 | + |
| 33 | + if "repositories" not in base_data: |
| 34 | + sys.exit(f"Base file '{args.base}' is missing the 'repositories' key") |
| 35 | + if overlay_data and "repositories" not in overlay_data: |
| 36 | + sys.exit(f"Overlay file '{args.overlay}' is missing the 'repositories' key") |
| 37 | + |
| 38 | + # Merge: overlay entries override base entries |
| 39 | + merged_data = base_data.copy() |
| 40 | + merged_data["repositories"].update(overlay_data.get("repositories", {})) |
| 41 | + |
| 42 | + try: |
| 43 | + with open(args.output, "w") as cf: |
| 44 | + yaml.dump(merged_data, cf, default_flow_style=False) |
| 45 | + except Exception as e: |
| 46 | + sys.exit(f"Error writing to output file '{args.output}': {e}") |
| 47 | + |
| 48 | + print(f"Successfully merged into {args.output}") |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + main() |
0 commit comments