-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathtopology.py
More file actions
168 lines (132 loc) · 5.99 KB
/
topology.py
File metadata and controls
168 lines (132 loc) · 5.99 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
from typing import List, Set, Tuple
from collections import defaultdict, deque
from osa_tool.utils.logger import logger
class DependencyGraph:
"""
Builds and manages dependency graph for functions/methods.
The graph represents "who calls whom" relationships where:
- Node: unique identifier for a function/method (file_path:function_name)
- Edge: A → B means "A calls B" (A depends on B)
Topological sort ensures B is processed before A.
"""
def __init__(self, parsed_structure: dict):
"""
Initialize dependency graph from parsed structure.
Args:
parsed_structure: Output from OSA_TreeSitter.analyze_directory()
Format: {file_path: {"structure": [...], "imports": {...}}}
"""
self.parsed_structure = parsed_structure
self.graph = defaultdict(set)
self.reverse_graph = defaultdict(set)
self.nodes = {}
self.node_to_file = {}
self._build_graph()
def _build_graph(self):
"""Build dependency graph from parsed structure."""
for file_path, file_meta in self.parsed_structure.items():
structure = file_meta.get("structure", [])
for item in structure:
if item["type"] == "class":
class_name = item["name"]
for method in item["methods"]:
method_name = method["method_name"]
node_id = f"{file_path}:{class_name}.{method_name}"
self.nodes[node_id] = {
"type": "method",
"file": file_path,
"class": class_name,
"name": method_name,
"metadata": method,
}
self.node_to_file[node_id] = file_path
elif item["type"] == "function":
function_name = item["details"]["method_name"]
node_id = f"{file_path}:{function_name}"
self.nodes[node_id] = {
"type": "function",
"file": file_path,
"name": function_name,
"metadata": item["details"],
}
self.node_to_file[node_id] = file_path
for node_id, node_info in self.nodes.items():
metadata = node_info["metadata"]
method_calls = metadata.get("method_calls", [])
for call in method_calls:
dependency_node = self._resolve_call(node_id, call)
if dependency_node and dependency_node in self.nodes:
if node_id != dependency_node: # Avoiding self-recursion
self.graph[node_id].add(dependency_node)
self.reverse_graph[dependency_node].add(node_id)
else:
logger.debug(f"Skipping self-recursion for node {node_id}")
def _resolve_call(self, caller_node_id: str, call_name: str) -> str:
"""
Resolve a method call to a node ID.
Args:
caller_node_id: ID of the calling node
call_name: Name of the called function (e.g., "foo", "self.bar", "ClassName.baz")
Returns:
Resolved node_id or None if not found
"""
caller_file = self.node_to_file[caller_node_id]
caller_info = self.nodes[caller_node_id]
if call_name.startswith("self."):
method_name = call_name.replace("self.", "")
if caller_info["type"] == "method":
class_name = caller_info["class"]
node_id = f"{caller_file}:{class_name}.{method_name}"
return node_id if node_id in self.nodes else None
elif "." in call_name:
parts = call_name.split(".")
class_name = parts[0]
method_name = ".".join(parts[1:])
node_id = f"{caller_file}:{class_name}.{method_name}"
if node_id in self.nodes:
return node_id
for file_path in self.parsed_structure.keys():
node_id = f"{file_path}:{class_name}.{method_name}"
if node_id in self.nodes:
return node_id
else:
node_id = f"{caller_file}:{call_name}"
if node_id in self.nodes:
return node_id
for file_path in self.parsed_structure.keys():
node_id = f"{file_path}:{call_name}"
if node_id in self.nodes:
return node_id
return None
def get_node_metadata(self, node_id: str) -> dict:
"""Get metadata for a node."""
return self.nodes.get(node_id, {})
def get_dependencies(self, node_id: str) -> Set[str]:
"""Get direct dependencies of a node."""
return self.graph.get(node_id, set())
def get_statistics(self) -> dict:
"""Get graph statistics for debugging."""
total_nodes = len(self.nodes)
total_edges = sum(len(deps) for deps in self.graph.values())
nodes_with_deps = sum(1 for deps in self.graph.values() if deps)
max_deps = max((len(deps) for deps in self.graph.values()), default=0)
return {
"total_nodes": total_nodes,
"total_edges": total_edges,
"nodes_with_dependencies": nodes_with_deps,
"max_dependencies_per_node": max_deps,
"average_dependencies": total_edges / total_nodes if total_nodes > 0 else 0,
}
def build_dependency_graph(parsed_structure: dict) -> DependencyGraph:
"""
Build dependency graph from parsed structure.
Args:
parsed_structure: Output from OSA_TreeSitter.analyze_directory()
Returns:
DependencyGraph instance
"""
graph = DependencyGraph(parsed_structure)
stats = graph.get_statistics()
logger.info(f"Dependency graph ready: {len(graph.nodes)} nodes")
logger.debug(f"Dependency graph built: {stats}")
return graph