-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_crawler.py
More file actions
652 lines (529 loc) · 22.7 KB
/
project_crawler.py
File metadata and controls
652 lines (529 loc) · 22.7 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
"""
HCE Phase 3, Component 1: Project Crawler.
Walks a codebase and populates an EntityGraph with FILE nodes, FUNCTION
nodes (functions, classes, structs, traits, …), and IMPORTS / CALLS /
PART_OF edges.
Supported languages:
Python (.py) — full AST parsing via ``ast`` module
Java (.java) — regex-based
JavaScript/TypeScript (.js, .jsx, .ts, .tsx) — regex-based
Go (.go) — regex-based
Rust (.rs) — regex-based
C/C++ (.c, .h, .cpp, .hpp, .cc) — regex-based
Ruby (.rb) — regex-based
"""
from __future__ import annotations
import ast
import logging
import re
import sys
from dataclasses import dataclass, field
from fnmatch import fnmatch
from pathlib import Path
from typing import Any, Callable
from hce_core import EdgeType, EntityGraph, NodeType
log = logging.getLogger(__name__)
# ── Parse Result (common intermediate representation) ─────────────────────
@dataclass
class ParseResult:
"""Language-agnostic output produced by every parser.
* **definitions** — ``(name, lineno, kind)`` tuples for each
class / function / struct / trait / … found in the file.
* **imports** — module or package names that the file imports.
* **calls** — ``(func_node_id, [called_names])`` pairs so that
the second-pass call resolver can wire CALLS edges.
"""
definitions: list[tuple[str, int, str]] = field(default_factory=list)
imports: list[str] = field(default_factory=list)
calls: list[tuple[str, list[str]]] = field(default_factory=list)
# ── Python parser (AST) ──────────────────────────────────────────────────
def _parse_python(source: str, file_id: str) -> ParseResult:
"""Parse a Python source file using the ``ast`` module."""
try:
tree = ast.parse(source, filename=file_id)
except SyntaxError as exc:
log.warning("Syntax error in %s: %s", file_id, exc)
return ParseResult()
result = ParseResult()
result.imports = _extract_imports(tree)
for node in ast.walk(tree):
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):
name = node.name
node_id = f"{file_id}::{name}"
if isinstance(node, ast.AsyncFunctionDef):
kind = "async_function"
elif isinstance(node, ast.ClassDef):
kind = "class"
else:
kind = "function"
result.definitions.append((name, node.lineno, kind))
# Collect call names inside this definition's body.
calls: list[str] = []
for child in ast.walk(node):
if isinstance(child, ast.Call):
called = _resolve_call_name(child)
if called:
calls.append(called)
result.calls.append((node_id, calls))
return result
def _resolve_call_name(call_node: ast.Call) -> str | None:
"""Best-effort extraction of the called function name from an
``ast.Call`` node. Returns the simple name for ``foo()`` and the
attribute name for ``obj.foo()``; returns ``None`` for anything more
complex."""
func = call_node.func
if isinstance(func, ast.Name):
return func.id
if isinstance(func, ast.Attribute):
return func.attr
return None
def _extract_imports(tree: ast.Module) -> list[str]:
"""Return a list of top-level module names imported by *tree*."""
modules: list[str] = []
for node in ast.iter_child_nodes(tree):
if isinstance(node, ast.Import):
for alias in node.names:
modules.append(alias.name)
elif isinstance(node, ast.ImportFrom):
if node.module:
modules.append(node.module)
return modules
# ── Java parser (regex) ──────────────────────────────────────────────────
_JAVA_DEF_RE = re.compile(
r"^\s*(?:public|protected|private|static|abstract|final|synchronized|native|strictfp|\s)*"
r"(?:class|interface|enum|record)\s+(\w+)",
re.MULTILINE,
)
_JAVA_METHOD_RE = re.compile(
r"^\s*(?:public|protected|private|static|abstract|final|synchronized|native|\s)*"
r"(?:<[^>]+>\s+)?" # optional generic type params
r"[\w\[\]<>,\s]+\s+(\w+)\s*\(", # return-type name(
re.MULTILINE,
)
_JAVA_IMPORT_RE = re.compile(r"^\s*import\s+(?:static\s+)?([\w.]+);", re.MULTILINE)
def _parse_java(source: str, file_id: str) -> ParseResult:
result = ParseResult()
for m in _JAVA_DEF_RE.finditer(source):
name = m.group(1)
lineno = source[: m.start()].count("\n") + 1
# Determine kind from matched text
text = m.group(0)
if "interface" in text:
kind = "interface"
elif "enum" in text:
kind = "enum"
elif "record" in text:
kind = "record"
else:
kind = "class"
result.definitions.append((name, lineno, kind))
for m in _JAVA_METHOD_RE.finditer(source):
name = m.group(1)
# Skip false positives that are actually class/interface/enum keywords
if name in ("class", "interface", "enum", "record", "if", "for",
"while", "switch", "catch", "return", "new", "throw"):
continue
lineno = source[: m.start()].count("\n") + 1
result.definitions.append((name, lineno, "method"))
for m in _JAVA_IMPORT_RE.finditer(source):
result.imports.append(m.group(1))
return result
# ── JavaScript / TypeScript parser (regex) ───────────────────────────────
_JS_FUNC_RE = re.compile(
r"^\s*(?:export\s+)?(?:default\s+)?(?:async\s+)?function\s+(\w+)",
re.MULTILINE,
)
_JS_CLASS_RE = re.compile(
r"^\s*(?:export\s+)?(?:default\s+)?(?:abstract\s+)?class\s+(\w+)",
re.MULTILINE,
)
_JS_ARROW_RE = re.compile(
r"^\s*(?:export\s+)?(?:const|let|var)\s+(\w+)\s*=\s*(?:async\s+)?(?:\([^)]*\)|[a-zA-Z_]\w*)\s*=>",
re.MULTILINE,
)
_JS_TS_IFACE_RE = re.compile(
r"^\s*(?:export\s+)?(?:interface|type)\s+(\w+)",
re.MULTILINE,
)
_JS_IMPORT_FROM_RE = re.compile(
r"""(?:import\s+.*?\s+from\s+['"]([^'"]+)['"]|require\s*\(\s*['"]([^'"]+)['"]\s*\))""",
re.MULTILINE,
)
def _parse_javascript(source: str, file_id: str) -> ParseResult:
result = ParseResult()
for m in _JS_FUNC_RE.finditer(source):
lineno = source[: m.start()].count("\n") + 1
kind = "async_function" if "async" in m.group(0) else "function"
result.definitions.append((m.group(1), lineno, kind))
for m in _JS_CLASS_RE.finditer(source):
lineno = source[: m.start()].count("\n") + 1
result.definitions.append((m.group(1), lineno, "class"))
for m in _JS_ARROW_RE.finditer(source):
lineno = source[: m.start()].count("\n") + 1
kind = "async_function" if "async" in m.group(0) else "function"
result.definitions.append((m.group(1), lineno, kind))
for m in _JS_TS_IFACE_RE.finditer(source):
lineno = source[: m.start()].count("\n") + 1
text = m.group(0)
kind = "interface" if "interface" in text else "type"
result.definitions.append((m.group(1), lineno, kind))
for m in _JS_IMPORT_FROM_RE.finditer(source):
module = m.group(1) or m.group(2)
result.imports.append(module)
return result
# ── Go parser (regex) ────────────────────────────────────────────────────
_GO_FUNC_RE = re.compile(
r"^func\s+(?:\(\s*\w+\s+\*?\w+\s*\)\s+)?(\w+)\s*\(",
re.MULTILINE,
)
_GO_TYPE_RE = re.compile(
r"^type\s+(\w+)\s+(struct|interface)\b",
re.MULTILINE,
)
_GO_IMPORT_SINGLE_RE = re.compile(
r"""^\s*import\s+"([^"]+)"\s*$""",
re.MULTILINE,
)
_GO_IMPORT_BLOCK_RE = re.compile(
r"import\s*\((.*?)\)",
re.DOTALL,
)
_GO_IMPORT_LINE_RE = re.compile(r'"([^"]+)"')
def _parse_go(source: str, file_id: str) -> ParseResult:
result = ParseResult()
for m in _GO_FUNC_RE.finditer(source):
lineno = source[: m.start()].count("\n") + 1
result.definitions.append((m.group(1), lineno, "function"))
for m in _GO_TYPE_RE.finditer(source):
lineno = source[: m.start()].count("\n") + 1
result.definitions.append((m.group(1), lineno, m.group(2)))
for m in _GO_IMPORT_SINGLE_RE.finditer(source):
result.imports.append(m.group(1))
for m in _GO_IMPORT_BLOCK_RE.finditer(source):
for line_m in _GO_IMPORT_LINE_RE.finditer(m.group(1)):
result.imports.append(line_m.group(1))
return result
# ── Rust parser (regex) ──────────────────────────────────────────────────
_RUST_FN_RE = re.compile(
r"^\s*(?:pub(?:\s*\([^)]*\))?\s+)?(?:async\s+)?(?:unsafe\s+)?fn\s+(\w+)",
re.MULTILINE,
)
_RUST_STRUCT_RE = re.compile(
r"^\s*(?:pub(?:\s*\([^)]*\))?\s+)?struct\s+(\w+)",
re.MULTILINE,
)
_RUST_TRAIT_RE = re.compile(
r"^\s*(?:pub(?:\s*\([^)]*\))?\s+)?trait\s+(\w+)",
re.MULTILINE,
)
_RUST_IMPL_RE = re.compile(
r"^\s*impl(?:\s*<[^>]*>)?\s+(\w+)",
re.MULTILINE,
)
_RUST_ENUM_RE = re.compile(
r"^\s*(?:pub(?:\s*\([^)]*\))?\s+)?enum\s+(\w+)",
re.MULTILINE,
)
_RUST_USE_RE = re.compile(
r"^\s*(?:pub\s+)?use\s+([\w:]+)",
re.MULTILINE,
)
def _parse_rust(source: str, file_id: str) -> ParseResult:
result = ParseResult()
for m in _RUST_FN_RE.finditer(source):
lineno = source[: m.start()].count("\n") + 1
kind = "async_function" if "async" in m.group(0) else "function"
result.definitions.append((m.group(1), lineno, kind))
for m in _RUST_STRUCT_RE.finditer(source):
lineno = source[: m.start()].count("\n") + 1
result.definitions.append((m.group(1), lineno, "struct"))
for m in _RUST_TRAIT_RE.finditer(source):
lineno = source[: m.start()].count("\n") + 1
result.definitions.append((m.group(1), lineno, "trait"))
for m in _RUST_IMPL_RE.finditer(source):
lineno = source[: m.start()].count("\n") + 1
result.definitions.append((m.group(1), lineno, "impl"))
for m in _RUST_ENUM_RE.finditer(source):
lineno = source[: m.start()].count("\n") + 1
result.definitions.append((m.group(1), lineno, "enum"))
for m in _RUST_USE_RE.finditer(source):
# "std::collections::HashMap" -> "std::collections"
path = m.group(1)
parts = path.split("::")
if len(parts) > 1:
result.imports.append("::".join(parts[:-1]))
else:
result.imports.append(path)
return result
# ── C/C++ parser (regex) ────────────────────────────────────────────────
_C_FUNC_RE = re.compile(
r"^(?!.*\b(?:if|for|while|switch|return|else|catch|throw)\b)"
r"[\w\s\*&:<>,]+?\s+(\w+)\s*\([^;]*\)\s*\{",
re.MULTILINE,
)
_C_CLASS_RE = re.compile(
r"^\s*(?:class|struct)\s+(\w+)",
re.MULTILINE,
)
_C_NAMESPACE_RE = re.compile(
r"^\s*namespace\s+(\w+)",
re.MULTILINE,
)
_C_INCLUDE_RE = re.compile(
r"""^\s*#include\s+[<"]([^>"]+)[>"]""",
re.MULTILINE,
)
def _parse_c_cpp(source: str, file_id: str) -> ParseResult:
result = ParseResult()
for m in _C_FUNC_RE.finditer(source):
name = m.group(1)
# Skip common false positives
if name in ("if", "for", "while", "switch", "return", "else",
"catch", "throw", "main", "class", "struct",
"namespace", "typedef", "enum", "union"):
continue
lineno = source[: m.start()].count("\n") + 1
result.definitions.append((name, lineno, "function"))
for m in _C_CLASS_RE.finditer(source):
lineno = source[: m.start()].count("\n") + 1
text = m.group(0)
kind = "class" if "class" in text else "struct"
result.definitions.append((m.group(1), lineno, kind))
for m in _C_NAMESPACE_RE.finditer(source):
lineno = source[: m.start()].count("\n") + 1
result.definitions.append((m.group(1), lineno, "namespace"))
for m in _C_INCLUDE_RE.finditer(source):
result.imports.append(m.group(1))
return result
# ── Ruby parser (regex) ─────────────────────────────────────────────────
_RUBY_DEF_RE = re.compile(
r"^\s*def\s+(self\.)?(\w+[!?=]?)",
re.MULTILINE,
)
_RUBY_CLASS_RE = re.compile(
r"^\s*class\s+(\w+)",
re.MULTILINE,
)
_RUBY_MODULE_RE = re.compile(
r"^\s*module\s+(\w+)",
re.MULTILINE,
)
_RUBY_REQUIRE_RE = re.compile(
r"""^\s*require(?:_relative)?\s+['"]([^'"]+)['"]""",
re.MULTILINE,
)
def _parse_ruby(source: str, file_id: str) -> ParseResult:
result = ParseResult()
for m in _RUBY_DEF_RE.finditer(source):
lineno = source[: m.start()].count("\n") + 1
name = m.group(2)
kind = "class_method" if m.group(1) else "method"
result.definitions.append((name, lineno, kind))
for m in _RUBY_CLASS_RE.finditer(source):
lineno = source[: m.start()].count("\n") + 1
result.definitions.append((m.group(1), lineno, "class"))
for m in _RUBY_MODULE_RE.finditer(source):
lineno = source[: m.start()].count("\n") + 1
result.definitions.append((m.group(1), lineno, "module"))
for m in _RUBY_REQUIRE_RE.finditer(source):
result.imports.append(m.group(1))
return result
# ── Language routing ─────────────────────────────────────────────────────
_LANGUAGE_MAP: dict[str, Callable[[str, str], ParseResult]] = {
".py": _parse_python,
".java": _parse_java,
".js": _parse_javascript,
".jsx": _parse_javascript,
".ts": _parse_javascript,
".tsx": _parse_javascript,
".go": _parse_go,
".rs": _parse_rust,
".c": _parse_c_cpp,
".h": _parse_c_cpp,
".cpp": _parse_c_cpp,
".hpp": _parse_c_cpp,
".cc": _parse_c_cpp,
".rb": _parse_ruby,
}
_ALL_EXTENSIONS = sorted(_LANGUAGE_MAP.keys())
# Known extensions for stripping in import resolution.
_KNOWN_EXTENSIONS = set(_LANGUAGE_MAP.keys())
# ── Configuration ────────────────────────────────────────────────────────
@dataclass
class CrawlConfig:
"""Controls which files/directories the crawler visits."""
extensions: list[str] = field(default_factory=lambda: list(_ALL_EXTENSIONS))
ignore_patterns: list[str] = field(
default_factory=lambda: [
"__pycache__",
".git",
".venv",
"node_modules",
".pytest_cache",
"target",
"build",
"dist",
"vendor",
]
)
# ── Public API ───────────────────────────────────────────────────────────
def crawl_project(
root_path: str | Path,
config: CrawlConfig | None = None,
) -> EntityGraph:
"""Walk *root_path*, parse every matching source file, and return a
populated :class:`EntityGraph` containing FILE, FUNCTION, and CONCEPT
nodes together with PART_OF, IMPORTS, and CALLS edges.
Supports Python, Java, JS/TS, Go, Rust, C/C++, and Ruby.
"""
config = config or CrawlConfig()
root = Path(root_path).resolve()
graph = EntityGraph()
# Collect all matching source files.
source_files = _collect_files(root, config)
log.info("Crawling %s: found %d source files", root, len(source_files))
# First pass: create FILE and FUNCTION nodes + PART_OF edges, and
# record raw import/call data for the second pass.
file_imports: dict[str, list[str]] = {} # rel_path -> [module_name, ...]
func_calls: dict[str, list[str]] = {} # func_node_id -> [called_name, ...]
for src in source_files:
rel = src.relative_to(root)
rel_id = rel.as_posix() # forward-slash node_id
# FILE node -------------------------------------------------------
try:
source_text = src.read_text(encoding="utf-8")
except (OSError, UnicodeDecodeError) as exc:
log.warning("Could not read %s: %s", src, exc)
continue
line_count = source_text.count("\n") + (1 if source_text else 0)
graph.add_node(
node_id=rel_id,
node_type=NodeType.FILE,
label=src.name,
metadata={"path": str(rel), "lines": line_count},
)
# Dispatch to language-specific parser ----------------------------
ext = src.suffix.lower()
parser = _LANGUAGE_MAP.get(ext)
if parser is None:
log.debug("No parser for extension %s (%s), skipping parse", ext, src)
continue
parse_result = parser(source_text, rel_id)
# Create FUNCTION nodes + PART_OF edges from definitions ----------
for name, lineno, kind in parse_result.definitions:
node_id = f"{rel_id}::{name}"
graph.add_node(
node_id=node_id,
node_type=NodeType.FUNCTION,
label=name,
metadata={"lineno": lineno, "type": kind},
)
graph.add_edge(node_id, rel_id, EdgeType.PART_OF)
# Record imports for second pass ----------------------------------
file_imports[rel_id] = parse_result.imports
# Record calls for second pass ------------------------------------
for caller_id, called_names in parse_result.calls:
func_calls[caller_id] = called_names
# Second pass: resolve imports and calls against known nodes.
_resolve_imports(file_imports, graph, root)
_resolve_calls(func_calls, graph)
log.info("Crawl complete: %d nodes, %d edges", graph.node_count, graph.edge_count)
return graph
# ── File collection ──────────────────────────────────────────────────────
def _collect_files(root: Path, config: CrawlConfig) -> list[Path]:
"""Recursively collect files under *root* that match *config*."""
results: list[Path] = []
for item in sorted(root.rglob("*")):
# Skip anything whose path components match an ignore pattern.
if _should_ignore(item, root, config.ignore_patterns):
continue
if item.is_file() and item.suffix in config.extensions:
results.append(item)
return results
def _should_ignore(path: Path, root: Path, patterns: list[str]) -> bool:
"""Return True if any component of *path* (relative to *root*) matches
one of the ignore *patterns* via ``fnmatch``."""
rel = path.relative_to(root)
for part in rel.parts:
for pat in patterns:
if fnmatch(part, pat):
return True
return False
# ── Resolution passes ────────────────────────────────────────────────────
def _resolve_imports(
file_imports: dict[str, list[str]],
graph: EntityGraph,
root: Path,
) -> None:
"""Create IMPORTS edges. If the imported module resolves to a project
file, point to that file's node; otherwise create a CONCEPT node for
the external module."""
# Build a lookup: module dotted path -> file node_id for all known files.
file_node_ids = set(graph.get_nodes_by_type(NodeType.FILE))
module_to_file: dict[str, str] = {}
for fid in file_node_ids:
# Strip any known extension suffix: "pkg/sub/mod.py" -> "pkg.sub.mod"
fid_stripped = fid
for ext in _KNOWN_EXTENSIONS:
if fid_stripped.endswith(ext):
fid_stripped = fid_stripped[: -len(ext)]
break
dotted = fid_stripped.replace("/", ".")
module_to_file[dotted] = fid
# Also map just the filename stem for single-file modules.
stem = Path(fid).stem
if stem not in module_to_file:
module_to_file[stem] = fid
# Also map the raw file path (without extension) for JS-style imports
# like "./utils" -> "utils.js"
module_to_file[fid_stripped] = fid
for file_id, modules in file_imports.items():
for mod in modules:
target_id: str
# Normalize JS-style relative imports: "./utils" -> "utils"
normalized = mod.lstrip("./")
if normalized in module_to_file:
target_id = module_to_file[normalized]
elif mod in module_to_file:
target_id = module_to_file[mod]
else:
# Try dotted form for Go/Java/Rust paths
dotted_mod = mod.replace("/", ".").replace("::", ".")
if dotted_mod in module_to_file:
target_id = module_to_file[dotted_mod]
else:
# External module -> CONCEPT node
target_id = mod
if not graph.has_node(target_id):
graph.add_node(
target_id,
NodeType.CONCEPT,
label=mod,
metadata={"kind": "external_module"},
)
graph.add_edge(file_id, target_id, EdgeType.IMPORTS)
def _resolve_calls(
func_calls: dict[str, list[str]],
graph: EntityGraph,
) -> None:
"""Create CALLS edges for calls that match a known FUNCTION node by
name suffix (i.e. the label)."""
# Build name -> [node_id, ...] index for all FUNCTION nodes.
name_index: dict[str, list[str]] = {}
for nid in graph.get_nodes_by_type(NodeType.FUNCTION):
node_data = graph.get_node(nid)
if node_data:
label = node_data["label"]
name_index.setdefault(label, []).append(nid)
for caller_id, called_names in func_calls.items():
seen: set[str] = set()
for name in called_names:
if name in name_index:
for target_id in name_index[name]:
# Avoid self-calls and duplicate edges in the same caller.
if target_id == caller_id:
continue
pair = (caller_id, target_id)
if pair not in seen:
seen.add(pair)
graph.add_edge(caller_id, target_id, EdgeType.CALLS)