Your Name commited on
Commit
2f9ec38
1 Parent(s): 3249b31

自动更新程序

Browse files
Files changed (2) hide show
  1. check_proxy.py +84 -24
  2. main.py +3 -4
check_proxy.py CHANGED
@@ -19,33 +19,93 @@ def check_proxy(proxies):
19
  print(result)
20
  return result
21
 
22
-
23
- def auto_update():
 
 
24
  from toolbox import get_conf
25
- import requests
26
- import time
27
- import json
 
 
 
 
28
  proxies, = get_conf('proxies')
29
- response = requests.get("https://raw.githubusercontent.com/binary-husky/chatgpt_academic/master/version",
30
- proxies=proxies, timeout=1)
31
- remote_json_data = json.loads(response.text)
32
- remote_version = remote_json_data['version']
33
- if remote_json_data["show_feature"]:
34
- new_feature = "新功能:" + remote_json_data["new_feature"]
35
- else:
36
- new_feature = ""
37
- with open('./version', 'r', encoding='utf8') as f:
38
- current_version = f.read()
39
- current_version = json.loads(current_version)['version']
40
- if (remote_version - current_version) >= 0.05:
41
- print(
42
- f'\n新版本可用。新版本:{remote_version},当前版本:{current_version}。{new_feature}')
43
- print('Github更新地址:\nhttps://github.com/binary-husky/chatgpt_academic\n')
44
- time.sleep(3)
45
- return
46
- else:
47
- return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  if __name__ == '__main__':
51
  import os
 
19
  print(result)
20
  return result
21
 
22
+ def backup_and_download(current_version, remote_version):
23
+ """
24
+ 一键更新协议:备份和下载
25
+ """
26
  from toolbox import get_conf
27
+ import shutil, os, requests, zipfile
28
+ os.makedirs(f'./history', exist_ok=True)
29
+ backup_dir = f'./history/backup-{current_version}/'
30
+ new_version_dir = f'./history/new-version-{remote_version}/'
31
+ if os.path.exists(new_version_dir): return new_version_dir
32
+ os.makedirs(new_version_dir)
33
+ shutil.copytree('./', backup_dir, ignore=lambda x,y: ['history'])
34
  proxies, = get_conf('proxies')
35
+ r = requests.get('https://github.com/binary-husky/chatgpt_academic/archive/refs/heads/master.zip', proxies=proxies, stream=True)
36
+ zip_file_path = backup_dir+'/master.zip'
37
+ with open(zip_file_path, 'wb+') as f:
38
+ f.write(r.content)
39
+ dst_path = new_version_dir
40
+ with zipfile.ZipFile(zip_file_path, "r") as zip_ref:
41
+ for zip_info in zip_ref.infolist():
42
+ dst_file_path = os.path.join(dst_path, zip_info.filename)
43
+ if os.path.exists(dst_file_path):
44
+ os.remove(dst_file_path)
45
+ zip_ref.extract(zip_info, dst_path)
46
+ return new_version_dir
47
+
48
+ def patch_and_restart(path):
49
+ """
50
+ 一键更新协议:覆盖和重启
51
+ """
52
+ import distutils, shutil, os, sys, time
53
+ # if not using config_private, move origin config.py as config_private.py
54
+ if not os.path.exists('config_private.py'):
55
+ print('由于您没有设置config_private.py私密配置,现将您的现有配置移动至config_private.py以防止配置丢失,',
56
+ '另外您可以随时在history子文件夹下找回旧版的程序。')
57
+ shutil.copyfile('config.py', 'config_private.py')
58
+ distutils.dir_util.copy_tree(path+'/chatgpt_academic-master', './')
59
+ print('更新完成,您可以随时在history子文件夹下找回旧版的程序,5s之后重启')
60
+ for i in reversed(range(5)):
61
+ time.sleep(1); print(i)
62
+ print(' ------------------------------ -----------------------------------')
63
+ os.execl(sys.executable, 'python', 'main.py')
64
+
65
+ def get_current_version():
66
+ import json
67
+ try:
68
+ with open('./version', 'r', encoding='utf8') as f:
69
+ current_version = json.loads(f.read())['version']
70
+ except:
71
+ current_version = ""
72
+ return current_version
73
 
74
+ def auto_update():
75
+ """
76
+ 一键更新协议:查询版本和用户意见
77
+ """
78
+ try:
79
+ from toolbox import get_conf
80
+ import requests
81
+ import time
82
+ import json
83
+ proxies, = get_conf('proxies')
84
+ response = requests.get("https://raw.githubusercontent.com/binary-husky/chatgpt_academic/master/version", proxies=proxies, timeout=1)
85
+ remote_json_data = json.loads(response.text)
86
+ remote_version = remote_json_data['version']
87
+ if remote_json_data["show_feature"]:
88
+ new_feature = "新功能:" + remote_json_data["new_feature"]
89
+ else:
90
+ new_feature = ""
91
+ with open('./version', 'r', encoding='utf8') as f:
92
+ current_version = f.read()
93
+ current_version = json.loads(current_version)['version']
94
+ if (remote_version - current_version) >= 0.05:
95
+ print(
96
+ f'\n新版本可用。新版本:{remote_version},当前版本:{current_version}。{new_feature}')
97
+ print('(1)Github更新地址:\nhttps://github.com/binary-husky/chatgpt_academic\n')
98
+ user_instruction = input('(2)是否一键更新代码(Y/y+回车=确认,输入其他/无输入+回车=不更新)?')
99
+ if user_instruction in ['Y', 'y']:
100
+ path = backup_and_download(current_version, remote_version)
101
+ try: patch_and_restart(path)
102
+ except: print('更新失败。')
103
+ else:
104
+ return
105
+ else:
106
+ return
107
+ except:
108
+ print('自动更新程序未正常工作。')
109
 
110
  if __name__ == '__main__':
111
  import os
main.py CHANGED
@@ -1,4 +1,4 @@
1
- import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染
2
  import gradio as gr
3
  from request_llm.bridge_chatgpt import predict
4
  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():
163
  print(f"\t(亮色主体): http://localhost:{PORT}")
164
  print(f"\t(暗色主体): http://localhost:{PORT}/?__dark-theme=true")
165
  def open():
166
- time.sleep(2)
167
- try: auto_update() # 检查新版本
168
- except: pass
169
  webbrowser.open_new_tab(f"http://localhost:{PORT}/?__dark-theme=true")
170
  threading.Thread(target=open, name="open-browser", daemon=True).start()
 
171
 
172
  auto_opentab_delay()
173
  demo.title = "ChatGPT 学术优化"
 
1
+ import os, json; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染
2
  import gradio as gr
3
  from request_llm.bridge_chatgpt import predict
4
  from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper, DummyWith
 
163
  print(f"\t(亮色主体): http://localhost:{PORT}")
164
  print(f"\t(暗色主体): http://localhost:{PORT}/?__dark-theme=true")
165
  def open():
166
+ time.sleep(2) # 打开浏览器
 
 
167
  webbrowser.open_new_tab(f"http://localhost:{PORT}/?__dark-theme=true")
168
  threading.Thread(target=open, name="open-browser", daemon=True).start()
169
+ threading.Thread(target=auto_update, name="self-upgrade", daemon=True).start()
170
 
171
  auto_opentab_delay()
172
  demo.title = "ChatGPT 学术优化"