|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# Copyright 2026 Ague Samuel Amen |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +""" |
| 17 | +CX_Freeze Engine for PyCompiler_ARK. |
| 18 | +
|
| 19 | +This engine handles compilation of Python scripts using CX_Freeze, |
| 20 | +supporting onefile mode, windowed applications, and various customization options. |
| 21 | +""" |
| 22 | + |
| 23 | +from __future__ import annotations |
| 24 | + |
| 25 | +import os |
| 26 | +import platform |
| 27 | +import sys |
| 28 | +from typing import Optional |
| 29 | + |
| 30 | +from Core.engines_loader.base import CompilerEngine |
| 31 | +from Core.engines_loader.registry import register |
| 32 | + |
| 33 | + |
| 34 | +@register |
| 35 | +class CXFreezeEngine(CompilerEngine): |
| 36 | + """ |
| 37 | + CX_Freeze compilation engine. |
| 38 | + |
| 39 | + Features: |
| 40 | + - Onefile and onedir modes |
| 41 | + - Windowed/console mode selection |
| 42 | + - Custom output directory |
| 43 | + - Automatic venv detection and use |
| 44 | + - Icon specification |
| 45 | + """ |
| 46 | + |
| 47 | + id: str = "cx_freeze" |
| 48 | + name: str = "CX_Freeze" |
| 49 | + version: str = "1.0.0" |
| 50 | + required_core_version: str = "1.0.0" |
| 51 | + required_sdk_version: str = "1.0.0" |
| 52 | + |
| 53 | + @property |
| 54 | + def required_tools(self) -> list[str]: |
| 55 | + return ["cx_freeze"] |
| 56 | + |
| 57 | + def preflight(self, gui, file: str) -> bool: |
| 58 | + """Check if CX_Freeze is available in the venv.""" |
| 59 | + try: |
| 60 | + venv_manager = getattr(gui, "venv_manager", None) |
| 61 | + if not venv_manager: |
| 62 | + return True # Let build_command fail instead |
| 63 | + |
| 64 | + venv_path = venv_manager.resolve_project_venv() |
| 65 | + if not venv_path: |
| 66 | + return True # Let build_command fail instead |
| 67 | + |
| 68 | + if not venv_manager.has_tool_binary(venv_path, "cx_freeze"): |
| 69 | + # Try to install cx_freeze |
| 70 | + venv_manager.ensure_tools_installed(venv_path, ["cx_freeze"]) |
| 71 | + return False # Will be retried after installation |
| 72 | + |
| 73 | + return True |
| 74 | + except Exception: |
| 75 | + return True # Let build_command handle errors |
| 76 | + |
| 77 | + def build_command(self, gui, file: str) -> list[str]: |
| 78 | + """Build the CX_Freeze command line.""" |
| 79 | + try: |
| 80 | + venv_manager = getattr(gui, "venv_manager", None) |
| 81 | + |
| 82 | + # Resolve venv python |
| 83 | + if venv_manager: |
| 84 | + venv_path = venv_manager.resolve_project_venv() |
| 85 | + if venv_path: |
| 86 | + python_path = venv_manager.python_path(venv_path) |
| 87 | + else: |
| 88 | + python_path = sys.executable |
| 89 | + else: |
| 90 | + python_path = sys.executable |
| 91 | + |
| 92 | + # Start with python -m cx_Freeze |
| 93 | + cmd = [python_path, "-m", "cx_Freeze"] |
| 94 | + |
| 95 | + # Get options from GUI - use existing UI widgets from .ui file |
| 96 | + # Onefile vs Onedir (CX_Freeze uses --onefile) |
| 97 | + onefile = getattr(gui, "opt_onefile", None) |
| 98 | + if onefile and onefile.isChecked(): |
| 99 | + cmd.append("--onefile") |
| 100 | + else: |
| 101 | + # CX_Freeze defaults to onedir (build-exe directory) |
| 102 | + cmd.append("--build-exe") |
| 103 | + |
| 104 | + # Windowed (no console) - CX_Freeze uses --console or implies GUI |
| 105 | + windowed = getattr(gui, "opt_windowed", None) |
| 106 | + if windowed and windowed.isChecked(): |
| 107 | + # CX_Freeze uses --gui-name for windowed apps |
| 108 | + cmd.append("--gui-name=pythonw") |
| 109 | + |
| 110 | + # Clean build |
| 111 | + clean = getattr(gui, "opt_clean", None) |
| 112 | + if clean and clean.isChecked(): |
| 113 | + cmd.append("--clean") |
| 114 | + |
| 115 | + # Output directory - CX_Freeze uses --build-dir |
| 116 | + output_dir = getattr(gui, "output_dir_input", None) |
| 117 | + if output_dir and output_dir.text().strip(): |
| 118 | + cmd.extend(["--build-dir", output_dir.text().strip()]) |
| 119 | + |
| 120 | + # Name - CX_Freeze uses --name |
| 121 | + name_input = getattr(gui, "output_name_input", None) |
| 122 | + if name_input and name_input.text().strip(): |
| 123 | + cmd.extend(["--name", name_input.text().strip()]) |
| 124 | + |
| 125 | + # Add the target file |
| 126 | + cmd.append(file) |
| 127 | + |
| 128 | + return cmd |
| 129 | + |
| 130 | + except Exception as e: |
| 131 | + try: |
| 132 | + if hasattr(gui, "log"): |
| 133 | + gui.log.append(f"❌ Erreur construction commande CX_Freeze: {e}\n") |
| 134 | + except Exception: |
| 135 | + pass |
| 136 | + return [] |
| 137 | + |
| 138 | + def program_and_args(self, gui, file: str) -> Optional[tuple[str, list[str]]]: |
| 139 | + """Return the program and args for QProcess.""" |
| 140 | + cmd = self.build_command(gui, file) |
| 141 | + if not cmd: |
| 142 | + return None |
| 143 | + return cmd[0], cmd[1:] |
| 144 | + |
| 145 | + def environment(self, gui, file: str) -> Optional[dict[str, str]]: |
| 146 | + """Return environment variables for the compilation process.""" |
| 147 | + try: |
| 148 | + env = {} |
| 149 | + |
| 150 | + # Set PYTHONIOENCODING for proper output handling |
| 151 | + env["PYTHONIOENCODING"] = "utf-8" |
| 152 | + |
| 153 | + # Disable PYTHONUTF8 mode to avoid conflicts |
| 154 | + env["PYTHONUTF8"] = "0" |
| 155 | + |
| 156 | + # Set LC_ALL for consistent output |
| 157 | + env["LC_ALL"] = "C" |
| 158 | + |
| 159 | + return env if env else None |
| 160 | + except Exception: |
| 161 | + return None |
| 162 | + |
| 163 | + def on_success(self, gui, file: str) -> None: |
| 164 | + """Handle successful compilation.""" |
| 165 | + try: |
| 166 | + # Log success message with output location |
| 167 | + output_dir = getattr(gui, "output_dir_input", None) |
| 168 | + if output_dir and output_dir.text().strip(): |
| 169 | + try: |
| 170 | + if hasattr(gui, "log"): |
| 171 | + gui.log.append(f"📁 Compilation CX_Freeze terminée. Sortie dans: {output_dir.text().strip()}\n") |
| 172 | + except Exception: |
| 173 | + pass |
| 174 | + except Exception: |
| 175 | + pass |
| 176 | + |
| 177 | + def create_tab(self, gui): |
| 178 | + """ |
| 179 | + Return None to use existing UI widgets from the .ui file. |
| 180 | + The CX_Freeze tab is already defined in the UI with opt_onefile, |
| 181 | + opt_windowed, opt_clean, output_dir_input, etc. |
| 182 | + """ |
| 183 | + return None |
| 184 | + |
| 185 | + def get_log_prefix(self, file_basename: str) -> str: |
| 186 | + return f"CX_Freeze ({self.version})" |
| 187 | + |
| 188 | + def should_compile_file(self, gui, file: str, selected_files: list[str], python_files: list[str]) -> bool: |
| 189 | + """Determine if a file should be included in the compilation queue.""" |
| 190 | + # Skip non-Python files |
| 191 | + if not file.endswith(".py"): |
| 192 | + return False |
| 193 | + return True |
| 194 | + |
| 195 | + def apply_i18n(self, gui, tr: dict) -> None: |
| 196 | + """Apply internationalization translations to the engine UI.""" |
| 197 | + pass |
| 198 | + |
0 commit comments