feat: DB管线补全 + 新增orchestrator_db/program_schema/to_sql + 清理临时脚本

This commit is contained in:
hangshuo652
2026-07-11 14:55:52 +08:00
parent 40e8a50ab4
commit af37e33b98
32 changed files with 3232 additions and 255 deletions
+116 -16
View File
@@ -3,14 +3,15 @@ import re, struct
from pathlib import Path
from typing import Any
def analyze_fd_layout(source_text: str) -> dict[str, dict]:
"""From preprocessed COBOL source, extract FD file layouts."""
from .read import parse_file_control, parse_file_section, parse_data_division, extract_data_division, scan_open_statements
def analyze_fd_layout(source_text: str, copybook_dirs: list[str] = None) -> dict[str, dict]:
"""From COBOL source, extract FD file layouts."""
from .read import preprocess, parse_file_control, parse_file_section, parse_data_division, extract_data_division, scan_open_statements
fc = parse_file_control(source_text) if source_text else {}
fs = parse_file_section(source_text) if source_text else {}
ops = scan_open_statements(source_text) if source_text else {}
dd = extract_data_division(source_text)
pp = preprocess(source_text, extra_search_paths=copybook_dirs)
fc = parse_file_control(pp) if pp else {}
fs = parse_file_section(pp) if pp else {}
ops = scan_open_statements(pp) if pp else {}
dd = extract_data_division(pp)
all_fields = parse_data_division(dd) if dd else []
layouts = {}
@@ -25,6 +26,7 @@ def analyze_fd_layout(source_text: str) -> dict[str, dict]:
if f.name == rec_name:
found = True
rec_level = f.level
rec_field_obj = f
continue
if found:
if f.level is not None and f.level <= rec_level:
@@ -37,14 +39,45 @@ def analyze_fd_layout(source_text: str) -> dict[str, dict]:
else:
length = 0
ftype = pi.type if pi else "unknown"
usage = f.usage if f.usage else None
children.append({
"name": f.name, "pic": str(f.pic or ""),
"type": ftype, "length": length, "offset": offset,
"usage": usage,
"pic_info": {
"type": f.pic_info.type if f.pic_info else "unknown",
"digits": f.pic_info.digits if f.pic_info else 0,
"decimal": f.pic_info.decimal if f.pic_info else 0,
"length": f.pic_info.length if f.pic_info else 0,
"signed": f.pic_info.signed if f.pic_info else False,
} if f.pic_info else None,
})
offset += length
# If record has no elementary children but the 01-level has PIC info
# (e.g. 01 SYSINREC PIC X(080)), use it as a single opaque field
if not children and rec_field_obj and rec_field_obj.pic_info:
pi = rec_field_obj.pic_info
length = pi.length or 0
if length > 0:
children.append({
"name": rec_field_obj.name,
"pic": str(rec_field_obj.pic or ""),
"type": "alphanumeric",
"length": length,
"offset": 0,
"usage": None,
"pic_info": {
"type": "alphanumeric",
"digits": 0,
"decimal": 0,
"length": length,
"signed": False,
},
})
offset = length
records.append({"record_name": rec_name, "fields": children, "record_length": offset})
assign_to = fc.get(fd_name, {}).get("assign_to", fd_name)
assign_to = fc.get(fd_name, {}).get("assign", fd_name)
layouts[assign_to] = {
"fd_name": fd_name, "records": records,
"direction": ops.get(fd_name, "INPUT"),
@@ -70,6 +103,23 @@ def select_records_for_file(records: list[dict], layout: dict) -> list[dict]:
def _format_value(value: Any, field: dict) -> bytes:
"""Format a value for COBOL fixed-length storage."""
from . import file_io
usage = field.get("usage")
if usage in ("COMP", "COMP-3", "BINARY", "PACKED-DECIMAL"):
pic_info = field.get("pic_info") or {}
packed = file_io.pack_value(str(value) if value is not None else "", {
"usage": usage,
"pic_info": pic_info,
})
want_len = file_io.get_storage_length({
"usage": usage,
"pic_info": pic_info,
})
if len(packed) < want_len:
packed = packed.rjust(want_len, b'\x00')
return packed[:want_len]
ftype = field["type"]
length = field["length"]
val = str(value) if value is not None else ""
@@ -80,7 +130,6 @@ def _format_value(value: Any, field: dict) -> bytes:
except (ValueError, TypeError):
num = 0
num = abs(num)
# Truncate to fit PIC digits
max_val = 10 ** length - 1
if num > max_val:
num = max_val
@@ -104,8 +153,7 @@ def write_flat_file(records: list[dict], layout: dict, outpath: Path, field_filt
return
# Pick the record with the most fields (best coverage for multi-record FDs)
rec = max(layout["records"], key=lambda r: (len(r["fields"]), r["record_length"]))
rec_len = rec["record_length"]
if rec_len == 0:
if rec["record_length"] == 0:
return
rec_fields = rec["fields"]
@@ -114,23 +162,25 @@ def write_flat_file(records: list[dict], layout: dict, outpath: Path, field_filt
with open(outpath, "wb") as f:
for row in records:
buf = bytearray(rec_len)
buf = bytearray()
for field in rec_fields:
val = row.get(field["name"], "")
formatted = _format_value(val, field)
end = min(field["offset"] + len(formatted), rec_len)
buf[field["offset"]:end] = formatted[:end - field["offset"]]
buf.extend(formatted)
f.write(buf)
def write_all_files(records: list[dict], source_text: str, outdir: Path, prefix: str = ""):
def write_all_files(records: list[dict], source_text: str, outdir: Path, prefix: str = "", copybook_dirs: list[str] = None):
"""Analyze source, write flat files for all INPUT FDs."""
outdir = Path(outdir)
layouts = analyze_fd_layout(source_text)
layouts = analyze_fd_layout(source_text, copybook_dirs=copybook_dirs)
written = []
for filename, layout in layouts.items():
if layout["direction"] == "OUTPUT":
continue
# Skip SYSIN files — handled separately by write_sysin_file
if layout["fd_name"] == "SYSINFILE":
continue
fnames = set()
for rec in layout["records"]:
for f in rec["fields"]:
@@ -152,3 +202,53 @@ def write_all_files(records: list[dict], source_text: str, outdir: Path, prefix:
write_flat_file(filtered, layout, outpath)
written.append((filename, outpath, len(filtered)))
return written
def write_sysin_file(records: list[dict], source_text: str, outdir: Path, prefix: str = "", copybook_dirs: list[str] = None):
"""Generate SYSIN configuration card file from FD layout + generated records."""
outdir = Path(outdir)
layouts = analyze_fd_layout(source_text, copybook_dirs=copybook_dirs)
sysin_filename = None
sysin_layout = None
for filename, layout in layouts.items():
if layout["fd_name"] == "SYSINFILE":
sysin_filename = filename
sysin_layout = layout
break
if not sysin_layout:
return None
# Determine record length from layout
rec_length = 0
for rec in sysin_layout["records"]:
if rec["record_length"] > rec_length:
rec_length = rec["record_length"]
if rec_length == 0:
rec_length = 80
# Extract unique employee IDs from records (skip sentinel '00000000')
emp_ids = sorted(set(
r.get("R01EMP-ID", "") for r in records
if r.get("R01EMP-ID") and r["R01EMP-ID"] != "00000000"
))
# Limit to 8 per T card (78 chars of data: 8 * (8+1) = 72 fits)
emp_ids = emp_ids[:8]
# Build SYSIN card records
# Card format: position 1 = type, position 2 = space (ignored), position 3+ = data
lines = [
f"P YEAR-MONTH=202607", # Period card
f"M MODE=NORMAL", # Mode card
]
if emp_ids:
lines.append(f"T {','.join(emp_ids)}") # Target card
# Write as fixed-length flat file
outpath = outdir / (prefix + sysin_filename)
with open(outpath, "wb") as f:
for line in lines:
buf = line.encode("ascii", errors="replace")
if len(buf) < rec_length:
buf = buf.ljust(rec_length, b" ")
f.write(buf[:rec_length])
return outpath