Spaces:
Runtime error
Runtime error
Create ScriptOrchestrator.py
Browse files- pages/ScriptOrchestrator.py +105 -0
pages/ScriptOrchestrator.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import sys, os, subprocess, hashlib
|
| 3 |
+
|
| 4 |
+
# ─── Ensure utils/ is importable ──────────────────────────────
|
| 5 |
+
UTILS_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
| 6 |
+
if UTILS_PATH not in sys.path:
|
| 7 |
+
sys.path.append(UTILS_PATH)
|
| 8 |
+
|
| 9 |
+
from utils.docgen import generate_doc
|
| 10 |
+
from utils.summarizer import summarize_text
|
| 11 |
+
|
| 12 |
+
st.title("🛠️ Local Script Orchestration & Execution")
|
| 13 |
+
st.write("Scan, queue, and execute Omniscient scripts with results ingested back into the system.")
|
| 14 |
+
|
| 15 |
+
# Init state
|
| 16 |
+
if "local_scripts" not in st.session_state:
|
| 17 |
+
st.session_state.local_scripts = []
|
| 18 |
+
if "script_runs" not in st.session_state:
|
| 19 |
+
st.session_state.script_runs = []
|
| 20 |
+
|
| 21 |
+
SCAN_DIRS = [
|
| 22 |
+
"/opt/omniscient/bin",
|
| 23 |
+
"/opt/omniscient/scripts",
|
| 24 |
+
"/opt/omniscient/ai"
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
def scan_scripts():
|
| 28 |
+
scripts = []
|
| 29 |
+
for d in SCAN_DIRS:
|
| 30 |
+
if os.path.exists(d):
|
| 31 |
+
for root, _, files in os.walk(d):
|
| 32 |
+
for f in files:
|
| 33 |
+
if f.endswith((".sh", ".py")):
|
| 34 |
+
path = os.path.join(root, f)
|
| 35 |
+
try:
|
| 36 |
+
with open(path, "r", errors="ignore") as fh:
|
| 37 |
+
content = fh.read()
|
| 38 |
+
sha1 = hashlib.sha1(content.encode()).hexdigest()
|
| 39 |
+
doc = generate_doc(f, path, content)
|
| 40 |
+
summary = summarize_text(content)
|
| 41 |
+
|
| 42 |
+
scripts.append({
|
| 43 |
+
"name": f,
|
| 44 |
+
"path": path,
|
| 45 |
+
"sha1": sha1,
|
| 46 |
+
"doc": doc,
|
| 47 |
+
"summary": summary
|
| 48 |
+
})
|
| 49 |
+
except Exception as e:
|
| 50 |
+
st.error(f"⚠️ Error reading {f}: {e}")
|
| 51 |
+
return scripts
|
| 52 |
+
|
| 53 |
+
def run_script(script_path: str):
|
| 54 |
+
"""Execute a script safely and capture output"""
|
| 55 |
+
try:
|
| 56 |
+
if script_path.endswith(".sh"):
|
| 57 |
+
result = subprocess.run(["bash", script_path], capture_output=True, text=True, timeout=60)
|
| 58 |
+
elif script_path.endswith(".py"):
|
| 59 |
+
result = subprocess.run(["python3", script_path], capture_output=True, text=True, timeout=60)
|
| 60 |
+
else:
|
| 61 |
+
return {"error": "Unsupported script type"}
|
| 62 |
+
|
| 63 |
+
return {
|
| 64 |
+
"script": script_path,
|
| 65 |
+
"stdout": result.stdout,
|
| 66 |
+
"stderr": result.stderr,
|
| 67 |
+
"returncode": result.returncode
|
| 68 |
+
}
|
| 69 |
+
except Exception as e:
|
| 70 |
+
return {"script": script_path, "error": str(e)}
|
| 71 |
+
|
| 72 |
+
# ─── Scan Scripts ─────────────────────────────────────────────
|
| 73 |
+
if st.button("🔍 Scan Local Scripts"):
|
| 74 |
+
scripts = scan_scripts()
|
| 75 |
+
st.session_state.local_scripts = scripts
|
| 76 |
+
st.success(f"✅ Found {len(scripts)} scripts.")
|
| 77 |
+
|
| 78 |
+
# ─── Script Selection ─────────────────────────────────────────
|
| 79 |
+
if st.session_state.local_scripts:
|
| 80 |
+
script_names = [s["name"] for s in st.session_state.local_scripts]
|
| 81 |
+
selected = st.multiselect("Select scripts to execute", script_names)
|
| 82 |
+
|
| 83 |
+
if st.button("▶️ Run Selected Scripts"):
|
| 84 |
+
for s in st.session_state.local_scripts:
|
| 85 |
+
if s["name"] in selected:
|
| 86 |
+
run_result = run_script(s["path"])
|
| 87 |
+
st.session_state.script_runs.append(run_result)
|
| 88 |
+
if "error" in run_result:
|
| 89 |
+
st.error(f"❌ {s['name']} failed: {run_result['error']}")
|
| 90 |
+
elif run_result["returncode"] != 0:
|
| 91 |
+
st.error(f"❌ {s['name']} exited with {run_result['returncode']}")
|
| 92 |
+
st.code(run_result["stderr"])
|
| 93 |
+
else:
|
| 94 |
+
st.success(f"✅ {s['name']} ran successfully")
|
| 95 |
+
st.code(run_result["stdout"][:500]) # preview output
|
| 96 |
+
|
| 97 |
+
# ─── Display Past Runs ────────────────────────────────────────
|
| 98 |
+
if st.session_state.script_runs:
|
| 99 |
+
st.subheader("📜 Script Run History")
|
| 100 |
+
for r in st.session_state.script_runs[-5:]: # show last 5
|
| 101 |
+
st.markdown(f"**Script:** {r.get('script')} \n**Return Code:** {r.get('returncode')}")
|
| 102 |
+
if r.get("stdout"):
|
| 103 |
+
st.code(r["stdout"][:300])
|
| 104 |
+
if r.get("stderr"):
|
| 105 |
+
st.code(r["stderr"][:300])
|