优化更新提示

This commit is contained in:
CMHopeSunshine 2023-01-06 21:56:31 +08:00
parent 1ebd44296b
commit 905245be79

View File

@ -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}'