-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_graph.py
More file actions
257 lines (213 loc) · 8.57 KB
/
Copy pathcode_graph.py
File metadata and controls
257 lines (213 loc) · 8.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
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
from __future__ import annotations
import ast
import re
from collections import defaultdict
from dataclasses import asdict, dataclass
from pathlib import Path
from typing import Any
from patchsmith.models import RepositoryIndex
from patchsmith.retrieval_features import cached_read_text, is_test_path, path_terms
TOKEN_RE = re.compile(r"[A-Za-z_][A-Za-z0-9_]+")
# Memoize the most recently built graph per repo path. The fingerprint over the
# Python files' (path, mtime, size) invalidates the cache when sources change,
# so retries that modify the workspace rebuild correctly.
_GRAPH_CACHE: dict[str, tuple[tuple[tuple[str, int, int], ...], CodeContextGraph]] = {}
@dataclass(frozen=True)
class CodeGraphNode:
node_id: str
kind: str
name: str
path: str | None = None
def to_dict(self) -> dict[str, Any]:
return asdict(self)
@dataclass(frozen=True)
class CodeGraphEdge:
source: str
target: str
relation: str
def to_dict(self) -> dict[str, Any]:
return asdict(self)
@dataclass(frozen=True)
class CodeContextGraph:
nodes: dict[str, CodeGraphNode]
edges: list[CodeGraphEdge]
file_terms: dict[str, set[str]]
related_paths: dict[str, set[str]]
def terms_for_path(self, path: str) -> set[str]:
return self.file_terms.get(path, set())
def neighbors_for_path(self, path: str) -> set[str]:
return self.related_paths.get(path, set())
def to_dict(self) -> dict[str, Any]:
return {
"nodes": [node.to_dict() for node in self.nodes.values()],
"edges": [edge.to_dict() for edge in self.edges],
"file_terms": {path: sorted(terms) for path, terms in self.file_terms.items()},
"related_paths": {path: sorted(paths) for path, paths in self.related_paths.items()},
}
def build_code_context_graph(*, repo_path: Path, repo_index: RepositoryIndex) -> CodeContextGraph:
fingerprint = _repo_python_fingerprint(repo_path, repo_index)
cache_key = str(repo_path)
cached = _GRAPH_CACHE.get(cache_key)
if cached is not None and cached[0] == fingerprint:
return cached[1]
graph = _build_code_context_graph(repo_path=repo_path, repo_index=repo_index)
_GRAPH_CACHE[cache_key] = (fingerprint, graph)
return graph
def _repo_python_fingerprint(
repo_path: Path, repo_index: RepositoryIndex
) -> tuple[tuple[str, int, int], ...]:
entries: list[tuple[str, int, int]] = []
for file in repo_index.files:
if file.language != "Python":
continue
try:
stat = (repo_path / file.path).stat()
except OSError:
entries.append((file.path, -1, -1))
continue
entries.append((file.path, stat.st_mtime_ns, stat.st_size))
entries.sort()
return tuple(entries)
def _build_code_context_graph(*, repo_path: Path, repo_index: RepositoryIndex) -> CodeContextGraph:
module_to_path = {
module: file.path
for file in repo_index.files
if file.language == "Python"
for module in _module_names_for_path(file.path)
}
nodes: dict[str, CodeGraphNode] = {}
edges: list[CodeGraphEdge] = []
file_terms: dict[str, set[str]] = {}
related_paths: dict[str, set[str]] = defaultdict(set)
for file in repo_index.files:
if file.language != "Python":
continue
file_node = _file_node_id(file.path)
nodes[file_node] = CodeGraphNode(
node_id=file_node,
kind="file",
name=file.path,
path=file.path,
)
text = cached_read_text(repo_path / file.path)
terms = set(path_terms(file.path, drop_stopwords=False))
parsed = _parse_python(text)
symbols = _symbols_from_ast(parsed) if parsed else _symbols_from_text(text)
imports = _imports_from_ast(parsed) if parsed else _imports_from_text(text)
for symbol in symbols:
symbol_node = f"symbol:{file.path}:{symbol}"
nodes[symbol_node] = CodeGraphNode(
node_id=symbol_node,
kind="symbol",
name=symbol,
path=file.path,
)
edges.append(CodeGraphEdge(file_node, symbol_node, "defines"))
terms.update(_name_terms(symbol))
for module in imports:
target_path = module_to_path.get(module)
if target_path and target_path != file.path:
target_node = _file_node_id(target_path)
edges.append(CodeGraphEdge(file_node, target_node, "imports"))
related_paths[file.path].add(target_path)
related_paths[target_path].add(file.path)
if is_test_path(file.path) and not is_test_path(target_path):
edges.append(CodeGraphEdge(file_node, target_node, "tests"))
edges.append(CodeGraphEdge(target_node, file_node, "covered_by"))
else:
module_node = f"module:{module}"
nodes[module_node] = CodeGraphNode(
node_id=module_node,
kind="import",
name=module,
path=None,
)
edges.append(CodeGraphEdge(file_node, module_node, "imports"))
terms.update(_name_terms(module))
file_terms[file.path] = terms
_add_test_basename_edges(repo_index, edges, related_paths)
return CodeContextGraph(
nodes=dict(sorted(nodes.items())),
edges=edges,
file_terms=file_terms,
related_paths={path: set(paths) for path, paths in related_paths.items()},
)
def _add_test_basename_edges(
repo_index: RepositoryIndex,
edges: list[CodeGraphEdge],
related_paths: dict[str, set[str]],
) -> None:
sources = [
file.path
for file in repo_index.files
if file.language == "Python" and not is_test_path(file.path)
]
source_by_stem = {Path(path).stem: path for path in sources}
for file in repo_index.files:
if file.language != "Python" or not is_test_path(file.path):
continue
stem = Path(file.path).stem
source_stem = stem.removeprefix("test_").removesuffix("_test")
source_path = source_by_stem.get(source_stem)
if not source_path:
continue
test_node = _file_node_id(file.path)
source_node = _file_node_id(source_path)
edges.append(CodeGraphEdge(test_node, source_node, "tests"))
edges.append(CodeGraphEdge(source_node, test_node, "covered_by"))
related_paths[file.path].add(source_path)
related_paths[source_path].add(file.path)
def _file_node_id(path: str) -> str:
return f"file:{path}"
def _module_names_for_path(path: str) -> set[str]:
without_suffix = path[:-3] if path.endswith(".py") else path
parts = without_suffix.split("/")
names = {parts[-1]}
if parts and parts[0] in {"src", "lib"}:
names.add(".".join(parts[1:]))
names.add(".".join(parts))
return {name for name in names if name}
def _parse_python(text: str) -> ast.AST | None:
try:
return ast.parse(text)
except SyntaxError:
return None
def _symbols_from_ast(parsed: ast.AST) -> set[str]:
symbols: set[str] = set()
for node in ast.walk(parsed):
if isinstance(node, ast.FunctionDef | ast.AsyncFunctionDef | ast.ClassDef):
symbols.add(node.name)
return symbols
def _imports_from_ast(parsed: ast.AST) -> set[str]:
imports: set[str] = set()
for node in ast.walk(parsed):
if isinstance(node, ast.Import):
imports.update(alias.name.split(".", 1)[0] for alias in node.names)
elif isinstance(node, ast.ImportFrom) and node.module:
imports.add(node.module.split(".", 1)[0])
return imports
def _symbols_from_text(text: str) -> set[str]:
return {
match.group(1)
for match in re.finditer(
r"^\s*(?:def|class)\s+([A-Za-z_][A-Za-z0-9_]*)",
text,
re.MULTILINE,
)
}
def _imports_from_text(text: str) -> set[str]:
modules: set[str] = set()
for match in re.finditer(r"^\s*import\s+([A-Za-z_][A-Za-z0-9_.]*)", text, re.MULTILINE):
modules.add(match.group(1).split(".", 1)[0])
for match in re.finditer(
r"^\s*from\s+([A-Za-z_][A-Za-z0-9_.]*)\s+import\s+",
text,
re.MULTILINE,
):
modules.add(match.group(1).split(".", 1)[0])
return modules
def _name_terms(name: str) -> set[str]:
terms: set[str] = set()
for raw_token in TOKEN_RE.findall(name.replace(".", "_")):
terms.update(part.lower() for part in raw_token.split("_") if len(part) > 2)
return terms