From 04218f40f7b257d4a08b83bf8729902d24b68857 Mon Sep 17 00:00:00 2001 From: li-xiaochen <397721316@qq.com> Date: Sat, 14 Dec 2024 22:14:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E5=B8=83=E5=8C=85=E4=BB=8Etar?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=BA7z=EF=BC=8C=E8=87=AA=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E9=80=82=E9=85=8D=E6=96=B0=E5=8F=91=E5=8C=85=E6=96=B9?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++- launcher.py | 10 +++++----- launcher/constants.py | 3 ++- launcher/file/utils.py | 9 +++++++++ launcher/log.py | 4 +++- launcher/webview/__init__.py | 3 ++- launcher/webview/api.py | 28 +++++++++++++++++++--------- 7 files changed, 42 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index a5e8995..bfd89e0 100644 --- a/README.md +++ b/README.md @@ -15,5 +15,6 @@ pyinstaller -w --add-data "ui/dist:ui/dist" --add-data "launcher/sys_config/conf 在dist文件夹生成launcher文件夹 ```bash -tar -cf dist/launcher.tar dist/launcher +cd dist +py7zr c launcher.7z launcher ``` diff --git a/launcher.py b/launcher.py index b850fb9..115cc4f 100644 --- a/launcher.py +++ b/launcher.py @@ -1,8 +1,8 @@ import mimetypes -import os +import shutil from pathlib import Path -from launcher.constants import upgrade_script_name +from launcher.constants import update_tmp_folder from launcher.webview import start_webview mimetypes.add_type("text/html", ".html") @@ -11,8 +11,8 @@ mimetypes.add_type("application/javascript", ".js") if __name__ == '__main__': - # 如果当前路径存在更新脚本,则删除 - if Path(upgrade_script_name).exists(): - os.remove(upgrade_script_name) + # 如果当前路径存在临时文件夹,则删除 + if Path(update_tmp_folder).exists(): + shutil.rmtree(update_tmp_folder) start_webview() diff --git a/launcher/constants.py b/launcher/constants.py index 60ec018..da1c637 100644 --- a/launcher/constants.py +++ b/launcher/constants.py @@ -4,6 +4,8 @@ constants.py 该模块定义了应用程序中使用的各种常量。这些常量包括API URL和其他配置参数。 """ +# 更新临时文件夹名 +update_tmp_folder = "download_tmp" # 更新脚本名 upgrade_script_name = "upgrade.bat" @@ -21,4 +23,3 @@ mirror_list = { "tuna": "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple", "sjtu": "https://mirror.sjtu.edu.cn/pypi/web/simple", } - diff --git a/launcher/file/utils.py b/launcher/file/utils.py index e78dfcd..cd8a378 100644 --- a/launcher/file/utils.py +++ b/launcher/file/utils.py @@ -1,3 +1,6 @@ +import os + + def format_size(size_bytes): """格式化文件大小为人类可读的形式""" for unit in ['B', 'KB', 'MB', 'GB', 'TB']: @@ -5,3 +8,9 @@ def format_size(size_bytes): return f"{size_bytes:.2f} {unit}" size_bytes /= 1024 return f"{size_bytes:.2f} PB" + + +def ensure_directory_exists(directory): + """确保目录存在,如果不存在则创建""" + if not os.path.exists(directory): + os.makedirs(directory) diff --git a/launcher/log.py b/launcher/log.py index 954ccc3..1797c98 100644 --- a/launcher/log.py +++ b/launcher/log.py @@ -1,5 +1,6 @@ import io import logging +import os import sys from logging.handlers import RotatingFileHandler @@ -21,8 +22,9 @@ def setup_logger(): console_handler.setLevel(log_level) # 文件输出 + file_path = os.path.join(os.getcwd(), "launcher.log") file_handler = RotatingFileHandler( - "../launcher.log", maxBytes=5 * 1024 * 1024, backupCount=3, encoding='utf-8' + file_path, maxBytes=5 * 1024 * 1024, backupCount=3, encoding='utf-8' ) file_handler.setLevel(logging.INFO) diff --git a/launcher/webview/__init__.py b/launcher/webview/__init__.py index 6371a19..77fe4a6 100644 --- a/launcher/webview/__init__.py +++ b/launcher/webview/__init__.py @@ -8,5 +8,6 @@ window = None def start_webview(): global window - window = webview.create_window(f"mower-ng launcher {sys_config.get('version')}", sys_config.get('url'), js_api=Api()) + window = webview.create_window(f"mower-ng launcher {sys_config.get('version')}", sys_config.get('url'), + js_api=Api()) webview.start() diff --git a/launcher/webview/api.py b/launcher/webview/api.py index 09fdd02..e8e5c0b 100644 --- a/launcher/webview/api.py +++ b/launcher/webview/api.py @@ -14,6 +14,8 @@ from launcher import config from launcher.constants import download_git_url, download_python_url, get_new_version_url, upgrade_script_name, \ mirror_list from launcher.file.download import init_download, download_file +from launcher.file.extract import extract_7z_file +from launcher.file.utils import ensure_directory_exists from launcher.log import logger from launcher.sys_config import sys_config from launcher.webview.events import custom_event @@ -74,25 +76,33 @@ class Api: # 更新启动器本身 def update_self(self, download_url): logger.info(f"开始更新启动器 {download_url}") - # 下载压缩包的全路径 file_name = os.path.basename(download_url) + file_name = "launcher.7z" current_path = os.getcwd() - if not download_file("launcher", download_url, current_path): + download_tmp_folder = os.path.join(current_path, "download_tmp") + # 确保 download_tmp 文件夹存在 + ensure_directory_exists(download_tmp_folder) + if not download_file("launcher", download_url, download_tmp_folder): return "下载新版本失败" - download_path = os.path.join(current_path, file_name) - script_path = os.path.join(os.getcwd(), upgrade_script_name) - folder_path = os.path.join(os.getcwd(), "_internal") - exe_path = os.path.join(os.getcwd(), "launcher.exe") + download_path = os.path.join(download_tmp_folder, file_name) + if not extract_7z_file("launcher", download_path, download_tmp_folder, True): + return "解压新版本失败" + + exe_path = os.path.join(current_path, "launcher.exe") + folder_path = os.path.join(current_path, "_internal") + new_exe_path = os.path.join(download_tmp_folder, "launcher", "launcher.exe") + new_folder_path = os.path.join(download_tmp_folder, "launcher", "_internal") + script_path = os.path.join(download_tmp_folder, upgrade_script_name) with open(script_path, "w") as b: temp_list = "@echo off\n" temp_list += "timeout /t 3 /nobreak\n" # 等待进程退出 temp_list += f"rmdir {folder_path}\n" # 删除_internal temp_list += f"del {exe_path}\n" # 删除exe - temp_list += f"tar -xf {download_path} -C ..\n" # 解压压缩包 - temp_list += "timeout /t 1 /nobreak\n" # 等待解压 + temp_list += f"xcopy /e /i /y {new_folder_path} {folder_path}\n" + temp_list += f"move {new_exe_path} {exe_path}\n" + temp_list += "timeout /t 1 /nobreak\n" # 等待操作完成 temp_list += f"start {exe_path}\n" # 启动新程序 - temp_list += f"del {download_path}\n" # 删除压缩包 temp_list += "exit" b.write(temp_list) # 不显示cmd窗口