import os import subprocess import sys from pathlib import Path def package(): print("--- Brain CLI Packaging Tool ---") # 1. Resolve paths # We assume this script is in /scripts/ script_dir = Path(__file__).resolve().parent root_dir = script_dir.parent entry_point = root_dir / "run.py" print(f"Project Root: {root_dir}") print(f"Entry Point: {entry_point}") # 2. Verify entry point exists if not entry_point.exists(): print(f"ERROR: Cannot find '{entry_point}'") print("Please ensure you are running this from the project root or the scripts folder.") return # 3. Clean previous builds dist_dir = root_dir / "dist" build_dir = root_dir / "build" spec_file = root_dir / "brain.spec" # 4. Define PyInstaller command # --onefile: Single EXE # --paths: Add root to python path so 'import brain' works # --add-data: Include templates separator = ";" if os.name == "nt" else ":" cmd = [ "pyinstaller", "--onefile", "--name", "ever-brain", "--paths", str(root_dir), "--add-data", f"brain/templates{separator}brain/templates", "--clean", str(entry_point) ] print(f"Running Command: {' '.join(cmd)}\n") try: # Run from root_dir so relative paths in --add-data work subprocess.run(cmd, check=True, cwd=str(root_dir)) print(f"\nSUCCESS! Your CLI is ready at: {root_dir / 'dist' / 'ever-brain.exe'}") except subprocess.CalledProcessError as e: print(f"\nERROR during packaging: {e}") except Exception as e: print(f"\nAN UNEXPECTED ERROR OCCURRED: {e}") if __name__ == "__main__": package()