2025-08-05 14:38:54 +04:00

136 lines
5.0 KiB
Python

#!/usr/bin/env python3
"""
PLC Routines Generator - Main Entry Point
This script provides backward compatibility while using the new unified CLI internally.
For new usage, prefer the unified CLI: python -m src.unified_cli
"""
from __future__ import annotations
import sys
import argparse
from pathlib import Path
import subprocess
def run_l5x_to_acd_compilation(safety_l5x: Path, main_l5x: Path) -> bool:
"""Run the L5X to ACD compilation step."""
l5x2acd_dir = Path("../L5X2ACD Compiler")
l5x2acd_script = l5x2acd_dir / "l5x_to_acd.py"
if not l5x2acd_script.exists():
print(f"ERROR: L5X2ACD compiler not found at {l5x2acd_script}")
return False
if not safety_l5x.exists():
print(f"ERROR: SafetyProgram L5X not found at {safety_l5x}")
return False
if not main_l5x.exists():
print(f"ERROR: MainProgram L5X not found at {main_l5x}")
return False
print(f"\n=== Compiling L5X files to ACD ===")
print(f"Safety L5X: {safety_l5x}")
print(f"Main L5X: {main_l5x}")
try:
# Run the L5X2ACD compiler
result = subprocess.run([
sys.executable,
str(l5x2acd_script),
str(safety_l5x.resolve()),
str(main_l5x.resolve())
], cwd=l5x2acd_dir, capture_output=True, text=True)
if result.returncode == 0:
print("SUCCESS: L5X to ACD compilation completed successfully")
print(result.stdout)
return True
else:
print("ERROR: L5X to ACD compilation failed")
print("STDOUT:", result.stdout)
print("STDERR:", result.stderr)
return False
except Exception as e:
print(f"ERROR: Error running L5X2ACD compiler: {e}")
return False
def main() -> None:
"""Main entry point that maps old generate_all.py behavior to new unified CLI."""
# Parse arguments to maintain backward compatibility
parser = argparse.ArgumentParser(description="Generate PLC routine artifacts")
parser.add_argument('--config', type=Path, default=Path(__file__).parent.parent / 'generator_config.json', help='Configuration file')
parser.add_argument('--excel-file', type=Path, help='Excel file to process')
parser.add_argument('--output-dir', type=Path, help='Output directory')
parser.add_argument('--log-level', choices=['DEBUG', 'INFO', 'WARNING', 'ERROR'], default='INFO', help='Log level')
parser.add_argument('--log-file', type=Path, help='Log file path')
parser.add_argument('--project-name', help='Project name (for compatibility, not used)')
parser.add_argument('--zones', help='JSON string with zones data')
# New compilation option
parser.add_argument('--compile-acd', action='store_true', help='Compile L5X files to ACD after generation')
# Legacy argument for compatibility
parser.add_argument('--desc-ip-mode', action='store_true', help='(Deprecated) DESC_IP extraction is now the default')
args = parser.parse_args()
if args.desc_ip_mode:
print("WARNING: --desc-ip-mode flag is deprecated. DESC_IP extraction is now the default mode.")
# Construct arguments for unified CLI
unified_args = ['--log-level', args.log_level]
if args.config:
unified_args.extend(['--config', str(args.config)])
if args.excel_file:
unified_args.extend(['--excel-file', str(args.excel_file)])
if args.zones:
unified_args.extend(['--zones', args.zones])
# Add the 'all' command
unified_args.append('all')
if args.output_dir:
unified_args.extend(['--output-dir', str(args.output_dir)])
# Import and call the unified CLI
from src.unified_cli import main as unified_main
print("=== PLC Routines Generator (Refactored) ===")
print("Using unified CLI internally...")
print()
try:
# Generate L5X files
unified_main(unified_args)
# If compilation requested, run L5X2ACD compiler
if args.compile_acd:
# Determine output directory
output_dir = args.output_dir or Path('.')
safety_l5x = output_dir / "SafetyProgram_Generated.L5X"
main_l5x = output_dir / "MainProgram_Generated.L5X"
success = run_l5x_to_acd_compilation(safety_l5x, main_l5x)
if not success:
print("\nERROR: Compilation failed")
sys.exit(1)
else:
print("\nSUCCESS: Complete PLC generation and compilation successful!")
else:
print("\nSUCCESS: L5X generation completed. Use --compile-acd to also generate ACD files.")
except SystemExit as e:
if e.code != 0:
print(f"\nERROR: Generation failed with exit code {e.code}")
sys.exit(e.code)
if __name__ == '__main__':
main()