-
Notifications
You must be signed in to change notification settings - Fork 31
/
cli.py
148 lines (113 loc) · 3.86 KB
/
cli.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
from collections import OrderedDict
from google.cloud import storage
from hashlib import sha256
from staticjinja import make_site
import click
import json
import os
import sys
@click.group()
def cli():
pass
def die(msg):
print(msg)
sys.exit(1)
def get_version(impl_name):
fname = os.path.join('src', impl_name, 'version')
if not os.path.exists(fname):
die("Could not find version of implementation {}".format(impl_name))
return open(fname).read().strip()
@click.command()
def postprocess():
if not os.path.exists('report.json'):
die("No report found to process")
report = json.load(open('report.json'))['report']
impls = ['eclair', 'lightning', 'lnd', 'ptarmigan']
report['versions'] = OrderedDict(sorted({i: get_version(i) for i in impls}.items()))
# Any unique random id would do really
version_string = "_".join([k + "-" + v for k, v in report['versions'].items()])
report['id'] = sha256(version_string.encode('ASCII')).hexdigest()
with open(os.path.join('reports', report['id'] + ".json"), "w") as f:
f.write(json.dumps(report))
def group_tests(report):
tests = report['tests']
report['tests'] = {}
for t in tests:
# Strip filename
splits = t['name'][9:].split('[')
name = splits[0]
config = splits[1][:-1]
t['name'] = config
del t['setup']
del t['teardown']
if name not in report['tests']:
report['tests'][name] = {'subtests': [], 'total': 0, 'success': 0}
report['tests'][name]['subtests'].append(t)
report['tests'][name]['total'] += 1
if t['outcome'] == 'passed':
report['tests'][name]['success'] += 1
report['tests'][name]['subtests'] = sorted(report['tests'][name]['subtests'], key=lambda x: x['name'])
return report
def ratio_to_color(ratio):
if ratio > 0.95:
return 'success'
elif ratio > 0.5:
return 'warning'
return 'danger'
def load_reports(template):
reports = []
for fname in os.listdir("reports"):
with open(os.path.join("reports", fname), 'r') as f:
report = json.loads(f.read())
ratio = report['summary']['passed'] / report['summary']['num_tests']
report['summary']['color'] = ratio_to_color(ratio)
reports.append(group_tests(report))
reports = sorted(reports, key=lambda x: x['created_at'])[::-1]
return {'reports': reports}
def load_report(template):
with open(template.filename, 'r') as f:
report = json.loads(f.read())
return group_tests(report)
def render_report(env, template, **report):
report_template = env.get_template("_report.html")
out = "%s/%s.html" % (env.outpath, report['id'])
for k, v in report['tests'].items():
ratio = v['success'] / v['total']
report['tests'][k]['color'] = ratio_to_color(ratio)
report_template.stream(**report).dump(out)
@click.command()
def html():
global entries
s = make_site(
contexts=[
('index.html', load_reports),
('.*.json', load_report),
],
rules=[
('.*.json', render_report),
],
outpath='output',
staticpaths=('static/',)
)
s.render()
def _get_storage_client():
return storage.Client(project=os.getenv("GCP_PROJECT"))
@click.command()
@click.argument('report_id')
def upload(report_id):
filename = report_id + '.json'
client = _get_storage_client()
bucket = client.bucket(os.getenv('GCP_STORAGE_BUCKET'))
blob = bucket.blob(filename)
with open(os.path.join("reports", filename)) as f:
contents = f.read()
blob.upload_from_string(
contents,
content_type='application/json')
url = blob.public_url
return url
if __name__ == '__main__':
cli.add_command(html)
cli.add_command(postprocess)
cli.add_command(upload)
cli()