-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathrun.py
More file actions
313 lines (250 loc) · 10.7 KB
/
Copy pathrun.py
File metadata and controls
313 lines (250 loc) · 10.7 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
from typing import Dict, Any
from graph_sitter import Codebase
from graph_sitter.core.statements.for_loop_statement import ForLoopStatement
from graph_sitter.core.statements.if_block_statement import IfBlockStatement
from graph_sitter.core.statements.try_catch_statement import TryCatchStatement
from graph_sitter.core.statements.while_statement import WhileStatement
from graph_sitter.core.expressions.binary_expression import BinaryExpression
from graph_sitter.core.expressions.unary_expression import UnaryExpression
from graph_sitter.core.expressions.comparison_expression import ComparisonExpression
import math
import re
import requests
def calculate_cyclomatic_complexity(function):
def analyze_statement(statement):
complexity = 0
if isinstance(statement, IfBlockStatement):
complexity += 1
if hasattr(statement, "elif_statements"):
complexity += len(statement.elif_statements)
elif isinstance(statement, (ForLoopStatement, WhileStatement)):
complexity += 1
elif isinstance(statement, TryCatchStatement):
complexity += len(getattr(statement, "except_blocks", []))
if hasattr(statement, "condition") and isinstance(statement.condition, str):
complexity += statement.condition.count(" and ") + statement.condition.count(" or ")
if hasattr(statement, "nested_code_blocks"):
for block in statement.nested_code_blocks:
complexity += analyze_block(block)
return complexity
def analyze_block(block):
if not block or not hasattr(block, "statements"):
return 0
return sum(analyze_statement(stmt) for stmt in block.statements)
return 1 + analyze_block(function.code_block) if hasattr(function, "code_block") else 1
def cc_rank(complexity):
if complexity < 0:
raise ValueError("Complexity must be a non-negative value")
ranks = [
(1, 5, "A"),
(6, 10, "B"),
(11, 20, "C"),
(21, 30, "D"),
(31, 40, "E"),
(41, float("inf"), "F"),
]
for low, high, rank in ranks:
if low <= complexity <= high:
return rank
return "F"
def calculate_doi(cls):
"""Calculate the depth of inheritance for a given class."""
return len(cls.superclasses)
def get_operators_and_operands(function):
operators = []
operands = []
for statement in function.code_block.statements:
for call in statement.function_calls:
operators.append(call.name)
for arg in call.args:
operands.append(arg.source)
if hasattr(statement, "expressions"):
for expr in statement.expressions:
if isinstance(expr, BinaryExpression):
operators.extend([op.source for op in expr.operators])
operands.extend([elem.source for elem in expr.elements])
elif isinstance(expr, UnaryExpression):
operators.append(expr.ts_node.type)
operands.append(expr.argument.source)
elif isinstance(expr, ComparisonExpression):
operators.extend([op.source for op in expr.operators])
operands.extend([elem.source for elem in expr.elements])
if hasattr(statement, "expression"):
expr = statement.expression
if isinstance(expr, BinaryExpression):
operators.extend([op.source for op in expr.operators])
operands.extend([elem.source for elem in expr.elements])
elif isinstance(expr, UnaryExpression):
operators.append(expr.ts_node.type)
operands.append(expr.argument.source)
elif isinstance(expr, ComparisonExpression):
operators.extend([op.source for op in expr.operators])
operands.extend([elem.source for elem in expr.elements])
return operators, operands
def calculate_halstead_volume(operators, operands):
n1 = len(set(operators))
n2 = len(set(operands))
N1 = len(operators)
N2 = len(operands)
N = N1 + N2
n = n1 + n2
if n > 0:
volume = N * math.log2(n)
return volume, N1, N2, n1, n2
return 0, N1, N2, n1, n2
def count_lines(source: str):
"""Count different types of lines in source code."""
if not source.strip():
return 0, 0, 0, 0
lines = [line.strip() for line in source.splitlines()]
loc = len(lines)
sloc = len([line for line in lines if line])
in_multiline = False
comments = 0
code_lines = []
i = 0
while i < len(lines):
line = lines[i]
code_part = line
if not in_multiline and "#" in line:
comment_start = line.find("#")
if not re.search(r'["\'].*#.*["\']', line[:comment_start]):
code_part = line[:comment_start].strip()
if line[comment_start:].strip():
comments += 1
if ('"""' in line or "'''" in line) and not (line.count('"""') % 2 == 0 or line.count("'''") % 2 == 0):
if in_multiline:
in_multiline = False
comments += 1
else:
in_multiline = True
comments += 1
if line.strip().startswith('"""') or line.strip().startswith("'''"):
code_part = ""
elif in_multiline:
comments += 1
code_part = ""
elif line.strip().startswith("#"):
comments += 1
code_part = ""
if code_part.strip():
code_lines.append(code_part)
i += 1
lloc = 0
continued_line = False
for line in code_lines:
if continued_line:
if not any(line.rstrip().endswith(c) for c in ("\\", ",", "{", "[", "(")):
continued_line = False
continue
lloc += len([stmt for stmt in line.split(";") if stmt.strip()])
if any(line.rstrip().endswith(c) for c in ("\\", ",", "{", "[", "(")):
continued_line = True
return loc, lloc, sloc, comments
def calculate_maintainability_index(halstead_volume: float, cyclomatic_complexity: float, loc: int) -> int:
"""Calculate the normalized maintainability index for a given function."""
if loc <= 0:
return 100
try:
raw_mi = 171 - 5.2 * math.log(max(1, halstead_volume)) - 0.23 * cyclomatic_complexity - 16.2 * math.log(max(1, loc))
normalized_mi = max(0, min(100, raw_mi * 100 / 171))
return int(normalized_mi)
except (ValueError, TypeError):
return 0
def get_github_repo_description(repo_url):
api_url = f"https://api.github.com/repos/{repo_url}"
response = requests.get(api_url)
if response.status_code == 200:
repo_data = response.json()
return repo_data.get("description", "No description available")
else:
return ""
def analyze_repo(repo_url: str) -> Dict[str, Any]:
"""Analyze a repository and return comprehensive metrics."""
codebase = Codebase.from_repo(repo_url)
num_files = len(codebase.files(extensions="*"))
num_functions = len(codebase.functions)
num_classes = len(codebase.classes)
total_loc = total_lloc = total_sloc = total_comments = 0
total_complexity = 0
total_volume = 0
total_mi = 0
total_doi = 0
for file in codebase.files:
loc, lloc, sloc, comments = count_lines(file.source)
total_loc += loc
total_lloc += lloc
total_sloc += sloc
total_comments += comments
callables = codebase.functions + [m for c in codebase.classes for m in c.methods]
num_callables = 0
for func in callables:
if not hasattr(func, "code_block"):
continue
complexity = calculate_cyclomatic_complexity(func)
operators, operands = get_operators_and_operands(func)
volume, _, _, _, _ = calculate_halstead_volume(operators, operands)
loc = len(func.code_block.source.splitlines())
mi_score = calculate_maintainability_index(volume, complexity, loc)
total_complexity += complexity
total_volume += volume
total_mi += mi_score
num_callables += 1
for cls in codebase.classes:
doi = calculate_doi(cls)
total_doi += doi
desc = get_github_repo_description(repo_url)
results = {
"repo_url": repo_url,
"line_metrics": {
"total": {
"loc": total_loc,
"lloc": total_lloc,
"sloc": total_sloc,
"comments": total_comments,
"comment_density": (total_comments / total_loc * 100) if total_loc > 0 else 0,
},
},
"cyclomatic_complexity": {
"average": total_complexity if num_callables > 0 else 0,
},
"depth_of_inheritance": {
"average": total_doi / len(codebase.classes) if codebase.classes else 0,
},
"halstead_metrics": {
"total_volume": int(total_volume),
"average_volume": int(total_volume / num_callables) if num_callables > 0 else 0,
},
"maintainability_index": {
"average": int(total_mi / num_callables) if num_callables > 0 else 0,
},
"description": desc,
"num_files": num_files,
"num_functions": num_functions,
"num_classes": num_classes,
}
return results
if __name__ == "__main__":
repo_url = "codegen-sh/graph-sitter"
results = analyze_repo(repo_url)
print("\n📊 Repository Analysis Report 📊")
print("=" * 50)
print(f"📁 Repository: {results['repo_url']}")
print(f"📝 Description: {results['description']}")
print("\n📈 Basic Metrics:")
print(f" • Files: {results['num_files']}")
print(f" • Functions: {results['num_functions']}")
print(f" • Classes: {results['num_classes']}")
print("\n📏 Line Metrics:")
line_metrics = results["line_metrics"]["total"]
print(f" • Lines of Code: {line_metrics['loc']:,}")
print(f" • Logical Lines: {line_metrics['lloc']:,}")
print(f" • Source Lines: {line_metrics['sloc']:,}")
print(f" • Comments: {line_metrics['comments']:,}")
print(f" • Comment Density: {line_metrics['comment_density']:.1f}%")
print("\n🔄 Complexity Metrics:")
print(f" • Average Cyclomatic Complexity: {results['cyclomatic_complexity']['average']:.1f}")
print(f" • Average Maintainability Index: {results['maintainability_index']['average']}")
print(f" • Average Depth of Inheritance: {results['depth_of_inheritance']['average']:.1f}")
print(f" • Total Halstead Volume: {results['halstead_metrics']['total_volume']:,}")
print(f" • Average Halstead Volume: {results['halstead_metrics']['average_volume']:,}")