-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdater.py
More file actions
278 lines (216 loc) · 9.54 KB
/
Copy pathupdater.py
File metadata and controls
278 lines (216 loc) · 9.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python3
"""
Xbox Backup Manager Updater
Safely replaces the main application executable with an updated version.
"""
import logging
import os
import shutil
import subprocess
import sys
import time
from pathlib import Path
import psutil
# Configuration constants
EXE_NAME = "XboxBackupManager.exe"
MAX_WAIT_TIME = 30 # Maximum seconds to wait for process to exit
PROCESS_CHECK_INTERVAL = 0.5 # Seconds between process checks
UPDATE_MARKER_FILE = ".update_in_progress"
# Set up logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[logging.FileHandler("updater.log"), logging.StreamHandler(sys.stdout)],
)
logger = logging.getLogger(__name__)
class UpdaterError(Exception):
"""Custom exception for updater-specific errors"""
pass
class XboxBackupManagerUpdater:
"""Safe updater for Xbox Backup Manager application"""
def __init__(self, temp_exe_path: str):
self.temp_exe_path = Path(temp_exe_path).resolve()
self.target_exe_path = Path(EXE_NAME).resolve()
self.backup_exe_path = Path(f"{EXE_NAME}.backup").resolve()
self.update_marker = Path(UPDATE_MARKER_FILE)
logger.info("Updater initialized:")
logger.info(f" Temp executable: {self.temp_exe_path}")
logger.info(f" Target executable: {self.target_exe_path}")
def validate_inputs(self):
"""Validate that all required files exist and are valid"""
# Check if temp executable exists and is a file
if not self.temp_exe_path.exists():
raise UpdaterError(f"Temporary executable not found: {self.temp_exe_path}")
if not self.temp_exe_path.is_file():
raise UpdaterError(f"Temporary path is not a file: {self.temp_exe_path}")
# Check if temp executable has reasonable size (not empty, not too large)
file_size = self.temp_exe_path.stat().st_size
if file_size < 1024: # Less than 1KB
raise UpdaterError("Temporary executable appears to be too small")
if file_size > 500 * 1024 * 1024: # More than 500MB
raise UpdaterError("Temporary executable appears to be too large")
# Check if temp executable is actually executable
if not os.access(self.temp_exe_path, os.X_OK):
raise UpdaterError("Temporary file is not executable")
logger.info(f"Validation passed. Temp file size: {file_size:,} bytes")
def is_target_process_running(self) -> bool:
"""Check if the target application is currently running"""
try:
for proc in psutil.process_iter(["name", "exe"]):
proc_info = proc.info
if proc_info["name"] == EXE_NAME:
logger.debug(f"Found running process: {proc_info['name']}")
return True
except (psutil.NoSuchProcess, psutil.AccessDenied) as e:
# Process disappeared or access denied - not critical
logger.debug(f"Process check warning: {e}")
return False
def wait_for_process_exit(self):
"""Wait for the target application to exit, with timeout"""
logger.info(f"Waiting for {EXE_NAME} to exit...")
start_time = time.time()
while self.is_target_process_running():
elapsed = time.time() - start_time
if elapsed > MAX_WAIT_TIME:
raise UpdaterError(
f"Timeout waiting for {EXE_NAME} to exit after {MAX_WAIT_TIME}s"
)
logger.debug(f"Still waiting... ({elapsed:.1f}s)")
time.sleep(PROCESS_CHECK_INTERVAL)
logger.info("Target application has exited")
# Give a moment for file handles to be released
time.sleep(1)
def create_backup(self):
"""Create a backup of the current executable"""
if self.target_exe_path.exists():
try:
# Remove old backup if it exists
if self.backup_exe_path.exists():
self.backup_exe_path.unlink()
# Create new backup
shutil.copy2(self.target_exe_path, self.backup_exe_path)
logger.info(f"Created backup: {self.backup_exe_path}")
except Exception as e:
logger.warning(f"Failed to create backup: {e}")
def replace_executable(self):
"""Replace the target executable with the new version"""
try:
# Remove the old executable
if self.target_exe_path.exists():
self.target_exe_path.unlink()
logger.info(f"Removed old executable: {self.target_exe_path}")
# Move new executable into place
shutil.move(str(self.temp_exe_path), str(self.target_exe_path))
logger.info(f"Moved new executable to: {self.target_exe_path}")
# Verify the new file exists and is executable
if not self.target_exe_path.exists():
raise UpdaterError("New executable was not created successfully")
if not os.access(self.target_exe_path, os.X_OK):
raise UpdaterError("New executable is not executable")
except Exception as e:
# Try to restore from backup if something went wrong
self.restore_from_backup()
raise UpdaterError(f"Failed to replace executable: {e}")
def restore_from_backup(self):
"""Restore the executable from backup if update failed"""
if self.backup_exe_path.exists() and not self.target_exe_path.exists():
try:
shutil.move(str(self.backup_exe_path), str(self.target_exe_path))
logger.info("Restored executable from backup")
except Exception as e:
logger.error(f"Failed to restore from backup: {e}")
def launch_application(self):
"""Launch the updated application"""
try:
# Get the directory containing the executable
work_dir = self.target_exe_path.parent
# Prepare launch arguments
launch_args = [str(self.target_exe_path)]
# Add environment variable to indicate this was launched by updater
env = os.environ.copy()
env["XBOX_BACKUP_UPDATED"] = "1"
env["XBOX_BACKUP_UPDATER_PID"] = str(os.getpid())
logger.info(f"Launching application: {self.target_exe_path}")
# Launch the application
process = subprocess.Popen(
launch_args,
cwd=str(work_dir),
env=env,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
stdin=subprocess.DEVNULL,
# Windows-specific: don't create new console window
creationflags=subprocess.CREATE_NO_WINDOW if os.name == "nt" else 0,
)
# Wait briefly to ensure successful launch
time.sleep(2)
# Check if process is still running
if process.poll() is not None:
raise UpdaterError("Launched application exited immediately")
logger.info(f"Successfully launched application (PID: {process.pid})")
return process.pid
except Exception as e:
raise UpdaterError(f"Failed to launch application: {e}")
def cleanup(self):
"""Clean up temporary files and markers"""
try:
# Remove backup file if update was successful
if self.backup_exe_path.exists():
self.backup_exe_path.unlink()
logger.info("Cleaned up backup file")
# Remove update marker
if self.update_marker.exists():
self.update_marker.unlink()
logger.info("Removed update marker")
except Exception as e:
logger.warning(f"Cleanup warning: {e}")
def perform_update(self):
"""Perform the complete update process"""
try:
# Create update marker
self.update_marker.touch()
logger.info("Starting Xbox Backup Manager update process")
# Step 1: Validate inputs
self.validate_inputs()
# Step 2: Wait for application to exit
self.wait_for_process_exit()
# Step 3: Create backup
self.create_backup()
# Step 4: Replace executable
self.replace_executable()
# Step 5: Launch updated application
self.launch_application()
# Step 6: Cleanup
self.cleanup()
logger.info("Update completed successfully")
return True
except UpdaterError as e:
logger.error(f"Update failed: {e}")
return False
except Exception as e:
logger.error(f"Unexpected error during update: {e}")
return False
def main():
"""Main updater entry point"""
logger.info("Xbox Backup Manager Updater starting")
# Validate command line arguments
if len(sys.argv) != 2:
logger.error("Usage: updater.exe <temp_executable_path>")
print("Usage: updater.exe <temp_executable_path>")
sys.exit(1)
temp_exe_path = sys.argv[1]
try:
# Create and run updater
updater = XboxBackupManagerUpdater(temp_exe_path)
success = updater.perform_update()
if success:
logger.info("Update process completed successfully")
sys.exit(0)
else:
logger.error("Update process failed")
sys.exit(1)
except Exception as e:
logger.error(f"Fatal error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()