|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Microsoft Corporation. |
| 3 | +# Licensed under the MIT License. |
| 4 | + |
| 5 | +import argparse |
| 6 | +import json |
| 7 | +import os |
| 8 | +import sys |
| 9 | +from collections import defaultdict |
| 10 | + |
| 11 | +_REPO_KEY = "Repo" |
| 12 | +_SPEC_PATH_KEY = "SpecPath" |
| 13 | +_SRPM_PATH_KEY = "SrpmPath" |
| 14 | + |
| 15 | + |
| 16 | +def find_srpm_duplicates(specs_file_paths: list[str]) -> list[tuple[str, set[str]]]: |
| 17 | + """ |
| 18 | + Analyze multiple specs JSON files to find specs producing the same SRPM. |
| 19 | + """ |
| 20 | + srpm_to_specs = defaultdict(set) |
| 21 | + |
| 22 | + for specs_file_path in specs_file_paths: |
| 23 | + with open(specs_file_path, "r") as f: |
| 24 | + data = json.load(f) |
| 25 | + |
| 26 | + if _REPO_KEY not in data: |
| 27 | + raise ValueError( |
| 28 | + f"Invalid JSON format in {specs_file_path}. Expected '{_REPO_KEY}' key." |
| 29 | + ) |
| 30 | + |
| 31 | + # Process each item in the repo |
| 32 | + for item in data["Repo"]: |
| 33 | + if _SRPM_PATH_KEY not in item or _SPEC_PATH_KEY not in item: |
| 34 | + raise ValueError( |
| 35 | + f"Invalid JSON format in {specs_file_path}. Expected '{_SPEC_PATH_KEY}' and '{_SRPM_PATH_KEY}' keys in each element of '{_REPO_KEY}'." |
| 36 | + ) |
| 37 | + |
| 38 | + srpm = os.path.basename(item[_SRPM_PATH_KEY]) |
| 39 | + srpm_to_specs[srpm].add(item[_SPEC_PATH_KEY]) |
| 40 | + |
| 41 | + return [ |
| 42 | + (srpm, specs_paths) |
| 43 | + for srpm, specs_paths in srpm_to_specs.items() |
| 44 | + if len(specs_paths) > 1 |
| 45 | + ] |
| 46 | + |
| 47 | + |
| 48 | +if __name__ == "__main__": |
| 49 | + parser = argparse.ArgumentParser() |
| 50 | + parser.add_argument( |
| 51 | + "specs_file_paths", |
| 52 | + nargs="+", |
| 53 | + help="Paths to the specs JSON files to analyze.", |
| 54 | + ) |
| 55 | + args = parser.parse_args() |
| 56 | + |
| 57 | + srpm_duplicates = find_srpm_duplicates(args.specs_file_paths) |
| 58 | + if srpm_duplicates: |
| 59 | + print("Error: detected specs building the same SRPM.", file=sys.stderr) |
| 60 | + for srpm, specs_paths in srpm_duplicates: |
| 61 | + print(f"{srpm}:", file=sys.stderr) |
| 62 | + for spec_path in specs_paths: |
| 63 | + print(f" - {spec_path}", file=sys.stderr) |
| 64 | + print(file=sys.stderr) |
| 65 | + sys.exit(1) |
| 66 | + |
| 67 | + print("No SRPM duplicates found.") |
0 commit comments