From 905245be7921fbd184542097afaba84feb497e6a Mon Sep 17 00:00:00 2001 From: CMHopeSunshine <277073121@qq.com> Date: Fri, 6 Jan 2023 21:56:31 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E4=BC=98=E5=8C=96`=E6=9B=B4?= =?UTF-8?q?=E6=96=B0`=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LittlePaimon/utils/update.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/LittlePaimon/utils/update.py b/LittlePaimon/utils/update.py index b40d402..c1fa97c 100644 --- a/LittlePaimon/utils/update.py +++ b/LittlePaimon/utils/update.py @@ -1,4 +1,5 @@ import datetime +import re from pathlib import Path import git @@ -28,7 +29,9 @@ async def check_update(): return f'当前已是最新版本:{__version__}' result = '检查到更新,日志如下:\n' for i, commit in enumerate(remote_commit, start=1): - time_str = (datetime.datetime.strptime(commit['commit']['committer']['date'], '%Y-%m-%dT%H:%M:%SZ') + datetime.timedelta(hours=8)).strftime('%Y-%m-%d %H:%M:%S') + time_str = (datetime.datetime.strptime(commit['commit']['committer']['date'], + '%Y-%m-%dT%H:%M:%SZ') + datetime.timedelta(hours=8)).strftime( + '%Y-%m-%d %H:%M:%S') result += f'{i}.{time_str}\n' + commit['commit']['message'].replace(':bug:', '🐛').replace( ':sparkles:', '✨').replace(':memo:', '📝') + '\n' return result @@ -36,16 +39,28 @@ async def check_update(): @run_sync def update(): + pyproject_file = Path().parent / 'pyproject.toml' + pyproject_raw_content = pyproject_file.read_text(encoding='utf-8') + if raw_plugins_load := re.search(r'^plugins = \[.*]$', pyproject_raw_content, flags=re.M): + pyproject_new_content = pyproject_raw_content.replace(raw_plugins_load.group(), 'plugins = []') + else: + pyproject_new_content = pyproject_raw_content + pyproject_file.write_text(pyproject_new_content, encoding='utf-8') try: repo = git.Repo(Path().absolute()) except InvalidGitRepositoryError: - return '没有发现git仓库,无法通过git更新' + return '没有发现git仓库,无法通过git更新,请手动下载最新版本的文件进行替换。' origin = repo.remotes.origin - # repo.git.stash() try: origin.pull() except GitCommandError as e: - return f'更新失败,错误信息:{e},请手动进行更新' - # finally: - # repo.git.stash('pop') + if 'timeout' in e or 'unable to access' in e: + return '更新失败,连接git仓库超时,请重试或修改源为代理源后再重试。' + elif ' Your local changes to the following files would be overwritten by merge' in e: + return ('error: Your local changes to the following files would be overwritten by merge\n' + '更新失败,本地修改过文件导致冲突,可在命令行运行git pull查看冲突的文件是哪些,请解决冲突后再更新。') + return f'更新失败,错误信息:{e},请尝试手动进行更新' + pyproject_new_content = pyproject_file.read_text(encoding='utf-8') + pyproject_new_content = pyproject_new_content.replace('plugins = []', raw_plugins_load.group()) + pyproject_file.write_text(pyproject_new_content, encoding='utf-8') return f'更新完成,版本:{__version__}\n最新更新日志为:\n{repo.head.commit.message.replace(":bug:", "🐛").replace(":sparkles:", "✨").replace(":memo:", "📝")}\n可使用命令[@bot 重启]重启{NICKNAME}'