-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprofile.py
122 lines (98 loc) · 3.57 KB
/
profile.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
import os
import sys
import string
import datetime
import pytz
import socket
import json
import platform
import subprocess
import hashlib
import base64
PYTHON_VERSION = sys.version_info[0]
base_path = os.path.abspath(os.path.dirname(__file__))
base_dist_path = os.path.join(base_path, "dist")
def generate_app_profile():
profile_template_file = os.path.join(base_path, "src/apps/profile-template.js")
package_json_file = os.path.join(base_path, "package.json")
with open(package_json_file) as f:
package = json.loads(f.read())
with open(profile_template_file) as f:
profile_template = string.Template(f.read())
app_name = package["config"]["app"]
profile_name = os.path.join(base_path, "src/apps", "{}-profile.js".format(app_name))
now = datetime.datetime.now(pytz.timezone("Australia/Perth"))
package.update(package["config"])
package["distributionType"] = sys.argv[1] if len(sys.argv) >= 2 else "unknown"
vendor_file = os.path.join(base_dist_path, package["distributionType"], "vendor.js")
if not os.path.exists(vendor_file):
raise Exception("Vendor file({}) is missing".format(vendor_file))
m = hashlib.md5()
with open(vendor_file, "rb") as f:
m.update(f.read())
vendor_md5 = base64.urlsafe_b64encode(m.digest())
# Python 2/3 compatibility:
if PYTHON_VERSION == 3:
vendor_md5 = vendor_md5.rstrip(b"=")
else:
vendor_md5 = vendor_md5.rstrip("=")
package.update(
{
"build_datetime": now.strftime("%Y-%m-%d %H:%M:%S %Z(%z)"),
"build_date": now.strftime("%Y-%m-%d %Z(%z)"),
"build_time": now.strftime("%H-%M-%S %Z(%z)"),
"build_platform": platform.system(),
"build_host": socket.gethostname(),
"vendor_md5": vendor_md5,
}
)
# get the latest git commit.
latest_commit = subprocess.check_output(["git", "log", "-n", "1"]).splitlines()
commit_info = {}
for line in latest_commit:
for k, v in [
(b"commit", "commit"),
(b"Author:", "commit_author"),
(b"Merge:", ""),
(b"Date:", "commit_date"),
(b"", "commit_message"),
]:
if line[: len(k)] == k:
if v and line[len(k) :].strip():
if v in commit_info:
commit_info[v] = "{}\\n{}".format(
commit_info[v], line[len(k) :].strip().decode("utf-8")
)
else:
commit_info[v] = line[len(k) :].strip().decode("utf-8")
break
if "commit" in commit_info:
commit_info["commit"] = commit_info["commit"][:7]
package.update(commit_info)
# get the branch info
branch = (
[
b
for b in subprocess.check_output(["git", "branch"]).splitlines()
if b.strip().startswith(b"*")
][0]
.strip()[1:]
.strip()
.decode("utf-8")
)
package["repository_branch"] = branch
# tranform value to json string
for k, v in package.items():
# Python 2/3 compatibility: forcibly convert byte characters back to a string.
if isinstance(v, bytes):
v = "".join(chr(i) for i in v)
package[k] = json.dumps(v)
for key, val in package.items():
print("{}: {}".format(key, val))
profile = profile_template.safe_substitute(package)
print("\nPROFILE:")
print(profile)
with open(profile_name, "w") as f:
f.write(profile)
if __name__ == "__main__":
generate_app_profile()