-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerInfoCreater.py
68 lines (65 loc) · 2.35 KB
/
ServerInfoCreater.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
#coding:utf-8
import os;
import os.path;
import hashlib;
import ConfigParser;
import json;
import sys;
import urllib;
#----------------------------↓↓↓↓↓↓↓↓↓↓↓↓↓ 通用方法 ↓↓↓↓↓↓↓↓↓↓↓↓↓----------------------------
# 计算文件md5
def get_file_md5(filename):
if not os.path.isfile(filename):
return
myhash = hashlib.md5()
f = open(filename,'rb')
while True:
b = f.read(8096)
if not b :
break
myhash.update(b)
f.close()
return myhash.hexdigest()
# 字符串MD5
def get_string_md5(inputString):
md5String = hashlib.md5()
md5String.update(inputString)
return md5String.hexdigest()
# 下载文件, 并保存
def download_file_save_to_path(urlString, savePath):
the_request = requests.get(urlString)
if the_request:
with open(savePath, "wb") as save_data:
save_data.write(the_request.content)
#----------------------------↑↑↑↑↑↑↑↑↑↑↑↑↑ 通用方法 ↑↑↑↑↑↑↑↑↑↑↑↑↑----------------------------
if __name__ == "__main__":
# 配置文件
_config_file_path = "./Server.cfg"
cfg = ConfigParser.ConfigParser()
cfg.read(_config_file_path)
# 读取配置
_server_mods_path = cfg.get('Server Info', 'server_mods');
_save_file = cfg.get('Server Info', 'save_file');
# 保存的内容
server_info_dic = {};
mod_list = [];
# 遍历mods文件
for parent,dirnames,filenames in os.walk(_server_mods_path):
for filename in filenames:
# print os.path.join(parent,filename) + "===" + get_string_md5(filename) + "===" + get_file_md5(os.path.join(parent,filename));
# jar文件才保存
if ".jar" in filename:
file_info_dict = {
"file_name" : os.path.join(parent,filename).replace(_server_mods_path,""),
"file_name_md5" : get_string_md5(filename),
"file_md5" : get_file_md5(os.path.join(parent,filename))
}
mod_list.append(file_info_dict)
# 整合json
server_info_dic = {
"mods_list" : mod_list,
"delete_mod" : []
};
# 保存到文件
with open(_save_file, 'w') as file:
file.write(json.dumps(server_info_dic, indent=2, ensure_ascii=False)) # 支持中文