Spaces:
Running
Running
加入自动更新协议
Browse files- check_proxy.py +30 -11
check_proxy.py
CHANGED
@@ -19,20 +19,26 @@ def check_proxy(proxies):
|
|
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
|
|
|
|
|
|
|
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):
|
|
|
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(
|
|
|
36 |
zip_file_path = backup_dir+'/master.zip'
|
37 |
with open(zip_file_path, 'wb+') as f:
|
38 |
f.write(r.content)
|
@@ -45,11 +51,16 @@ def backup_and_download(current_version, remote_version):
|
|
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
|
|
|
|
|
|
|
|
|
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以防止配置丢失,',
|
@@ -58,19 +69,22 @@ def patch_and_restart(path):
|
|
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)
|
|
|
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 |
一键更新协议:查询版本和用户意见
|
@@ -81,7 +95,8 @@ def auto_update():
|
|
81 |
import time
|
82 |
import json
|
83 |
proxies, = get_conf('proxies')
|
84 |
-
response = requests.get(
|
|
|
85 |
remote_json_data = json.loads(response.text)
|
86 |
remote_version = remote_json_data['version']
|
87 |
if remote_json_data["show_feature"]:
|
@@ -98,14 +113,18 @@ def auto_update():
|
|
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:
|
102 |
-
|
|
|
|
|
103 |
else:
|
|
|
104 |
return
|
105 |
else:
|
106 |
return
|
107 |
except:
|
108 |
-
print('
|
|
|
109 |
|
110 |
if __name__ == '__main__':
|
111 |
import os
|
|
|
19 |
print(result)
|
20 |
return result
|
21 |
|
22 |
+
|
23 |
def backup_and_download(current_version, remote_version):
|
24 |
"""
|
25 |
一键更新协议:备份和下载
|
26 |
"""
|
27 |
from toolbox import get_conf
|
28 |
+
import shutil
|
29 |
+
import os
|
30 |
+
import requests
|
31 |
+
import zipfile
|
32 |
os.makedirs(f'./history', exist_ok=True)
|
33 |
backup_dir = f'./history/backup-{current_version}/'
|
34 |
new_version_dir = f'./history/new-version-{remote_version}/'
|
35 |
+
if os.path.exists(new_version_dir):
|
36 |
+
return new_version_dir
|
37 |
os.makedirs(new_version_dir)
|
38 |
+
shutil.copytree('./', backup_dir, ignore=lambda x, y: ['history'])
|
39 |
proxies, = get_conf('proxies')
|
40 |
+
r = requests.get(
|
41 |
+
'https://github.com/binary-husky/chatgpt_academic/archive/refs/heads/master.zip', proxies=proxies, stream=True)
|
42 |
zip_file_path = backup_dir+'/master.zip'
|
43 |
with open(zip_file_path, 'wb+') as f:
|
44 |
f.write(r.content)
|
|
|
51 |
zip_ref.extract(zip_info, dst_path)
|
52 |
return new_version_dir
|
53 |
|
54 |
+
|
55 |
def patch_and_restart(path):
|
56 |
"""
|
57 |
一键更新协议:覆盖和重启
|
58 |
"""
|
59 |
+
import distutils
|
60 |
+
import shutil
|
61 |
+
import os
|
62 |
+
import sys
|
63 |
+
import time
|
64 |
# if not using config_private, move origin config.py as config_private.py
|
65 |
if not os.path.exists('config_private.py'):
|
66 |
print('由于您没有设置config_private.py私密配置,现将您的现有配置移动至config_private.py以防止配置丢失,',
|
|
|
69 |
distutils.dir_util.copy_tree(path+'/chatgpt_academic-master', './')
|
70 |
print('更新完成,您可以随时在history子文件夹下找回旧版的程序,5s之后重启')
|
71 |
for i in reversed(range(5)):
|
72 |
+
time.sleep(1)
|
73 |
+
print(i)
|
74 |
print(' ------------------------------ -----------------------------------')
|
75 |
os.execl(sys.executable, 'python', 'main.py')
|
76 |
|
77 |
+
|
78 |
def get_current_version():
|
79 |
import json
|
80 |
try:
|
81 |
+
with open('./version', 'r', encoding='utf8') as f:
|
82 |
current_version = json.loads(f.read())['version']
|
83 |
except:
|
84 |
current_version = ""
|
85 |
return current_version
|
86 |
|
87 |
+
|
88 |
def auto_update():
|
89 |
"""
|
90 |
一键更新协议:查询版本和用户意见
|
|
|
95 |
import time
|
96 |
import json
|
97 |
proxies, = get_conf('proxies')
|
98 |
+
response = requests.get(
|
99 |
+
"https://raw.githubusercontent.com/binary-husky/chatgpt_academic/master/version", proxies=proxies, timeout=1)
|
100 |
remote_json_data = json.loads(response.text)
|
101 |
remote_version = remote_json_data['version']
|
102 |
if remote_json_data["show_feature"]:
|
|
|
113 |
user_instruction = input('(2)是否一键更新代码(Y/y+回车=确认,输入其他/无输入+回车=不更新)?')
|
114 |
if user_instruction in ['Y', 'y']:
|
115 |
path = backup_and_download(current_version, remote_version)
|
116 |
+
try:
|
117 |
+
patch_and_restart(path)
|
118 |
+
except:
|
119 |
+
print('更新失败。')
|
120 |
else:
|
121 |
+
print('自动更新程序:已禁用')
|
122 |
return
|
123 |
else:
|
124 |
return
|
125 |
except:
|
126 |
+
print('自动更新程序:已禁用')
|
127 |
+
|
128 |
|
129 |
if __name__ == '__main__':
|
130 |
import os
|