#!/usr/bin/env python3 """Manual test of batch processing pipeline.""" import json, sys, shutil, traceback, asyncio from pathlib import Path sys.path.insert(0, str(Path(__file__).parent.parent)) from batch import read_xlsx_xml, extract_zip, analyze_structure import openpyxl # Setup source_dir = "/tmp/batch_manual/source" Path(source_dir).mkdir(parents=True, exist_ok=True) source_files = extract_zip("/tmp/smetas.zip", source_dir) target_orig = "/tmp/ССР_сводный.xlsx" target_file = "/tmp/batch_manual/result.xlsx" shutil.copy2(target_orig, target_file) print(f"Source files: {len(source_files)}") print(f"Target: {target_file}") # Get script from GLM task = "B5=шифр, H29=строит работы, H30=монтаж, H31=оборуд, H32=прочие (тыс.руб). Внести в сводный файл по шифру (столбец B), значения в столбцы D,E,F,G" analysis = asyncio.run(analyze_structure(source_files, target_orig, task)) script = analysis.get("python_script", "") desc = analysis.get("description", "") print(f"Description: {desc}") print(f"Script ({len(script)} chars):") print(script) print("---") if not script: print("NO SCRIPT. Analysis:", json.dumps(analysis, ensure_ascii=False, indent=2)[:1000]) sys.exit(1) # Fix common GLM mistakes import re script_fixed = script.replace("job['errors'] += 1", "job['errors'].append('error')") script_fixed = script_fixed.replace('job["errors"] += 1', 'job["errors"].append("error")') script_fixed = re.sub(r"wb_target\['([^']+)'\]", r"(wb_target['\1'] if '\1' in wb_target.sheetnames else wb_target.active)", script_fixed) script_fixed = re.sub(r'wb_target\["([^"]+)"\]', r'(wb_target["\1"] if "\1" in wb_target.sheetnames else wb_target.active)', script_fixed) # If wrapped in def main(), call it if "def main()" in script_fixed and "if __name__" in script_fixed: script_fixed = re.sub(r"if __name__\s*==\s*['\"]__main__['\"]\s*:\s*\n\s*main\(\)", "", script_fixed) script_fixed += "\nmain()\n" # Execute job = {"processed": 0, "errors": []} try: exec(script_fixed, { "__builtins__": __builtins__, "source_files": source_files, "target_file": target_file, "job": job, "read_xlsx_xml": read_xlsx_xml, "Path": Path, "openpyxl": openpyxl, "json": json, }) print(f"\nSUCCESS! Processed: {job['processed']}/{len(source_files)}") print(f"Errors: {len(job['errors'])}") except Exception: traceback.print_exc() print(f"\nFAILED. Processed: {job['processed']}") # Verify result wb = openpyxl.load_workbook(target_file) ws = wb.active print(f"\nResult (sheet '{ws.title}'):") for r in range(3, 14): vals = [] for c in range(1, 9): v = ws.cell(r, c).value vals.append(str(v) if v is not None else "") print(" Row %d: %s" % (r, " | ".join(vals)))