-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsil_run.py
66 lines (52 loc) · 1.9 KB
/
sil_run.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
import argparse
import os
import signal
import subprocess
import time
DEFAULT_RUNTIME = 5 * 60 # 5 minutes
DEFAULT_OUTFILE = "sil_logs.log"
# KEYWORD SEARCHES:
# List of all keywords to probe the log for
# Enter as a dictionary of KEYWORD - COLOR combos
KEYWORDS = {"WARNING": "\033[93m", "ERROR": "\033[91m"}
def FSW_simulate(runtime: float, outfile: str) -> None:
try:
with open(outfile, "w") as log_file:
process = subprocess.Popen(["./run.sh", "simulate"], stdout=log_file, stderr=log_file, preexec_fn=os.setsid)
print(f"Running simulation for {runtime} seconds, output written to {outfile}")
time.sleep(runtime)
print("Terminating...")
os.killpg(os.getpgid(process.pid), signal.SIGKILL)
except Exception as e:
print(f"Error: {e}")
def parse_FSW_logs(outfile):
errors_detected = False
with open(outfile, "r") as log_file:
for line in log_file:
for keyword in KEYWORDS.keys():
if keyword in line:
print(f"{KEYWORDS[keyword]}{line}")
if keyword == "ERROR":
errors_detected = True
if errors_detected:
raise Exception("FSW Simulation Failed")
if __name__ == "__main__":
# Define Parser
parser = argparse.ArgumentParser(prog="SIL_tester")
# Add arguments
parser.add_argument(
"--duration",
default=DEFAULT_RUNTIME,
help=f"Duration (in seconds) to simulate FSW for [float, default: {DEFAULT_RUNTIME}s]",
)
parser.add_argument(
"--outfile",
default=DEFAULT_OUTFILE,
help=f"Log file to save FSW logs to [string, default: {DEFAULT_OUTFILE}] \n NOTE: Enter filename with extension",
)
# Parse Arguments
args = parser.parse_args()
# Run script
FSW_simulate(int(args.duration), args.outfile)
# Parse Logs
parse_FSW_logs(args.outfile)