-
Notifications
You must be signed in to change notification settings - Fork 1
/
new_version_update.py
140 lines (113 loc) · 4.17 KB
/
new_version_update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import os
import requests
import hashlib
supported_version = ["", "0.4", "0.5", "0.5.1", "0.5.2", "0.5.3", "0.5.4", "0.5.5", "0.5.6", "0.5.7"]
current_version = "0.5.8"
version_disc = """
-------------------
{} 版本更新说明:
- 修复了异常。
-------------------
""".format(current_version)
remoteBaseURL = "https://cdn.jsdelivr.net/gh/FxxkTheLife/LibraryIsMyHome"
localBaseURL = "."
dir_to_make = ["/backend/",
"/console_view/",
"/preset/"]
file_must_exist = [("/preset/login.json", "[]"),
("/preset/seat.json", "[]")]
file_to_update = ["/backend/__init__.py",
"/backend/constant.py",
"/backend/cookie.py",
"/backend/exception.py",
"/backend/login.py",
"/backend/my_seat.py",
"/backend/preliminary.py",
"/backend/request.py",
"/backend/reserve.py",
"/backend/sign.py",
"/backend/start.py",
"/backend/supervise.py",
"/console_view/__init__.py",
"/console_view/console_home.py",
"/console_view/console_login.py",
"/console_view/input_booking_mess.py",
"/console_start.py",
"/console_start.command",
"/console_start.bat",
"/requirements.txt",
"/version",
"/check4update.py",
"/check4update.command",
"/check4update.bat"]
def isUpToDate(fileName, url):
with open(fileName, "r", encoding="utf-8") as f:
file = f.read()
hash = hashlib.sha256(file.encode('utf-8')).hexdigest()
urlcode = requests.get(url).text
urlhash = hashlib.sha256(urlcode.encode('utf-8')).hexdigest()
if hash == urlhash:
return True
else:
return False
def update(path, url):
print("正在下载: {}".format(url))
response = requests.get(url)
response.encoding = 'utf-8'
if response.status_code == 200:
with open(path, "w", encoding="utf-8") as f:
f.write(response.text)
else:
print("\033[31m下载遇到错误\033[0m")
def checkForUpdates(path, url):
if not isUpToDate(path, url):
update(path, url)
return True
else:
return False
def update_file(file):
global remoteBaseURL, localBaseURL
localURL = localBaseURL + file
remoteURL = remoteBaseURL + file
if os.path.exists(localURL):
checkForUpdates(localURL, remoteURL)
else:
update(localURL, remoteURL)
def update_dir(path):
global localBaseURL
localURL = localBaseURL + path
if not os.path.exists(localURL):
os.makedirs(localURL)
# 主命令
def update_command():
global localBaseURL
for path in dir_to_make:
update_dir(path)
for file in file_to_update:
update_file(file)
for file_info in file_must_exist:
localURL = localBaseURL + file_info[0]
if not os.path.exists(localURL):
with open(localURL, "w") as f:
f.write(file_info[1])
def start_update(version, new_version):
print("\033[34m验证中...\033[0m")
if version == new_version:
print("\033[32m你当前版本已是此版本\033[0m")
return
elif version not in supported_version:
print("\033[31m更新失败:你当前版本已不支持更新到这个版本\033[0m")
return
print(version_disc)
if version == "":
print("\033[32m下载新版本 {}\033[0m".format(new_version))
else:
print("\033[32m当前版本 {} ====> 更新到版本 {}\033[0m".format(version, new_version))
input("回车开始更新 >>>")
print("\033[34m更新中...\033[0m")
global remoteBaseURL, localBaseURL
remoteBaseURL += "@" + new_version
update_command()
print("\033[32m恭喜!更新已完成,欢迎使用新版本 {} ~~~\033[0m".format(new_version))
print("\033[32m运行当前目录下 console_start.py 即可运行脚本\n"
"Windows 用户可运行 console_start.bat 来使用,Mac 用户可运行 console_start.command 来使用\033[0m")