|
| 1 | +# evmone: Fast Ethereum Virtual Machine implementation |
| 2 | +# Copyright 2025 The evmone Authors. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import json |
| 6 | +from bs4 import BeautifulSoup |
| 7 | +import sys |
| 8 | + |
| 9 | +def add_annotations_to_coverage_html(html_path, json_path, output_path): |
| 10 | + # Load annotations from JSON file |
| 11 | + with open(json_path, 'r', encoding='utf-8') as jf: |
| 12 | + annotations = json.load(jf) |
| 13 | + |
| 14 | + # Parse HTML |
| 15 | + with open(html_path, 'r', encoding='utf-8') as hf: |
| 16 | + soup = BeautifulSoup(hf, 'html.parser') |
| 17 | + |
| 18 | + # Find the coverage file table |
| 19 | + # Commonly, the file list is inside a <table class="index"> or similar |
| 20 | + table = soup.find('table') |
| 21 | + if not table: |
| 22 | + raise RuntimeError("Could not find a table in the HTML file") |
| 23 | + |
| 24 | + # Add new column header |
| 25 | + header_row = table.find('tr') |
| 26 | + if header_row: |
| 27 | + new_th = soup.new_tag('th') |
| 28 | + new_th.string = 'Annotation' |
| 29 | + header_row.append(new_th) |
| 30 | + |
| 31 | + # Iterate over table rows (skip header) |
| 32 | + rows = table.find_all('tr')[1:] |
| 33 | + for row in rows: |
| 34 | + cols = row.find_all(['td', 'th']) |
| 35 | + if not cols: |
| 36 | + continue |
| 37 | + |
| 38 | + # The first column usually contains the filename |
| 39 | + file_link = cols[0].find('a') |
| 40 | + filename = None |
| 41 | + if file_link and file_link.text: |
| 42 | + filename = file_link.text.strip() |
| 43 | + else: |
| 44 | + filename = cols[0].text.strip() |
| 45 | + |
| 46 | + annotation_list = annotations.get(filename, []) |
| 47 | + |
| 48 | + # Convert list to HTML bullet list or plain text |
| 49 | + if annotation_list: |
| 50 | + annotation_html = "<ul>" + "".join( |
| 51 | + f"<li>{note}</li>" for note in annotation_list |
| 52 | + ) + "</ul>" |
| 53 | + else: |
| 54 | + annotation_html = "<i>—</i>" |
| 55 | + |
| 56 | + # Add new cell |
| 57 | + new_td = soup.new_tag("td") |
| 58 | + new_td["class"] = "column-entry" |
| 59 | + new_td.append(BeautifulSoup(annotation_html, "html.parser")) |
| 60 | + row.append(new_td) |
| 61 | + |
| 62 | + # Save modified HTML |
| 63 | + with open(output_path, 'w', encoding='utf-8') as out: |
| 64 | + out.write(str(soup)) |
| 65 | + |
| 66 | + print(f"✅ Annotated HTML saved to: {output_path}") |
| 67 | + |
| 68 | + |
| 69 | +def main(): |
| 70 | + if len(sys.argv) != 4: |
| 71 | + print("Usage: uv run add-annotations coverage/html/index.html annotations.json coverage_with_annotations.html>") |
| 72 | + sys.exit(1) |
| 73 | + |
| 74 | + html_path, json_path, output_path = sys.argv[1:] |
| 75 | + add_annotations_to_coverage_html(html_path, json_path, output_path) |
0 commit comments