revert shell runner for now
This commit is contained in:
@@ -1,27 +1,15 @@
|
|||||||
import subprocess
|
import subprocess
|
||||||
|
import shlex
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
TIMEOUT_SECONDS = 60
|
TIMEOUT_SECONDS = 60
|
||||||
SCRIPTS_DIR = Path("./scripts/generated")
|
|
||||||
|
|
||||||
def _save_script(script: str) -> Path:
|
|
||||||
"""Save the original (pre-Set-Location) script to ./scripts/generated/ with a timestamp name."""
|
|
||||||
SCRIPTS_DIR.mkdir(parents=True, exist_ok=True)
|
|
||||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
|
|
||||||
path = SCRIPTS_DIR / f"ai_{timestamp}.ps1"
|
|
||||||
path.write_text(script, encoding="utf-8")
|
|
||||||
return path
|
|
||||||
|
|
||||||
def run_powershell(script: str, base_dir: str) -> str:
|
def run_powershell(script: str, base_dir: str) -> str:
|
||||||
"""
|
"""
|
||||||
Run a PowerShell script with working directory set to base_dir.
|
Run a PowerShell script with working directory set to base_dir.
|
||||||
Saves the script to ./scripts/generated/ before running.
|
|
||||||
Returns a string combining stdout, stderr, and exit code.
|
Returns a string combining stdout, stderr, and exit code.
|
||||||
Raises nothing - all errors are captured into the return string.
|
Raises nothing - all errors are captured into the return string.
|
||||||
"""
|
"""
|
||||||
saved_path = _save_script(script)
|
|
||||||
|
|
||||||
# Prepend Set-Location so the AI doesn't need to worry about cwd
|
# Prepend Set-Location so the AI doesn't need to worry about cwd
|
||||||
full_script = f"Set-Location -LiteralPath '{base_dir}'\n{script}"
|
full_script = f"Set-Location -LiteralPath '{base_dir}'\n{script}"
|
||||||
|
|
||||||
@@ -33,16 +21,16 @@ def run_powershell(script: str, base_dir: str) -> str:
|
|||||||
timeout=TIMEOUT_SECONDS,
|
timeout=TIMEOUT_SECONDS,
|
||||||
cwd=base_dir
|
cwd=base_dir
|
||||||
)
|
)
|
||||||
parts = [f"SAVED: {saved_path}"]
|
parts = []
|
||||||
if result.stdout.strip():
|
if result.stdout.strip():
|
||||||
parts.append(f"STDOUT:\n{result.stdout.strip()}")
|
parts.append(f"STDOUT:\n{result.stdout.strip()}")
|
||||||
if result.stderr.strip():
|
if result.stderr.strip():
|
||||||
parts.append(f"STDERR:\n{result.stderr.strip()}")
|
parts.append(f"STDERR:\n{result.stderr.strip()}")
|
||||||
parts.append(f"EXIT CODE: {result.returncode}")
|
parts.append(f"EXIT CODE: {result.returncode}")
|
||||||
return "\n".join(parts)
|
return "\n".join(parts) if parts else f"EXIT CODE: {result.returncode}"
|
||||||
except subprocess.TimeoutExpired:
|
except subprocess.TimeoutExpired:
|
||||||
return f"SAVED: {saved_path}\nERROR: command timed out after {TIMEOUT_SECONDS}s"
|
return f"ERROR: command timed out after {TIMEOUT_SECONDS}s"
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
return f"SAVED: {saved_path}\nERROR: powershell executable not found"
|
return "ERROR: powershell executable not found"
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return f"SAVED: {saved_path}\nERROR: {e}"
|
return f"ERROR: {e}"
|
||||||
|
|||||||
Reference in New Issue
Block a user