import subprocess import json import os import sys # ---- Git helpers ------------------------------------------------------------ def run_git(*args) -> str | None: try: return subprocess.check_output(["git", *args], stderr=subprocess.STDOUT).decode().strip() except Exception as e: print(f"❌ git {' '.join(args)} failed: {e}") return None def get_commit_hash() -> str: return run_git("rev-parse", "HEAD") def get_current_branch() -> str: # Normal case name = run_git("rev-parse", "--abbrev-ref", "HEAD") if name and name != "HEAD": return name # Fallback (newer git) name = run_git("branch", "--show-current") if name: return name return None def get_remote_url() -> str: url = run_git("remote", "get-url", "origin") return url # ---- Config update ---------------------------------------------------------- def update_config(config_path: str, remote_url: str, branch_name: str, commit_hash: str): if not os.path.exists(config_path): print(f"Config file not found: {config_path}") sys.exit(1) with open(config_path, "r", encoding="utf-8") as f: config = json.load(f) config['GIT'] = { "REPO_URL": remote_url, "BRANCH_NAME": branch_name, "COMMIT_HASH": commit_hash, } with open(config_path, "w", encoding="utf-8") as f: json.dump(config, f, indent=4) print(f"✔ Updated {config_path} with Git values") # ---- Build ------------------------------------------------------------------ def run_pyinstaller(): script_dir = os.path.dirname(os.path.abspath(__file__)) command = [ "pyinstaller", "--distpath", "..", "--noconfirm", "--paths", "common", "--collect-all", "screens", "--add-data", os.path.join(script_dir, "common", "static") + ";static", "--add-data", os.path.join(script_dir, "Projects") + ";Projects", "main.py", ] print(f"Running build: {' '.join(command)}") subprocess.run(command, check=True) # ==== MAIN =================================================================== if __name__ == "__main__": remote_url = get_remote_url() if not remote_url: print("❌ Failed to get remote URL") sys.exit(1) branch_name = get_current_branch() if not branch_name: print("❌ Failed to get branch name") sys.exit(1) commit_hash = get_commit_hash() if not commit_hash: print("❌ Failed to get commit hash") sys.exit(1) config_file_path = os.path.join(os.path.dirname(os.getcwd()), "Final", "config.json") update_config( config_file_path, remote_url, branch_name, commit_hash, ) run_pyinstaller() print("✔ Build completed successfully")