|
4 | 4 | import os |
5 | 5 | import subprocess |
6 | 6 | import sys |
| 7 | +import tempfile |
7 | 8 |
|
8 | 9 | from src.core.updater import UpdateManager |
9 | 10 | from src.utils.translations import translator |
@@ -125,12 +126,47 @@ def install_update(self): |
125 | 126 |
|
126 | 127 | if reply == QMessageBox.Yes: |
127 | 128 | # Ejecutar el instalador y cerrar esta app |
128 | | - try: |
129 | | - # En Windows, startfile es útil para ejecutar ejecutables/instaladores |
130 | | - os.startfile(self.installer_path) |
131 | | - sys.exit(0) |
132 | | - except Exception as e: |
133 | | - QMessageBox.critical(self, self.tr("error"), self.tr("update_start_error", error=e)) |
| 129 | + # Determine if we are running as a frozen application (exe) |
| 130 | + if getattr(sys, 'frozen', False): |
| 131 | + current_exe = sys.executable |
| 132 | + new_exe = self.installer_path |
| 133 | + |
| 134 | + # Create a batch script to handle the swap |
| 135 | + # 1. Wait for current app to close |
| 136 | + # 2. Overwrite current exe with new one |
| 137 | + # 3. Restart the application |
| 138 | + # 4. cleanup |
| 139 | + batch_content = f""" |
| 140 | +@echo off |
| 141 | +timeout /t 3 /nobreak > NUL |
| 142 | +move /Y "{new_exe}" "{current_exe}" |
| 143 | +start "" "{current_exe}" |
| 144 | +del "%~f0" |
| 145 | +""" |
| 146 | + try: |
| 147 | + fd, bat_path = tempfile.mkstemp(suffix=".bat", text=True) |
| 148 | + with os.fdopen(fd, 'w') as f: |
| 149 | + f.write(batch_content) |
| 150 | + |
| 151 | + # Launch the batch file without creating a window |
| 152 | + CREATE_NO_WINDOW = 0x08000000 |
| 153 | + subprocess.Popen([bat_path], shell=True, creationflags=CREATE_NO_WINDOW) |
| 154 | + |
| 155 | + # Close the application |
| 156 | + sys.exit(0) |
| 157 | + |
| 158 | + except Exception as e: |
| 159 | + QMessageBox.critical(self, self.tr("error"), f"Error preparing update: {e}") |
| 160 | + else: |
| 161 | + # Development mode / Source code |
| 162 | + try: |
| 163 | + # Just launch the new exe to test it, but we can't overwrite source code |
| 164 | + QMessageBox.information(self, "Dev Mode", |
| 165 | + "Running from source. The downloaded EXE will be launched, but source code is not updated.") |
| 166 | + os.startfile(self.installer_path) |
| 167 | + sys.exit(0) |
| 168 | + except Exception as e: |
| 169 | + QMessageBox.critical(self, self.tr("error"), self.tr("update_start_error", error=e)) |
134 | 170 | else: |
135 | 171 | self.accept() # Cerrar diálogo pero seguir usando la app antigua temporalmente |
136 | 172 |
|
|
0 commit comments