-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommander.py
executable file
·305 lines (233 loc) · 8.38 KB
/
commander.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env python3
'''
This script is a do-it-all script for building and installing Nibi and its modules.
It also runs tests for Nibi, its modules, and it can run the benchmark / performance tests.
'''
import os
import shutil
import subprocess
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-n", "--install_nibi", action="store_true", help="Builds and installs Nibi library and application")
parser.add_argument("-m", "--install_modules", action="store_true", help="Builds and installs Nibi modules")
parser.add_argument("-d", "--debug", action="store_true", help="Builds Nibi and modules in debug mode")
parser.add_argument("-r", "--reldebug", action="store_true", help="Builds Nibi and modules in relase mode with debug symbols")
parser.add_argument("-t", "--test", action="store_true", help="Runs all tests for Nibi")
parser.add_argument("-v", "--valgrind", action="store_true", help="Runs valgrind checks")
parser.add_argument("-p", "--perf", action="store_true", help="Runs performance tests for Nibi")
parser.add_argument("-c", "--check_modules", action="store_true", help="Details all modules installed, and runs their tests")
parser.add_argument("-a", "--all_the_things", action="store_true", help="Installs Nibi, modules, runs tests, and runs performance tests")
parser.add_argument("-s", "--scrub", action="store_true", help="Scrub the system of all Nibi and modules and build files")
parser.add_argument('--smod', help="Install a specfic module")
args = parser.parse_args()
if not any(vars(args).values()):
print("No arguments provided.\n")
parser.print_help()
exit(0)
cwd = os.getcwd()
build_type = "-DCMAKE_BUILD_TYPE=Release"
if args.debug:
build_type = "-DCMAKE_BUILD_TYPE=Debug"
elif args.reldebug:
build_type = "-DCMAKE_BUILD_TYPE=RelWithDebInfo"
NIBI_PATH = os.environ.get('NIBI_PATH')
def scrub():
os.chdir("./nibi")
if os.path.exists("./build"):
shutil.rmtree("./build")
if NIBI_PATH is not None:
target_dest = NIBI_PATH + "/modules"
if os.path.exists(target_dest):
shutil.rmtree(target_dest)
os.chdir(cwd)
if args.scrub:
scrub()
if NIBI_PATH is None:
print("Please set NIBI_PATH to the path where you want to install NIBI")
exit(1)
print("NIBI_PATH is set to: " + NIBI_PATH)
if not os.path.exists(NIBI_PATH):
print("NIBI_PATH does not exist! Please create it and try again.")
exit(1)
def program_exists(cmd):
result = subprocess.run(["which", cmd], stdout=subprocess.PIPE)
return result.returncode == 0
def ensure_nibi_installed():
if not program_exists("nibi"):
print("Nibi is not installed yet. Please install it with -n and try again.")
exit(1)
def execute_command(cmd, show_output=False):
result = subprocess.run(cmd, stdout=subprocess.PIPE)
if result.returncode != 0:
print("\nCommand failed: " + str(cmd) + ". Output:\n ", result.stdout)
exit(1)
if show_output:
print("\nExecuted: " + str(cmd) + ". Output:\n " + result.stdout.decode("utf-8"))
value = ""
try:
value = result.stdout.decode("utf-8")
except Exception as e:
print(e)
print(">>> ", result.stdout)
exit(1)
return value
def install_std():
target_dest = NIBI_PATH + "/std"
if os.path.exists(target_dest):
print("Updating existing std library")
shutil.rmtree(target_dest)
# copy std dir to target_dest
shutil.copytree("./std", target_dest)
print("Adding config.nibi")
target_dest = NIBI_PATH + "/config.nibi"
if os.path.exists(target_dest):
print("Updating existing config.nibi")
os.remove(target_dest)
shutil.copyfile("./config.nibi", target_dest)
def build_and_install_nibi():
print("Building and installing Nibi library and application")
os.chdir("./nibi")
if not os.path.exists("./build"):
os.mkdir("./build")
os.chdir("./build")
execute_command(["cmake", "..", build_type])
execute_command(["make", "-j4"])
execute_command(["sudo", "make", "install"])
print("SUCCESS")
os.chdir("../")
print("Installing std library")
install_std()
os.chdir(cwd)
def build_current_module(module):
library_name = module + ".lib"
print("Building library: " + library_name)
if os.path.exists("./" + library_name):
os.remove("./" + library_name)
if not os.path.exists("./build"):
os.mkdir("./build")
os.chdir("./build")
execute_command(["cmake", "..", build_type])
execute_command(["make", "-j4"])
if not os.path.exists(module + ".lib"):
print("No library found for module: " + module)
exit(1)
shutil.copyfile(library_name, "../" + library_name)
os.chdir("../")
shutil.rmtree("./build")
def install_module(module):
module_dir = os.getcwd()
os.chdir(module)
target_dest = NIBI_PATH + "/modules/" + module
if os.path.exists(target_dest):
print("Updating existing module: " + module)
shutil.rmtree(target_dest)
if not os.path.exists(NIBI_PATH + "/modules"):
os.mkdir(NIBI_PATH + "/modules")
if not os.path.exists(target_dest):
os.mkdir(target_dest)
print("Installing module to : " + target_dest)
if os.path.exists("CMakeLists.txt"):
print("Found CMakeLists.txt in module: " + module)
build_current_module(module)
files = os.listdir(".")
for file in files:
if file.endswith(".nibi") or file.endswith(".lib"):
print("Copying file: " + file)
shutil.copyfile(file, target_dest + "/" + file)
if os.path.isdir(file) and file == "tests":
print("Copying tests: " + file)
shutil.copytree(file, target_dest + "/" + file)
os.chdir(module_dir)
def build_and_install_modules():
os.chdir("./nibi/modules")
for module in os.listdir("."):
print("Building and installing module: " + module)
install_module(module)
print("\n")
os.chdir(cwd)
def build_and_install_specific_module(module_name):
os.chdir("./nibi/modules")
if not os.path.isdir(module_name):
print("Given item '", module_name, "' is not a builtin module. Modules are:")
for x in os.listdir("."):
print("\t", x)
os.chdir(cwd)
exit(1)
install_module(module_name)
os.chdir(cwd)
def setup_tests():
# One of the tests requires a module to be built, but not installed
os.chdir("./tests/test_scripts/tests/module")
build_current_module("module")
os.chdir(cwd)
os.chdir("./tests/test_scripts/tests/ffi")
build_current_module("module")
os.chdir(cwd)
def run_tests():
os.chdir("./tests/test_scripts")
print("Running tests...")
execute_command(["python3", "run.py", "nibi"], True)
print("SUCCESS")
os.chdir(cwd)
def run_perfs():
os.chdir("./tests/test_perfs")
print("Running performance tests... this may take a couple of minutes...")
result = execute_command(["python3", "run.py", "nibi"])
print(result)
print("SUCCESS")
os.chdir(cwd)
def run_valgrind():
if not program_exists("valgrind"):
print("valgrind is not installed. Please install it and try again.")
exit(1)
os.chdir("./tests/test_valgrind")
print("Running valgrind tests... this may take a couple of minutes...")
result = execute_command(["python3", "run.py", "nibi"])
print(result)
print("SUCCESS")
os.chdir(cwd)
def check_modules():
os.chdir(NIBI_PATH)
if not os.path.exists("./modules"):
print("No modules to check. Exiting.")
exit(0)
os.chdir("./modules")
modules = os.listdir("./")
print("\nFound ", len(modules), " installed modules.\n")
for module in os.listdir("./"):
print("Checking module: " + module)
print(execute_command(["nibi", "-m", module]))
print("\n<MODULE TESTS>\n")
print(execute_command(["nibi", "-t", module]))
print("\n------------\n")
os.chdir(cwd)
if not program_exists("make"):
print("make is not installed. Please install it and try again.")
exit(1)
if not program_exists("cmake"):
print("CMake is not installed. Please install it and try again.")
exit(1)
if args.install_nibi or args.all_the_things:
build_and_install_nibi()
if args.smod is not None:
build_and_install_specific_module(args.smod)
if args.install_modules or args.all_the_things:
if not os.path.exists("./nibi/modules"):
print("No modules found. Exiting.")
exit(0)
print("\n")
build_and_install_modules()
if args.test or args.all_the_things:
ensure_nibi_installed()
# Ensure tests are setup
setup_tests()
# Run tests
run_tests()
if args.valgrind or args.all_the_things:
run_valgrind()
if args.check_modules or args.all_the_things:
ensure_nibi_installed()
check_modules()
if args.perf or args.all_the_things:
ensure_nibi_installed()
run_perfs()