-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
80 lines (61 loc) · 2.43 KB
/
build.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
import os
import json
import hashlib
import subprocess
parent_path = os.path.dirname(os.path.realpath(__file__))
def md5sum(full_path):
with open(full_path, "rb") as rf:
return hashlib.md5(rf.read()).hexdigest()
def get_or_create():
conf_path = os.path.join(parent_path, "config.json")
conf = {}
if not os.path.isfile(conf_path):
print("config.json not found, build.py is root path. auto write config.json")
module_name = os.path.basename(parent_path)
conf["module"] = module_name
conf["version"] = "0.1"
conf["home_url"] = "Module_%s.asp" % module_name
conf["title"] = "title of " + module_name
conf["description"] = "description of " + module_name
else:
with open(conf_path, "r", encoding="utf-8") as fc:
conf = json.loads(fc.read())
return conf
def pack_folder(module_name: str):
subprocess.run(["7z", "a", "-ttar", f"{module_name}.tar", module_name], check=True)
subprocess.run(
["7z", "a", "-tgzip", f"{module_name}.tar.gz", f"{module_name}.tar"],
check=True,
)
if os.path.exists(f"{module_name}.tar"):
os.remove(f"{module_name}.tar")
else:
print(f"Failed to create {module_name}.tar")
def build_module():
try:
conf = get_or_create()
except Exception as e:
print(f"config.json file format is incorrect: {e}")
if "module" not in conf:
print(" module is not in config.json")
return
module_path = os.path.join(parent_path, conf["module"])
if not os.path.isdir(module_path):
print("not found %s dir, check config.json is module ?" % module_path)
return
install_path = os.path.join(parent_path, conf["module"], "install.sh")
if not os.path.isfile(install_path):
print("not found %s file, check install.sh file")
return
print("build...")
open(parent_path + "/" + conf["module"] + "/" + "version", "w").write(
conf["version"]
)
pack_folder(conf["module"])
conf["md5"] = md5sum(os.path.join(parent_path, conf["module"] + ".tar.gz"))
conf_path = os.path.join(parent_path, "config.json")
with open(conf_path, "w", encoding="utf-8") as fw:
json.dump(conf, fw, sort_keys=True, indent=4, ensure_ascii=False)
print("build done", conf["module"] + ".tar.gz")
if __name__ == "__main__":
build_module()