forked from khscience/OSkhQuant
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate_manager.py
More file actions
741 lines (629 loc) · 31.1 KB
/
update_manager.py
File metadata and controls
741 lines (629 loc) · 31.1 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
# update_manager.py
import os
import json
import sys
import logging
import hashlib
import tempfile
import subprocess
import requests
from datetime import datetime
from urllib.parse import urlparse
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import (
QMessageBox, QProgressDialog, QApplication, QDialog,
QVBoxLayout, QLabel, QProgressBar
)
from PyQt5.QtCore import Qt, QThread, pyqtSignal, QObject, QUrl
from PyQt5.QtGui import QDesktopServices
from version import get_version_info # 导入版本信息
import ctypes
class UpdateCheckThread(QThread):
"""检查更新的线程类"""
# 定义信号
update_available = pyqtSignal(dict) # 发现新版本时发出
check_finished = pyqtSignal(bool, str) # 检查完成时发出 (成功/失败, 消息)
error_occurred = pyqtSignal(str) # 发生错误时发出
def __init__(self, current_version, update_url, update_channel='stable'):
super().__init__()
self.current_version = current_version
self.update_url = update_url
self.update_channel = update_channel
self._is_running = True
def stop(self):
"""停止线程"""
self._is_running = False
def run(self):
"""运行更新检查"""
if not self._is_running:
return
try:
logging.info(f"开始检查更新,当前版本:{self.current_version}")
# 构建版本检查URL
url = f"{self.update_url}/version.json"
logging.debug(f"检查更新URL: {url}")
# 发送请求,减少超时时间为3秒
response = requests.get(url, timeout=3)
response.raise_for_status() # 抛出HTTP错误
version_info = response.json()
logging.debug(f"获取到版本信息: {version_info}")
# 检查版本信息格式(支持 download_url 或 filename 二选一)
base_required = ['version', 'force_update', 'checksum']
if not all(field in version_info for field in base_required):
raise ValueError("版本信息缺少必要字段")
if not (version_info.get('download_url') or version_info.get('filename')):
raise ValueError("版本信息缺少下载地址(download_url 或 filename)")
# 验证更新通道
if 'channel' in version_info and version_info['channel'] != self.update_channel:
logging.info(f"忽略不同通道的更新:{version_info['channel']}")
self.check_finished.emit(True, "当前已是最新版本")
return
# 比较版本号
if self.compare_versions(version_info['version'], self.current_version):
logging.info(f"发现新版本:{version_info['version']}")
self.update_available.emit(version_info)
else:
logging.info("当前已是最新版本")
self.check_finished.emit(True, "当前已是最新版本")
except requests.exceptions.ConnectionError:
msg = "无法连接到更新服务器,已跳过更新检查"
logging.info(msg) # 改为info级别
self.check_finished.emit(True, msg) # 改为True,允许程序继续
except requests.exceptions.Timeout:
msg = "连接更新服务器超时,已跳过更新检查"
logging.info(msg) # 改为info级别
self.check_finished.emit(True, msg) # 改为True,允许程序继续
except requests.exceptions.RequestException as e:
msg = f"检查更新失败,已跳过更新检查: {str(e)}"
logging.info(msg) # 改为info级别,因为这是预期内的情况
self.check_finished.emit(True, msg) # 改为True,允许程序继续
except ValueError as e:
msg = f"版本信息格式无效,已跳过更新检查: {str(e)}"
logging.warning(msg) # 使用warning级别,因为这可能表示服务器配置问题
self.check_finished.emit(True, msg) # 改为True,允许程序继续
except Exception as e:
msg = f"检查更新时发生未知错误,已跳过更新检查: {str(e)}"
logging.error(msg, exc_info=True) # 保持error级别,因为这是意外错误
self.check_finished.emit(True, msg) # 改为True,允许程序继续
@staticmethod
def compare_versions(new_version, current_version):
"""比较版本号,如果new_version大于current_version返回True"""
try:
logging.debug(f"比较版本: 新版本={new_version}, 当前版本={current_version}") # 添加日志
# 移除版本号前的'v'或'V'并转换为小写
new_version = new_version.lower().strip('v')
current_version = current_version.lower().strip('v')
# 将版本号分割为数字列表
new_parts = [int(x) for x in new_version.split('.')]
current_parts = [int(x) for x in current_version.split('.')]
# 确保两个版本号列表长度相同
while len(new_parts) < len(current_parts):
new_parts.append(0)
while len(current_parts) < len(new_parts):
current_parts.append(0)
# 逐位比较版本号
for new, current in zip(new_parts, current_parts):
if new > current:
logging.debug("发现新版本") # 添加日志
return True
elif new < current:
logging.debug("当前版本更新") # 添加日志
return False
# 如果所有位都相同,返回False(不需要更新)
logging.debug("版本号相同") # 添加日志
return False
except Exception as e:
logging.error(f"版本号比较出错: {str(e)}", exc_info=True)
return False
class UpdateDownloadThread(QThread):
"""下载更新的线程类"""
progress = pyqtSignal(int)
finished = pyqtSignal(bool, str)
error_occurred = pyqtSignal(str)
def __init__(self, url, save_path, version_info):
super().__init__()
self.url = url
self.save_path = save_path
self.version_info = version_info
def run(self):
try:
# 构建下载URL(优先使用后端返回的 download_url)
if self.version_info.get('download_url'):
download_url = self.version_info['download_url']
elif self.version_info.get('filename'):
download_url = f"{self.url}/files/{self.version_info['filename']}"
else:
raise Exception("无有效下载地址")
logging.info(f"开始下载更新文件: {download_url}")
# 确保保存目录存在
os.makedirs(self.save_path, exist_ok=True)
# 构建保存文件路径
filename = self.version_info.get('filename')
if not filename:
try:
parsed = urlparse(download_url)
filename = os.path.basename(parsed.path) or 'khquant_update.exe'
except Exception:
filename = 'khquant_update.exe'
# 兼容Windows安装器,若无.exe后缀则补上
if not filename.lower().endswith('.exe'):
filename += '.exe'
save_file = os.path.join(self.save_path, filename)
logging.info(f"更新文件将保存到: {save_file}")
# 下载文件
response = requests.get(download_url, stream=True)
response.raise_for_status()
# 获取文件大小
total_size = int(response.headers.get('content-length', 0))
# 先将文件下载到临时文件
temp_file = save_file + '.tmp'
try:
with open(temp_file, 'wb') as f:
if total_size == 0:
f.write(response.content)
else:
downloaded = 0
for data in response.iter_content(chunk_size=8192):
downloaded += len(data)
f.write(data)
progress = int((downloaded / total_size) * 100)
self.progress.emit(progress)
# 下载完成后,检查文件是否为有效的可执行文件
if os.path.exists(temp_file) and os.path.getsize(temp_file) > 0:
# 如果目标文件已存在,先删除
if os.path.exists(save_file):
os.remove(save_file)
# 将临时文件重命名为正式文件
os.rename(temp_file, save_file)
logging.info(f"更新包下载完成: {save_file}")
self.finished.emit(True, save_file)
else:
raise Exception("下载文件无效")
except Exception as e:
# 清理临时文件
if os.path.exists(temp_file):
try:
os.remove(temp_file)
except:
pass
raise e
except Exception as e:
error_msg = f"下载更新文件时出错: {str(e)}"
logging.error(error_msg, exc_info=True)
self.error_occurred.emit(error_msg)
self.finished.emit(False, error_msg)
class UpdateProgressDialog(QDialog):
"""更新进度对话框"""
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("软件更新")
self.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint)
self.setFixedSize(400, 150)
# 创建布局
layout = QVBoxLayout()
# 添加状态标签
self.status_label = QLabel("正在下载更新...")
layout.addWidget(self.status_label)
# 添加进度条
self.progress_bar = QProgressBar()
self.progress_bar.setTextVisible(True)
layout.addWidget(self.progress_bar)
# 添加详细信息标签
self.detail_label = QLabel("")
self.detail_label.setWordWrap(True)
layout.addWidget(self.detail_label)
self.setLayout(layout)
def update_progress(self, value):
"""更新进度条"""
self.progress_bar.setValue(value)
def update_status(self, status):
"""更新状态文本"""
self.status_label.setText(status)
def update_detail(self, detail):
"""更新详细信息"""
self.detail_label.setText(detail)
class UpdateManager(QObject):
"""软件更新管理器"""
# 添加类级别的信号定义
check_finished = pyqtSignal(bool, str) # 检查完成信号
def __init__(self, parent=None):
super().__init__(parent)
self.parent = parent
# 设置临时目录
if getattr(sys, 'frozen', False):
# 打包环境
base_dir = os.path.dirname(sys.executable)
else:
# 开发环境
base_dir = os.path.dirname(os.path.abspath(__file__))
self.temp_dir = os.path.join(base_dir, 'temp')
os.makedirs(self.temp_dir, exist_ok=True)
# 尝试从version.py导入版本信息
try:
from version import get_version, get_version_info, get_channel
self.current_version = get_version()
self.update_channel = get_channel()
self.version_info = get_version_info()
except ImportError:
logging.warning("无法导入version模块,使用默认版本信息")
self.current_version = "1.0.0" # 默认版本
self.update_channel = "stable" # 默认通道
self.version_info = {
"version": "1.0.0",
"build_date": "2024-02-20",
"channel": "stable",
"app_name": "看海量化回测平台"
}
# 系列二更新基路径
self.update_url = "https://khsci.com/khQuant/update"
self.update_thread = None
self.download_thread = None
def show_current_version(self):
"""显示当前版本信息"""
info = (
f"{self.version_info.get('app_name', '看海量化回测平台')}\n"
f"版本: {self.current_version}\n"
f"构建日期: {self.version_info.get('build_date', '2024-02-20')}\n"
f"更新通道: {self.update_channel}\n"
f"\nCopyright © 2024 看海科技"
)
if self.parent:
QMessageBox.about(self.parent, "版本信息", info)
return info
@staticmethod
def compare_versions(new_version, current_version):
"""比较版本号,如果new_version大于current_version返回True"""
try:
logging.debug(f"比较版本: 新版本={new_version}, 当前版本={current_version}") # 添加日志
# 移除版本号前的'v'或'V'并转换为小写
new_version = new_version.lower().strip('v')
current_version = current_version.lower().strip('v')
# 将版本号分割为数字列表
new_parts = [int(x) for x in new_version.split('.')]
current_parts = [int(x) for x in current_version.split('.')]
# 确保两个版本号列表长度相同
while len(new_parts) < len(current_parts):
new_parts.append(0)
while len(current_parts) < len(new_parts):
current_parts.append(0)
# 逐位比较版本号
for new, current in zip(new_parts, current_parts):
if new > current:
logging.debug("发现新版本") # 添加日志
return True
elif new < current:
logging.debug("当前版本更新") # 添加日志
return False
# 如果所有位都相同,返回False(不需要更新)
logging.debug("版本号相同") # 添加日志
return False
except Exception as e:
logging.error(f"版本号比较出错: {str(e)}", exc_info=True)
return False
def check_for_updates(self, current_version):
"""
检查更新
:param current_version: 当前版本号
"""
try:
logging.info("开始检查软件更新")
response = requests.post(
'https://khsci.com/khQuant/wp-admin/admin-ajax.php',
data={
'action': 'kh_check_update',
'version': current_version,
'channel': self.update_channel
},
timeout=5
)
if response.status_code == 200:
data = response.json()
logging.debug(f"服务器响应: {data}") # 添加日志
if data.get('success'):
# 系列二端点返回 { success: true, data: {...} }
version_info = data.get('data', {})
logging.debug(f"获取到版本信息: {version_info}") # 添加日志
# 检查是否有新版本
if self.compare_versions(version_info.get('version', ''), current_version):
logging.info(f"发现新版本: {version_info['version']}")
# 显示更新对话框
self.handle_update_available(version_info)
else:
logging.info("当前已是最新版本")
self.check_finished.emit(True, "当前已是最新版本")
else:
error_msg = data.get('data', '检查更新失败')
logging.warning(f"检查更新失败: {error_msg}")
self.check_finished.emit(False, error_msg)
else:
error_msg = f"服务器响应错误: {response.status_code}"
logging.error(error_msg)
self.check_finished.emit(False, error_msg)
except Exception as e:
error_msg = f"检查更新时出错: {str(e)}"
logging.error(error_msg, exc_info=True)
self.check_finished.emit(False, error_msg)
def handle_update_available(self, version_info):
try:
# 先发出成信号,避免界面卡住
self.check_finished.emit(True, f"发现新版本:{version_info['version']}")
# 使用 QTimer 延迟显示对话框
QTimer.singleShot(100, lambda: self._show_update_dialog(version_info))
except Exception as e:
logging.error(f"处理更新信息时出错: {str(e)}", exc_info=True)
self.check_finished.emit(False, f"处理更新信息时出错: {str(e)}")
def _show_update_dialog(self, version_info):
"""显示更新对话框"""
try:
download_url = version_info.get('download_url') or (
f"{self.update_url.rstrip('/')}/files/{version_info.get('filename','')}" if version_info.get('filename') else ''
)
# 检查是否为强制更新
is_force_update = version_info.get('force_update', False)
if is_force_update:
# 强制更新时显示不同的对话框
msg = QMessageBox(self.parent)
msg.setIcon(QMessageBox.Warning)
msg.setWindowTitle("强制更新")
msg.setTextFormat(Qt.RichText)
desc = version_info.get('description', '无更新说明')
link_html = f"<br>下载链接:<a href=\"{download_url}\">{download_url}</a>" if download_url else ""
msg.setText(f"发现重要更新 {version_info['version']}<br>此更新为强制更新,必须安装才能继续使用。{link_html}")
msg.setInformativeText(f"更新说明:\n{desc}")
# 添加自定义按钮
close_btn = msg.addButton("关闭", QMessageBox.RejectRole)
msg.setDefaultButton(close_btn)
# 重写关闭事件
msg.closeEvent = lambda event: self.handle_force_update_close(event)
# 额外按钮:打开下载链接(可选)
open_link_btn = None
if download_url:
open_link_btn = msg.addButton("打开下载链接", QMessageBox.ActionRole)
reply = msg.exec_()
if open_link_btn and msg.clickedButton() == open_link_btn:
try:
QDesktopServices.openUrl(QUrl(download_url))
except Exception:
pass
# 强制更新情况下,打开链接后仍然要求退出程序
QApplication.quit()
# 无论点击什么按钮,强制更新时都退出程序
QApplication.quit()
else:
# 普通更新时的对话框
msg = QMessageBox(self.parent)
msg.setIcon(QMessageBox.Information)
msg.setWindowTitle("发现新版本")
msg.setTextFormat(Qt.RichText)
desc = version_info.get('description', '无更新说明')
link_html = f"<br>下载链接:<a href=\"{download_url}\">{download_url}</a>" if download_url else ""
msg.setText(f"发现新版本 {version_info['version']}{link_html}")
msg.setInformativeText(f"更新说明:\n{desc}")
later_btn = msg.addButton("稍后", QMessageBox.NoRole)
open_link_btn = None
if download_url:
open_link_btn = msg.addButton("打开下载链接", QMessageBox.ActionRole)
msg.setDefaultButton(later_btn)
msg.exec_()
if open_link_btn and msg.clickedButton() == open_link_btn:
try:
QDesktopServices.openUrl(QUrl(download_url))
except Exception:
pass
except Exception as e:
logging.error(f"显示更新对话框时出错: {str(e)}", exc_info=True)
QMessageBox.critical(self.parent, "错误", f"显示更新对话框时出错: {str(e)}")
def handle_force_update_close(self, event):
"""处理强制更新对话框的关闭事件"""
logging.info("用户尝试关闭强制更新对话框")
QApplication.quit()
event.accept()
def download_update(self, version_info):
"""下载更新"""
try:
# 创建进度对话框
self.progress_dialog = UpdateProgressDialog(self.parent)
self.progress_dialog.setModal(True)
# 如果是强制更新,禁用关闭按钮
if version_info.get('force_update', False):
self.progress_dialog.setWindowFlags(
self.progress_dialog.windowFlags() & ~Qt.WindowCloseButtonHint
)
# 确保下载URL正确
download_url = self.update_url.rstrip('/') # 移除末尾的斜杠
# 创建下载线程
self.download_thread = UpdateDownloadThread(
download_url,
self.temp_dir,
version_info
)
# 连接信号
self.download_thread.progress.connect(self.progress_dialog.update_progress)
self.download_thread.finished.connect(lambda success, result:
self.handle_download_finished(success, result, version_info))
self.download_thread.error_occurred.connect(self.handle_error)
# 开始下载
self.download_thread.start()
self.progress_dialog.exec_()
except Exception as e:
logging.error(f"启动下载时出错: {str(e)}", exc_info=True)
if version_info.get('force_update', False):
QMessageBox.critical(self.parent, "错误", f"下载更新失败: {str(e)}\n程序将退出。")
QApplication.quit()
else:
QMessageBox.warning(self.parent, "下载错误", f"启动下载时出错: {str(e)}")
def handle_download_close(self, event, version_info):
"""处理下载对话框的关闭事件"""
# 停止下载线程
if self.download_thread and self.download_thread.isRunning():
self.download_thread.terminate()
self.download_thread.wait()
if version_info.get('force_update', False):
# 对于强制更新,直接退出程序
QApplication.quit()
event.accept()
def handle_download_finished(self, success, result, version_info):
"""处理下载完成"""
try:
self.progress_dialog.close()
if success:
if version_info.get('force_update', False):
# 强制更新时显示带有强制更新说明的确认对话框
reply = QMessageBox.question(
self.parent,
"重要更新",
"重要更新包已下载完成,此更新包含关键功能更新,需要立即安装。\n"
"安装过程中软件将关闭,请确认是否现在安装?",
QMessageBox.Yes | QMessageBox.No
)
if reply == QMessageBox.Yes:
self.install_update(result)
else:
# 如果用户选择不安装强制更新,则退出程序
QApplication.quit()
else:
# 普通更新显示标准确认对话框
reply = QMessageBox.question(
self.parent,
"下载完成",
"更新包下载完成,是否现在安装?\n"
"安装过程中软件将关闭。",
QMessageBox.Yes | QMessageBox.No
)
if reply == QMessageBox.Yes:
self.install_update(result)
else:
error_msg = f"下载更新失败: {result}"
if version_info.get('force_update', False):
QMessageBox.critical(self.parent, "下载失败", f"{error_msg}\n程序将退出。")
QApplication.quit()
else:
QMessageBox.critical(self.parent, "下载失败", error_msg)
except Exception as e:
logging.error(f"处理下载完成时出错: {str(e)}", exc_info=True)
if version_info.get('force_update', False):
QMessageBox.critical(self.parent, "错误", f"处理下载完成时出错: {str(e)}\n程序将退出。")
QApplication.quit()
else:
QMessageBox.critical(self.parent, "错误", f"处理下载完成时出错: {str(e)}")
def install_update(self, update_file):
"""安装更新"""
try:
logging.info(f"准备安装更新: {update_file}")
# 验证更新文件是否存在且为exe
if not os.path.exists(update_file) or not update_file.endswith('.exe'):
raise Exception(f"无效的更新文件: {update_file}")
# 创建更新批处理文件
batch_file = os.path.join(self.temp_dir, "update.bat")
current_exe = sys.executable
current_pid = str(os.getpid())
# 修改批处理文件内容,增加更多的安全检查和延迟
batch_content = [
'@echo off',
'setlocal EnableDelayedExpansion',
'cd /d %~dp0',
# 等待主程序退出
'echo 正在等待程序关闭...',
':wait_loop',
f'tasklist | find "{os.path.basename(current_exe)}" >nul 2>&1',
'if not errorlevel 1 (',
' timeout /t 2 /nobreak >nul',
' goto wait_loop',
')',
# 增加额外的等待时间确保进程完全关闭
'echo 确保程序完全关闭...',
'timeout /t 5 /nobreak >nul',
# 强制结束可能残留的进程
f'taskkill /F /FI "IMAGENAME eq {os.path.basename(current_exe)}" /T >nul 2>&1',
'timeout /t 2 /nobreak >nul',
# 开始安装
'echo 正在安装更新...',
f'start /wait "" "{os.path.abspath(update_file)}" /SILENT /SUPPRESSMSGBOXES /NOCANCEL /NORESTART',
# 检查安装结果
'if errorlevel 1 (',
' echo 安装失败',
' pause',
' exit /b 1',
')',
# 增加安装后的等待时间
'echo 安装完成,正在完成最后配置...',
'timeout /t 10 /nobreak >nul',
# 清理文件
'echo 清理临时文件...',
f'del /F /Q "{update_file}"',
# 使用 wscript 来隐藏启动窗口
'echo 正在启动新版本...',
'echo Set WshShell = CreateObject("WScript.Shell") > "%temp%\\start_app.vbs"',
f'echo WshShell.Run """{current_exe}""", 1, False >> "%temp%\\start_app.vbs"',
'wscript "%temp%\\start_app.vbs"',
'timeout /t 2 /nobreak >nul',
'del "%temp%\\start_app.vbs"',
# 删除批处理自身
'timeout /t 1 /nobreak >nul',
'(goto) 2>nul & del "%~f0"'
]
# 写入批处理文件
with open(batch_file, 'w', encoding='gbk') as f:
f.write('\n'.join(batch_content))
logging.info("正在启动更新程序...")
# 启动更新批处理
if ctypes.windll.shell32.IsUserAnAdmin() == 0:
ctypes.windll.shell32.ShellExecuteW(
None,
"runas",
"cmd.exe",
f'/c "{batch_file}"',
os.path.dirname(batch_file),
1
)
else:
subprocess.Popen(
f'cmd /c "{batch_file}"',
shell=True,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.CREATE_NO_WINDOW
)
# 延长退出延迟时间
QTimer.singleShot(2000, QApplication.quit)
except Exception as e:
error_msg = f"安装更新时出错: {str(e)}"
logging.error(error_msg, exc_info=True)
QMessageBox.critical(self.parent, "安装失败", error_msg)
def handle_error(self, error_msg):
"""处理错误"""
logging.error(f"更新过程出错: {error_msg}")
QMessageBox.critical(self.parent, "更新错误", error_msg)
def on_check_finished(self, success, message):
"""处理更新检查完成"""
if not success and not message.startswith("当前已是最新版本"):
logging.warning(f"更新检查失败: {message}")
def cleanup(self):
"""清理临时文件和线程"""
try:
# 停止所有线程
if self.update_thread and self.update_thread.isRunning():
self.update_thread.stop()
self.update_thread.wait()
if self.download_thread and self.download_thread.isRunning():
self.download_thread.stop()
self.download_thread.wait()
# 清理临时文件
if os.path.exists(self.temp_dir):
for file in os.listdir(self.temp_dir):
try:
file_path = os.path.join(self.temp_dir, file)
if os.path.isfile(file_path):
os.remove(file_path)
except Exception as e:
logging.error(f"清理临时文件失败: {str(e)}")
except Exception as e:
logging.error(f"清理更新管理器时出错: {str(e)}")
def __del__(self):
"""析构函数,确保资源被正确释放"""
self.cleanup()
def check_update():
current_version = "V1.1.1" # 获取当前版本号
data = {
'action': 'check_update',
'client_version': current_version
}
# 发送请求到服务器