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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
#!/usr/bin/env python3
import os
import os.path
import re
from sys import argv
required_config_keys = set(["CRUPEST_DOMAIN", "CRUPEST_UID",
"CRUPEST_GID", "CRUPEST_HALO_DB_PASSWORD"])
print("It's happy to see you!\n")
# get script dir in relative path
script_dir = os.path.dirname(__file__)
template_dir = script_dir
project_dir = os.path.normpath(os.path.join(script_dir, "../"))
print("First let's check all the templates...")
# get all filenames ending with .template
filenames = [os.path.basename(f)[:-len('.template')] for f in os.listdir(
template_dir) if f.endswith(".template")]
print("I have found following template files:")
for filename in filenames:
print(filename)
print("")
# if command is 'clean'
if len(argv) > 1 and argv[1] == "clean":
print("Are you sure you want to delete all generated files? (y/N)")
if input() == "y":
print("Deleting all generated files...")
for filename in filenames:
os.remove(os.path.join(project_dir, filename))
print("Your workspace is clean now! However config file is still there! See you!")
exit(0)
sub_regex = re.compile(r"\{\{\s*([a-zA-Z0-9_]+?)\s*\}\}")
var_set = set()
for template in os.listdir(template_dir):
if not template.endswith(".template"):
continue
with open(os.path.join(template_dir, template), "r") as f:
content = f.read()
match_list = sub_regex.finditer(content)
for match in match_list:
var_set.add(match.group(1))
print("I have found following variables needed in templates:")
for var in var_set:
print(var, end=" ")
print("")
# check vars
if not var_set == required_config_keys:
print("The variables needed in templates are not same to the explicitly declared ones! There must be something wrong.")
print("The explicitly declared ones are:")
for var in required_config_keys:
print(var, end=" ")
print("Try to check template files and edit the var list at the head of this script. Aborted! See you next time!")
exit(1)
print("Now let's check if they are already generated...")
conflict = False
# check if there exists any generated files
for filename in filenames:
if os.path.exists(os.path.join(project_dir, filename)):
print(f"Found {filename}")
conflict = True
if conflict:
print("It seems there are some files already generated. Do you want to overwrite them? (y/N)")
if input() != "y":
print("Great! Check the existing files and see you next time!")
exit()
else:
print("No conflict found. Let's go on!\n")
print("Check for existing config file...")
config_path = os.path.join(project_dir, "data/config")
# check if there exists a config file
if not os.path.exists(config_path):
print("No existing config file found. Don't worry. Let's create one! Just tell me your domain name:")
domain = input()
my_uid = os.getuid()
my_gid = os.getgid()
halo_db_password = os.urandom(8).hex()
config_content = f"CRUPEST_DOMAIN={domain}\nCRUPEST_UID={my_uid}\nCRUPEST_GID={my_gid}\nCRUPEST_HALO_DB_PASSWORD={halo_db_password}\n"
# create data dir if not exist
if not os.path.exists(os.path.join(project_dir, "data")):
os.mkdir(os.path.join(project_dir, "data"))
# write config file
with open(config_path, "w") as f:
f.write(config_content)
print(
f"Everything else is auto generated. The config file is written into {config_path}. You had better keep it well. And here is the content:")
print(config_content)
print("If you think it's not ok, you can stop here and edit it. Or let's go on? (Y/n)")
if input() == "n":
print("Great! Check the config file and see you next time!")
exit()
else:
print("Looks like you have already had a config file. Let's check the content:")
with open(config_path, "r") as f:
print(f.read())
print("Is it good enough? (Y/n)")
if input() == "n":
print("Great! Check the config file and see you next time!")
exit()
# Parse config file
with open(config_path, "r") as f:
config = {}
# read line with line number
for line_number, line in enumerate(f.readlines()):
# check if it's a comment
if line.startswith("#"):
continue
# check if there is a '='
if line.find("=") == -1:
print(
f"Invalid config file. Please check line {line_number + 1}. There is even no '='! Aborted!")
# split at first '='
key, value = line.split("=", 1)
key = key.strip()
value = value.strip()
config[key] = value
# check if all required keys are in config
for key in required_config_keys:
if key not in config:
print(
f"Invalid config file. Please check if {key} is in the config file. Aborted!")
exit()
print("Finally, everything is ready. Let's generate the files:")
# generate files
for filename in filenames:
print(f"Generating {filename}...")
with open(os.path.join(template_dir, filename + ".template"), "r") as f:
content = f.read()
content = sub_regex.sub(lambda m: config[m.group(1)], content)
with open(os.path.join(project_dir, filename), "w") as f:
f.write(content)
print("\n🍻All done! See you next time!")
|