-
Notifications
You must be signed in to change notification settings - Fork 7
/
render.py
490 lines (401 loc) · 14.4 KB
/
render.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
"""
Rendering utilities for locust parse results.
"""
import argparse
import copy
import json
import os
import sys
import textwrap
from typing import Any, Callable, Dict, Iterable, List, Optional, Set, Tuple, Union
from google.protobuf.json_format import Parse
import lxml
from lxml.html import builder as E
import yaml
from . import parse
from .render_pb2 import IndexKey, NestedChange
SerializedIndexKey = Tuple[str, Optional[str], str, int]
def serialize_index_key(index_key: IndexKey) -> SerializedIndexKey:
return (index_key.filepath, index_key.revision, index_key.name, index_key.line)
def deserialize_index_key(serialized_index_key: SerializedIndexKey) -> IndexKey:
filepath, maybe_revision, name, line = serialized_index_key
return IndexKey(filepath=filepath, revision=maybe_revision, name=name, line=line)
def get_key(change: parse.LocustChange) -> IndexKey:
return IndexKey(
filepath=change.filepath,
revision=change.revision,
name=change.name,
line=change.line,
)
def parent_key(change: parse.LocustChange) -> Optional[IndexKey]:
if change.parent is None:
return None
return IndexKey(
filepath=change.filepath,
revision=change.revision,
name=change.parent.name,
line=change.parent.line,
)
def nest_results(
changes: Iterable[parse.LocustChange],
) -> Dict[str, List[NestedChange]]:
results: Dict[str, List[NestedChange]] = {}
index: Dict[SerializedIndexKey, parse.LocustChange] = {
serialize_index_key(get_key(change)): change for change in changes
}
children: Dict[SerializedIndexKey, List[SerializedIndexKey]] = {
key: [] for key in index
}
for change in changes:
change_parent = parent_key(change)
if change_parent and change_parent.name:
children[serialize_index_key(change_parent)].append(
serialize_index_key(get_key(change))
)
nested_results: Dict[str, List[NestedChange]] = {}
keys_to_process = sorted(
[k for k in index], key=lambda k: len(children[k]), reverse=True
)
visited_keys: Set[SerializedIndexKey] = set()
def process_change(
change_key: SerializedIndexKey, visited: Set[SerializedIndexKey]
) -> NestedChange:
visited.add(change_key)
if not children[change_key]:
return NestedChange(
key=deserialize_index_key(change_key),
change=index[change_key],
children=[],
)
change_children = [
process_change(child_key, visited) for child_key in children[change_key]
]
return NestedChange(
key=deserialize_index_key(change_key),
change=index[change_key],
children=change_children,
)
for key in keys_to_process:
if key in visited_keys:
continue
change = index[key]
if change.filepath not in results:
results[change.filepath] = []
nested_change = process_change(key, visited_keys)
results[change.filepath].append(nested_change)
return results
def nested_change_to_dict(nested_change: NestedChange) -> Dict[str, Any]:
result = {
"name": nested_change.change.name,
"type": nested_change.change.change_type,
"line": nested_change.change.line,
"changed_lines": nested_change.change.changed_lines,
"total_lines": nested_change.change.total_lines,
}
children_list: List[Dict[str, Any]] = []
if nested_change.children:
children_list = [
nested_change_to_dict(child) for child in nested_change.children
]
result["children"] = children_list
return result
def results_dict(raw_results: Dict[str, List[NestedChange]]) -> Dict[str, Any]:
results = {
"locust": [
{
"file": filepath,
"changes": [
nested_change_to_dict(nested_change)
for nested_change in nested_changes
],
}
for filepath, nested_changes in raw_results.items()
]
}
return results
def render_json(results: Dict[str, Any]) -> str:
return json.dumps(results)
def render_yaml(results: Dict[str, Any]) -> str:
return yaml.dump(results, sort_keys=False)
def change_representation_full(
change: Dict[str, Any], link: str, filepath: str, current_depth: int, max_depth: int
) -> Optional[Any]:
"""
Generator of uncompressed html markdown.
"""
change_elements: List[Any] = [
E.B("Name: "),
E.A(change["name"], href=link),
E.BR(),
E.B("Type: "),
E.SPAN(change["type"]),
E.BR(),
E.B("Changed lines: "),
E.SPAN(str(change["changed_lines"])),
]
if change["total_lines"]:
change_elements.extend(
[E.BR(), E.B("Total lines: "), E.SPAN(str(change["total_lines"]))]
)
if change["children"]:
change_elements.extend([E.BR(), E.B("Changes:")])
child_elements = []
for child in change["children"]:
child_element = render_change_as_html(
child, filepath, current_depth + 1, max_depth
)
if child_element is not None:
child_elements.append(child_element)
change_elements.append(E.UL(*child_elements))
return change_elements
def change_representation_compressed(
change: Dict[str, Any], link: str, filepath: str, current_depth: int, max_depth: int
) -> Optional[Any]:
"""
Generator of compressed html markdown.
"""
change_elements: List[Any] = [
E.B(change["type"]),
E.SPAN(" "),
E.A(change["name"], href=link),
E.B(" changed lines: "),
E.SPAN(str(change["changed_lines"])),
]
if change["total_lines"]:
change_elements.extend([E.SPAN("/"), E.SPAN(str(change["total_lines"]))])
if change["children"]:
change_elements.extend([E.BR()])
child_elements = []
for child in change["children"]:
child_element = render_change_as_html(
child, filepath, current_depth + 1, max_depth, True
)
if child_element is not None:
child_elements.append(child_element)
change_elements.append(E.UL(*child_elements))
return change_elements
def render_change_as_html(
change: Dict[str, Any],
filepath: str,
current_depth: int,
max_depth: int,
compressed: Optional[bool] = False,
) -> Optional[Any]:
"""
Returns nested part of report in compressed or uncompressed format.
"""
if current_depth >= max_depth:
return None
link = change.get("link")
if link is None:
link = filepath
if compressed:
change_elements = change_representation_compressed(
change, link, filepath, current_depth, max_depth
)
else:
change_elements = change_representation_full(
change, link, filepath, current_depth, max_depth
)
return E.LI(*change_elements)
def html_file_section_handler_vanilla(item: Dict[str, Any]) -> Any:
filepath = item["file"]
file_url = item.get("file_url", filepath)
change_elements = [
render_change_as_html(change, filepath, 0, 2) for change in item["changes"]
]
file_elements = [
E.H4(E.A(filepath, href=file_url)),
E.B("Changes:"),
E.UL(*change_elements),
]
return E.DIV(*file_elements)
def generate_html_section_handler_github(
render_change: Callable[
[Dict[str, Any], str, int, int, Optional[bool]], Optional[Any]
],
compressed: Optional[bool] = False,
) -> Callable[[Any], Any]:
"""
Generates a change wrapper, inside which contains a report on each
function or class depending on the compressed or full format.
"""
def html_file_section_handler_github(item: Dict[str, Any]) -> Any:
filepath = item["file"]
file_url = item.get("file_url", filepath)
change_elements = [
render_change(change, filepath, 0, 2, compressed)
for change in item["changes"]
]
file_summary_element = E.A(filepath, href=file_url)
file_elements = [
E.B("Changes:"),
E.UL(*change_elements),
]
file_elements_div = E.DIV(*file_elements)
file_details_element = E.DIV(
lxml.html.fromstring(
f"<details><summary>{lxml.html.tostring(file_summary_element).decode()}</summary>"
f"{lxml.html.tostring(file_elements_div).decode()}</details>"
)
)
return file_details_element
return html_file_section_handler_github
def generate_render_html(
file_section_handler: Callable[[Dict[str, Any]], Any]
) -> Callable[[Dict[str, Any]], str]:
def render_html(results: Dict[str, Any]) -> str:
heading = E.H2(
E.A("Locust", href="https://github.com/simiotics/locust"), " summary"
)
body_elements = [heading]
refs = results.get("refs")
if refs is not None:
body_elements.extend([E.H3("Git references")])
body_elements.extend([E.B("Initial: "), E.SPAN(refs["initial"]), E.BR()])
if refs["terminal"] is not None:
body_elements.extend(
[E.B("Terminal: "), E.SPAN(refs["terminal"]), E.BR()]
)
body_elements.append(E.HR())
changes_by_file = results["locust"]
for item in changes_by_file:
item_element = file_section_handler(item)
body_elements.append(item_element)
html = E.HTML(E.BODY(*body_elements))
results_string = lxml.html.tostring(html).decode()
return results_string
return render_html
def enrich_with_refs(
results: Dict[str, Any], initial_ref: str, terminal_ref: Optional[str]
) -> Dict[str, Any]:
enriched_results = copy.deepcopy(results)
enriched_results["refs"] = {
"initial": initial_ref,
"terminal": terminal_ref,
}
return enriched_results
def enrich_with_metadata(
results: Dict[str, Any], metadata: Dict[str, Any]
) -> Dict[str, Any]:
enriched_results = copy.deepcopy(results)
for key, value in metadata.items():
enriched_results[key] = value
return enriched_results
# TODO(neeraj): Recursively enrich change children with GitHub links
def enrich_with_github_links(
results: Dict[str, Any], github_repo_url: str, terminal_ref: Optional[str]
) -> Dict[str, Any]:
if terminal_ref is None:
raise ValueError("Cannot create GitHub links without a reference to link to")
if github_repo_url[-1] == "/":
github_repo_url = github_repo_url[:-1]
enriched_results = copy.deepcopy(results)
def _enrich_changes(changes: List[Dict[str, Any]], root_url: str) -> None:
"""
Mutates a list of changes with the updated root URL
"""
for change in changes:
change["link"] = f"{root_url}#L{change['line']}"
if change.get("children") is not None:
_enrich_changes(change["children"], root_url)
for item in enriched_results["locust"]:
relative_filepath = "/".join(item["file"].split(os.sep))
if relative_filepath[0] == "/":
relative_filepath = relative_filepath[1:]
file_url = f"{github_repo_url}/blob/{terminal_ref}/{relative_filepath}"
item["file_url"] = file_url
_enrich_changes(item["changes"], file_url)
return enriched_results
renderers: Dict[str, Callable[[Dict[str, List[NestedChange]]], str]] = {
"json": render_json,
"yaml": render_yaml,
"html": generate_render_html(html_file_section_handler_vanilla),
# html-github kept for for backwards compatibility
"html-github": generate_render_html(
generate_html_section_handler_github(render_change_as_html)
),
"github": generate_render_html(
generate_html_section_handler_github(render_change_as_html, compressed=True)
),
"github-full": generate_render_html(
generate_html_section_handler_github(render_change_as_html)
),
}
def populate_argument_parser(parser: argparse.ArgumentParser) -> None:
"""
Populates an argparse ArgumentParser object with the commonly used arguments for this module.
Mutates the provided parser.
"""
parser.add_argument(
"-f",
"--format",
choices=renderers,
default="json",
help="Format in which to render results",
)
parser.add_argument(
"--github",
required=False,
default=None,
help=(
"[Optional] URL for GitHub repository where code is hosted "
"(e.g. https://github.com/git/git)"
),
)
parser.add_argument(
"-m",
"--metadata",
type=json.loads,
default=None,
help="JSON object specifying additional metadata for Locust summary",
)
def run(
parse_result: parse.ParseResult,
render_format: str,
github_url: Optional[str],
additional_metadata: Optional[Dict[str, Any]] = None,
) -> str:
changes = parse_result.changes
nested_results = nest_results(changes)
results = results_dict(nested_results)
results = enrich_with_refs(
results, parse_result.initial_ref, parse_result.terminal_ref
)
if additional_metadata is not None:
results = enrich_with_metadata(results, additional_metadata)
if github_url is not None and parse_result.terminal_ref is not None:
results = enrich_with_github_links(
results, github_url, parse_result.terminal_ref
)
renderer = renderers[render_format]
results_string = renderer(results)
return results_string
def main():
parser = argparse.ArgumentParser(description="Locust: rendering functionality")
populate_argument_parser(parser)
parser.add_argument(
"-i",
"--input",
type=argparse.FileType("r"),
default=sys.stdin,
help="Path to parse result. If not specified, reads from stdin.",
)
parser.add_argument(
"-o",
"--output",
type=argparse.FileType("w"),
default=sys.stdout,
help="Path to write summary to",
)
args = parser.parse_args()
with args.input as ifp:
parse_result = Parse(ifp.read(), parse.ParseResult())
summary = run(parse_result, args.format, args.github, args.metadata)
try:
with args.output as ofp:
print(summary, file=ofp)
except BrokenPipeError:
pass
if __name__ == "__main__":
main()