#!/usr/bin/env python3 """Split selected files into N deterministic, duration-balanced core or Gloo shards. Every selected file is assigned once, even without a duration or Gloo marker. Core uses the table's default_seconds for every unlisted file. For Gloo, unlisted files whose code (not strings or comments) references ``pytest.mark.gloo`` use the table's default_seconds; other unlisted files have zero weight. Marker detection affects balance only. Paths must be canonical relative files inside tests/ (symlink escapes rejected). Duplicate input paths are deduplicated. Empty input produces N empty files. """ from __future__ import annotations import argparse import ast import json import math from pathlib import Path def load_durations(path: Path) -> tuple[float, dict[str, float]]: table = json.loads(path.read_text(encoding="utf-8")) if not isinstance(table, dict) or table.get("schema_version") != 1: raise ValueError("duration table schema_version must be 1") if not isinstance(table.get("source"), str) or not table["source"].strip(): raise ValueError("duration table requires a source") default = table.get("default_seconds") files = table.get("files") if not isinstance(files, dict): raise ValueError("duration table files must be an object") for seconds in [default, *files.values()]: if (isinstance(seconds, bool) or not isinstance(seconds, (int, float)) or not math.isfinite(seconds) or seconds < 0): raise ValueError("duration weights must be finite nonnegative seconds") return default, files def has_gloo_marker(source: str) -> bool: """Return whether Python source uses the ``pytest.mark.gloo`` marker expression.""" try: tree = ast.parse(source) except SyntaxError: return False return any( isinstance(node, ast.Attribute) and ast.unparse(node) == "pytest.mark.gloo" for node in ast.walk(tree) ) def shard_tests( selected: list[str], default_seconds: float, durations: dict[str, float], *, root: Path, shards: int = 2, kind: str = "gloo", ) -> tuple[list[str], ...]: if kind not in {"gloo", "core"}: raise ValueError("kind must be gloo or core") if type(shards) is not int or shards < 1: raise ValueError("shards must be a positive integer") weighted = [] for name in sorted(set(selected)): path = Path(name) if (not name.startswith("tests/") or path.as_posix() != name or ".." in path.parts or not (root / path).is_file() or not (root / path).resolve().is_relative_to((root / "tests").resolve())): raise ValueError(f"selected path must be a file inside tests/: {name!r}") weight = durations.get(name) if weight is None: weight = default_seconds if kind == "core" or has_gloo_marker( (root / path).read_text(encoding="utf-8") ) else 0 weighted.append((name, weight)) partitions: tuple[list[str], ...] = tuple([] for _ in range(shards)) loads = [0.0] * shards for name, weight in sorted(weighted, key=lambda item: (-item[1], item[0])): side = min(range(shards), key=lambda index: (loads[index], index)) partitions[side].append(name) loads[side] += weight return partitions def main() -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--selected", type=Path, default=Path("test-results/selected-tests.txt")) parser.add_argument("--kind", choices=("gloo", "core"), default="gloo") parser.add_argument("--durations", type=Path) parser.add_argument("--output-dir", type=Path, default=Path("test-results")) parser.add_argument("--shards", type=int, default=2) args = parser.parse_args() try: default, durations = load_durations( args.durations or Path(f"ci/{args.kind}-test-durations.json"), ) shards = shard_tests( args.selected.read_text(encoding="utf-8").splitlines(), default, durations, root=Path.cwd(), shards=args.shards, kind=args.kind, ) args.output_dir.mkdir(parents=True, exist_ok=True) for side, paths in enumerate(shards): (args.output_dir / f"selected-{args.kind}-{side}.txt").write_text( "".join(f"{path}\n" for path in paths), encoding="utf-8", ) except (OSError, ValueError) as exc: print(f"PR test sharding failed: {exc}") return 1 return 0 if __name__ == "__main__": raise SystemExit(main())