|
| 1 | +"""Entrypoint detection: a post-pass over the built L1 symbol table (#27). |
| 2 | +
|
| 3 | +Runs AFTER the symbol table exists so every view reference resolves as a |
| 4 | +lookup against ids that already exist. Additive metadata: a failure here |
| 5 | +loses flags, never the analysis. |
| 6 | +""" |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +from pathlib import Path |
| 10 | +from typing import Iterable, Iterator |
| 11 | + |
| 12 | +from codeanalyzer.schema.py_schema import PyApplication, PyCallable, PyClass |
| 13 | +from codeanalyzer.utils import logger |
| 14 | + |
| 15 | + |
| 16 | +def detect_entrypoints( |
| 17 | + app: PyApplication, project_dir: Path, rule_paths: Iterable[Path] = () |
| 18 | +) -> None: |
| 19 | + """Populate ``entrypoints`` on every callable and class, in place.""" |
| 20 | + try: |
| 21 | + _run_stages(app, project_dir, tuple(rule_paths)) |
| 22 | + except Exception as exc: # noqa: BLE001 - additive pass must never abort analysis |
| 23 | + logger.warning("entrypoint detection failed: %s", exc) |
| 24 | + app.entrypoint_report.errors.append(str(exc)) |
| 25 | + _derive_flags(app) |
| 26 | + |
| 27 | + |
| 28 | +def _run_stages(app: PyApplication, project_dir: Path, rule_paths: tuple) -> None: |
| 29 | + """Stages 0-4. Empty until Task 5; the skeleton exists so the contract does.""" |
| 30 | + return None |
| 31 | + |
| 32 | + |
| 33 | +def _derive_flags(app: PyApplication) -> None: |
| 34 | + for node in _walk(app): |
| 35 | + node.is_entrypoint = bool(node.entrypoints) |
| 36 | + |
| 37 | + |
| 38 | +def _walk(app: PyApplication) -> Iterator[object]: |
| 39 | + def walk_callable(c: PyCallable) -> Iterator[object]: |
| 40 | + yield c |
| 41 | + for inner in (c.callables or {}).values(): |
| 42 | + yield from walk_callable(inner) |
| 43 | + for cls in (c.types or {}).values(): |
| 44 | + yield from walk_class(cls) |
| 45 | + |
| 46 | + def walk_class(k: PyClass) -> Iterator[object]: |
| 47 | + yield k |
| 48 | + for m in (k.callables or {}).values(): |
| 49 | + yield from walk_callable(m) |
| 50 | + for inner in (k.types or {}).values(): |
| 51 | + yield from walk_class(inner) |
| 52 | + |
| 53 | + for mod in app.symbol_table.values(): |
| 54 | + for fn in (mod.functions or {}).values(): |
| 55 | + yield from walk_callable(fn) |
| 56 | + for cls in (mod.types or {}).values(): |
| 57 | + yield from walk_class(cls) |
0 commit comments