"""R5 REF: test of scripts/r5_ref/test_pass_commands.sh with the gate file ABSENT, on the CPU (REF_DEVICE=cpu). It never opens a real test manifest or image: it runs a copy of the script in which the manifest directory M is replaced by a folder of header-only stand-in manifests with the same file names. The gate file, the device logic, every command and every output path are those of the real script. Checks: (1) the gate file does not exist (else it stops); (2) the script reaches its last line; (3) its exit code is 1; (4) every prediction run printed REFUSED and FAILED; (5) all 6 prediction runs and 21 metrics steps were started in the order ours, rand, mix. Usage: python ref_gate_test.py """ import os import subprocess import sys real, work = sys.argv[1], sys.argv[2] D = "/path/to/TURKIC_OCR_DATA" gate = D + "/outputs/r5/ref/test_pass_started.txt" if os.path.exists(gate): sys.exit("STOP: the gate file exists; this test is only for the gate-absent case") src = open(real, encoding="utf-8").read() old = "M=$D/data/processed/r5/manifests\n" assert src.count(old) == 1, "manifest line not found exactly once" man = work + "/manifests" os.makedirs(man, exist_ok=True) for n in ("kohtd_test", "kohtd_test_p2", "kohtd_test_p2b", "kohtd_rand_test"): with open("%s/%s.tsv" % (man, n), "w", encoding="utf-8") as f: f.write("id\timage\ttext\tlang\tscript\tkind\tsource\tsplit\tgroup\tunit\n") # header only: no rows, no image test_copy = work + "/test_pass_commands_gatetest.sh" open(test_copy, "w", encoding="utf-8").write(src.replace(old, "M=%s\n" % man)) env = dict(os.environ, REF_DEVICE="cpu") r = subprocess.run(["bash", test_copy], env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=1500) out = r.stdout.decode("utf-8", "replace") open(work + "/gate_absent_output.txt", "w", encoding="utf-8").write(out) lines = out.splitlines() runs = [l.split(" ", 2)[2].rsplit(" ", 1)[0] for l in lines if l.startswith("=== RUN ")] failed = [l for l in lines if l.startswith("=== FAILED ")] refused = [l for l in lines if l.startswith("REFUSED:")] pred_runs = [x for x in runs if not x.startswith("metrics")] ok = True def check(name, cond): global ok print(("PASS " if cond else "FAIL ") + name) ok = ok and cond check("script reached its last line (final summary printed)", any(l.startswith("reference-arm test pass ended:") for l in lines)) check("exit code is 1", r.returncode == 1) check("6 prediction runs started in the order ours, rand, mix", pred_runs == ["R1 ours (i)", "R2 ours (i)", "R1 rand (ii)", "R2 rand (ii)", "R1 mix (iii)", "R2 mix (iii)"]) check("21 metrics steps started (9 ours + 3 rand + 9 mix)", sum(1 for x in runs if x.startswith("metrics")) == 9 + 3 + 9) check("every prediction run printed REFUSED (6)", len(refused) >= 6) check("every run is reported FAILED, none OK", len(failed) == len(runs) and not any(l.startswith("=== OK ") for l in lines)) print("runs started:", len(runs), "| FAILED lines:", len(failed), "| REFUSED lines:", len(refused), "| exit code:", r.returncode) print("order of prediction runs:", pred_runs) print("last 3 lines:", lines[-3:]) sys.exit(0 if ok else 1)