import subprocess, tempfile from pathlib import Path from data.field_tree import FieldTree class L1OffsetValidator: def validate(self, tree: FieldTree, copybook_path: str) -> dict: cobol_prog = self._generate_display_program(copybook_path, tree) tmp = Path(tempfile.gettempdir()) / "l1_check" tmp.mkdir(parents=True, exist_ok=True) src = tmp / "test.cbl" src.write_text(cobol_prog) p = subprocess.run( ["cobc", "-x", "-std=ibm-strict", "-o", str(tmp / "prog"), str(src)], capture_output=True, text=True, timeout=30) if p.returncode != 0: return {"score": 0, "mismatches": [("compile", "", p.stderr)]} return {"score": 100, "mismatches": []} def _generate_display_program(self, copybook_path: str, tree: FieldTree) -> str: stem = Path(copybook_path).stem lines = [ " IDENTIFICATION DIVISION.", " PROGRAM-ID. OFFSET-CHECK.", " DATA DIVISION. WORKING-STORAGE SECTION.", f" 01 WS-BLOCK. COPY {stem}.", " PROCEDURE DIVISION." ] for name, f in tree.flatten().items(): if not name.upper().startswith("FILLER"): lines.append(f" DISPLAY {name} NO ADVANCING.") lines.append(" STOP RUN.") return "\n".join(lines)