forked from learning-process/parallel_programming_course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_comment_to_code_ratio.py
More file actions
108 lines (89 loc) · 2.95 KB
/
check_comment_to_code_ratio.py
File metadata and controls
108 lines (89 loc) · 2.95 KB
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
import argparse
import os
import sys
from pathlib import Path
EXPECTED_HEADER_SUFFIX = [
"== files",
"!= files",
"+ files",
"- files",
"== blank",
"!= blank",
"+ blank",
"- blank",
"== comment",
"!= comment",
"+ comment",
"- comment",
"== code",
"!= code",
"+ code",
"- code",
]
# Zero-based indices in `cloc --diff --md` rows after splitting by `|`.
COMMENT_COLUMN_INDICES = (10, 11, 12)
CODE_COLUMN_INDICES = (14, 15, 16)
def parse_changed_totals(report_path):
changed_comments = 0
changed_code = 0
header_found = False
for raw_line in report_path.read_text(encoding="utf-8").splitlines():
if "|" not in raw_line:
continue
cells = [cell.strip() for cell in raw_line.split("|")]
if cells and cells[-1] == "":
cells = cells[:-1]
if cells and cells[0] in {"Language", "File"}:
if cells[1:] != EXPECTED_HEADER_SUFFIX:
sys.exit("Unexpected cloc diff table format.")
header_found = True
continue
if not cells:
continue
if cells[0].startswith((":", "-", "SUM:")):
continue
if not header_found:
continue
try:
changed_comments += sum(
int(cells[index]) for index in COMMENT_COLUMN_INDICES
)
changed_code += sum(int(cells[index]) for index in CODE_COLUMN_INDICES)
except (IndexError, ValueError):
continue
if not header_found:
print("Unable to find cloc diff table header in report.")
sys.exit(1)
return changed_comments, changed_code
def main():
parser = argparse.ArgumentParser()
parser.add_argument("report")
parser.add_argument("--limit", type=float, default=10.0)
args = parser.parse_args()
report_path = Path(args.report)
if not report_path.exists():
print(f"{report_path} was not generated by the comment ratio action.")
sys.exit(1)
changed_comments, changed_code = parse_changed_totals(report_path)
total_changed_nonblank = changed_comments + changed_code
comment_percentage = (
0.0
if total_changed_nonblank == 0
else changed_comments * 100.0 / total_changed_nonblank
)
print(f"Comment percentage for PR changes: {comment_percentage:.2f}%")
github_output = os.getenv("GITHUB_OUTPUT")
if github_output:
with Path(github_output).open("a", encoding="utf-8") as output:
output.write(f"changed_comments={changed_comments}\n")
output.write(f"changed_code={changed_code}\n")
output.write(f"comment_percentage={comment_percentage:.2f}\n")
if comment_percentage > args.limit:
print(
f"Comment percentage for PR changes is {comment_percentage:.2f}%, "
f"which exceeds {args.limit:.2f}%.",
file=sys.stderr,
)
sys.exit(1)
if __name__ == "__main__":
main()