-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev_tools.py
72 lines (63 loc) · 2.68 KB
/
dev_tools.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
import sys
import re
import getopt
TEMPLATE_FILE_PATHS = [ './site/webApp/view/partial/FooterPartialTemplate.html' ]
BUILD_FILE_PATHS = [ './site/webApp/view/partial/FooterPartial.html' ]
def githubSideRendering(argv):
# constants
ejs_pattern = "(<%=\s([a-zA-Z\-\_]+)\s%>)"
if len(TEMPLATE_FILE_PATHS) != len(BUILD_FILE_PATHS):
raise Exception("Invalid paths")
# terminal argument handling
arg_help = "{0} -h <help> <hash> <commit-msg> <commit-date>".format(argv[0])
if (len(argv) == 1):
sys.exit("No arguments passed to script")
commit_hash = ""
commit_msg = ""
commit_date = ""
try:
opts, args = getopt.getopt(argv[1:], "h", ["help", "hash=", "commit-msg=", "commit-date="])
if (len(args) != 0):
sys.exit(f"Unknown arguments passed {args}")
except getopt.GetoptError as err:
sys.exit(f"Unknown command try: --help")
for opt, arg in opts:
if opt in ("-h", "--help"):
print(arg_help)
return
elif opt in ("--hash"):
commit_hash = arg
elif opt in ("--commit-msg"):
commit_msg = arg
elif opt in ("--commit-date"):
commit_date = arg
# function body
for i in range(len(TEMPLATE_FILE_PATHS)):
TEMPLATE_FILE_PATH = TEMPLATE_FILE_PATHS[i]
BUILD_FILE_PATH = BUILD_FILE_PATHS[i]
template_file_content = ""
with open(TEMPLATE_FILE_PATH) as f:
template_file_content = f.read()
print("🧊🧊🧊")
print("input string:\n", template_file_content)
print("🧊🧊🧊\n")
matches = re.findall(ejs_pattern, template_file_content)
if (len(matches) == 0):
print(f"No matches found in {TEMPLATE_FILE_PATH}")
for x in matches:
key_in_templating_syntax = x[0] # Example value: <%= latest-version %>
key = x[1] # Example value: latest-version
if (not key):
raise SystemError("Match pattern does not have 2 groups: ", x)
if (key == "commit-hash"):
print(f"replacing: {key} with '{commit_hash}'")
template_file_content = template_file_content.replace(key_in_templating_syntax, commit_hash)
if (key == "commit-date"):
print(f"replacing: {key} with '{commit_date}'")
template_file_content = template_file_content.replace(key_in_templating_syntax, commit_date)
print("🔥🔥🔥")
print("output string:\n", template_file_content)
print("🔥🔥🔥")
with open(BUILD_FILE_PATH, 'w') as f:
f.write(template_file_content)
githubSideRendering(sys.argv)