-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprototype.py
More file actions
66 lines (46 loc) · 1.57 KB
/
Copy pathprototype.py
File metadata and controls
66 lines (46 loc) · 1.57 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
import ast
from pathlib import Path
MAX_FUNCTION_LINES = 8
def analyze_file(file_path: Path) -> None:
try:
source_code = file_path.read_text(encoding="utf-8")
except OSError as error:
print(f"Dosya okunamadı: {file_path}")
print(f"Hata: {error}")
return
try:
syntax_tree = ast.parse(source_code)
except SyntaxError as error:
print(
f"Syntax hatası: {file_path}:{error.lineno} "
f"{error.msg}"
)
return
finding_count = 0
for node in ast.walk(syntax_tree):
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
start_line = node.lineno
end_line = node.end_lineno or node.lineno
function_length = end_line - start_line + 1
if function_length > MAX_FUNCTION_LINES:
finding_count += 1
print(
f"[MEDIUM] {file_path}:{start_line} "
f"'{node.name}' fonksiyonu "
f"{function_length} satır uzunluğundadır."
)
print(
f"Öneri: Fonksiyonu {MAX_FUNCTION_LINES} "
)
if finding_count == 0:
print("Uzun fonksiyon bulunamadı.")
else:
print(f"\nToplam bulgu: {finding_count}")
def main() -> None:
target_file = Path("examples/vulnerable_code.py")
if not target_file.exists():
print(f"Dosya bulunamadı: {target_file}")
return
analyze_file(target_file)
if __name__ == "__main__":
main()