From d12acd5f3171f0e1f9f28103637470ecf148b80a Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Sun, 5 Mar 2023 11:05:42 +0800 Subject: [PATCH 1/6] =?UTF-8?q?chore:=20git=E5=BF=BD=E7=95=A5temp=E7=9B=AE?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 7011765..c992502 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ plugins/ /revcfg.py prompts/ logs/ -sensitive.json \ No newline at end of file +sensitive.json +temp/ \ No newline at end of file From 79e970c4c351f41e7e4b4457ce3d3ff7c8a099a8 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Sun, 5 Mar 2023 11:09:24 +0800 Subject: [PATCH 2/6] =?UTF-8?q?chore(deps):=20=E5=88=A0=E9=99=A4dulwich?= =?UTF-8?q?=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 628307b..3bae775 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,5 @@ requests~=2.28.1 openai~=0.27.0 -pip~=22.3.1 -dulwich~=0.21.3 colorlog~=6.6.0 yiri-mirai~=0.2.6.1 websockets~=10.4 From 792366e221cbb70c7ec429fc0bb53f4363034baa Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Sun, 5 Mar 2023 11:56:40 +0800 Subject: [PATCH 3/6] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E5=9F=BA?= =?UTF-8?q?=E4=BA=8E=E8=AF=AD=E4=B9=89=E5=8C=96=E7=89=88=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- main.py | 6 ++- pkg/utils/updater.py | 109 +++++++++++++++++++++++++++++++++---------- requirements.txt | 1 + 4 files changed, 93 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index c992502..362973b 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ plugins/ prompts/ logs/ sensitive.json -temp/ \ No newline at end of file +temp/ +current_tag \ No newline at end of file diff --git a/main.py b/main.py index dae29ae..c4ff6ab 100644 --- a/main.py +++ b/main.py @@ -314,10 +314,14 @@ if __name__ == '__main__': if not os.path.exists('banlist.py'): shutil.copy('banlist-template.py', 'banlist.py') - # 检查是否有sensitive.json, + # 检查是否有sensitive.json if not os.path.exists("sensitive.json"): shutil.copy("sensitive-template.json", "sensitive.json") + # 检查temp目录 + if not os.path.exists("temp/"): + os.mkdir("temp/") + if len(sys.argv) > 1 and sys.argv[1] == 'init_db': init_db() sys.exit(0) diff --git a/pkg/utils/updater.py b/pkg/utils/updater.py index 7c55dd7..d053503 100644 --- a/pkg/utils/updater.py +++ b/pkg/utils/updater.py @@ -1,4 +1,9 @@ import datetime +import logging +import os.path + +import requests +import json import pkg.utils.context @@ -29,33 +34,85 @@ def pull_latest(repo_path: str) -> bool: def update_all() -> bool: - """使用dulwich更新源码""" - check_dulwich_closure() - import dulwich - try: - before_commit_id = get_current_commit_id() - from dulwich import porcelain - repo = porcelain.open_repo('.') - porcelain.pull(repo) + """检查更新并下载源码""" + current_tag = "v0.1.0" + if os.path.exists("current_tag"): + with open("current_tag", "r") as f: + current_tag = f.read() - change_log = "" + rls_list_resp = requests.get( + url="https://api.github.com/repos/RockChinQ/QChatGPT/releases" + ) - for entry in repo.get_walker(): - if str(entry.commit.id)[2:-1] == before_commit_id: - break - tz = datetime.timezone(datetime.timedelta(hours=entry.commit.commit_timezone // 3600)) - dt = datetime.datetime.fromtimestamp(entry.commit.commit_time, tz) - change_log += dt.strftime('%Y-%m-%d %H:%M:%S') + " [" + str(entry.commit.message, encoding="utf-8").strip()+"]\n" + rls_list = rls_list_resp.json() - if change_log != "": - pkg.utils.context.get_qqbot_manager().notify_admin("代码拉取完成,更新内容如下:\n"+change_log) - return True - else: - return False - except ModuleNotFoundError: - raise Exception("dulwich模块未安装,请查看 https://github.com/RockChinQ/QChatGPT/issues/77") - except dulwich.porcelain.DivergedBranches: - raise Exception("分支不一致,自动更新仅支持master分支,请手动更新(https://github.com/RockChinQ/QChatGPT/issues/76)") + latest_rls = {} + rls_notes = [] + for rls in rls_list: + rls_notes.append(rls['name']) # 使用发行名称作为note + if rls['tag_name'] == current_tag: + break + + if latest_rls == {}: + latest_rls = rls + print(rls_notes) + if latest_rls == {}: # 没有新版本 + return False + + # 下载最新版本的zip到temp目录 + logging.info("开始下载最新版本: {}".format(latest_rls['zipball_url'])) + zip_url = latest_rls['zipball_url'] + zip_resp = requests.get(url=zip_url) + zip_data = zip_resp.content + + # 检查temp/updater目录 + if not os.path.exists("temp"): + os.mkdir("temp") + if not os.path.exists("temp/updater"): + os.mkdir("temp/updater") + with open("temp/updater/{}.zip".format(latest_rls['tag_name']), "wb") as f: + f.write(zip_data) + + logging.info("下载最新版本完成: {}".format("temp/updater/{}.zip".format(latest_rls['tag_name']))) + + # 解压zip到temp/updater// + import zipfile + # 检查目标文件夹 + if os.path.exists("temp/updater/{}".format(latest_rls['tag_name'])): + import shutil + shutil.rmtree("temp/updater/{}".format(latest_rls['tag_name'])) + os.mkdir("temp/updater/{}".format(latest_rls['tag_name'])) + with zipfile.ZipFile("temp/updater/{}.zip".format(latest_rls['tag_name']), 'r') as zip_ref: + zip_ref.extractall("temp/updater/{}".format(latest_rls['tag_name'])) + + # 覆盖源码 + source_root = "" + # 找到temp/updater//中的第一个子目录路径 + for root, dirs, files in os.walk("temp/updater/{}".format(latest_rls['tag_name'])): + if root != "temp/updater/{}".format(latest_rls['tag_name']): + source_root = root + break + + # 覆盖源码 + import shutil + for root, dirs, files in os.walk(source_root): + # 覆盖所有子文件子目录 + for file in files: + src = os.path.join(root, file) + dst = src.replace(source_root, ".") + if os.path.exists(dst): + os.remove(dst) + shutil.copy(src, dst) + + # 把current_tag写入文件 + current_tag = latest_rls['tag_name'] + with open("current_tag", "w") as f: + f.write(current_tag) + + # 通知管理员 + import pkg.utils.context + pkg.utils.context.get_qqbot_manager().notify_admin("已更新到最新版本: {}\n更新日志:\n{}".format(current_tag, "\n".join(rls_notes))) + return True def is_repo(path: str) -> bool: @@ -144,3 +201,7 @@ def is_new_version_available() -> bool: latest_commit_id = str(fetch_res[b'HEAD'])[2:-1] return current_commit_id != latest_commit_id + + +if __name__ == "__main__": + update_all() diff --git a/requirements.txt b/requirements.txt index 3bae775..a61c1a0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ requests~=2.28.1 openai~=0.27.0 +dulwich~=0.21.3 colorlog~=6.6.0 yiri-mirai~=0.2.6.1 websockets~=10.4 From 424b970469c2533a46aaf2415b5b20bae8e999b4 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Sun, 5 Mar 2023 11:58:18 +0800 Subject: [PATCH 4/6] =?UTF-8?q?feat:=20=E5=B0=86=E4=BE=9D=E8=B5=96?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E6=9B=B4=E6=94=B9=E5=88=B0main=E6=B5=81?= =?UTF-8?q?=E7=A8=8B=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/main.py b/main.py index c4ff6ab..e52a1ce 100644 --- a/main.py +++ b/main.py @@ -107,11 +107,18 @@ def reset_logging(): def main(first_time_init=False): global known_exception_caught - # 检查并创建plugins、prompts目录 - check_path = ["plugins", "prompts"] - for path in check_path: - if not os.path.exists(path): - os.mkdir(path) + import config + # 更新openai库到最新版本 + if not hasattr(config, 'upgrade_dependencies') or config.upgrade_dependencies: + print("正在更新依赖库,请等待...") + if not hasattr(config, 'upgrade_dependencies'): + print("这个操作不是必须的,如果不想更新,请在config.py中添加upgrade_dependencies=False") + else: + print("这个操作不是必须的,如果不想更新,请在config.py中将upgrade_dependencies设置为False") + try: + ensure_dependencies() + except Exception as e: + print("更新openai库失败:{}, 请忽略或自行更新".format(e)) known_exception_caught = False try: @@ -322,6 +329,12 @@ if __name__ == '__main__': if not os.path.exists("temp/"): os.mkdir("temp/") + # 检查并创建plugins、prompts目录 + check_path = ["plugins", "prompts"] + for path in check_path: + if not os.path.exists(path): + os.mkdir(path) + if len(sys.argv) > 1 and sys.argv[1] == 'init_db': init_db() sys.exit(0) @@ -347,17 +360,4 @@ if __name__ == '__main__': # pkg.utils.configmgr.set_config_and_reload("quote_origin", False) requests.packages.urllib3.disable_warnings(InsecureRequestWarning) - import config - # 更新openai库到最新版本 - if not hasattr(config, 'upgrade_dependencies') or config.upgrade_dependencies: - print("正在更新依赖库,请等待...") - if not hasattr(config, 'upgrade_dependencies'): - print("这个操作不是必须的,如果不想更新,请在config.py中添加upgrade_dependencies=False") - else: - print("这个操作不是必须的,如果不想更新,请在config.py中将upgrade_dependencies设置为False") - try: - ensure_dependencies() - except Exception as e: - print("更新openai库失败:{}, 请忽略或自行更新".format(e)) - main(True) From 964ad42cb4d5428f16b800babc465995efadba42 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Sun, 5 Mar 2023 12:02:59 +0800 Subject: [PATCH 5/6] =?UTF-8?q?perf:=20=E5=AE=8C=E5=96=84=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 --- pkg/utils/updater.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/utils/updater.py b/pkg/utils/updater.py index d053503..9d41d09 100644 --- a/pkg/utils/updater.py +++ b/pkg/utils/updater.py @@ -55,7 +55,7 @@ def update_all() -> bool: if latest_rls == {}: latest_rls = rls - print(rls_notes) + logging.info("更新日志: {}".format(rls_notes)) if latest_rls == {}: # 没有新版本 return False @@ -111,7 +111,7 @@ def update_all() -> bool: # 通知管理员 import pkg.utils.context - pkg.utils.context.get_qqbot_manager().notify_admin("已更新到最新版本: {}\n更新日志:\n{}".format(current_tag, "\n".join(rls_notes))) + pkg.utils.context.get_qqbot_manager().notify_admin("已更新到最新版本: {}\n更新日志:\n{}\n新功能通常可以在config-template.py中看到,完整的更新日志请前往 https://github.com/RockChinQ/QChatGPT/releases 查看".format(current_tag, "\n".join(rls_notes))) return True From f8abe906749688f9a67628a161ba09c8842cbe44 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Sun, 5 Mar 2023 12:09:44 +0800 Subject: [PATCH 6/6] =?UTF-8?q?perf:=20=E5=AE=8C=E5=96=84=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E6=A3=80=E6=9F=A5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 2 +- pkg/utils/updater.py | 65 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/main.py b/main.py index e52a1ce..379cbfb 100644 --- a/main.py +++ b/main.py @@ -270,7 +270,7 @@ def main(first_time_init=False): import pkg.utils.updater try: if pkg.utils.updater.is_new_version_available(): - pkg.utils.context.get_qqbot_manager().notify_admin("新版本可用,请发送 !update 进行自动更新") + pkg.utils.context.get_qqbot_manager().notify_admin("新版本可用,请发送 !update 进行自动更新\n更新日志:\n{}".format("\n".join(pkg.utils.updater.get_rls_notes()))) else: logging.info("当前已是最新版本") diff --git a/pkg/utils/updater.py b/pkg/utils/updater.py index 9d41d09..4c74bc9 100644 --- a/pkg/utils/updater.py +++ b/pkg/utils/updater.py @@ -33,19 +33,33 @@ def pull_latest(repo_path: str) -> bool: return True -def update_all() -> bool: - """检查更新并下载源码""" - current_tag = "v0.1.0" - if os.path.exists("current_tag"): - with open("current_tag", "r") as f: - current_tag = f.read() - +def get_release_list() -> list: + """获取发行列表""" rls_list_resp = requests.get( url="https://api.github.com/repos/RockChinQ/QChatGPT/releases" ) rls_list = rls_list_resp.json() + return rls_list + + +def get_current_tag() -> str: + """获取当前tag""" + current_tag = "v0.1.0" + if os.path.exists("current_tag"): + with open("current_tag", "r") as f: + current_tag = f.read() + + return current_tag + + +def update_all() -> bool: + """检查更新并下载源码""" + current_tag = get_current_tag() + + rls_list = get_release_list() + latest_rls = {} rls_notes = [] for rls in rls_list: @@ -189,18 +203,41 @@ def get_current_commit_id() -> str: def is_new_version_available() -> bool: """检查是否有新版本""" - check_dulwich_closure() + # 从github获取release列表 + rls_list = get_release_list() + if rls_list is None: + return False - from dulwich import porcelain + # 获取当前版本 + current_tag = get_current_tag() - repo = porcelain.open_repo('.') - fetch_res = porcelain.ls_remote(porcelain.get_remote_repo(repo, "origin")[1]) + # 检查是否有新版本 + for rls in rls_list: + if rls['tag_name'] == current_tag: + return False + else: + return True - current_commit_id = get_current_commit_id() - latest_commit_id = str(fetch_res[b'HEAD'])[2:-1] +def get_rls_notes() -> list: + """获取更新日志""" + # 从github获取release列表 + rls_list = get_release_list() + if rls_list is None: + return None - return current_commit_id != latest_commit_id + # 获取当前版本 + current_tag = get_current_tag() + + # 检查是否有新版本 + rls_notes = [] + for rls in rls_list: + if rls['tag_name'] == current_tag: + break + + rls_notes.append(rls['name']) + + return rls_notes if __name__ == "__main__":