|
| 1 | +#! /usr/bin/env python |
| 2 | + |
| 3 | +import json |
| 4 | +import re |
| 5 | +from argparse import ArgumentParser |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | + |
| 9 | +def extract_all_sources_entries(text): |
| 10 | + entry_pattern = re.compile( |
| 11 | + r""" |
| 12 | + Types:\s*(?P<Types>[^\n]+)\s+ |
| 13 | + URIs:\s*(?P<URIs>[^\n]+)\s+ |
| 14 | + Suites:\s*(?P<Suites>[^\n]+)\s+ |
| 15 | + Components:\s*(?P<Components>[^\n]+) |
| 16 | + (?:\s+Signed-By:\s*(?P<SignedBy>[^\n]+))? |
| 17 | + (?:\s+Architectures:\s*(?P<Architectures>[^\n]+))? |
| 18 | + """, |
| 19 | + re.VERBOSE, |
| 20 | + ) |
| 21 | + |
| 22 | + entries = [] |
| 23 | + for match in entry_pattern.finditer(text): |
| 24 | + entries.append( |
| 25 | + { |
| 26 | + "Types": match.group("Types").strip().split(" "), |
| 27 | + "URIs": match.group("URIs").strip().split(" "), |
| 28 | + "Suites": match.group("Suites").strip().split(" "), |
| 29 | + "Components": match.group("Components").strip().split(" "), |
| 30 | + "Architectures": match.group("Architectures").strip().split(",") |
| 31 | + if match.group("Architectures") |
| 32 | + else None, |
| 33 | + "SignedBy": match.group("SignedBy").strip() if match.group("SignedBy") else None, |
| 34 | + } |
| 35 | + ) |
| 36 | + |
| 37 | + return entries |
| 38 | + |
| 39 | + |
| 40 | +if __name__ == "__main__": |
| 41 | + parser = ArgumentParser() |
| 42 | + parser.add_argument("file", type=str, help="Path to the sources files", nargs="+") |
| 43 | + args = parser.parse_args() |
| 44 | + entries = [] |
| 45 | + for file in args.file: |
| 46 | + with Path(file).open("r") as f: |
| 47 | + content = f.read() |
| 48 | + |
| 49 | + entries.extend(extract_all_sources_entries(content)) |
| 50 | + print(json.dumps(entries, indent=2)) |
| 51 | + |
0 commit comments