-
Notifications
You must be signed in to change notification settings - Fork 3
/
report.py
executable file
·89 lines (78 loc) · 2.37 KB
/
report.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
#!/usr/bin/env python3
import sys
import time
import threading
import webbrowser
from http.server import HTTPServer, SimpleHTTPRequestHandler
from functools import partial
import argparse
from pathlib import Path
import shutil
from jinja2 import Template
def start_server(args):
handler = partial(SimpleHTTPRequestHandler, directory=args.output)
httpd = HTTPServer((args.address, int(args.port)), handler)
httpd.serve_forever()
# Main logic
def main(args=None):
# create output folder if doesn't exist
Path(args.output).mkdir(parents=True, exist_ok=True)
# prepare needed files
# copy metrics.json to the appropriate folder
shutil.copy(args.input + "/metrics.json", args.output)
# modify report.htm.prototype template to generate appropriate html file
with open("./report.html.prototype") as f:
template = Template(f.read())
# fill template with the source of the metric data which
# will be the metrics.json file
# save the template as index.html to the appropriate reports folder
(template.stream(metric_source="metrics.json")
.dump(args.output + "/index.html"))
threading.Thread(target=start_server, args=(args,)).start()
webbrowser.open_new("http://" + args.address + ":" + args.port)
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
sys.exit(0)
# Parse arguments and call main
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate report")
parser.add_argument(
"-i",
"--input",
metavar="STRING",
help="Input folder",
required=False,
dest="input",
default="./data",
)
parser.add_argument(
"-o",
"--output",
metavar="STRING",
help="Output report folder",
required=False,
dest="output",
default="./report",
)
parser.add_argument(
"-a",
"--address",
metavar="STRING",
help="Address to bind and serve the report",
required=False,
dest="address",
default="localhost",
)
parser.add_argument(
"-p",
"--port",
metavar="STRING",
help="Port to bind and serve the report",
required=False,
dest="port",
default="8080",
)
# Parse the arguments
sys.exit(main(parser.parse_args()))