Files
2125_GCE/gx_cli/commands.py
T

132 lines
3.8 KiB
Python
Raw Normal View History

import sys
from pathlib import Path
from gx_compiler.compiler import GXCompiler, CompilerError
from runtime_executor.integration import run_gx_with_summary
from runtime_executor.gx_loader import load_gx, GXLoaderError
def cmd_compile(source_path: str, output_path: str = None) -> int:
try:
source = Path(source_path)
if not source.exists():
print(f"Error: Source file not found: {source_path}", file=sys.stderr)
return 1
if output_path is None:
output_path = str(source.with_suffix(".gx"))
print(f"Compiling {source_path}{output_path}")
GXCompiler.compile_to_gx(
source_path=source_path,
output_path=output_path,
verbose=False
)
output = Path(output_path)
size = output.stat().st_size
print(f"Success: {output_path} ({size} bytes)")
return 0
except CompilerError as e:
print(f"Compilation error: {e}", file=sys.stderr)
return 1
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
return 1
def cmd_run(gx_path: str) -> int:
try:
gx = Path(gx_path)
if not gx.exists():
print(f"Error: File not found: {gx_path}", file=sys.stderr)
return 1
print(f"Running {gx_path}")
summary = run_gx_with_summary(gx_path)
if summary['success']:
print(f"Success: Executed {summary['segments_executed']} segments in {summary['duration']:.4f}s")
return 0
else:
print(f"Failed: {len(summary['errors'])} error(s)")
for err in summary['errors']:
print(f" - {err}", file=sys.stderr)
return 1
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
return 1
def cmd_inspect(gx_path: str) -> int:
try:
gx = Path(gx_path)
if not gx.exists():
print(f"Error: File not found: {gx_path}", file=sys.stderr)
return 1
manifest, payload = load_gx(gx_path)
print("\n[Manifest]")
print(f" version: {manifest.get('version')}")
print(f" source_file: {manifest.get('source_file')}")
print(f" source_type: {manifest.get('source_type')}")
print(f" origin: {manifest.get('origin')}")
print(f" compression_model: {manifest.get('compression_model')}")
print(f" contributor: {manifest.get('contributor')}")
segments = manifest.get('codex_lineage', {}).get('segments', [])
print(f"\n[Segments] ({len(segments)})")
for seg in segments:
seg_id = seg.get('id')
start = seg.get('start')
end = seg.get('end')
print(f" {seg_id}: lines {start}-{end}")
print(f"\n[Payload] {len(payload)} bytes (compressed)")
return 0
except GXLoaderError as e:
print(f"Load error: {e}", file=sys.stderr)
return 1
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
return 1
def cmd_summary(gx_path: str) -> int:
try:
gx = Path(gx_path)
if not gx.exists():
print(f"Error: File not found: {gx_path}", file=sys.stderr)
return 1
manifest, payload = load_gx(gx_path)
segments = manifest.get('codex_lineage', {}).get('segments', [])
print(f"GX File: {gx_path}")
print(f"Source: {manifest.get('source_file')}")
print(f"Type: {manifest.get('source_type')}")
print(f"Segments: {len(segments)}")
print(f"Compressed: {len(payload)} bytes")
print(f"Version: {manifest.get('version')}")
return 0
except GXLoaderError as e:
print(f"Load error: {e}", file=sys.stderr)
return 1
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
return 1