| 1 | """Trace the X11 APK index dependencies to their requesters and released ELF consumers.""" |
| 2 | import argparse |
| 3 | import csv |
| 4 | import re |
| 5 | from collections import defaultdict |
| 6 | from pathlib import Path |
| 7 | |
| 8 | |
| 9 | def rows(path): |
| 10 | with path.open() as source: |
| 11 | return list(csv.DictReader(source, delimiter="\t")) |
| 12 | |
| 13 | |
| 14 | def depname(dependency): |
| 15 | return re.split(r"[<>=~]", dependency, maxsplit=1)[0] |
| 16 | |
| 17 | |
| 18 | def report(args): |
| 19 | inputs = [row for row in rows(args.inventory / "all.tsv") |
| 20 | if row["repository"] in ("main", "community")] |
| 21 | arch, = {row["arch"] for row in inputs} |
| 22 | selected = {(row["repository"], row["binary"], row["version"]) for row in inputs} |
| 23 | packages = {} |
| 24 | requesters = defaultdict(set) |
| 25 | for repo in ("main", "community"): |
| 26 | for record in (args.inventory / (repo + ".APKINDEX")).read_text().strip().split("\n\n"): |
| 27 | fields = dict(line.split(":", 1) for line in record.splitlines()) |
| 28 | key = (repo, fields["P"], fields["V"]) |
| 29 | if key in selected: |
| 30 | if key in packages or fields["A"] not in (arch, "noarch"): |
| 31 | raise ValueError(f"ambiguous or foreign index record for {key}") |
| 32 | packages[key] = {depname(value) for value in fields.get("p", "").split()} |
| 33 | for value in fields.get("D", "").split(): |
| 34 | requesters[depname(value)].add(fields["P"]) |
| 35 | if selected != packages.keys(): |
| 36 | raise ValueError(f"input rows missing from APKINDEX: {selected - packages.keys()}") |
| 37 | |
| 38 | consumers = defaultdict(set) |
| 39 | for layer, path in (("image", args.image_needed), ("x11", args.x11_needed)): |
| 40 | for row in rows(path): |
| 41 | for soname in filter(None, row["needed"].split(",")): |
| 42 | consumers[soname].add(layer + "/" + row["release_path"]) |
| 43 | with args.output.open("x") as out: |
| 44 | writer = csv.writer(out, delimiter="\t", lineterminator="\n") |
| 45 | writer.writerow(("package", "origin", "profiles", "provided_sonames", |
| 46 | "index_requesters", "release_elf_consumers")) |
| 47 | for row in inputs: |
| 48 | name = row["binary"] |
| 49 | provided = packages[row["repository"], name, row["version"]] |
| 50 | sonames = sorted(s[3:] for s in provided if s.startswith("so:")) |
| 51 | # One binary may provide a package alias, a SONAME and a command. |
| 52 | parents = {parent for key in provided | {name} for parent in requesters[key] if parent != name} |
| 53 | elfs = {path for soname in sonames for path in consumers[soname]} |
| 54 | writer.writerow((name, row["source"], row["profiles"], ",".join(sonames), |
| 55 | ",".join(sorted(parents)), ",".join(sorted(elfs)))) |
| 56 | |
| 57 | |
| 58 | if __name__ == "__main__": |
| 59 | parser = argparse.ArgumentParser(description=__doc__) |
| 60 | for arg in ("inventory", "image_needed", "x11_needed", "output"): |
| 61 | parser.add_argument(arg, type=Path) |
| 62 | report(parser.parse_args()) |
| 63 | |