"""Plan the default unit groups from the full go-list inventory. Usage: go list ./... | python3 unit-test-shards.py MODULE OUT_DIR EVENT HEAD_REF CHANGED_FILE The complete partition covers every package exactly once. Existing scope rules then select heavy packages only on ordinary PRs that touch their direct files. """ from pathlib import Path import sys HEAVY = ("core/storage", "core/frontend/durable", "core/host/master", "core/authority") def partition(module, packages): if not module or any(c.isspace() for c in module): raise ValueError("expected one module path") if not packages or len(packages) != len(set(packages)): raise ValueError("empty package list or duplicate package") if any(p != module and not p.startswith(module + "/") for p in packages): raise ValueError("package outside the selected module") heavy = [module + "/" + p for p in HEAVY] if not set(heavy) <= set(packages): raise ValueError("required heavy packages missing from go list") return {"g1": heavy[:1], "g2": heavy[1:], "g3": sorted(set(packages) - set(heavy))} def select(module, groups, event, head_ref, changed): selected = set() if event != "push" and not head_ref.startswith("train/"): for p in HEAVY: if any(f.startswith(p + "/") and f[len(p) + 1:] and "/" not in f[len(p) + 1:] for f in changed): selected.add(module + "/" + p) return {name: [p for p in packages if name == "g3" or p in selected] for name, packages in groups.items()} def main(argv): if len(argv) != 6: print(__doc__.strip(), file=sys.stderr) return 2 try: groups = partition(argv[1], sys.stdin.read().split()) groups = select(argv[1], groups, argv[3], argv[4], Path(argv[5]).read_text().splitlines()) out = Path(argv[2]) out.mkdir(parents=True, exist_ok=True) for name, packages in groups.items(): (out / (name + ".txt")).write_text("".join(p + "\n" for p in packages), encoding="utf-8") print(f"{name}: {len(packages)} packages") except (OSError, ValueError) as error: print(f"unit-test-shards: {error}", file=sys.stderr) return 2 return 0 if __name__ == "__main__": sys.exit(main(sys.argv))