|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import argparse |
| 6 | +from dataclasses import dataclass |
| 7 | +from pathlib import Path |
| 8 | +from typing import Final |
| 9 | + |
| 10 | +import bs4 |
| 11 | +import markdown |
| 12 | +from github import Auth, Github |
| 13 | + |
| 14 | +EXPECTED_HEADINGS: Final[list[str]] = [ |
| 15 | + "Guideline", |
| 16 | + "Guideline Name", |
| 17 | + "MISRA C:2025 Status", |
| 18 | + "Decidability", |
| 19 | + "Scope", |
| 20 | + "Rationale", |
| 21 | + "Applicability", |
| 22 | + "Adjusted Category", |
| 23 | +] |
| 24 | + |
| 25 | + |
| 26 | +@dataclass(eq=True) |
| 27 | +class MISRA_Rule: |
| 28 | + title: str |
| 29 | + section: str | None = None |
| 30 | + status: str | None = None |
| 31 | + decidability: str | None = None |
| 32 | + scope: str | None = None |
| 33 | + rationale: str | None = None |
| 34 | + applicability: str | None = None |
| 35 | + category: str | None = None |
| 36 | + |
| 37 | + @classmethod |
| 38 | + def from_cols(cls, cols: list[str | None]) -> MISRA_Rule | None: |
| 39 | + assert len(cols) == len(EXPECTED_HEADINGS), ( |
| 40 | + f"Expected {len(EXPECTED_HEADINGS)}, got {len(cols)}" |
| 41 | + ) |
| 42 | + # Cannot create rule without a title |
| 43 | + title = cols[EXPECTED_HEADINGS.index("Guideline Name")] |
| 44 | + if title is None: |
| 45 | + return None |
| 46 | + return MISRA_Rule( |
| 47 | + title=title, |
| 48 | + section=cols[EXPECTED_HEADINGS.index("Guideline")], |
| 49 | + status=cols[EXPECTED_HEADINGS.index("MISRA C:2025 Status")], |
| 50 | + decidability=cols[EXPECTED_HEADINGS.index("Decidability")], |
| 51 | + scope=cols[EXPECTED_HEADINGS.index("Scope")], |
| 52 | + rationale=cols[EXPECTED_HEADINGS.index("Rationale")], |
| 53 | + applicability=cols[EXPECTED_HEADINGS.index("Applicability")], |
| 54 | + category=cols[EXPECTED_HEADINGS.index("Adjusted Category")], |
| 55 | + ) |
| 56 | + |
| 57 | + @property |
| 58 | + def issue_body(self) -> str: |
| 59 | + # FIXME(senier): Properly layout (we could even use .github/ISSUE_TEMPLATE/CODING-GUILDELINE.yml to validate the format) |
| 60 | + # FIXME(senier): Transform into dedicated coding guidline object and do layouting there |
| 61 | + return str(self) |
| 62 | + |
| 63 | + |
| 64 | +def convert_md(file: Path) -> list[MISRA_Rule] | None: |
| 65 | + result = None |
| 66 | + |
| 67 | + with file.open() as f: |
| 68 | + html = markdown.markdown(f.read(), extensions=["tables"], output_format="xhtml") |
| 69 | + soup = bs4.BeautifulSoup(html, features="lxml") |
| 70 | + |
| 71 | + table = soup.find("table") |
| 72 | + if table is None or not isinstance(table, bs4.Tag): |
| 73 | + return None |
| 74 | + |
| 75 | + headings = table.find("thead") |
| 76 | + if headings is None or not isinstance(headings, bs4.Tag): |
| 77 | + return None |
| 78 | + |
| 79 | + values = [h.text for h in headings.find_all("th")] |
| 80 | + if values != EXPECTED_HEADINGS: |
| 81 | + return None |
| 82 | + |
| 83 | + body = table.find("tbody") |
| 84 | + if body is None or not isinstance(body, bs4.Tag): |
| 85 | + return None |
| 86 | + |
| 87 | + for row in body.find_all("tr"): |
| 88 | + if row is None or not isinstance(row, bs4.Tag): |
| 89 | + continue |
| 90 | + |
| 91 | + cols = [r.text or None for r in row.find_all("td")] |
| 92 | + assert len(cols) == 0 or len(cols) == len(EXPECTED_HEADINGS), f"{cols}" |
| 93 | + |
| 94 | + # skip empty rows |
| 95 | + if all(c is None for c in cols): |
| 96 | + continue |
| 97 | + |
| 98 | + if result is None: |
| 99 | + result = [] |
| 100 | + rule = MISRA_Rule.from_cols(cols) |
| 101 | + if rule is not None: |
| 102 | + result.append(rule) |
| 103 | + return result |
| 104 | + |
| 105 | + |
| 106 | +def create_issues(repo_name: str, token: str, rules: list[MISRA_Rule]): |
| 107 | + auth = Auth.Token(token=token) |
| 108 | + github = Github(auth=auth) |
| 109 | + repo = github.get_repo(repo_name) |
| 110 | + |
| 111 | + for rule in rules: |
| 112 | + if rule.title is None: |
| 113 | + continue |
| 114 | + repo.create_issue(title=rule.title, body=rule.issue_body) |
| 115 | + |
| 116 | + |
| 117 | +def import_rules(file: Path, repo: str, token: str) -> int | str: |
| 118 | + md = convert_md(file) |
| 119 | + if md is None: |
| 120 | + return "No rules found" |
| 121 | + create_issues(repo_name=repo, token=token, rules=md) |
| 122 | + return 1 |
| 123 | + |
| 124 | + |
| 125 | +def main() -> int | str: |
| 126 | + parser = argparse.ArgumentParser() |
| 127 | + parser.add_argument( |
| 128 | + "-m", |
| 129 | + "--markdown", |
| 130 | + type=Path, |
| 131 | + required=True, |
| 132 | + help="Markdown file to extract rules from", |
| 133 | + ) |
| 134 | + parser.add_argument( |
| 135 | + "-r", |
| 136 | + "--repository", |
| 137 | + type=str, |
| 138 | + required=True, |
| 139 | + help="Github repository to import rules to (format: account/repository)", |
| 140 | + ) |
| 141 | + parser.add_argument( |
| 142 | + "-a", |
| 143 | + "--auth-token", |
| 144 | + type=str, |
| 145 | + required=True, |
| 146 | + help="Github authentication token", |
| 147 | + ) |
| 148 | + args = parser.parse_args() |
| 149 | + return import_rules(file=args.markdown, repo=args.repository, token=args.auth_token) |
| 150 | + |
| 151 | + |
| 152 | +if __name__ == "__main__": |
| 153 | + main() |
0 commit comments