|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Example: Programmatic Session Control |
| 4 | +
|
| 5 | +Demonstrates how to use non-interactive sessions for: |
| 6 | +- Multi-step command sequences |
| 7 | +- Inspecting session output programmatically |
| 8 | +- Handling session state and errors |
| 9 | +- Building automation scripts |
| 10 | +""" |
| 11 | + |
| 12 | +import sys |
| 13 | +import time |
| 14 | +from sandd import Server |
| 15 | + |
| 16 | + |
| 17 | +def main(): |
| 18 | + # Start server |
| 19 | + server = Server("0.0.0.0", 8765) |
| 20 | + print(f"Server listening on {server.address}") |
| 21 | + |
| 22 | + # Wait for daemon |
| 23 | + daemon_id = "daemon-1" |
| 24 | + print(f"\nWaiting for daemon '{daemon_id}'...") |
| 25 | + if not server.wait_for_daemon(daemon_id, timeout=30): |
| 26 | + print(f"Daemon '{daemon_id}' did not connect") |
| 27 | + sys.exit(1) |
| 28 | + |
| 29 | + print(f"Daemon '{daemon_id}' connected!\n") |
| 30 | + |
| 31 | + # Create a non-interactive session |
| 32 | + print("=== Creating Session ===") |
| 33 | + session = server.new_session(daemon_id, rows=24, cols=80) |
| 34 | + print("Session created\n") |
| 35 | + |
| 36 | + # Example 1: Execute command and capture output |
| 37 | + print("=== Example 1: Basic Command ===") |
| 38 | + session.write(b"echo 'Hello from session'\n") |
| 39 | + time.sleep(0.2) |
| 40 | + output = session.read(timeout=1.0) |
| 41 | + if output: |
| 42 | + print(f"Output: {output.decode()}") |
| 43 | + |
| 44 | + # Example 2: Multi-step workflow |
| 45 | + print("\n=== Example 2: Multi-Step Workflow ===") |
| 46 | + steps = [ |
| 47 | + ("mkdir -p /tmp/test", "Creating directory"), |
| 48 | + ("cd /tmp/test", "Changing directory"), |
| 49 | + ("pwd", "Verifying location"), |
| 50 | + ("echo 'test' > file.txt", "Creating file"), |
| 51 | + ("cat file.txt", "Reading file"), |
| 52 | + ] |
| 53 | + |
| 54 | + for cmd, description in steps: |
| 55 | + print(f"{description}: {cmd}") |
| 56 | + session.write(f"{cmd}\n".encode()) |
| 57 | + time.sleep(0.1) |
| 58 | + output = session.read(timeout=1.0) |
| 59 | + if output: |
| 60 | + result = output.decode().strip() |
| 61 | + if result: |
| 62 | + print(f" → {result}") |
| 63 | + |
| 64 | + # Example 3: Error handling |
| 65 | + print("\n=== Example 3: Error Handling ===") |
| 66 | + session.write(b"exit 42\n") # Exit with non-zero code |
| 67 | + time.sleep(0.2) |
| 68 | + |
| 69 | + # Try to write after exit - should fail gracefully |
| 70 | + try: |
| 71 | + session.write(b"echo 'after exit'\n") |
| 72 | + output = session.read(timeout=1.0) |
| 73 | + if output: |
| 74 | + print(f"Output: {output.decode()}") |
| 75 | + except Exception as e: |
| 76 | + print(f"Session closed (expected): {e}") |
| 77 | + |
| 78 | + # Example 4: Create new session for long-running task |
| 79 | + print("\n=== Example 4: Long-Running Task ===") |
| 80 | + session2 = server.new_session(daemon_id) |
| 81 | + session2.write(b"for i in 1 2 3; do echo \"Step $i\"; sleep 1; done\n") |
| 82 | + |
| 83 | + # Stream output as it arrives |
| 84 | + start = time.time() |
| 85 | + while time.time() - start < 5: |
| 86 | + output = session2.read(timeout=0.5) |
| 87 | + if output: |
| 88 | + print(output.decode(), end='', flush=True) |
| 89 | + else: |
| 90 | + break |
| 91 | + |
| 92 | + session2.close() |
| 93 | + print("\n\nSession closed") |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + main() |
0 commit comments