|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../../.." && pwd)" |
| 5 | +PID_FILE="$ROOT/.tmp/examples-auto-run.pid" |
| 6 | +LOG_DIR="$ROOT/.tmp/examples-start-logs" |
| 7 | +RERUN_FILE="$ROOT/.tmp/examples-rerun.txt" |
| 8 | + |
| 9 | +ensure_dirs() { |
| 10 | + mkdir -p "$LOG_DIR" "$ROOT/.tmp" |
| 11 | +} |
| 12 | + |
| 13 | +is_running() { |
| 14 | + local pid="$1" |
| 15 | + [[ -n "$pid" ]] && ps -p "$pid" >/dev/null 2>&1 |
| 16 | +} |
| 17 | + |
| 18 | +cmd_start() { |
| 19 | + ensure_dirs |
| 20 | + local background=0 |
| 21 | + if [[ "${1:-}" == "--background" ]]; then |
| 22 | + background=1 |
| 23 | + shift |
| 24 | + fi |
| 25 | + |
| 26 | + local ts main_log stdout_log |
| 27 | + ts="$(date +%Y%m%d-%H%M%S)" |
| 28 | + main_log="$LOG_DIR/main_${ts}.log" |
| 29 | + stdout_log="$LOG_DIR/stdout_${ts}.log" |
| 30 | + |
| 31 | + local run_cmd=( |
| 32 | + uv run examples/run_examples.py |
| 33 | + --auto-mode |
| 34 | + --write-rerun |
| 35 | + --main-log "$main_log" |
| 36 | + --logs-dir "$LOG_DIR" |
| 37 | + ) |
| 38 | + |
| 39 | + if [[ "$background" -eq 1 ]]; then |
| 40 | + if [[ -f "$PID_FILE" ]]; then |
| 41 | + local pid |
| 42 | + pid="$(cat "$PID_FILE" 2>/dev/null || true)" |
| 43 | + if is_running "$pid"; then |
| 44 | + echo "examples/run_examples.py already running (pid=$pid)." |
| 45 | + exit 1 |
| 46 | + fi |
| 47 | + fi |
| 48 | + ( |
| 49 | + trap '' HUP |
| 50 | + export EXAMPLES_INTERACTIVE_MODE="${EXAMPLES_INTERACTIVE_MODE:-auto}" |
| 51 | + export APPLY_PATCH_AUTO_APPROVE="${APPLY_PATCH_AUTO_APPROVE:-1}" |
| 52 | + export SHELL_AUTO_APPROVE="${SHELL_AUTO_APPROVE:-1}" |
| 53 | + export AUTO_APPROVE_MCP="${AUTO_APPROVE_MCP:-1}" |
| 54 | + export EXAMPLES_INCLUDE_INTERACTIVE="${EXAMPLES_INCLUDE_INTERACTIVE:-1}" |
| 55 | + export EXAMPLES_INCLUDE_SERVER="${EXAMPLES_INCLUDE_SERVER:-0}" |
| 56 | + export EXAMPLES_INCLUDE_AUDIO="${EXAMPLES_INCLUDE_AUDIO:-0}" |
| 57 | + export EXAMPLES_INCLUDE_EXTERNAL="${EXAMPLES_INCLUDE_EXTERNAL:-0}" |
| 58 | + cd "$ROOT" |
| 59 | + exec "${run_cmd[@]}" "$@" > >(tee "$stdout_log") 2>&1 |
| 60 | + ) & |
| 61 | + local pid=$! |
| 62 | + echo "$pid" >"$PID_FILE" |
| 63 | + echo "Started run_examples.py (pid=$pid)" |
| 64 | + echo "Main log: $main_log" |
| 65 | + echo "Stdout log: $stdout_log" |
| 66 | + echo "Run '.codex/skills/examples-auto-run/scripts/run.sh validate \"$main_log\"' after it finishes." |
| 67 | + return 0 |
| 68 | + fi |
| 69 | + |
| 70 | + export EXAMPLES_INTERACTIVE_MODE="${EXAMPLES_INTERACTIVE_MODE:-auto}" |
| 71 | + export APPLY_PATCH_AUTO_APPROVE="${APPLY_PATCH_AUTO_APPROVE:-1}" |
| 72 | + export SHELL_AUTO_APPROVE="${SHELL_AUTO_APPROVE:-1}" |
| 73 | + export AUTO_APPROVE_MCP="${AUTO_APPROVE_MCP:-1}" |
| 74 | + export EXAMPLES_INCLUDE_INTERACTIVE="${EXAMPLES_INCLUDE_INTERACTIVE:-1}" |
| 75 | + export EXAMPLES_INCLUDE_SERVER="${EXAMPLES_INCLUDE_SERVER:-0}" |
| 76 | + export EXAMPLES_INCLUDE_AUDIO="${EXAMPLES_INCLUDE_AUDIO:-0}" |
| 77 | + export EXAMPLES_INCLUDE_EXTERNAL="${EXAMPLES_INCLUDE_EXTERNAL:-0}" |
| 78 | + cd "$ROOT" |
| 79 | + set +e |
| 80 | + "${run_cmd[@]}" "$@" 2>&1 | tee "$stdout_log" |
| 81 | + local run_status=${PIPESTATUS[0]} |
| 82 | + set -e |
| 83 | + return "$run_status" |
| 84 | +} |
| 85 | + |
| 86 | +cmd_stop() { |
| 87 | + if [[ ! -f "$PID_FILE" ]]; then |
| 88 | + echo "No pid file; nothing to stop." |
| 89 | + return 0 |
| 90 | + fi |
| 91 | + local pid |
| 92 | + pid="$(cat "$PID_FILE" 2>/dev/null || true)" |
| 93 | + if [[ -z "$pid" ]]; then |
| 94 | + rm -f "$PID_FILE" |
| 95 | + echo "Pid file empty; cleaned." |
| 96 | + return 0 |
| 97 | + fi |
| 98 | + if ! is_running "$pid"; then |
| 99 | + rm -f "$PID_FILE" |
| 100 | + echo "Process $pid not running; cleaned pid file." |
| 101 | + return 0 |
| 102 | + fi |
| 103 | + echo "Stopping pid $pid ..." |
| 104 | + kill "$pid" 2>/dev/null || true |
| 105 | + sleep 1 |
| 106 | + if is_running "$pid"; then |
| 107 | + echo "Sending SIGKILL to $pid ..." |
| 108 | + kill -9 "$pid" 2>/dev/null || true |
| 109 | + fi |
| 110 | + rm -f "$PID_FILE" |
| 111 | + echo "Stopped." |
| 112 | +} |
| 113 | + |
| 114 | +cmd_status() { |
| 115 | + if [[ -f "$PID_FILE" ]]; then |
| 116 | + local pid |
| 117 | + pid="$(cat "$PID_FILE" 2>/dev/null || true)" |
| 118 | + if is_running "$pid"; then |
| 119 | + echo "Running (pid=$pid)" |
| 120 | + return 0 |
| 121 | + fi |
| 122 | + fi |
| 123 | + echo "Not running." |
| 124 | +} |
| 125 | + |
| 126 | +cmd_logs() { |
| 127 | + ensure_dirs |
| 128 | + ls -1t "$LOG_DIR" |
| 129 | +} |
| 130 | + |
| 131 | +cmd_tail() { |
| 132 | + ensure_dirs |
| 133 | + local file="${1:-}" |
| 134 | + if [[ -z "$file" ]]; then |
| 135 | + file="$(ls -1t "$LOG_DIR" | head -n1)" |
| 136 | + fi |
| 137 | + if [[ -z "$file" ]]; then |
| 138 | + echo "No log files yet." |
| 139 | + exit 1 |
| 140 | + fi |
| 141 | + tail -f "$LOG_DIR/$file" |
| 142 | +} |
| 143 | + |
| 144 | +collect_rerun() { |
| 145 | + ensure_dirs |
| 146 | + local log_file="${1:-}" |
| 147 | + if [[ -z "$log_file" ]]; then |
| 148 | + log_file="$(ls -1t "$LOG_DIR"/main_*.log 2>/dev/null | head -n1)" |
| 149 | + fi |
| 150 | + if [[ -z "$log_file" ]] || [[ ! -f "$log_file" ]]; then |
| 151 | + echo "No main log file found." |
| 152 | + exit 1 |
| 153 | + fi |
| 154 | + cd "$ROOT" |
| 155 | + uv run examples/run_examples.py --collect "$log_file" --output "$RERUN_FILE" |
| 156 | +} |
| 157 | + |
| 158 | +cmd_rerun() { |
| 159 | + ensure_dirs |
| 160 | + local file="${1:-$RERUN_FILE}" |
| 161 | + if [[ ! -s "$file" ]]; then |
| 162 | + echo "Rerun list is empty: $file" |
| 163 | + exit 0 |
| 164 | + fi |
| 165 | + local ts main_log stdout_log |
| 166 | + ts="$(date +%Y%m%d-%H%M%S)" |
| 167 | + main_log="$LOG_DIR/main_${ts}.log" |
| 168 | + stdout_log="$LOG_DIR/stdout_${ts}.log" |
| 169 | + cd "$ROOT" |
| 170 | + export EXAMPLES_INTERACTIVE_MODE="${EXAMPLES_INTERACTIVE_MODE:-auto}" |
| 171 | + export APPLY_PATCH_AUTO_APPROVE="${APPLY_PATCH_AUTO_APPROVE:-1}" |
| 172 | + export SHELL_AUTO_APPROVE="${SHELL_AUTO_APPROVE:-1}" |
| 173 | + export AUTO_APPROVE_MCP="${AUTO_APPROVE_MCP:-1}" |
| 174 | + set +e |
| 175 | + uv run examples/run_examples.py --auto-mode --rerun-file "$file" --write-rerun --main-log "$main_log" --logs-dir "$LOG_DIR" 2>&1 | tee "$stdout_log" |
| 176 | + local run_status=${PIPESTATUS[0]} |
| 177 | + set -e |
| 178 | + return "$run_status" |
| 179 | +} |
| 180 | + |
| 181 | +usage() { |
| 182 | + cat <<'EOF' |
| 183 | +Usage: run.sh <start|stop|status|logs|tail|collect|rerun> [args...] |
| 184 | +
|
| 185 | +Commands: |
| 186 | + start [--filter ... | other args] Run examples in auto mode (foreground). Pass --background to run detached. |
| 187 | + stop Kill the running auto-run (if any). |
| 188 | + status Show whether it is running. |
| 189 | + logs List log files (.tmp/examples-start-logs). |
| 190 | + tail [logfile] Tail the latest (or specified) log. |
| 191 | + collect [main_log] Parse a main log and write failed examples to .tmp/examples-rerun.txt. |
| 192 | + rerun [rerun_file] Run only the examples listed in .tmp/examples-rerun.txt. |
| 193 | +
|
| 194 | +Environment overrides: |
| 195 | + EXAMPLES_INTERACTIVE_MODE (default auto) |
| 196 | + EXAMPLES_INCLUDE_SERVER/INTERACTIVE/AUDIO/EXTERNAL (defaults: 0/1/0/0) |
| 197 | + APPLY_PATCH_AUTO_APPROVE, SHELL_AUTO_APPROVE, AUTO_APPROVE_MCP (default 1 in auto mode) |
| 198 | +EOF |
| 199 | +} |
| 200 | + |
| 201 | +default_cmd="start" |
| 202 | +if [[ $# -eq 0 && -s "$RERUN_FILE" ]]; then |
| 203 | + default_cmd="rerun" |
| 204 | +fi |
| 205 | + |
| 206 | +case "${1:-$default_cmd}" in |
| 207 | + start) shift || true; cmd_start "$@" ;; |
| 208 | + stop) shift || true; cmd_stop ;; |
| 209 | + status) shift || true; cmd_status ;; |
| 210 | + logs) shift || true; cmd_logs ;; |
| 211 | + tail) shift; cmd_tail "${1:-}" ;; |
| 212 | + collect) shift || true; collect_rerun "${1:-}" ;; |
| 213 | + rerun) shift || true; cmd_rerun "${1:-}" ;; |
| 214 | + *) usage; exit 1 ;; |
| 215 | +esac |
0 commit comments