init: cobol-java migration verification platform v3 (42 tests, JCL module)
This commit is contained in:
+101
@@ -0,0 +1,101 @@
|
||||
"""JCL Executor — executes parsed JCL steps using platform components."""
|
||||
from pathlib import Path
|
||||
from jcl.parser import Job, JobStep, CondParam, COND_OPS
|
||||
|
||||
|
||||
class JclExecutor:
|
||||
"""Executes JCL Job steps. Uses platform CobolRunner for COBOL programs."""
|
||||
|
||||
def __init__(self, root_dir: str, cobol_dir: str, copybook_dir: str):
|
||||
self.root_dir = Path(root_dir)
|
||||
self.cobol_dir = Path(cobol_dir)
|
||||
self.copybook_dir = Path(copybook_dir)
|
||||
self.bin_dir = self.root_dir / "bin"
|
||||
self.bin_dir.mkdir(exist_ok=True)
|
||||
self.last_rc: int = 0
|
||||
self.step_rcs: dict[str, int] = {}
|
||||
self.results: dict[str, dict] = {}
|
||||
|
||||
def run(self, job: Job) -> int:
|
||||
for step in job.steps:
|
||||
rc = self._execute_step(step)
|
||||
self.last_rc = rc
|
||||
self.step_rcs[step.step_name] = rc
|
||||
|
||||
return self.last_rc
|
||||
|
||||
def _execute_step(self, step: JobStep) -> int:
|
||||
# COND check
|
||||
if step.cond and not self._check_cond(step.cond):
|
||||
self.results[step.step_name] = {"status": "SKIPPED", "cond": str(step.cond)}
|
||||
return 0
|
||||
|
||||
prog = step.program.upper()
|
||||
if prog == "SORT":
|
||||
return self._run_sort(step)
|
||||
|
||||
# Use platform's CobolRunner
|
||||
import sys
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent))
|
||||
|
||||
from runners.cobol_runner import CobolRunner
|
||||
|
||||
cbl_path = self.cobol_dir / f"{step.program}.cbl"
|
||||
runner = CobolRunner()
|
||||
build = runner.compile(str(cbl_path))
|
||||
if not build.success:
|
||||
self.results[step.step_name] = {"status": "COMPILE_ERROR", "log": build.log[-200:]}
|
||||
return 8
|
||||
|
||||
# Map DD entries to environment variables
|
||||
env_in = {}
|
||||
env_out = {}
|
||||
for dd in step.dd_entries:
|
||||
name = dd.dd_name.upper()
|
||||
if dd.dsn:
|
||||
path = self._resolve_path(dd.dsn)
|
||||
if name in ("TRANSIN", "MEMBER", "VALIDIN", "RATE", "BILLING"):
|
||||
env_in[name] = str(path)
|
||||
elif name in ("VALIDOUT", "REJECT", "REPORTERR", "CALCOUT", "STMT", "SUMMARY"):
|
||||
env_out[name] = str(path)
|
||||
|
||||
input_path = env_in.get(list(env_in.keys())[0], "")
|
||||
output_path = env_out.get(list(env_out.keys())[0], str(self.root_dir / "data" / "work" / f"{step.step_name.lower()}_out.bin"))
|
||||
|
||||
run = runner.run(build.artifact_path, input_path, output_path)
|
||||
self.results[step.step_name] = {
|
||||
"status": "OK" if run.success else "ERROR",
|
||||
"rc": 0 if run.success else 12
|
||||
}
|
||||
return 0 if run.success else 12
|
||||
|
||||
def _check_cond(self, cond: CondParam) -> bool:
|
||||
if cond.step_name:
|
||||
target = self.step_rcs.get(cond.step_name, 0)
|
||||
return not COND_OPS.get(cond.operator, lambda x, y: False)(target, cond.code)
|
||||
return True
|
||||
|
||||
def _run_sort(self, step: JobStep) -> int:
|
||||
import subprocess
|
||||
infile = None
|
||||
outfile = None
|
||||
for dd in step.dd_entries:
|
||||
n = dd.dd_name.upper()
|
||||
if n == "SORTIN" and dd.dsn:
|
||||
infile = self._resolve_path(dd.dsn)
|
||||
elif n == "SORTOUT" and dd.dsn:
|
||||
outfile = self._resolve_path(dd.dsn)
|
||||
|
||||
if not infile or not outfile:
|
||||
return 12
|
||||
|
||||
if infile.exists():
|
||||
lines = sorted(infile.read_text().splitlines())
|
||||
outfile.write_text("\n".join(lines))
|
||||
self.results[step.step_name] = {"status": "OK", "rc": 0}
|
||||
return 0
|
||||
|
||||
def _resolve_path(self, dsn: str) -> Path:
|
||||
import re
|
||||
dsn = re.sub(r"\(\+?\d+\)", "", dsn).strip(".")
|
||||
return (self.root_dir / dsn.lstrip("/")).resolve()
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
"""JCL Parser — Phase 2. Parses JCL scripts into structured Job objects."""
|
||||
import re
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Optional
|
||||
|
||||
|
||||
@dataclass
|
||||
class DDEntry:
|
||||
dd_name: str
|
||||
dsn: Optional[str] = None
|
||||
disp: Optional[str] = None
|
||||
sysout: Optional[str] = None
|
||||
inline_data: list[str] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass
|
||||
class CondParam:
|
||||
code: int
|
||||
operator: str # EQ, NE, GT, GE, LT, LE
|
||||
step_name: Optional[str] = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class JobStep:
|
||||
step_name: str
|
||||
program: str
|
||||
dd_entries: list[DDEntry] = field(default_factory=list)
|
||||
cond: Optional[CondParam] = None
|
||||
parm: Optional[str] = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class Job:
|
||||
job_name: str
|
||||
steps: list[JobStep] = field(default_factory=list)
|
||||
|
||||
|
||||
COND_OPS = {
|
||||
"EQ": lambda rc, code: rc == code, "NE": lambda rc, code: rc != code,
|
||||
"GT": lambda rc, code: rc > code, "GE": lambda rc, code: rc >= code,
|
||||
"LT": lambda rc, code: rc < code, "LE": lambda rc, code: rc <= code,
|
||||
}
|
||||
|
||||
|
||||
def parse_jcl(filepath: str) -> Optional[Job]:
|
||||
"""Parse a JCL file into a Job object."""
|
||||
with open(filepath, "r", encoding="utf-8") as f:
|
||||
lines = _merge_continuations(f.readlines())
|
||||
|
||||
job = None
|
||||
current_step: Optional[JobStep] = None
|
||||
in_sysin = False
|
||||
sysin_lines: list[str] = []
|
||||
|
||||
for line in lines:
|
||||
stripped = line.strip()
|
||||
if stripped.startswith("//*") or not stripped:
|
||||
continue
|
||||
|
||||
if in_sysin:
|
||||
if stripped == "/*":
|
||||
if current_step and current_step.dd_entries:
|
||||
current_step.dd_entries[-1].inline_data = sysin_lines
|
||||
sysin_lines = []; in_sysin = False
|
||||
else:
|
||||
sysin_lines.append(stripped)
|
||||
continue
|
||||
|
||||
if not stripped.startswith("//"):
|
||||
continue
|
||||
|
||||
content = stripped[2:].strip()
|
||||
|
||||
# JOB
|
||||
if re.search(r"\bJOB\b", content, re.IGNORECASE):
|
||||
job = Job(job_name=content.split()[0])
|
||||
continue
|
||||
|
||||
# EXEC
|
||||
m = re.match(r"(\w+)\s+EXEC\s+(?:PGM=)?(\w+)", content, re.IGNORECASE)
|
||||
if m:
|
||||
step_name, program = m.group(1), m.group(2)
|
||||
cond = None
|
||||
cm = re.search(r"COND=\s*\(\s*(\d+)\s*,\s*(\w+)", content, re.IGNORECASE)
|
||||
if cm:
|
||||
cond = CondParam(code=int(cm.group(1)), operator=cm.group(2).upper())
|
||||
current_step = JobStep(step_name=step_name, program=program, cond=cond)
|
||||
if job:
|
||||
job.steps.append(current_step)
|
||||
continue
|
||||
|
||||
# DD
|
||||
m = re.match(r"(\w+)\s+DD\s*(.*)", content, re.IGNORECASE)
|
||||
if m and current_step is not None:
|
||||
dd = DDEntry(dd_name=m.group(1))
|
||||
params = m.group(2)
|
||||
dm = re.search(r"DSN=\s*([^\s,]+)", params, re.IGNORECASE)
|
||||
if dm:
|
||||
dd.dsn = dm.group(1)
|
||||
if dd.dd_name.upper() == "SYSIN" and "*" in params:
|
||||
in_sysin = True
|
||||
current_step.dd_entries.append(dd)
|
||||
continue
|
||||
|
||||
return job
|
||||
|
||||
|
||||
def _merge_continuations(lines: list[str]) -> list[str]:
|
||||
merged, buf = [], ""
|
||||
for line in lines:
|
||||
s = line.rstrip("\n\r")
|
||||
buf = (buf + s) if buf else s
|
||||
if s.rstrip().endswith(",") and not s.strip().startswith("//*"):
|
||||
continue
|
||||
merged.append(buf); buf = ""
|
||||
if buf:
|
||||
merged.append(buf)
|
||||
return merged
|
||||
Reference in New Issue
Block a user