69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""Indicizzazione Gitea one-shot — eseguire con indexer fermo per evitare OOM."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
|
if ROOT not in sys.path:
|
|
sys.path.insert(0, ROOT)
|
|
|
|
|
|
def _load_dotenv() -> None:
|
|
env_path = os.path.join(ROOT, ".env")
|
|
if not os.path.isfile(env_path):
|
|
return
|
|
with open(env_path, encoding="utf-8") as fh:
|
|
for line in fh:
|
|
line = line.strip()
|
|
if not line or line.startswith("#") or "=" not in line:
|
|
continue
|
|
key, _, value = line.partition("=")
|
|
if key.strip() and key.strip() not in os.environ:
|
|
os.environ[key.strip()] = value.strip().strip("'").strip('"')
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description="Indicizza repo Gitea nel vector store")
|
|
parser.add_argument("--repo", help="owner/name (default: tutti i repo configurati)")
|
|
parser.add_argument("--user", default="daniele", help="Utente MCP/Gitea token")
|
|
parser.add_argument("--max-files", type=int, default=0, help="Limite file per repo (0=config)")
|
|
parser.add_argument("--force", action="store_true", help="Re-indicizza anche file invariati")
|
|
args = parser.parse_args()
|
|
|
|
_load_dotenv()
|
|
if os.path.isfile("/.dockerenv"):
|
|
os.environ.setdefault("MCP_DB", "/data/loogle_mcp.db")
|
|
os.environ.setdefault("MCP_VECTOR_FALLBACK", "/data/vector_fallback.db")
|
|
|
|
from app.db import init_db
|
|
from app.knowledge import gitea_indexer
|
|
|
|
init_db()
|
|
max_files = args.max_files or None
|
|
|
|
if args.repo:
|
|
result = gitea_indexer.index_repo(
|
|
args.repo,
|
|
username=args.user,
|
|
force=args.force,
|
|
max_files=max_files,
|
|
)
|
|
print(json.dumps(result, ensure_ascii=False, indent=2))
|
|
else:
|
|
result = gitea_indexer.index_all(max_files_per_repo=max_files)
|
|
print(json.dumps(result, ensure_ascii=False, indent=2))
|
|
|
|
stats = gitea_indexer.index_stats()
|
|
print("stats", json.dumps(stats, ensure_ascii=False))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|