-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodehotspots.py
More file actions
205 lines (177 loc) · 5.56 KB
/
codehotspots.py
File metadata and controls
205 lines (177 loc) · 5.56 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
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
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import json
from dataclasses import asdict, dataclass
from pathlib import Path
from typing import Iterable
DEFAULT_EXTENSIONS = {
".py",
".js",
".ts",
".tsx",
".jsx",
".java",
".go",
".rs",
".c",
".cc",
".cpp",
".h",
".hpp",
".md",
".sh",
}
SKIP_DIRS = {
".git",
".hg",
".svn",
"node_modules",
".venv",
"venv",
"__pycache__",
"dist",
"build",
".next",
}
@dataclass
class FileHotspot:
path: str
score: int
lines: int
long_lines: int
todo_count: int
fixme_count: int
max_indent: int
def iter_source_files(root: Path, extensions: set[str] | None = None) -> Iterable[Path]:
chosen_extensions = extensions or DEFAULT_EXTENSIONS
for path in root.rglob("*"):
if not path.is_file():
continue
if any(part in SKIP_DIRS for part in path.parts):
continue
if path.suffix.lower() in chosen_extensions:
yield path
def analyze_file(path: Path, root: Path, max_line_length: int = 100) -> FileHotspot:
text = path.read_text(encoding="utf-8", errors="replace")
lines = text.splitlines()
long_lines = 0
todo_count = 0
fixme_count = 0
max_indent = 0
for raw_line in lines:
line = raw_line.rstrip("\n")
stripped = line.lstrip(" \t")
if len(line) > max_line_length:
long_lines += 1
lower_line = line.lower()
todo_count += lower_line.count("todo")
fixme_count += lower_line.count("fixme")
if stripped:
indent_chars = len(line) - len(stripped)
indent_depth = indent_chars // 4 if indent_chars else 0
if indent_depth > max_indent:
max_indent = indent_depth
line_count = len(lines)
score = (
line_count // 50
+ (long_lines * 2)
+ (todo_count * 3)
+ (fixme_count * 4)
+ max(0, max_indent - 3) * 2
)
return FileHotspot(
path=str(path.relative_to(root)),
score=score,
lines=line_count,
long_lines=long_lines,
todo_count=todo_count,
fixme_count=fixme_count,
max_indent=max_indent,
)
def analyze_tree(root: Path, max_line_length: int = 100) -> list[FileHotspot]:
hotspots = [
analyze_file(path, root, max_line_length=max_line_length)
for path in iter_source_files(root)
]
hotspots.sort(key=lambda item: (-item.score, -item.lines, item.path))
return hotspots
def parse_extensions(raw_value: str | None) -> set[str] | None:
if not raw_value:
return None
chosen = set()
for item in raw_value.split(","):
extension = item.strip().lower()
if not extension:
continue
if not extension.startswith("."):
extension = f".{extension}"
chosen.add(extension)
return chosen or None
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description="Scan a repository and rank likely maintenance hotspots."
)
parser.add_argument("root", nargs="?", default=".", help="Directory to scan")
parser.add_argument("--top", type=int, default=10, help="Number of files to show")
parser.add_argument(
"--max-line-length",
type=int,
default=100,
help="Threshold for counting long lines",
)
parser.add_argument(
"--extensions",
help="Comma-separated list like py,js,ts to restrict scanned file types",
)
parser.add_argument("--json", action="store_true", help="Emit JSON output")
parser.add_argument("--markdown", action="store_true", help="Emit a Markdown table")
return parser
def render_table(hotspots: list[FileHotspot], top: int) -> str:
shown = hotspots[:top]
header = (
f"{'score':>5} {'lines':>5} {'long':>4} "
f"{'todo':>4} {'fixme':>5} {'indent':>6} path"
)
rows = [header, "-" * len(header)]
for item in shown:
rows.append(
f"{item.score:>5} {item.lines:>5} {item.long_lines:>4} "
f"{item.todo_count:>4} {item.fixme_count:>5} {item.max_indent:>6} {item.path}"
)
if not shown:
rows.append("No matching source files found.")
return "\n".join(rows)
def render_markdown(hotspots: list[FileHotspot], top: int) -> str:
shown = hotspots[:top]
rows = [
"| score | lines | long | todo | fixme | indent | path |",
"| ---: | ---: | ---: | ---: | ---: | ---: | --- |",
]
for item in shown:
rows.append(
f"| {item.score} | {item.lines} | {item.long_lines} | "
f"{item.todo_count} | {item.fixme_count} | {item.max_indent} | `{item.path}` |"
)
if not shown:
rows.append("| 0 | 0 | 0 | 0 | 0 | 0 | No matching source files found. |")
return "\n".join(rows)
def main() -> int:
parser = build_parser()
args = parser.parse_args()
root = Path(args.root).resolve()
extensions = parse_extensions(args.extensions)
hotspots = [
analyze_file(path, root, max_line_length=args.max_line_length)
for path in iter_source_files(root, extensions=extensions)
]
hotspots.sort(key=lambda item: (-item.score, -item.lines, item.path))
if args.json:
print(json.dumps([asdict(item) for item in hotspots[: args.top]], indent=2))
elif args.markdown:
print(render_markdown(hotspots, args.top))
else:
print(render_table(hotspots, args.top))
return 0
if __name__ == "__main__":
raise SystemExit(main())