-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_dev.py
executable file
·409 lines (345 loc) · 12.2 KB
/
run_dev.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/env python3
import argparse
import os
import pathlib
import subprocess
parser = argparse.ArgumentParser()
parser.add_argument(
"--test",
action="store_true",
help="Runs the unit tests available in the library/test folder",
)
parser.add_argument(
"--virtual",
action="store_true",
help="Runs the library in a simulated environment",
)
parser.add_argument(
"-n",
type=int,
help="Number of nodes to run in the library in a simulated environment. Needs to be used in combination with --virtual",
)
parser.add_argument(
"-t",
type=float,
help="Duration of the simulation in the simulated environment in seconds. You can specify float values like 0.1 seconds. Needs to be used in combination with --virtual",
)
parser.add_argument(
"-l",
type=int,
help="Number of logs to append to the leader. Needs to be used in combination with --virtual",
)
parser.add_argument(
"-r",
action="store_true",
help="Randomizes the order of nodes while running the simulation. Needs to be used in combination with --virtual",
)
parser.add_argument(
"-k",
type=int,
help="Sets leader kill time. Needs to be used in combination with --virtual",
)
parser.add_argument(
"-p",
type=int,
help="The USB port to use, enter a number starting from 0. Needs to be used in combination with --pupload",
)
parser.add_argument(
"-e",
type=str,
help="The example to build, enter its name without the ino extension. For example 'basic'. Needs to be used in combination with --pupload",
)
parser.add_argument(
"--clean",
action="store_true",
help="Cleans the project folder by deleting the temporary files and other build artifacts",
)
parser.add_argument(
"--pbuild",
action="store_true",
help="Builds the 'basic.ino' example only to make sure that there are no compilation errors in the library",
)
parser.add_argument(
"--pupload",
action="store_true",
help="Builds and uploads the 'basic.ino' example to the board",
)
parser.add_argument(
"--pmonitor",
action="store_true",
help="Start a serial monitor",
)
parser.add_argument(
"--pum",
action="store_true",
help="Builds, uploads the 'basic.ino' example to the board, and starts a serial monitor",
)
parser.add_argument(
"--shell",
action="store_true",
help="Runs the Docker container in interactive mode and gives shell access",
)
parser.add_argument(
"--docs",
action="store_true",
help="Generates documentation from the doxygen comments",
)
parser.add_argument(
"--init",
action="store_true",
help="Initialize and setup the repository for development",
)
class RunDev:
def __init__(self, args):
self.project_path = pathlib.Path(__file__).parent.absolute()
self.submodules = ["ArduinoJson", "TaskScheduler"]
self.color = {
"HEADER": "\033[95m",
"OKBLUE": "\033[94m",
"OKCYAN": "\033[96m",
"OKGREEN": "\033[92m",
"WARNING": "\033[93m",
"FAIL": "\033[91m",
"ENDC": "\033[0m",
"BOLD": "\033[1m",
"UNDERLINE": "\033[4m",
}
if not args.init:
self.check_submodules()
if args.test:
self.module_test()
elif args.virtual:
if (args.t is None) or (args.n is None) or (args.l is None):
print("Please specify: ")
print("> Number of nodes with -n argument")
print("> Simulation time in seconds with -t argument")
print("> Leader kill time in seconds with -k argument")
print(
"> Number of logs to append to the leader with -l argument"
)
print("> To shuffle the node order with -r argument")
self.exit_code()
self.module_virtual(args.t, args.n, args.r, args.l, args.k)
elif args.shell:
self.module_shell()
elif args.clean:
self.module_clean()
elif args.docs:
self.module_docs()
elif args.pbuild:
# if args.e is None:
# print("Please specify which example to build with -e argument")
# self.exit_code()
self.module_build()
elif args.pupload:
if args.e is None:
print("Please specify which example to build with -e argument")
self.exit_code()
self.module_upload(example=args.e)
elif args.pmonitor:
if args.e is None:
print(
"Please specify which example is currently running with -e argument"
)
print("^ this is needed for stack tracing and error decoding")
self.exit_code()
self.module_monitor(example=args.e)
elif args.pum:
if args.e is None:
print("Please specify which example to build with -e argument")
self.exit_code()
self.module_upload(example=args.e)
self.module_monitor(example=args.e)
elif args.init:
self.module_init()
def module_monitor(self, example="basic", port=0):
self.chown_ttyUSBX_to_user(port)
self.run_command_in_docker(
f"cd library/examples/{example} && platformio device monitor --baud 115200 --filter esp8266_exception_decoder",
True,
)
def module_upload(self, example="basic", port=0):
self.chown_ttyUSBX_to_user(port)
print(
self.color["OKCYAN"]
+ ">> 'Uploading %s example'" % example
+ self.color["ENDC"]
)
self.run_command_in_docker(
f"cd library/examples/{example} && pio run --target upload", True
)
def module_build(self, example="basic"):
print(
self.color["OKCYAN"]
+ ">> 'Building %s example'" % example
+ self.color["ENDC"]
)
self.run_command_in_docker(
"platformio lib --global install painlessMesh 'Adafruit BusIO' 'Adafruit GFX Library' 'Adafruit SSD1306' PCA9635"
+ "&&"
+ "cd library"
+ "&&"
+ f'platformio ci --lib="." --board=nodemcuv2 examples/{example}/{example}.ino -O "build_flags = -Wall -Wextra -Wno-unused-parameter"'
)
def module_docs(self):
self.run_command_in_docker("cd docs && doxygen")
def module_clean(self):
os.system("cd library && make clean")
os.system(
"cd library"
+ "&&"
+ "find . -iwholename '*cmake*' -not -name CMakeLists.txt -delete"
)
os.system("rm -rf library/Makefile")
os.system("rm -rf library/compile_commands.json")
os.system("rm -rf library/bin")
os.system("rm -rf library/.pio")
os.system("rm -rf docs/html")
print(self.color["OKGREEN"] + "Cleaned!" + self.color["ENDC"])
def module_shell(self):
self.check_root_access()
os.system(
"docker run --user=1000 -it -v %s:/ramen -v %s:/.platformio ghcr.io/a5-015/ramen/ramen-dev"
% (self.project_path, os.path.join(self.project_path, ".cache"))
)
def module_virtual(self, t, n, r, l, k):
command = ""
if k is None:
k = 0
if r:
command = "./bin/virtual_esp -t %s -n %s -l %s -k %s -r" % (
t,
n,
l,
k,
)
else:
command = "./bin/virtual_esp -t %s -n %s -l %s -k %s " % (
t,
n,
l,
k,
)
self.run_command_in_docker(
"cd library"
+ "&&"
+ "cmake ."
+ "&&"
+ "make virtual_esp"
+ "&&"
+ "echo '\n\033[95m===============================================================================\nRunning the virtual network:\033[0m'"
+ "&&"
+ "echo '\033[95m>> The colorful outputs are outputted by the virtual network\033[0m'"
+ "&&"
+ "echo '\033[95m>> The white outputs are outputted by ramen\033[0m\n'"
+ "&&"
+ command
+ "&&"
# Delete gcov files related to the simulator, we don't want them
+ "gcovr --delete --root='.' --filter='virtual_esp.*' > /dev/null 2>&1"
)
def module_test(self):
self.run_command_in_docker(
"cd library"
+ "&&"
+ "cmake ."
+ "&&"
+ "make ramen_unit_tests"
+ "&&"
+ "echo '\n\033[96m===============================================================================\nRunning the tests:\033[0m\n'"
+ "&&"
+ "./bin/ramen_unit_tests"
+ "&&"
+ "gcovr --print-summary --root='.' --filter='/ramen/library/src/ramen/*' --exclude='virtual_esp.*'"
+ "&&"
+ "echo '\n\033[96mGenerating the HTML version of the coverage report under docs/coverage-report\033[0m'"
+ "&&"
+ "gcovr --delete --root='.' --filter='/ramen/library/src/ramen/*' --exclude='virtual_esp.*' --html --html-details -o ../docs/coverage-report/coverage.html"
)
def module_init(self):
self.check_root_access()
print(
self.color["OKGREEN"]
+ ">> Installing required system packages"
+ self.color["ENDC"]
)
os.system("apt install -y -qq docker clang clang-format clang-tidy")
print(
self.color["OKGREEN"]
+ ">> Installing required python packages"
+ self.color["ENDC"]
)
os.system("pip3 install -U platformio black pylint")
print(
self.color["OKGREEN"]
+ ">> Initialing the submodules"
+ self.color["ENDC"]
)
os.system("git submodule update --init --recursive")
print(
self.color["OKGREEN"]
+ ">> Checking out the correct version of the submodules"
+ self.color["ENDC"]
)
os.system(
"cd library/test/TaskScheduler"
+ "&&"
+ "git checkout b36cc818db89430b7564d1c56a668937f6dae6ec"
)
print(self.color["OKGREEN"] + ">> DONE <<" + self.color["ENDC"])
def run_command_in_docker(self, command, privileged=False):
"""
Runs the given command in the docker container
"""
if privileged:
privileged_option = "--privileged"
else:
privileged_option = ""
self.check_root_access()
os.system(
'docker run --user=1000 %s -t -v %s:/ramen -v %s:/.platformio ghcr.io/a5-015/ramen/ramen-dev /bin/bash -c "%s"'
% (
privileged_option,
self.project_path,
os.path.join(self.project_path, ".cache"),
command,
)
)
def check_root_access(self):
"""
Checks if the script has root privileges
"""
if os.geteuid() != 0:
print("Please run me with sudo or give me root access somehow")
self.exit_code()
def check_submodules(self):
"""
Checks if the submodules are initialized
"""
for module in self.submodules:
if (
os.listdir(
os.path.join(self.project_path, "library/test", module)
)
== []
):
print(
"Initialize submodules first with 'git submodule update --init --recursive'"
)
self.exit_code()
def chown_ttyUSBX_to_user(self, port="0"):
"""
Applies chown 1000:dialout to /dev/ttyUSB0 in order to upload code to the board
"""
self.check_root_access()
print(
self.color["OKCYAN"]
+ ">> 'sudo chown 1000:dialout /dev/ttyUSB%s'" % port
+ self.color["ENDC"]
)
os.system("chown 1000:dialout /dev/ttyUSB%s" % port)
def exit_code(self):
exit()
if __name__ == "__main__":
args = parser.parse_args()
run_dev = RunDev(args)