File size: 2,721 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
import argparse
import sys
import os
import re
import logging
from utils import (
    run_command,
    init_environment,
    write_file,
    append_file,
    emit_problem_matchers,
    add_common_arguments,
)

DEFAULT_LINE_LENGTH_LIMIT = 100

def load_cpplint_filters(conf_file="cpplint.cfg"):
    """Load cpplint filters from a configuration file."""
    try:
        with open(conf_file, "r", encoding="utf-8") as f:
            filters = f.read().strip()
            return filters
    except Exception as e:
        logging.warning(f"Could not load {conf_file}: {e}")
        return ""

def count_cpplint_errors(log_text: str) -> int:
    """
    Count the number of cpplint error/warning lines.
    """
    matches = re.findall(r"\[[0-9]+\]$", log_text, re.MULTILINE)
    return len(matches)

def generate_markdown_report(aggregated_output: str, errors_count: int) -> str:
    """Generate a Markdown report section based on cpplint output and error count."""
    report_lines = []
    if errors_count > 0:
        report_lines.append(
            f"<details><summary>:warning: CppLint found {errors_count} errors / warnings</summary>"
        )
    else:
        report_lines.append(
            "<details><summary>:heavy_check_mark: No cpplint errors found </summary>"
        )
    report_lines.append("")
    report_lines.append("````")
    report_lines.append(aggregated_output)
    report_lines.append("````")
    report_lines.append("</details>")
    report_lines.append("")
    return "\n".join(report_lines)

def main():
    parser = argparse.ArgumentParser(
        description="Run cpplint on provided C++ files and append a Markdown report."
    )
    add_common_arguments(parser)
    args = parser.parse_args()
    init_environment(args)

    run_command(["pip", "install", "-q", "cpplint"], check=True)

    cpplint_filters = load_cpplint_filters("cpplint.cfg")

    aggregated_output = ""
    for file in args.files.split():
        cmd = [
            "cpplint",
            f"--filters={cpplint_filters}",
            f"--linelength={DEFAULT_LINE_LENGTH_LIMIT}",
            file,
        ]
        stdout, stderr, _ = run_command(cmd)
        aggregated_output += stdout + stderr + "\n"

    cpplint_log = os.path.join(args.log_dir, "cpplint.log")
    write_file(cpplint_log, aggregated_output)
    emit_problem_matchers(cpplint_log, "cpplint.json", "cpplint")

    errors_count = count_cpplint_errors(aggregated_output)
    logging.info(f"Found {errors_count} cpplint errors")

    report = generate_markdown_report(aggregated_output, errors_count)
    append_file(args.report_file, report)

    sys.exit(0 if errors_count == 0 else 1)

if __name__ == "__main__":
    main()