Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions graphify/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,64 @@ def save_cached(path: Path, result: dict, root: Path = Path("."), kind: str = "a
raise


def _derived_facts_dir(root: Path, cache_root: "Path | None", kind: str,
schema: int) -> Path:
"""Directory for a per-file derived-facts cache (e.g. Python symbol
resolution facts, #perf). Namespaced by package version AND ``schema`` —
the payload shape depends on the extractor code, so entries from another
version/schema must never be reused; a bump simply orphans the old dir.
"""
_out = Path(_GRAPHIFY_OUT)
anchor = cache_root if cache_root is not None else root
base = _out if _out.is_absolute() else Path(anchor).resolve() / _out
d = base / "cache" / kind / f"v{_EXTRACTOR_VERSION}-s{schema}"
d.mkdir(parents=True, exist_ok=True)
return d


def load_derived_facts(path: Path, root: Path, cache_root: "Path | None",
kind: str, schema: int) -> "dict | None":
"""Load a file's cached derived facts, keyed by the SAME content hash the
AST cache uses (so it is valid exactly when the file is unchanged). Returns
None on any miss or error — the caller recomputes. The payload is plain,
path-free JSON, so no portability rewrite is needed on load.
"""
try:
h = file_hash(path, root, cache_root=cache_root)
entry = _derived_facts_dir(root, cache_root, kind, schema) / f"{h}.json"
if entry.is_file():
return json.loads(entry.read_text(encoding="utf-8"))
except Exception:
return None
return None


def save_derived_facts(path: Path, data: dict, root: Path,
cache_root: "Path | None", kind: str, schema: int) -> None:
"""Persist a file's derived facts atomically. Best-effort: a failure just
means the next run recomputes them."""
try:
h = file_hash(path, root, cache_root=cache_root)
target_dir = _derived_facts_dir(root, cache_root, kind, schema)
entry = target_dir / f"{h}.json"
fd, tmp_path = tempfile.mkstemp(dir=target_dir, prefix=f"{h}.", suffix=".tmp")
try:
os.write(fd, json.dumps(data).encode())
os.close(fd)
_os_replace_with_fallback(tmp_path, entry)
except Exception:
try:
os.close(fd)
except OSError:
pass
try:
os.unlink(tmp_path)
except OSError:
pass
except Exception:
pass


def cached_files(root: Path = Path(".")) -> set[str]:
"""Return set of file hashes that have a valid cache entry (any kind)."""
base = Path(root).resolve() / _GRAPHIFY_OUT / "cache"
Expand Down
6 changes: 4 additions & 2 deletions graphify/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -6883,7 +6883,7 @@ def _describe_syntax_error(rel: str, line: "int | None", kept: int) -> str:
# marker set in the per-file extractor. Populated just before the pass that uses it.
callable_nids: set[str] = set()

_augment_symbol_resolution_edges(paths, all_nodes, all_edges, root)
_augment_symbol_resolution_edges(paths, all_nodes, all_edges, root, cache_root=cache_location)

# Merge a header-declared class (and its methods) with its sibling-impl
# definition into ONE node (C/C++/ObjC #1547/#1556). Runs BEFORE the id-remap
Expand Down Expand Up @@ -7359,7 +7359,9 @@ def _learn(e: dict) -> None:
if py_paths:
py_results = [r for r, p in zip(per_file, paths) if p.suffix == ".py"]
try:
cross_file_edges = _resolve_cross_file_imports(py_results, py_paths, all_nodes, all_edges)
cross_file_edges = _resolve_cross_file_imports(
py_results, py_paths, all_nodes, all_edges, root, cache_root=cache_location
)
all_edges.extend(cross_file_edges)
except Exception as exc:
import logging
Expand Down
Loading
Loading