#!/usr/bin/env python3 """The project's second judge (the check `improves`, which informs and does not gate a merge): for each length the pushed route has a code for, its largest code against the largest the default branch holds (constructions/ and routes/ of the check's own checkout, the approved version) and against the public record. A length improves when the route's code has at least one word more; it is a new record when it has at least one word more than the record. Passes when some length improves. On the default branch itself it reports what it holds and passes.""" import os import pathlib import sys from codes import INSTANCES, best_of, card, is_record, scan main_dir = pathlib.Path(sys.argv[2]) held = best_of(scan([main_dir / "constructions", main_dir / "routes"])) if os.environ.get("SS_ROUTE_DIR", "") == "routes": new = [k for k in INSTANCES if k in held and is_record(k, held[k][0])] holds = ", ".join(f"{k}: {held[k][0]}" for k in INSTANCES if k in held) or "no code" print(f"the default branch holds {holds}; " + (f"new records: {', '.join(new)}" if new else "no new record")) sys.exit(0) route = best_of(scan([pathlib.Path(sys.argv[1])])) if not route: print("no code in this route to compare") sys.exit(1) better, new = [], [] for k in (k for k in INSTANCES if k in route): size, name = route[k] h = held.get(k) up = h is None or size > h[0] better += [k] if up else [] new += [k] if is_record(k, size) else [] against = f"the default branch's {h[0]}, {size - h[0]:+d}" if h else "nothing on the default branch" print(f"{k}: {size} words ({name}) against {against}{': improves' if up else ''}; " + (f"a new record, above {card(k)}" if is_record(k, size) else f"the record is {card(k)}")) if not better: print("no improvement on the default branch") sys.exit(1) print(f"improves on the default branch: {', '.join(better)}; " + (f"new records: {', '.join(new)}" if new else "no new record"))