Skip to content

Commit ace518b

Browse files
committed
Fix auto-update mechanism to properly replace executable
1 parent c7a54c2 commit ace518b

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

src/ui/dialogs/update_dialog.py

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import subprocess
66
import sys
7+
import tempfile
78

89
from src.core.updater import UpdateManager
910
from src.utils.translations import translator
@@ -125,12 +126,47 @@ def install_update(self):
125126

126127
if reply == QMessageBox.Yes:
127128
# 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))
134170
else:
135171
self.accept() # Cerrar diálogo pero seguir usando la app antigua temporalmente
136172

0 commit comments

Comments
 (0)