-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
253 lines (237 loc) · 8.6 KB
/
main.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"""
@author: got991
@file: main.py
@contact: [email protected]
@github: https://github.com/got991
@license: GNU General Public License v3.0
"""
# main.py_summary_
# Raises:
# SystemError: 在不支持系统上时
# Exception: _发生未知错误时(变量 ik 非正常值)
# Exception: 版本核心 sha1 校验失败时
# Returns:
# _type_: _description_
import logging
import time
import configparser
import json
import os
import hashlib
import platform
import wget
# 检测系统 (Windows / Linux)
system = platform.platform().lower()
if "windows" in system or "nt" in system:
SystemType = "Windows"
elif "linux" in system:
SystemType = "Linux"
elif "darwin" in system or "mac" in system:
# else:
print("抱歉,暂不支持 Mac OS 系统")
raise SystemError("暂不支持 Mac OS 系统")
else:
print("未知系统!")
raise SystemError("暂不支持该系统")
# 初始化日志
# (已修复)删除了程序正在使用的日志文件 #
# 修复方法:将这一段放到生成日志之前运行
# 删除没有内容的日志文件
ct = len(os.listdir("log"))
for lf in os.listdir("log"):
if not os.path.getsize(f"log/{lf}") > 0:
os.remove(f"log/{lf}")
logging.info(f"删除无内容的日志文件:{lf}")
continue
elif ct == 0:
break
else:
continue
ct = ct - 1
# 删除无意义的日志文件(小于等于 3 行)
ct = len(os.listdir("log"))
for lf in os.listdir("log"):
with open(f"log/{lf}", "r", encoding="utf-8") as f:
lines = f.readlines()
if len(lines) <= 3:
os.remove(f"log/{lf}")
logging.info(f"删除无意义的日志文件:{lf}")
continue
elif ct == 0:
break
else:
continue
LOGFORMAT = "[%(pathname)s | %(funcName)s | %(process)d | %(lineno)d] [%(levelname)s %(asctime)s] %(message)s"
DATEFORMAT = "%Y/%m/%d %H:%M:%S"
LOGFILEFORMAT = "%Y_%m_%d-%H-%M-%S"
# [C:/hello/main.py | getServerjar | 8] [WARNING 2023/07/24 11:11:11] text here...
LOGFILENAME = f"{time.strftime(LOGFILEFORMAT)}.log"
with open(file=f"log/{LOGFILENAME}", mode="x", encoding="utf-8") as f:
pass
logging.basicConfig(
level=logging.NOTSET,
format=LOGFORMAT,
datefmt=DATEFORMAT,
filename=f"log/{LOGFILENAME}",
filemode="a"
)
logging.info(f"当前系统:{SystemType}")
logging.info("日志模块 初始化完成")
# 初始化配置
config = configparser.ConfigParser()
if not os.path.exists("config.ini"):
config["Server"] = {
'ServerVersion': 'auto',
'type': 'vanilla',
'useRelease': True,
'dev': False,
'done': False
}
with open(file='config.ini', mode='w', encoding="utf-8") as f:
config.write(f)
CFGREADSOON = True
else:
# config read at soon
CFGREADSOON = True
# 读取版本信息
try:
os.remove("version_manifest_v2.json")
os.remove("version_manifest.json")
except FileNotFoundError:
logging.debug("未检测到 version_manifest.json 文件,继续运行")
else:
logging.info("检测到 version_manifest.json 文件,删除以避免重复")
logging.info("开始下载 version_manifest json")
wget.download('https://piston-meta.mojang.com/mc/game/version_manifest.json')
wget.download(
'https://piston-meta.mojang.com/mc/game/version_manifest_v2.json')
print("\n", end="")
with open("version_manifest_v2.json", "r", encoding="utf-8") as f:
content = json.load(f)
latest_release = content['latest']['release']
latest_snapshot = content['latest']['snapshot']
# "latest": {
# "release": "1.20.1",
# "snapshot": "1.20.1"
# },
# print(platform.uname())
x = len(content['versions'])
i = 0
v = []
vx = []
while i < len(content['versions']):
v.append(content['versions'][i])
vx.append(content['versions'][i]['id'])
i += 1
logging.debug(f"共有 {x} 个版本")
if CFGREADSOON:
with open(file='config.ini', mode="r", encoding="utf-8") as f:
config = configparser.ConfigParser()
config.read_file(f)
type = config["Server"]["type"]
use_release = config["Server"]["useRelease"]
if config["Server"]["ServerVersion"] == 'auto':
if use_release == 'True':
ServerVersion = latest_release
else:
ServerVersion = latest_snapshot
else:
ServerVersion = config["Server"]["ServerVersion"]
logging.info(f"已检测到所需版本 {ServerVersion}")
ic = 0
for nm in vx:
if nm == ServerVersion:
logging.debug(f"该版本存在")
ik = 0
break
elif ic == len(vx) - 1:
logging.warning(f"该版本不存在")
ik = 1
break
ic = ic + 1
if ik == 1:
logging.info(f"使用最新版本")
if use_release == 'True':
ServerVersion = latest_release
else:
ServerVersion = latest_snapshot
elif ik == 0:
logging.info("使用当前版本")
else:
logging.critical("发生未知错误")
raise Exception("发生未知错误: 变量 ik 非正常值")
logging.info(f"开始下载版本")
url = content['versions'][vx.index(ServerVersion)]['url']
try:
os.remove(f"{ServerVersion}.json")
except FileNotFoundError:
logging.debug(f"未检测到 {ServerVersion}.json 文件,继续运行")
else:
logging.info(f"检测到 {ServerVersion}.json 文件,删除以避免重复")
logging.info(f"开始下载版本信息文件 {ServerVersion}.json")
wget.download(url)
print("\n", end="")
with open(f"{ServerVersion}.json", "r", encoding="utf-8") as f:
content = json.load(f)
logging.info(f"版本信息文件 {ServerVersion}.json 下载完成")
logging.debug(f"开始读取")
DownloadSHA1 = content['downloads']['server']['sha1']
DownloadSize = content['downloads']['server']['size']
DownloadUrl = content['downloads']['server']['url']
try:
os.remove("server.jar")
except FileNotFoundError:
logging.debug("未检测到 server.jar 文件,继续运行")
else:
logging.info("检测到 server.jar 文件,删除以避免重复")
wget.download(DownloadUrl)
print("\n", end="")
def getSha1(filename): # 计算sha1
sha1Obj = hashlib.sha1()
with open(filename, 'rb') as f:
sha1Obj.update(f.read())
return sha1Obj.hexdigest()
sha1 = getSha1("server.jar")
if sha1 != DownloadSHA1:
logging.error(f"版本核心 {ServerVersion} 下载失败")
logging.warning(f"sha1 校验失败,重试...")
DownloadSHA1 = content['downloads']['server']['sha1']
DownloadSize = content['downloads']['server']['size']
DownloadUrl = content['downloads']['server']['url']
try:
os.remove("server.jar")
except FileNotFoundError:
logging.debug("未检测到 server.jar 文件,继续运行")
else:
logging.info("检测到 server.jar 文件,删除以避免重复")
wget.download(DownloadUrl)
print("\n", end="")
sha1 = getSha1("server.jar")
if sha1 != DownloadSHA1:
logging.critical(f"版本核心 {ServerVersion} 下载失败:sha1 校验失败")
raise Exception(f"版本核心 {ServerVersion} 下载失败:sha1 校验失败")
else:
logging.info(f"版本核心 {ServerVersion} 下载成功")
# 下载 Java
logging.info("检测用户是否安装了 Java")
# if SystemType == "Linux":
# # Linux
# # 例:1.19.2 -> 1192
# svl = ServerVersion.split(".")
# i = len(svl)
# k = ""
# for s in svl:
# if i > 0:
# k = k + s
# else:
# break
# i = i - 1
# ServerVersionLX = k
# os.system("chmod +x server.jar")
# os.system(f"mkdir ./server")
# os.system(f"mkdir ./server/{ServerVersionLX}")
# os.system(f"mv server.jar ./server/{ServerVersionLX}/server.jar")
# elif SystemType == "Windows":
# # Windows
# os.system("mkdir ./server")
# os.system(f"mkdir ./server/{ServerVersion}")