From 1f90490230fc3e28472427dfce885cc17a2fa1df Mon Sep 17 00:00:00 2001 From: HibiKier <775757368@qq.com> Date: Tue, 20 Aug 2024 01:09:56 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E8=87=AA=E5=8A=A8=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zhenxun/plugins/auto_update/__init__.py | 4 +-- zhenxun/plugins/auto_update/_data_source.py | 39 +++++++++++++-------- zhenxun/plugins/auto_update/config.py | 3 +- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/zhenxun/plugins/auto_update/__init__.py b/zhenxun/plugins/auto_update/__init__.py index 615a5a3b..73481a1d 100644 --- a/zhenxun/plugins/auto_update/__init__.py +++ b/zhenxun/plugins/auto_update/__init__.py @@ -68,7 +68,7 @@ async def _(bot: Bot, session: EventSession, ver_type: Match[str]): await MessageUtils.build_message("更新版本失败...").finish() -driver = nonebot.get_driver() +# driver = nonebot.get_driver() # @driver.on_startup @@ -79,7 +79,7 @@ driver = nonebot.get_driver() # print(result) # print("-----------------------") # print("-----------------------") -# result = await UpdateManage.update(None, "", "release") +# result = await UpdateManage.update(None, "", "dev") # print("-----------------------") # print("-----------------------") # print(result) diff --git a/zhenxun/plugins/auto_update/_data_source.py b/zhenxun/plugins/auto_update/_data_source.py index 2bb01308..2f1c0276 100644 --- a/zhenxun/plugins/auto_update/_data_source.py +++ b/zhenxun/plugins/auto_update/_data_source.py @@ -1,6 +1,7 @@ import os import shutil import tarfile +import zipfile from pathlib import Path from nonebot.adapters import Bot @@ -14,7 +15,8 @@ from .config import ( BACKUP_PATH, BASE_PATH, DEV_URL, - DOWNLOAD_FILE, + DOWNLOAD_GZ_FILE, + DOWNLOAD_ZIP_FILE, MAIN_URL, PYPROJECT_FILE, PYPROJECT_LOCK_FILE, @@ -33,9 +35,13 @@ def _file_handle(latest_version: str | None): latest_version: 版本号 """ BACKUP_PATH.mkdir(exist_ok=True, parents=True) - tf = None logger.debug("开始解压文件压缩包...", "检查更新") - tf = tarfile.open(DOWNLOAD_FILE) + download_file = DOWNLOAD_GZ_FILE + if DOWNLOAD_GZ_FILE.exists(): + tf = tarfile.open(DOWNLOAD_GZ_FILE) + else: + download_file = DOWNLOAD_ZIP_FILE + tf = zipfile.ZipFile(DOWNLOAD_ZIP_FILE) tf.extractall(TMP_PATH) logger.debug("解压文件压缩包完成...", "检查更新") download_file_path = ( @@ -46,10 +52,10 @@ def _file_handle(latest_version: str | None): extract_path = download_file_path / "zhenxun" target_path = BASE_PATH if PYPROJECT_FILE.exists(): - logger.debug(f"备份文件: {PYPROJECT_FILE}", "检查更新") + logger.debug(f"移除备份文件: {PYPROJECT_FILE}", "检查更新") shutil.move(PYPROJECT_FILE, BACKUP_PATH / "pyproject.toml") if PYPROJECT_LOCK_FILE.exists(): - logger.debug(f"备份文件: {PYPROJECT_FILE}", "检查更新") + logger.debug(f"移除备份文件: {PYPROJECT_FILE}", "检查更新") shutil.move(PYPROJECT_LOCK_FILE, BACKUP_PATH / "poetry.lock") if _pyproject.exists(): logger.debug("移动文件: pyproject.toml", "检查更新") @@ -79,9 +85,9 @@ def _file_handle(latest_version: str | None): shutil.move(src_folder_path, dest_folder_path) else: logger.debug(f"源文件夹不存在: {src_folder_path}", "检查更新") - if DOWNLOAD_FILE.exists(): - logger.debug(f"删除下载文件: {DOWNLOAD_FILE}", "检查更新") - DOWNLOAD_FILE.unlink() + if download_file.exists(): + logger.debug(f"删除下载文件: {download_file}", "检查更新") + download_file.unlink() if extract_path.exists(): logger.debug(f"删除解压文件夹: {extract_path}", "检查更新") shutil.rmtree(extract_path) @@ -92,7 +98,7 @@ def _file_handle(latest_version: str | None): if latest_version: with open(VERSION_FILE, "w", encoding="utf8") as f: f.write(f"__version__: {latest_version}") - os.system(f"poetry run pip install -r {(Path() / 'pyproject.toml').absolute()}") + os.system(f"poetry install --directory={Path().absolute()}") class UpdateManage: @@ -145,12 +151,15 @@ class UpdateManage: f"开始更新版本:{cur_version} -> {new_version} | 下载链接:{url}", "检查更新", ) - # await PlatformUtils.send_superuser( - # bot, - # f"检测真寻已更新,版本更新:{cur_version} -> {new_version}\n开始更新...", - # user_id, - # ) - if await AsyncHttpx.download_file(url, DOWNLOAD_FILE): + await PlatformUtils.send_superuser( + bot, + f"检测真寻已更新,版本更新:{cur_version} -> {new_version}\n开始更新...", + user_id, + ) + download_file = ( + DOWNLOAD_GZ_FILE if version_type == "release" else DOWNLOAD_ZIP_FILE + ) + if await AsyncHttpx.download_file(url, download_file): logger.debug("下载真寻最新版文件完成...", "检查更新") if version_type != "release": new_version = None diff --git a/zhenxun/plugins/auto_update/config.py b/zhenxun/plugins/auto_update/config.py index 3285828a..bd992f4b 100644 --- a/zhenxun/plugins/auto_update/config.py +++ b/zhenxun/plugins/auto_update/config.py @@ -18,6 +18,7 @@ TMP_PATH = TEMP_PATH / "auto_update" BACKUP_PATH = Path() / "backup" -DOWNLOAD_FILE = TMP_PATH / "download_latest_file.tar.gz" +DOWNLOAD_GZ_FILE = TMP_PATH / "download_latest_file.tar.gz" +DOWNLOAD_ZIP_FILE = TMP_PATH / "download_latest_file.zip" REPLACE_FOLDERS = ["builtin_plugins", "plugins", "services", "utils", "models"]