| import subprocess
|
| import sys
|
| import os
|
| import time
|
|
|
|
|
| COMMAND_QUEUE_FILE = "command_queue.txt"
|
| POLL_INTERVAL_SECONDS = 2 |
| COMMAND_TIMEOUT_SECONDS = 120 |
|
|
| def execute_powershell_command(command, timeout=COMMAND_TIMEOUT_SECONDS): |
| """
|
| Executes a given command in PowerShell and returns the output.
|
| Handles potential errors and provides feedback.
|
| """
|
| try:
|
|
|
|
|
|
|
|
|
|
|
| process = subprocess.run( |
| ["powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", command], |
| capture_output=True, |
| text=True, |
| encoding='utf-8', |
| check=True, |
| timeout=timeout, |
| ) |
| return {
|
| "success": True,
|
| "output": process.stdout,
|
| "error": process.stderr
|
| }
|
| except FileNotFoundError:
|
| return {
|
| "success": False,
|
| "output": "",
|
| "error": "PowerShell executable not found. Please ensure PowerShell is installed and in your PATH."
|
| }
|
| except subprocess.CalledProcessError as e: |
| return { |
| "success": False, |
| "output": e.stdout, |
| "error": e.stderr |
| } |
| except subprocess.TimeoutExpired as e: |
| return { |
| "success": False, |
| "output": e.stdout or "", |
| "error": f"Command timed out after {timeout} seconds." |
| } |
| except Exception as e:
|
| return {
|
| "success": False,
|
| "output": "",
|
| "error": str(e)
|
| }
|
|
|
| def process_commands():
|
| """Reads commands from the queue file and executes them."""
|
| if not os.path.exists(COMMAND_QUEUE_FILE):
|
| print(f"Command queue file '{COMMAND_QUEUE_FILE}' not found. Creating it.")
|
| with open(COMMAND_QUEUE_FILE, "w", encoding='utf-8') as f:
|
| f.write("# This file will contain commands for jarvis_executor.py to run.\n")
|
| f.write("# Example: Start-Process notepad.exe\n")
|
| return
|
|
|
| commands_to_run = []
|
| try:
|
| with open(COMMAND_QUEUE_FILE, "r", encoding='utf-8') as f:
|
| lines = f.readlines()
|
|
|
| commands_to_run = [line.strip() for line in lines if line.strip() and not line.strip().startswith("#")]
|
| except Exception as e:
|
| print(f"Error reading command queue file: {e}")
|
| return
|
|
|
| if not commands_to_run:
|
|
|
| return
|
|
|
| print(f"\n--- Processing {len(commands_to_run)} command(s) from queue ---")
|
|
|
|
|
| try:
|
| with open(COMMAND_QUEUE_FILE, "w", encoding='utf-8') as f:
|
| f.write("# This file will contain commands for jarvis_executor.py to run.\n")
|
| f.write("# Example: Start-Process notepad.exe\n")
|
| except Exception as e:
|
| print(f"Error clearing command queue file: {e}")
|
|
|
|
|
| for command in commands_to_run:
|
| print(f"Executing: {command}")
|
| result = execute_powershell_command(command)
|
|
|
| if result["success"]:
|
| print("--- Command executed successfully ---")
|
| if result["output"]:
|
| print("Output:\n", result["output"])
|
| if result["error"]:
|
| print("Warnings/Messages:\n", result["error"])
|
| else:
|
| print("--- Command failed ---")
|
| print("Error:\n", result["error"])
|
| if result["output"]:
|
| print("Partial Output:\n", result["output"])
|
| print("------------------------------------------")
|
|
|
| def main():
|
| print("Jarvis Executor started. Monitoring command queue...")
|
| print(f"Looking for commands in '{COMMAND_QUEUE_FILE}' every {POLL_INTERVAL_SECONDS} seconds.")
|
| print("Press Ctrl+C to stop.")
|
| print("------------------------------------------")
|
|
|
|
|
| if not os.path.exists(COMMAND_QUEUE_FILE):
|
| print(f"Command queue file '{COMMAND_QUEUE_FILE}' not found. Creating it.")
|
| try:
|
| with open(COMMAND_QUEUE_FILE, "w", encoding='utf-8') as f:
|
| f.write("# This file will contain commands for jarvis_executor.py to run.\n")
|
| f.write("# Example: Start-Process notepad.exe\n")
|
| except Exception as e:
|
| print(f"Failed to create command queue file: {e}")
|
| return
|
|
|
| while True:
|
| try:
|
| process_commands()
|
| time.sleep(POLL_INTERVAL_SECONDS)
|
| except KeyboardInterrupt:
|
| print("\nExiting executor.")
|
| break
|
| except Exception as e:
|
| print(f"An unexpected error occurred in the executor loop: {e}")
|
|
|
| time.sleep(POLL_INTERVAL_SECONDS * 2)
|
|
|
| if __name__ == "__main__":
|
| main() |
|
|