Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
157 lines (128 loc) · 4.42 KB
/
Copy pathmain.py
File metadata and controls
157 lines (128 loc) · 4.42 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
"""Example 4: Code graph analysis and structural regression detection.
Demonstrates: import/call graph building and diffing.
"""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
from examples.utils import save_analysis_data
from vallm.core.graph_builder import build_python_graph
from vallm.core.graph_diff import diff_python_code
# Original code
before_code = """
import os
import json
from pathlib import Path
def read_config(path: str) -> dict:
with open(path) as f:
return json.load(f)
def process_data(config: dict) -> list:
items = config.get("items", [])
return [item.upper() for item in items]
def main():
config = read_config("config.json")
result = process_data(config)
print(result)
"""
# Modified code (with changes)
after_code = """
import json
import yaml
from pathlib import Path
from dataclasses import dataclass
@dataclass
class Config:
items: list[str]
debug: bool = False
def read_config(path: str) -> Config:
with open(path) as f:
data = yaml.safe_load(f)
return Config(**data)
def process_data(config: Config) -> list:
items = config.items
return [item.upper() for item in items]
def validate_config(config: Config) -> bool:
return len(config.items) > 0
def main():
config = read_config("config.yaml")
if validate_config(config):
result = process_data(config)
print(result)
"""
def main():
all_results = {}
print("=" * 60)
print("Code Graph: BEFORE")
print("=" * 60)
graph_before = build_python_graph(before_code, "mymodule")
imports_before = [(e.source_module, e.imported_name) for e in graph_before.imports]
functions_before = graph_before.functions
classes_before = graph_before.classes
calls_before = [(e.caller, e.callee) for e in graph_before.calls]
print(f"Imports: {imports_before}")
print(f"Functions: {functions_before}")
print(f"Classes: {classes_before}")
print(f"Calls: {calls_before}")
# Store before data
all_results["before"] = {
"imports": imports_before,
"functions": functions_before,
"classes": classes_before,
"calls": calls_before,
}
print("\n" + "=" * 60)
print("Code Graph: AFTER")
print("=" * 60)
graph_after = build_python_graph(after_code, "mymodule")
imports_after = [(e.source_module, e.imported_name) for e in graph_after.imports]
functions_after = graph_after.functions
classes_after = graph_after.classes
calls_after = [(e.caller, e.callee) for e in graph_after.calls]
print(f"Imports: {imports_after}")
print(f"Functions: {functions_after}")
print(f"Classes: {classes_after}")
print(f"Calls: {calls_after}")
# Store after data
all_results["after"] = {
"imports": imports_after,
"functions": functions_after,
"classes": classes_after,
"calls": calls_after,
}
print("\n" + "=" * 60)
print("Structural Diff")
print("=" * 60)
diff = diff_python_code(before_code, after_code)
print(f"Has changes: {diff.has_changes}")
print(f"Added imports: {diff.added_imports}")
print(f"Removed imports: {diff.removed_imports}")
print(f"Added functions: {diff.added_functions}")
print(f"Removed functions: {diff.removed_functions}")
print(f"Added classes: {diff.added_classes}")
# Store diff data
all_results["diff"] = {
"has_changes": diff.has_changes,
"added_imports": diff.added_imports,
"removed_imports": diff.removed_imports,
"added_functions": diff.added_functions,
"removed_functions": diff.removed_functions,
"added_classes": diff.added_classes,
"breaking_changes": diff.breaking_changes if diff.breaking_changes else [],
}
if diff.breaking_changes:
print("\n⚠️ Breaking changes detected:")
for change in diff.breaking_changes:
print(f" - {change}")
else:
print("\n✓ No breaking changes detected")
# Save all analysis data
save_analysis_data("graph_analysis", all_results)
# Print summary
print("\n" + "=" * 60)
print("SUMMARY")
print("=" * 60)
print(f"Changes detected: {diff.has_changes}")
print(f"Breaking changes: {len(diff.breaking_changes) if diff.breaking_changes else 0}")
print(f"Functions added: {len(diff.added_functions)}")
print(f"Classes added: {len(diff.added_classes)}")
if __name__ == "__main__":
main()