Prechádzať zdrojové kódy

fix wipe.sh: proper .env sourcing, inline Python, respect NEWS_MCP_DB_PATH

Lukas Goldschmidt 1 týždeň pred
rodič
commit
2c049a1c7e
2 zmenil súbory, kde vykonal 45 pridanie a 6 odobranie
  1. 2 1
      scripts/clear_all_but_feeds.py
  2. 43 5
      wipe.sh

+ 2 - 1
scripts/clear_all_but_feeds.py

@@ -1,11 +1,12 @@
 #!/usr/bin/env python3
 """Clear all news data but keep feed_state rows intact."""
 
+import os
 import sqlite3
 import sys
 from pathlib import Path
 
-DB_PATH = Path(__file__).resolve().parent.parent / "data" / "news.sqlite"
+DB_PATH = Path(os.getenv("NEWS_MCP_DB_PATH", str(Path(__file__).resolve().parent.parent / "data" / "news.sqlite")))
 
 if len(sys.argv) > 1:
     DB_PATH = Path(sys.argv[1])

+ 43 - 5
wipe.sh

@@ -2,11 +2,49 @@
 set -euo pipefail
 
 SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+
+# Activate repo virtualenv so python3 + deps are always found
 source "$SCRIPT_DIR/.venv/bin/activate"
 
-# Source .env which may set NEWS_MCP_DB_PATH
-set -a
-source "$SCRIPT_DIR/.env"
-set +a
+# Source .env (may override NEWS_MCP_DB_PATH)
+if [ -f "$SCRIPT_DIR/.env" ]; then
+  while IFS='=' read -r key val; do
+    # skip comments and blank lines
+    [[ "$key" =~ ^#.*$ || -z "$key" ]] && continue
+    # strip leading/trailing whitespace from key
+    key="$(echo "$key" | xargs)"
+    export "$key=$val"
+  done < "$SCRIPT_DIR/.env"
+fi
+
+# Wipe everything except feed_state (which holds feed config)
+python3 - "$SCRIPT_DIR" <<'PYEOF'
+import os, sqlite3, sys
+from pathlib import Path
+
+script_dir = Path(sys.argv[1])
+db_path = Path(os.getenv("NEWS_MCP_DB_PATH", str(script_dir / "data" / "news.sqlite")))
+
+if not db_path.exists():
+    print(f"DB not found: {db_path}", file=sys.stderr)
+    sys.exit(1)
+
+TABLES_TO_CLEAR = [
+    "cluster_entities",
+    "cluster_keywords",
+    "entity_metadata",
+    "clusters",
+    "meta",
+]
+
+conn = sqlite3.connect(str(db_path))
+for t in TABLES_TO_CLEAR:
+    conn.execute(f"DELETE FROM {t}")
+conn.commit()
+
+cur = conn.execute("SELECT COUNT(*) FROM feed_state")
+feeds = cur.fetchone()[0]
+conn.close()
 
-python3 "$SCRIPT_DIR/scripts/clear_all_but_feeds.py"
+print(f"Wiped {', '.join(TABLES_TO_CLEAR)}. feed_state rows kept: {feeds}")
+PYEOF