111 lines
4.4 KiB
Python
111 lines
4.4 KiB
Python
"""Local, code-only Graphify index; no keys, hooks or background processes."""
|
|
|
|
import importlib.metadata
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
OUT = ROOT / "graphify-out"
|
|
GRAPH = OUT / "graph.json"
|
|
VERSION = "0.9.93"
|
|
|
|
|
|
def run(*args):
|
|
subprocess.run([sys.executable, "-m", "graphify", *args], cwd=ROOT, check=True)
|
|
|
|
|
|
def read_graph(path=GRAPH):
|
|
data = json.loads(path.read_text(encoding="utf-8"))
|
|
if not data.get("nodes"):
|
|
raise ValueError("Graphify produced no nodes; the previous index was preserved")
|
|
return data
|
|
|
|
|
|
def build():
|
|
if OUT.is_symlink():
|
|
raise ValueError("Index output must be a directory inside the repository")
|
|
OUT.mkdir(exist_ok=True)
|
|
# A fresh output avoids incremental semantic preservation and shrink guards:
|
|
# deleted files must disappear, while a failed extraction keeps the old graph.
|
|
temporary = Path(tempfile.mkdtemp(prefix=".build-", dir=OUT))
|
|
try:
|
|
run("extract", str(ROOT), "--code-only", "--no-cluster", "--out", str(temporary))
|
|
candidate = temporary / "graphify-out" / "graph.json"
|
|
data = read_graph(candidate)
|
|
sources = {str(node.get("source_file", "")).replace("\\", "/")
|
|
for node in data["nodes"] if node.get("source_file")}
|
|
rust_sources = sorted(source for source in sources if source.endswith(".rs"))
|
|
if not rust_sources:
|
|
raise ValueError("No Rust AST sources; the previous index was preserved")
|
|
if any(any(part in {"target", ".git", "graphify-out", "secrets"}
|
|
for part in Path(source).parts) for source in sources):
|
|
raise ValueError("Excluded source found; the previous index was preserved")
|
|
edges = data.get("edges", data.get("links", []))
|
|
report = {
|
|
"graphify_version": VERSION,
|
|
"mode": "local-code-ast",
|
|
"nodes": len(data["nodes"]),
|
|
"edges": len(edges),
|
|
"rust_sources": rust_sources,
|
|
"source_files": sorted(sources),
|
|
"input_tokens": 0,
|
|
"output_tokens": 0,
|
|
}
|
|
# Atomic graph replacement happens only after extraction and validation.
|
|
os.replace(candidate, GRAPH)
|
|
(OUT / "index-report.json").write_text(
|
|
json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
|
(OUT / ".graphify_python").write_text(sys.executable, encoding="utf-8")
|
|
print(f"Indexed {len(data['nodes'])} nodes, {len(edges)} edges, "
|
|
f"{len(rust_sources)} Rust sources; LLM tokens: 0")
|
|
finally:
|
|
# Verify the exact cleanup target before recursive deletion on Windows.
|
|
resolved = temporary.resolve()
|
|
if (resolved.parent != OUT.resolve() or not resolved.name.startswith(".build-")
|
|
or not resolved.is_relative_to(ROOT)):
|
|
raise ValueError(f"Unsafe temporary cleanup path: {resolved}")
|
|
shutil.rmtree(resolved)
|
|
|
|
|
|
def main():
|
|
if importlib.metadata.version("graphifyy") != VERSION:
|
|
raise ValueError(f"Use graphifyy=={VERSION} for this workflow")
|
|
action, *args = sys.argv[1:] or ["update"]
|
|
if action in {"build", "update"}:
|
|
if args:
|
|
raise ValueError("build/update take no query arguments")
|
|
build()
|
|
elif action == "vocab":
|
|
tokens = set()
|
|
for node in read_graph()["nodes"]:
|
|
for word in re.findall(r"[^\W\d_]+", node.get("label", "")):
|
|
parts = re.findall(r"[A-Z]+(?=[A-Z][a-z])|[A-Z]?[a-z]+|[A-Z]+", word)
|
|
tokens.update(part.lower() for part in parts or [word]
|
|
if 3 <= len(part) <= 30)
|
|
print("\n".join(sorted(tokens)))
|
|
elif action == "status":
|
|
read_graph()
|
|
print((OUT / "index-report.json").read_text(encoding="utf-8"))
|
|
elif action in {"query", "path", "explain"}:
|
|
read_graph()
|
|
if not args:
|
|
raise ValueError(f"{action} requires search terms")
|
|
run(action, *args, "--graph", str(GRAPH))
|
|
else:
|
|
raise ValueError(f"Unknown action: {action}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except (ValueError, OSError, subprocess.CalledProcessError,
|
|
importlib.metadata.PackageNotFoundError) as error:
|
|
print(f"Graphify: {error}", file=sys.stderr)
|
|
sys.exit(1)
|