diff --git a/check_proxy.py b/check_proxy.py index 95a439e..e858a6c 100644 --- a/check_proxy.py +++ b/check_proxy.py @@ -19,33 +19,93 @@ def check_proxy(proxies): print(result) return result +def backup_and_download(current_version, remote_version): + """ + 一键更新协议:备份和下载 + """ + from toolbox import get_conf + import shutil, os, requests, zipfile + os.makedirs(f'./history', exist_ok=True) + backup_dir = f'./history/backup-{current_version}/' + new_version_dir = f'./history/new-version-{remote_version}/' + if os.path.exists(new_version_dir): return new_version_dir + os.makedirs(new_version_dir) + shutil.copytree('./', backup_dir, ignore=lambda x,y: ['history']) + proxies, = get_conf('proxies') + r = requests.get('https://github.com/binary-husky/chatgpt_academic/archive/refs/heads/master.zip', proxies=proxies, stream=True) + zip_file_path = backup_dir+'/master.zip' + with open(zip_file_path, 'wb+') as f: + f.write(r.content) + dst_path = new_version_dir + with zipfile.ZipFile(zip_file_path, "r") as zip_ref: + for zip_info in zip_ref.infolist(): + dst_file_path = os.path.join(dst_path, zip_info.filename) + if os.path.exists(dst_file_path): + os.remove(dst_file_path) + zip_ref.extract(zip_info, dst_path) + return new_version_dir + +def patch_and_restart(path): + """ + 一键更新协议:覆盖和重启 + """ + import distutils, shutil, os, sys, time + # if not using config_private, move origin config.py as config_private.py + if not os.path.exists('config_private.py'): + print('由于您没有设置config_private.py私密配置,现将您的现有配置移动至config_private.py以防止配置丢失,', + '另外您可以随时在history子文件夹下找回旧版的程序。') + shutil.copyfile('config.py', 'config_private.py') + distutils.dir_util.copy_tree(path+'/chatgpt_academic-master', './') + print('更新完成,您可以随时在history子文件夹下找回旧版的程序,5s之后重启') + for i in reversed(range(5)): + time.sleep(1); print(i) + print(' ------------------------------ -----------------------------------') + os.execl(sys.executable, 'python', 'main.py') + +def get_current_version(): + import json + try: + with open('./version', 'r', encoding='utf8') as f: + current_version = json.loads(f.read())['version'] + except: + current_version = "" + return current_version def auto_update(): - from toolbox import get_conf - import requests - import time - import json - proxies, = get_conf('proxies') - response = requests.get("https://raw.githubusercontent.com/binary-husky/chatgpt_academic/master/version", - proxies=proxies, timeout=1) - remote_json_data = json.loads(response.text) - remote_version = remote_json_data['version'] - if remote_json_data["show_feature"]: - new_feature = "新功能:" + remote_json_data["new_feature"] - else: - new_feature = "" - with open('./version', 'r', encoding='utf8') as f: - current_version = f.read() - current_version = json.loads(current_version)['version'] - if (remote_version - current_version) >= 0.05: - print( - f'\n新版本可用。新版本:{remote_version},当前版本:{current_version}。{new_feature}') - print('Github更新地址:\nhttps://github.com/binary-husky/chatgpt_academic\n') - time.sleep(3) - return - else: - return - + """ + 一键更新协议:查询版本和用户意见 + """ + try: + from toolbox import get_conf + import requests + import time + import json + proxies, = get_conf('proxies') + response = requests.get("https://raw.githubusercontent.com/binary-husky/chatgpt_academic/master/version", proxies=proxies, timeout=1) + remote_json_data = json.loads(response.text) + remote_version = remote_json_data['version'] + if remote_json_data["show_feature"]: + new_feature = "新功能:" + remote_json_data["new_feature"] + else: + new_feature = "" + with open('./version', 'r', encoding='utf8') as f: + current_version = f.read() + current_version = json.loads(current_version)['version'] + if (remote_version - current_version) >= 0.05: + print( + f'\n新版本可用。新版本:{remote_version},当前版本:{current_version}。{new_feature}') + print('(1)Github更新地址:\nhttps://github.com/binary-husky/chatgpt_academic\n') + user_instruction = input('(2)是否一键更新代码(Y/y+回车=确认,输入其他/无输入+回车=不更新)?') + if user_instruction in ['Y', 'y']: + path = backup_and_download(current_version, remote_version) + try: patch_and_restart(path) + except: print('更新失败。') + else: + return + else: + return + except: + print('自动更新程序未正常工作。') if __name__ == '__main__': import os diff --git a/main.py b/main.py index 67ee00a..b161c6f 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,4 @@ -import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染 +import os, json; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染 import gradio as gr from request_llm.bridge_chatgpt import predict from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper, DummyWith @@ -163,11 +163,10 @@ def auto_opentab_delay(): print(f"\t(亮色主体): http://localhost:{PORT}") print(f"\t(暗色主体): http://localhost:{PORT}/?__dark-theme=true") def open(): - time.sleep(2) - try: auto_update() # 检查新版本 - except: pass + time.sleep(2) # 打开浏览器 webbrowser.open_new_tab(f"http://localhost:{PORT}/?__dark-theme=true") threading.Thread(target=open, name="open-browser", daemon=True).start() + threading.Thread(target=auto_update, name="self-upgrade", daemon=True).start() auto_opentab_delay() demo.title = "ChatGPT 学术优化"