|
5e4b8e4…
|
anonymous
|
1 |
""" |
|
5e4b8e4…
|
anonymous
|
2 |
RepoIngester — walks a repository, parses source files with tree-sitter, |
|
5e4b8e4…
|
anonymous
|
3 |
and writes nodes + edges into the GraphStore. |
|
e52aae0…
|
lmata
|
4 |
|
|
e52aae0…
|
lmata
|
5 |
Supported languages (all via tree-sitter): |
|
e52aae0…
|
lmata
|
6 |
Python .py |
|
e52aae0…
|
lmata
|
7 |
TypeScript .ts .tsx |
|
e52aae0…
|
lmata
|
8 |
JavaScript .js .jsx |
|
e52aae0…
|
lmata
|
9 |
Go .go |
|
e52aae0…
|
lmata
|
10 |
Rust .rs |
|
e52aae0…
|
lmata
|
11 |
Java .java |
|
fa82b95…
|
lmata
|
12 |
Kotlin .kt .kts |
|
fa82b95…
|
lmata
|
13 |
C# .cs |
|
fa82b95…
|
lmata
|
14 |
PHP .php |
|
fa82b95…
|
lmata
|
15 |
Ruby .rb |
|
fa82b95…
|
lmata
|
16 |
Swift .swift |
|
fa82b95…
|
lmata
|
17 |
C .c .h |
|
fa82b95…
|
lmata
|
18 |
C++ .cpp .hpp .cc .cxx |
|
b45288f…
|
lmata
|
19 |
|
|
b45288f…
|
lmata
|
20 |
Infrastructure-as-Code: |
|
b45288f…
|
lmata
|
21 |
HCL .tf .hcl (Terraform / OpenTofu) |
|
b45288f…
|
lmata
|
22 |
Puppet .pp |
|
b45288f…
|
lmata
|
23 |
Bash .sh .bash .zsh |
|
b45288f…
|
lmata
|
24 |
Ansible .yml .yaml (detected heuristically, not via extension) |
|
5e4b8e4…
|
anonymous
|
25 |
""" |
|
5e4b8e4…
|
anonymous
|
26 |
|
|
df3d6fa…
|
lmata
|
27 |
import hashlib |
|
5e4b8e4…
|
anonymous
|
28 |
import logging |
|
df3d6fa…
|
lmata
|
29 |
import time |
|
5e4b8e4…
|
anonymous
|
30 |
from pathlib import Path |
|
5e4b8e4…
|
anonymous
|
31 |
|
|
df3d6fa…
|
lmata
|
32 |
from navegador.graph import queries |
|
3c74e66…
|
lmata
|
33 |
from navegador.graph.schema import NodeLabel |
|
5e4b8e4…
|
anonymous
|
34 |
from navegador.graph.store import GraphStore |
|
5e4b8e4…
|
anonymous
|
35 |
|
|
5e4b8e4…
|
anonymous
|
36 |
logger = logging.getLogger(__name__) |
|
5e4b8e4…
|
anonymous
|
37 |
|
|
5e4b8e4…
|
anonymous
|
38 |
# File extensions → language key |
|
5e4b8e4…
|
anonymous
|
39 |
LANGUAGE_MAP: dict[str, str] = { |
|
758c6b7…
|
lmata
|
40 |
".py": "python", |
|
758c6b7…
|
lmata
|
41 |
".ts": "typescript", |
|
758c6b7…
|
lmata
|
42 |
".tsx": "typescript", |
|
758c6b7…
|
lmata
|
43 |
".js": "javascript", |
|
758c6b7…
|
lmata
|
44 |
".jsx": "javascript", |
|
758c6b7…
|
lmata
|
45 |
".go": "go", |
|
758c6b7…
|
lmata
|
46 |
".rs": "rust", |
|
e52aae0…
|
lmata
|
47 |
".java": "java", |
|
fa82b95…
|
lmata
|
48 |
".kt": "kotlin", |
|
fa82b95…
|
lmata
|
49 |
".kts": "kotlin", |
|
fa82b95…
|
lmata
|
50 |
".cs": "csharp", |
|
fa82b95…
|
lmata
|
51 |
".php": "php", |
|
fa82b95…
|
lmata
|
52 |
".rb": "ruby", |
|
fa82b95…
|
lmata
|
53 |
".swift": "swift", |
|
fa82b95…
|
lmata
|
54 |
".c": "c", |
|
fa82b95…
|
lmata
|
55 |
".h": "c", |
|
fa82b95…
|
lmata
|
56 |
".cpp": "cpp", |
|
fa82b95…
|
lmata
|
57 |
".hpp": "cpp", |
|
fa82b95…
|
lmata
|
58 |
".cc": "cpp", |
|
fa82b95…
|
lmata
|
59 |
".cxx": "cpp", |
|
b45288f…
|
lmata
|
60 |
".tf": "hcl", |
|
b45288f…
|
lmata
|
61 |
".hcl": "hcl", |
|
b45288f…
|
lmata
|
62 |
".pp": "puppet", |
|
b45288f…
|
lmata
|
63 |
".sh": "bash", |
|
b45288f…
|
lmata
|
64 |
".bash": "bash", |
|
b45288f…
|
lmata
|
65 |
".zsh": "bash", |
|
5e4b8e4…
|
anonymous
|
66 |
} |
|
5e4b8e4…
|
anonymous
|
67 |
|
|
5e4b8e4…
|
anonymous
|
68 |
|
|
5e4b8e4…
|
anonymous
|
69 |
class RepoIngester: |
|
5e4b8e4…
|
anonymous
|
70 |
""" |
|
5e4b8e4…
|
anonymous
|
71 |
Parses a local code repository and populates a GraphStore. |
|
5e4b8e4…
|
anonymous
|
72 |
|
|
5e4b8e4…
|
anonymous
|
73 |
Usage: |
|
5e4b8e4…
|
anonymous
|
74 |
store = GraphStore.sqlite(".navegador/graph.db") |
|
5e4b8e4…
|
anonymous
|
75 |
ingester = RepoIngester(store) |
|
5e4b8e4…
|
anonymous
|
76 |
stats = ingester.ingest("/path/to/repo") |
|
95549e5…
|
lmata
|
77 |
|
|
95549e5…
|
lmata
|
78 |
Args: |
|
95549e5…
|
lmata
|
79 |
store: The graph store to write nodes and edges into. |
|
95549e5…
|
lmata
|
80 |
redact: When True, file contents are scanned for sensitive patterns |
|
95549e5…
|
lmata
|
81 |
(API keys, passwords, tokens, …) and any matches are replaced |
|
95549e5…
|
lmata
|
82 |
with ``[REDACTED]`` before the content is stored in graph nodes. |
|
5e4b8e4…
|
anonymous
|
83 |
""" |
|
5e4b8e4…
|
anonymous
|
84 |
|
|
95549e5…
|
lmata
|
85 |
def __init__(self, store: GraphStore, redact: bool = False) -> None: |
|
5e4b8e4…
|
anonymous
|
86 |
self.store = store |
|
95549e5…
|
lmata
|
87 |
self.redact = redact |
|
5e4b8e4…
|
anonymous
|
88 |
self._parsers: dict[str, "LanguageParser"] = {} |
|
95549e5…
|
lmata
|
89 |
if redact: |
|
95549e5…
|
lmata
|
90 |
from navegador.security import SensitiveContentDetector |
|
95549e5…
|
lmata
|
91 |
|
|
95549e5…
|
lmata
|
92 |
self._detector = SensitiveContentDetector() |
|
95549e5…
|
lmata
|
93 |
else: |
|
95549e5…
|
lmata
|
94 |
self._detector = None # type: ignore[assignment] |
|
5e4b8e4…
|
anonymous
|
95 |
|
|
df3d6fa…
|
lmata
|
96 |
def ingest( |
|
df3d6fa…
|
lmata
|
97 |
self, |
|
df3d6fa…
|
lmata
|
98 |
repo_path: str | Path, |
|
df3d6fa…
|
lmata
|
99 |
clear: bool = False, |
|
df3d6fa…
|
lmata
|
100 |
incremental: bool = False, |
|
df3d6fa…
|
lmata
|
101 |
) -> dict[str, int]: |
|
5e4b8e4…
|
anonymous
|
102 |
""" |
|
5e4b8e4…
|
anonymous
|
103 |
Ingest a repository into the graph. |
|
5e4b8e4…
|
anonymous
|
104 |
|
|
5e4b8e4…
|
anonymous
|
105 |
Args: |
|
5e4b8e4…
|
anonymous
|
106 |
repo_path: Path to the repository root. |
|
5e4b8e4…
|
anonymous
|
107 |
clear: If True, wipe the graph before ingesting. |
|
df3d6fa…
|
lmata
|
108 |
incremental: If True, skip files whose content hash hasn't changed. |
|
5e4b8e4…
|
anonymous
|
109 |
|
|
5e4b8e4…
|
anonymous
|
110 |
Returns: |
|
df3d6fa…
|
lmata
|
111 |
Dict with counts: files, functions, classes, edges, skipped. |
|
5e4b8e4…
|
anonymous
|
112 |
""" |
|
5e4b8e4…
|
anonymous
|
113 |
repo_path = Path(repo_path).resolve() |
|
5e4b8e4…
|
anonymous
|
114 |
if not repo_path.exists(): |
|
5e4b8e4…
|
anonymous
|
115 |
raise FileNotFoundError(f"Repository not found: {repo_path}") |
|
5e4b8e4…
|
anonymous
|
116 |
|
|
5e4b8e4…
|
anonymous
|
117 |
if clear: |
|
5e4b8e4…
|
anonymous
|
118 |
self.store.clear() |
|
5e4b8e4…
|
anonymous
|
119 |
|
|
5e4b8e4…
|
anonymous
|
120 |
# Create repository node |
|
758c6b7…
|
lmata
|
121 |
self.store.create_node( |
|
758c6b7…
|
lmata
|
122 |
NodeLabel.Repository, |
|
758c6b7…
|
lmata
|
123 |
{ |
|
758c6b7…
|
lmata
|
124 |
"name": repo_path.name, |
|
758c6b7…
|
lmata
|
125 |
"path": str(repo_path), |
|
04dabf9…
|
lmata
|
126 |
"file_path": "", |
|
758c6b7…
|
lmata
|
127 |
}, |
|
758c6b7…
|
lmata
|
128 |
) |
|
5e4b8e4…
|
anonymous
|
129 |
|
|
df3d6fa…
|
lmata
|
130 |
stats: dict[str, int] = { |
|
df3d6fa…
|
lmata
|
131 |
"files": 0, |
|
df3d6fa…
|
lmata
|
132 |
"functions": 0, |
|
df3d6fa…
|
lmata
|
133 |
"classes": 0, |
|
df3d6fa…
|
lmata
|
134 |
"edges": 0, |
|
df3d6fa…
|
lmata
|
135 |
"skipped": 0, |
|
df3d6fa…
|
lmata
|
136 |
} |
|
5e4b8e4…
|
anonymous
|
137 |
|
|
5e4b8e4…
|
anonymous
|
138 |
for source_file in self._iter_source_files(repo_path): |
|
5e4b8e4…
|
anonymous
|
139 |
language = LANGUAGE_MAP.get(source_file.suffix) |
|
5e4b8e4…
|
anonymous
|
140 |
if not language: |
|
5e4b8e4…
|
anonymous
|
141 |
continue |
|
df3d6fa…
|
lmata
|
142 |
|
|
df3d6fa…
|
lmata
|
143 |
rel_path = str(source_file.relative_to(repo_path)) |
|
df3d6fa…
|
lmata
|
144 |
content_hash = _file_hash(source_file) |
|
df3d6fa…
|
lmata
|
145 |
|
|
df3d6fa…
|
lmata
|
146 |
if incremental and self._file_unchanged(rel_path, content_hash): |
|
df3d6fa…
|
lmata
|
147 |
stats["skipped"] += 1 |
|
df3d6fa…
|
lmata
|
148 |
continue |
|
df3d6fa…
|
lmata
|
149 |
|
|
df3d6fa…
|
lmata
|
150 |
if incremental: |
|
df3d6fa…
|
lmata
|
151 |
self._clear_file_subgraph(rel_path) |
|
df3d6fa…
|
lmata
|
152 |
|
|
95549e5…
|
lmata
|
153 |
parse_path, effective_root = self._maybe_redact_to_tmp(source_file, repo_path) |
|
5e4b8e4…
|
anonymous
|
154 |
try: |
|
5e4b8e4…
|
anonymous
|
155 |
parser = self._get_parser(language) |
|
95549e5…
|
lmata
|
156 |
file_stats = parser.parse_file(parse_path, effective_root, self.store) |
|
5e4b8e4…
|
anonymous
|
157 |
stats["files"] += 1 |
|
5e4b8e4…
|
anonymous
|
158 |
stats["functions"] += file_stats.get("functions", 0) |
|
5e4b8e4…
|
anonymous
|
159 |
stats["classes"] += file_stats.get("classes", 0) |
|
5e4b8e4…
|
anonymous
|
160 |
stats["edges"] += file_stats.get("edges", 0) |
|
df3d6fa…
|
lmata
|
161 |
|
|
df3d6fa…
|
lmata
|
162 |
self._store_file_hash(rel_path, content_hash) |
|
5e4b8e4…
|
anonymous
|
163 |
except Exception: |
|
5e4b8e4…
|
anonymous
|
164 |
logger.exception("Failed to parse %s", source_file) |
|
95549e5…
|
lmata
|
165 |
finally: |
|
95549e5…
|
lmata
|
166 |
# Remove the temporary redacted directory if one was created |
|
95549e5…
|
lmata
|
167 |
if effective_root is not repo_path: |
|
95549e5…
|
lmata
|
168 |
import shutil |
|
e5e1a3b…
|
lmata
|
169 |
|
|
95549e5…
|
lmata
|
170 |
shutil.rmtree(effective_root, ignore_errors=True) |
|
b45288f…
|
lmata
|
171 |
|
|
b45288f…
|
lmata
|
172 |
# Ansible pass — heuristically detect and parse Ansible YAML files |
|
b45288f…
|
lmata
|
173 |
self._ingest_ansible(repo_path, stats, incremental) |
|
5e4b8e4…
|
anonymous
|
174 |
|
|
5e4b8e4…
|
anonymous
|
175 |
logger.info( |
|
df3d6fa…
|
lmata
|
176 |
"Ingested %s: %d files, %d functions, %d classes, %d skipped", |
|
758c6b7…
|
lmata
|
177 |
repo_path.name, |
|
758c6b7…
|
lmata
|
178 |
stats["files"], |
|
758c6b7…
|
lmata
|
179 |
stats["functions"], |
|
758c6b7…
|
lmata
|
180 |
stats["classes"], |
|
df3d6fa…
|
lmata
|
181 |
stats["skipped"], |
|
5e4b8e4…
|
anonymous
|
182 |
) |
|
5e4b8e4…
|
anonymous
|
183 |
return stats |
|
df3d6fa…
|
lmata
|
184 |
|
|
df3d6fa…
|
lmata
|
185 |
def watch( |
|
df3d6fa…
|
lmata
|
186 |
self, |
|
df3d6fa…
|
lmata
|
187 |
repo_path: str | Path, |
|
df3d6fa…
|
lmata
|
188 |
interval: float = 2.0, |
|
df3d6fa…
|
lmata
|
189 |
callback=None, |
|
df3d6fa…
|
lmata
|
190 |
) -> None: |
|
df3d6fa…
|
lmata
|
191 |
""" |
|
df3d6fa…
|
lmata
|
192 |
Watch a repo for changes and re-ingest incrementally. |
|
df3d6fa…
|
lmata
|
193 |
|
|
df3d6fa…
|
lmata
|
194 |
Args: |
|
df3d6fa…
|
lmata
|
195 |
repo_path: Path to the repository root. |
|
df3d6fa…
|
lmata
|
196 |
interval: Seconds between polls. |
|
df3d6fa…
|
lmata
|
197 |
callback: Optional callable receiving stats dict after each cycle. |
|
df3d6fa…
|
lmata
|
198 |
If callback returns False, the watch loop stops. |
|
df3d6fa…
|
lmata
|
199 |
""" |
|
df3d6fa…
|
lmata
|
200 |
repo_path = Path(repo_path).resolve() |
|
df3d6fa…
|
lmata
|
201 |
if not repo_path.exists(): |
|
df3d6fa…
|
lmata
|
202 |
raise FileNotFoundError(f"Repository not found: {repo_path}") |
|
df3d6fa…
|
lmata
|
203 |
|
|
df3d6fa…
|
lmata
|
204 |
# Initial full ingest |
|
df3d6fa…
|
lmata
|
205 |
stats = self.ingest(repo_path, incremental=True) |
|
df3d6fa…
|
lmata
|
206 |
if callback and callback(stats) is False: |
|
df3d6fa…
|
lmata
|
207 |
return |
|
df3d6fa…
|
lmata
|
208 |
|
|
df3d6fa…
|
lmata
|
209 |
while True: |
|
df3d6fa…
|
lmata
|
210 |
time.sleep(interval) |
|
df3d6fa…
|
lmata
|
211 |
stats = self.ingest(repo_path, incremental=True) |
|
df3d6fa…
|
lmata
|
212 |
if callback and callback(stats) is False: |
|
df3d6fa…
|
lmata
|
213 |
return |
|
df3d6fa…
|
lmata
|
214 |
|
|
df3d6fa…
|
lmata
|
215 |
def _file_unchanged(self, rel_path: str, content_hash: str) -> bool: |
|
df3d6fa…
|
lmata
|
216 |
result = self.store.query(queries.FILE_HASH, {"path": rel_path}) |
|
df3d6fa…
|
lmata
|
217 |
rows = result.result_set or [] |
|
df3d6fa…
|
lmata
|
218 |
if not rows or rows[0][0] is None: |
|
df3d6fa…
|
lmata
|
219 |
return False |
|
df3d6fa…
|
lmata
|
220 |
return rows[0][0] == content_hash |
|
df3d6fa…
|
lmata
|
221 |
|
|
df3d6fa…
|
lmata
|
222 |
def _clear_file_subgraph(self, rel_path: str) -> None: |
|
df3d6fa…
|
lmata
|
223 |
self.store.query(queries.DELETE_FILE_SUBGRAPH, {"path": rel_path}) |
|
df3d6fa…
|
lmata
|
224 |
|
|
df3d6fa…
|
lmata
|
225 |
def _store_file_hash(self, rel_path: str, content_hash: str) -> None: |
|
df3d6fa…
|
lmata
|
226 |
self.store.query( |
|
df3d6fa…
|
lmata
|
227 |
"MATCH (f:File {path: $path}) SET f.content_hash = $hash", |
|
df3d6fa…
|
lmata
|
228 |
{"path": rel_path, "hash": content_hash}, |
|
df3d6fa…
|
lmata
|
229 |
) |
|
df3d6fa…
|
lmata
|
230 |
|
|
95549e5…
|
lmata
|
231 |
def _maybe_redact_to_tmp(self, source_file: Path, repo_root: Path) -> tuple[Path, Path]: |
|
95549e5…
|
lmata
|
232 |
""" |
|
95549e5…
|
lmata
|
233 |
If redaction is enabled, return a *(parse_path, effective_repo_root)* |
|
95549e5…
|
lmata
|
234 |
tuple where *parse_path* can be passed to ``parser.parse_file`` and |
|
95549e5…
|
lmata
|
235 |
``parse_path.relative_to(effective_repo_root)`` still yields the |
|
95549e5…
|
lmata
|
236 |
correct relative path for graph node naming. |
|
95549e5…
|
lmata
|
237 |
|
|
95549e5…
|
lmata
|
238 |
When redaction is disabled or the file has no sensitive content, both |
|
95549e5…
|
lmata
|
239 |
returned values are the originals unchanged. |
|
95549e5…
|
lmata
|
240 |
|
|
95549e5…
|
lmata
|
241 |
The caller is responsible for deleting the temp directory when it is |
|
95549e5…
|
lmata
|
242 |
no longer needed. |
|
95549e5…
|
lmata
|
243 |
""" |
|
95549e5…
|
lmata
|
244 |
if not self.redact or self._detector is None: |
|
95549e5…
|
lmata
|
245 |
return source_file, repo_root |
|
95549e5…
|
lmata
|
246 |
|
|
95549e5…
|
lmata
|
247 |
try: |
|
95549e5…
|
lmata
|
248 |
original = source_file.read_text(encoding="utf-8", errors="replace") |
|
95549e5…
|
lmata
|
249 |
except OSError: |
|
95549e5…
|
lmata
|
250 |
return source_file, repo_root |
|
95549e5…
|
lmata
|
251 |
|
|
95549e5…
|
lmata
|
252 |
redacted = self._detector.redact(original) |
|
95549e5…
|
lmata
|
253 |
if redacted == original: |
|
95549e5…
|
lmata
|
254 |
return source_file, repo_root |
|
95549e5…
|
lmata
|
255 |
|
|
95549e5…
|
lmata
|
256 |
# Mirror the file at the same relative path inside a temp directory so |
|
95549e5…
|
lmata
|
257 |
# that parse_path.relative_to(tmp_root) == source_file.relative_to(repo_root). |
|
95549e5…
|
lmata
|
258 |
import tempfile |
|
95549e5…
|
lmata
|
259 |
|
|
95549e5…
|
lmata
|
260 |
rel = source_file.relative_to(repo_root) |
|
95549e5…
|
lmata
|
261 |
tmp_root = Path(tempfile.mkdtemp()) |
|
95549e5…
|
lmata
|
262 |
tmp_file = tmp_root / rel |
|
95549e5…
|
lmata
|
263 |
tmp_file.parent.mkdir(parents=True, exist_ok=True) |
|
95549e5…
|
lmata
|
264 |
tmp_file.write_text(redacted, encoding="utf-8") |
|
95549e5…
|
lmata
|
265 |
return tmp_file, tmp_root |
|
95549e5…
|
lmata
|
266 |
|
|
5e4b8e4…
|
anonymous
|
267 |
def _iter_source_files(self, repo_path: Path): |
|
3c74e66…
|
lmata
|
268 |
skip_dirs = { |
|
758c6b7…
|
lmata
|
269 |
".git", |
|
758c6b7…
|
lmata
|
270 |
".venv", |
|
758c6b7…
|
lmata
|
271 |
"venv", |
|
758c6b7…
|
lmata
|
272 |
"node_modules", |
|
758c6b7…
|
lmata
|
273 |
"__pycache__", |
|
758c6b7…
|
lmata
|
274 |
"dist", |
|
758c6b7…
|
lmata
|
275 |
"build", |
|
758c6b7…
|
lmata
|
276 |
".next", |
|
758c6b7…
|
lmata
|
277 |
"target", # Rust / Java (Maven/Gradle) |
|
758c6b7…
|
lmata
|
278 |
"vendor", # Go modules cache |
|
758c6b7…
|
lmata
|
279 |
".gradle", # Gradle cache |
|
3c74e66…
|
lmata
|
280 |
} |
|
5e4b8e4…
|
anonymous
|
281 |
for path in repo_path.rglob("*"): |
|
5e4b8e4…
|
anonymous
|
282 |
if path.is_file() and path.suffix in LANGUAGE_MAP: |
|
5e4b8e4…
|
anonymous
|
283 |
if not any(part in skip_dirs for part in path.parts): |
|
5e4b8e4…
|
anonymous
|
284 |
yield path |
|
95549e5…
|
lmata
|
285 |
|
|
b45288f…
|
lmata
|
286 |
def _ingest_ansible(self, repo_path: Path, stats: dict[str, int], incremental: bool) -> None: |
|
b45288f…
|
lmata
|
287 |
"""Detect and parse Ansible YAML files (playbooks, roles, tasks).""" |
|
b45288f…
|
lmata
|
288 |
from navegador.ingestion.ansible import AnsibleParser |
|
b45288f…
|
lmata
|
289 |
|
|
b45288f…
|
lmata
|
290 |
is_ansible_file = AnsibleParser.is_ansible_file |
|
b45288f…
|
lmata
|
291 |
|
|
b45288f…
|
lmata
|
292 |
ansible_parser: AnsibleParser | None = None |
|
b45288f…
|
lmata
|
293 |
|
|
b45288f…
|
lmata
|
294 |
for path in repo_path.rglob("*.yml"): |
|
b45288f…
|
lmata
|
295 |
if not path.is_file(): |
|
b45288f…
|
lmata
|
296 |
continue |
|
b45288f…
|
lmata
|
297 |
if any(part in (".git", ".venv", "venv", "node_modules") for part in path.parts): |
|
b45288f…
|
lmata
|
298 |
continue |
|
b45288f…
|
lmata
|
299 |
if not is_ansible_file(path, repo_path): |
|
b45288f…
|
lmata
|
300 |
continue |
|
b45288f…
|
lmata
|
301 |
|
|
b45288f…
|
lmata
|
302 |
rel_path = str(path.relative_to(repo_path)) |
|
b45288f…
|
lmata
|
303 |
content_hash = _file_hash(path) |
|
b45288f…
|
lmata
|
304 |
|
|
b45288f…
|
lmata
|
305 |
if incremental and self._file_unchanged(rel_path, content_hash): |
|
b45288f…
|
lmata
|
306 |
stats["skipped"] += 1 |
|
b45288f…
|
lmata
|
307 |
continue |
|
b45288f…
|
lmata
|
308 |
|
|
b45288f…
|
lmata
|
309 |
if incremental: |
|
b45288f…
|
lmata
|
310 |
self._clear_file_subgraph(rel_path) |
|
b45288f…
|
lmata
|
311 |
|
|
b45288f…
|
lmata
|
312 |
if ansible_parser is None: |
|
b45288f…
|
lmata
|
313 |
ansible_parser = AnsibleParser() |
|
b45288f…
|
lmata
|
314 |
try: |
|
b45288f…
|
lmata
|
315 |
file_stats = ansible_parser.parse_file(path, repo_path, self.store) |
|
b45288f…
|
lmata
|
316 |
stats["files"] += 1 |
|
b45288f…
|
lmata
|
317 |
stats["functions"] += file_stats.get("functions", 0) |
|
b45288f…
|
lmata
|
318 |
stats["classes"] += file_stats.get("classes", 0) |
|
b45288f…
|
lmata
|
319 |
stats["edges"] += file_stats.get("edges", 0) |
|
b45288f…
|
lmata
|
320 |
self._store_file_hash(rel_path, content_hash) |
|
b45288f…
|
lmata
|
321 |
except Exception: |
|
b45288f…
|
lmata
|
322 |
logger.exception("Failed to parse Ansible file %s", path) |
|
b45288f…
|
lmata
|
323 |
|
|
b45288f…
|
lmata
|
324 |
# Also check .yaml extension |
|
b45288f…
|
lmata
|
325 |
for path in repo_path.rglob("*.yaml"): |
|
b45288f…
|
lmata
|
326 |
if not path.is_file(): |
|
b45288f…
|
lmata
|
327 |
continue |
|
b45288f…
|
lmata
|
328 |
if any(part in (".git", ".venv", "venv", "node_modules") for part in path.parts): |
|
b45288f…
|
lmata
|
329 |
continue |
|
b45288f…
|
lmata
|
330 |
if not is_ansible_file(path, repo_path): |
|
b45288f…
|
lmata
|
331 |
continue |
|
b45288f…
|
lmata
|
332 |
|
|
b45288f…
|
lmata
|
333 |
rel_path = str(path.relative_to(repo_path)) |
|
b45288f…
|
lmata
|
334 |
content_hash = _file_hash(path) |
|
b45288f…
|
lmata
|
335 |
|
|
b45288f…
|
lmata
|
336 |
if incremental and self._file_unchanged(rel_path, content_hash): |
|
b45288f…
|
lmata
|
337 |
stats["skipped"] += 1 |
|
b45288f…
|
lmata
|
338 |
continue |
|
b45288f…
|
lmata
|
339 |
|
|
b45288f…
|
lmata
|
340 |
if incremental: |
|
b45288f…
|
lmata
|
341 |
self._clear_file_subgraph(rel_path) |
|
b45288f…
|
lmata
|
342 |
|
|
b45288f…
|
lmata
|
343 |
if ansible_parser is None: |
|
b45288f…
|
lmata
|
344 |
ansible_parser = AnsibleParser() |
|
b45288f…
|
lmata
|
345 |
try: |
|
b45288f…
|
lmata
|
346 |
file_stats = ansible_parser.parse_file(path, repo_path, self.store) |
|
b45288f…
|
lmata
|
347 |
stats["files"] += 1 |
|
b45288f…
|
lmata
|
348 |
stats["functions"] += file_stats.get("functions", 0) |
|
b45288f…
|
lmata
|
349 |
stats["classes"] += file_stats.get("classes", 0) |
|
b45288f…
|
lmata
|
350 |
stats["edges"] += file_stats.get("edges", 0) |
|
b45288f…
|
lmata
|
351 |
self._store_file_hash(rel_path, content_hash) |
|
b45288f…
|
lmata
|
352 |
except Exception: |
|
b45288f…
|
lmata
|
353 |
logger.exception("Failed to parse Ansible file %s", path) |
|
b45288f…
|
lmata
|
354 |
|
|
5e4b8e4…
|
anonymous
|
355 |
def _get_parser(self, language: str) -> "LanguageParser": |
|
5e4b8e4…
|
anonymous
|
356 |
if language not in self._parsers: |
|
5e4b8e4…
|
anonymous
|
357 |
if language == "python": |
|
5e4b8e4…
|
anonymous
|
358 |
from navegador.ingestion.python import PythonParser |
|
758c6b7…
|
lmata
|
359 |
|
|
5e4b8e4…
|
anonymous
|
360 |
self._parsers[language] = PythonParser() |
|
5e4b8e4…
|
anonymous
|
361 |
elif language in ("typescript", "javascript"): |
|
5e4b8e4…
|
anonymous
|
362 |
from navegador.ingestion.typescript import TypeScriptParser |
|
758c6b7…
|
lmata
|
363 |
|
|
5e4b8e4…
|
anonymous
|
364 |
self._parsers[language] = TypeScriptParser(language) |
|
e52aae0…
|
lmata
|
365 |
elif language == "go": |
|
e52aae0…
|
lmata
|
366 |
from navegador.ingestion.go import GoParser |
|
758c6b7…
|
lmata
|
367 |
|
|
e52aae0…
|
lmata
|
368 |
self._parsers[language] = GoParser() |
|
e52aae0…
|
lmata
|
369 |
elif language == "rust": |
|
e52aae0…
|
lmata
|
370 |
from navegador.ingestion.rust import RustParser |
|
758c6b7…
|
lmata
|
371 |
|
|
e52aae0…
|
lmata
|
372 |
self._parsers[language] = RustParser() |
|
e52aae0…
|
lmata
|
373 |
elif language == "java": |
|
e52aae0…
|
lmata
|
374 |
from navegador.ingestion.java import JavaParser |
|
758c6b7…
|
lmata
|
375 |
|
|
e52aae0…
|
lmata
|
376 |
self._parsers[language] = JavaParser() |
|
fa82b95…
|
lmata
|
377 |
elif language == "kotlin": |
|
fa82b95…
|
lmata
|
378 |
from navegador.ingestion.kotlin import KotlinParser |
|
fa82b95…
|
lmata
|
379 |
|
|
fa82b95…
|
lmata
|
380 |
self._parsers[language] = KotlinParser() |
|
fa82b95…
|
lmata
|
381 |
elif language == "csharp": |
|
fa82b95…
|
lmata
|
382 |
from navegador.ingestion.csharp import CSharpParser |
|
fa82b95…
|
lmata
|
383 |
|
|
fa82b95…
|
lmata
|
384 |
self._parsers[language] = CSharpParser() |
|
fa82b95…
|
lmata
|
385 |
elif language == "php": |
|
fa82b95…
|
lmata
|
386 |
from navegador.ingestion.php import PHPParser |
|
fa82b95…
|
lmata
|
387 |
|
|
fa82b95…
|
lmata
|
388 |
self._parsers[language] = PHPParser() |
|
fa82b95…
|
lmata
|
389 |
elif language == "ruby": |
|
fa82b95…
|
lmata
|
390 |
from navegador.ingestion.ruby import RubyParser |
|
fa82b95…
|
lmata
|
391 |
|
|
fa82b95…
|
lmata
|
392 |
self._parsers[language] = RubyParser() |
|
fa82b95…
|
lmata
|
393 |
elif language == "swift": |
|
fa82b95…
|
lmata
|
394 |
from navegador.ingestion.swift import SwiftParser |
|
fa82b95…
|
lmata
|
395 |
|
|
fa82b95…
|
lmata
|
396 |
self._parsers[language] = SwiftParser() |
|
fa82b95…
|
lmata
|
397 |
elif language == "c": |
|
fa82b95…
|
lmata
|
398 |
from navegador.ingestion.c import CParser |
|
fa82b95…
|
lmata
|
399 |
|
|
fa82b95…
|
lmata
|
400 |
self._parsers[language] = CParser() |
|
fa82b95…
|
lmata
|
401 |
elif language == "cpp": |
|
fa82b95…
|
lmata
|
402 |
from navegador.ingestion.cpp import CppParser |
|
fa82b95…
|
lmata
|
403 |
|
|
fa82b95…
|
lmata
|
404 |
self._parsers[language] = CppParser() |
|
b45288f…
|
lmata
|
405 |
elif language == "hcl": |
|
b45288f…
|
lmata
|
406 |
from navegador.ingestion.hcl import HCLParser |
|
b45288f…
|
lmata
|
407 |
|
|
b45288f…
|
lmata
|
408 |
self._parsers[language] = HCLParser() |
|
b45288f…
|
lmata
|
409 |
elif language == "puppet": |
|
b45288f…
|
lmata
|
410 |
from navegador.ingestion.puppet import PuppetParser |
|
b45288f…
|
lmata
|
411 |
|
|
b45288f…
|
lmata
|
412 |
self._parsers[language] = PuppetParser() |
|
b45288f…
|
lmata
|
413 |
elif language == "bash": |
|
b45288f…
|
lmata
|
414 |
from navegador.ingestion.bash import BashParser |
|
b45288f…
|
lmata
|
415 |
|
|
b45288f…
|
lmata
|
416 |
self._parsers[language] = BashParser() |
|
5e4b8e4…
|
anonymous
|
417 |
else: |
|
5e4b8e4…
|
anonymous
|
418 |
raise ValueError(f"Unsupported language: {language}") |
|
5e4b8e4…
|
anonymous
|
419 |
return self._parsers[language] |
|
df3d6fa…
|
lmata
|
420 |
|
|
df3d6fa…
|
lmata
|
421 |
|
|
df3d6fa…
|
lmata
|
422 |
def _file_hash(path: Path) -> str: |
|
df3d6fa…
|
lmata
|
423 |
"""SHA-256 content hash for a file.""" |
|
df3d6fa…
|
lmata
|
424 |
h = hashlib.sha256() |
|
df3d6fa…
|
lmata
|
425 |
h.update(path.read_bytes()) |
|
df3d6fa…
|
lmata
|
426 |
return h.hexdigest() |
|
5e4b8e4…
|
anonymous
|
427 |
|
|
5e4b8e4…
|
anonymous
|
428 |
|
|
5e4b8e4…
|
anonymous
|
429 |
class LanguageParser: |
|
5e4b8e4…
|
anonymous
|
430 |
"""Base class for language-specific AST parsers.""" |
|
5e4b8e4…
|
anonymous
|
431 |
|
|
5e4b8e4…
|
anonymous
|
432 |
def parse_file(self, path: Path, repo_root: Path, store: GraphStore) -> dict[str, int]: |
|
5e4b8e4…
|
anonymous
|
433 |
raise NotImplementedError |