forked from KTManager/Legacy-KTRules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
105 lines (81 loc) · 3.29 KB
/
build.py
File metadata and controls
105 lines (81 loc) · 3.29 KB
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
#!/usr/bin/env python3
import glob
import hashlib
import os
import shutil
import sys
import _jsonnet
def main():
# for simplicity, just strip all the '-'s for easy arg checking
args = {arg.replace("-", "") for arg in sys.argv[1:]}
# print usage on help or if unrecognized args show up
nonsense = args - {"release", "clean", "verbose", "help"}
if nonsense or "help" in args:
print(f"Usage: {sys.argv[0]} [--release] [--clean] [--verbose] [--help]\n")
print(" --release drops the '-dev' from the version number")
print(" --clean removes the out dir before building")
print(" --verbose is noisy")
print(" --help shows this page")
print("\nAlso, you can drop the '--'s on all of these, they're optional")
if nonsense:
print(f"\nUnrecognized option: {nonsense}")
sys.exit(1)
return
# cd to the script directory
os.chdir(os.path.dirname(os.path.abspath(__file__)))
# remove the out directory for a clean build
if "clean" in args and os.path.exists("out"):
if "verbose" in args:
print("removing out directory")
shutil.rmtree("out")
hashes = {}
# get all the json files and convert them
for file in glob.iglob("src/**/*.json*", recursive=True):
# jsonnet files get compiled, json gets copied
compiling = file.endswith(".jsonnet")
# out structure should match src structure, .jsonnet => .json
out_file = file.replace("src", "out", 1)
if compiling:
out_file = out_file[0:-3] # strip 'net' from jsonnet
# logging
if "verbose" in args:
print(f"{'compiling' if compiling else 'copying'} {file} to {out_file}")
# read in the input
with open(file, "r") as i:
input_content = i.read()
# hash the input
hashes[file] = hashlib.sha256(input_content.encode('utf-8')).hexdigest()
# generate the output
if compiling:
json_str = _jsonnet.evaluate_snippet(file, input_content)
else:
json_str = input_content
# write out
os.makedirs(os.path.dirname(out_file), exist_ok=True)
with open(out_file, "w") as o:
o.write(json_str)
# hash the hashes for version number
version_hash = hashlib.sha256()
for file, digest in hashes.items():
summary = file + " " + digest + "\n"
if "verbose" in args:
print(summary)
version_hash.update(summary.encode('utf-8'))
version_hash_digest = version_hash.hexdigest()
# write out version number
with open("src/version.txt", "r") as i:
with open("out/version.txt", "w") as o:
# read in
version = i.read().strip()
# add the version hash
version += "-" + version_hash_digest
# non-release builds get 'dev' so that KT Manager always picks them up as new
if "release" not in args:
version += "-dev"
# logging
if "verbose" in args:
print(f"adding version file: {version}")
# write out
o.write(version)
if __name__ == "__main__":
main()