@@ -267,8 +267,25 @@ def standard_update():
267267
268268 # 重命名更新文件为备份文件
269269 if os .path .exists (update_file ):
270- os .rename (update_file , update1_zip_name )
271- print (f"更新文件已重命名为: { update1_zip_name } " )
270+ # 如果目标备份文件已存在,先删除以避免重命名失败
271+ if os .path .exists (update1_zip_name ):
272+ try :
273+ os .remove (update1_zip_name )
274+ print (f"已删除旧的备份文件: { update1_zip_name } " )
275+ except Exception as e :
276+ error_message = f"删除旧备份文件失败: { e } "
277+ log_error (error_message )
278+ print (error_message )
279+ # 删除失败则中止更新
280+ sys .exit (error_message )
281+
282+ try :
283+ os .rename (update_file , update1_zip_name )
284+ print (f"更新文件已重命名为: { update1_zip_name } " )
285+ except Exception as e :
286+ error_message = f"重命名更新文件失败: { e } "
287+ log_error (error_message )
288+ sys .exit (error_message )
272289 else :
273290 error_message = "更新文件解压失败,更新已中止"
274291 log_error (error_message )
@@ -278,14 +295,22 @@ def standard_update():
278295
279296 # 重启程序
280297 print ("重启MFW程序..." )
281- if sys .platform .startswith ("win32" ):
282- subprocess .Popen (".\\ MFW.exe" )
283- elif sys .platform .startswith ("darwin" ):
284- subprocess .Popen ("./MFW" )
285- elif sys .platform .startswith ("linux" ):
286- subprocess .Popen ("./MFW" )
287- else :
288- sys .exit ("不支持的操作系统" )
298+ try :
299+ if sys .platform .startswith ("win32" ):
300+ subprocess .Popen (".\\ MFW.exe" )
301+ elif sys .platform .startswith ("darwin" ):
302+ subprocess .Popen ("./MFW" )
303+ elif sys .platform .startswith ("linux" ):
304+ subprocess .Popen ("./MFW" )
305+ else :
306+ error_message = "不支持的操作系统"
307+ log_error (error_message )
308+ sys .exit (error_message )
309+ except Exception as e :
310+ error_message = f"启动MFW程序失败: { e } "
311+ log_error (error_message )
312+ print (error_message )
313+ sys .exit (error_message )
289314
290315
291316def recovery_mode ():
@@ -314,28 +339,51 @@ def recovery_mode():
314339 print ("未找到恢复更新文件" )
315340
316341 # 重启程序
317- if sys .platform .startswith ("win32" ):
318- subprocess .Popen (".\\ MFW.exe" )
319- elif sys .platform .startswith ("darwin" ) or sys .platform .startswith ("linux" ):
320- subprocess .Popen ("./MFW" )
321- else :
322- sys .exit ("不支持的操作系统" )
323- print ("程序已重启" )
342+ try :
343+ if sys .platform .startswith ("win32" ):
344+ subprocess .Popen (".\\ MFW.exe" )
345+ elif sys .platform .startswith ("darwin" ) or sys .platform .startswith ("linux" ):
346+ subprocess .Popen ("./MFW" )
347+ else :
348+ error_message = "不支持的操作系统"
349+ log_error (error_message )
350+ sys .exit (error_message )
351+ print ("程序已重启" )
352+ except Exception as e :
353+ error_message = f"启动MFW程序失败: { e } "
354+ log_error (error_message )
355+ print (error_message )
356+ sys .exit (error_message )
324357
325358
326359if __name__ == "__main__" :
327- # 清理错误日志
328- if os .path .exists ("UPDATE_ERROR.log" ):
329- os .remove ("UPDATE_ERROR.log" )
360+ # 不再启动时删除旧日志,而是追加写入(便于排查历史错误)
361+ # 在日志中记录本次更新开始
362+ import time
363+ log_entry = f"\n { '=' * 60 } \n [{ time .strftime ('%Y-%m-%d %H:%M:%S' )} ] 更新程序启动\n { '=' * 60 } \n "
364+ try :
365+ with open ("UPDATE_ERROR.log" , "a" , encoding = "utf-8" ) as log_file :
366+ log_file .write (log_entry )
367+ except Exception :
368+ pass # 如果无法写入日志文件,继续执行
330369
331- if len (sys .argv ) > 1 and sys .argv [1 ] == "-update" :
332- standard_update ()
333- else :
334- mode = input ("1. 更新模式 / Standard update\n 2. 恢复模式 / Recovery update\n " )
335- if mode == "1" :
370+ try :
371+ if len (sys .argv ) > 1 and sys .argv [1 ] == "-update" :
336372 standard_update ()
337- elif mode == "2" :
338- recovery_mode ()
339373 else :
340- print ("无效输入 / Invalid input" )
341- input ("按回车键继续... / Press Enter to continue..." )
374+ mode = input ("1. 更新模式 / Standard update\n 2. 恢复模式 / Recovery update\n " )
375+ if mode == "1" :
376+ standard_update ()
377+ elif mode == "2" :
378+ recovery_mode ()
379+ else :
380+ print ("无效输入 / Invalid input" )
381+ input ("按回车键继续... / Press Enter to continue..." )
382+ except Exception as e :
383+ # 捕获所有未处理的异常并记录
384+ error_message = f"更新程序发生未捕获的异常: { type (e ).__name__ } : { e } "
385+ log_error (error_message )
386+ print (f"\n { error_message } " )
387+ print ("\n 更新失败,请查看 UPDATE_ERROR.log 了解详情" )
388+ input ("按回车键退出... / Press Enter to exit..." )
389+ sys .exit (1 )
0 commit comments