Repository navigation
Expand file tree
/
Copy pathmacOS Inspector.command
More file actions
executable file
·145 lines (129 loc) · 4.57 KB
/
Copy pathmacOS Inspector.command
File metadata and controls
executable file
·145 lines (129 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/zsh
set -eu
ROOT="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
HOST="127.0.0.1"
PORT="${MACOS_INSPECTOR_PORT:-8765}"
URL="http://${HOST}:${PORT}/"
OUTPUT_DIR="${MACOS_INSPECTOR_OUTPUT:-${ROOT}/macos-inspector-reports}"
STATE_DIR="${MACOS_INSPECTOR_STATE_DIR:-${ROOT}/tmp}"
LOG_FILE="${STATE_DIR}/dashboard-${PORT}.log"
PID_FILE="${STATE_DIR}/dashboard-${PORT}.pid"
SERVER_PID=""
PID_FILE_CREATED=0
alert_error() {
print -u2 -- "$1"
if [ "${MACOS_INSPECTOR_NO_ALERT:-0}" != "1" ] && [ -x /usr/bin/osascript ]; then
/usr/bin/osascript \
-e 'on run argv' \
-e 'display alert "macOS Inspector" message (item 1 of argv) as critical' \
-e 'end run' \
"$1" >/dev/null 2>&1 || true
fi
}
open_dashboard() {
LAUNCH_URL="$($PYTHON -c 'import json,sys; from pathlib import Path; p=Path(sys.argv[1]); data=json.loads(p.read_text()); assert data["port"]==int(sys.argv[2]); print(data["url"])' "${OUTPUT_DIR}/.macos-inspector-session-${PORT}.json" "$PORT" 2>/dev/null)" || {
alert_error "The running dashboard has no usable private session link in this release folder. Close its launcher and start this version again."
return 1
}
if [ "${MACOS_INSPECTOR_NO_OPEN:-0}" = "1" ]; then
print -- "$URL"
else
"$PYTHON" -c 'import json,subprocess,sys; from pathlib import Path; url=json.loads(Path(sys.argv[1]).read_text())["url"]; subprocess.run(["/usr/bin/osascript","-"], input="open location "+json.dumps(url)+"\n", text=True, stdout=subprocess.DEVNULL, check=True, timeout=10)' "${OUTPUT_DIR}/.macos-inspector-session-${PORT}.json"
fi
}
open_setup_help() {
if [ "${MACOS_INSPECTOR_NO_OPEN:-0}" != "1" ]; then
/usr/bin/open "${ROOT}/docs/START_HERE.html" >/dev/null 2>&1 || true
fi
}
dashboard_ready() {
/usr/bin/curl --silent --fail --max-time 1 "${URL}api/health" 2>/dev/null | /usr/bin/grep -Eq '"status"[[:space:]]*:[[:space:]]*"ok"'
}
cleanup() {
if [ -n "$SERVER_PID" ]; then
pid="$SERVER_PID"
SERVER_PID=""
/bin/kill "$pid" >/dev/null 2>&1 || true
wait "$pid" >/dev/null 2>&1 || true
fi
if [ "$PID_FILE_CREATED" = "1" ]; then
/bin/rm -f "$PID_FILE"
PID_FILE_CREATED=0
fi
}
handle_signal() {
cleanup
exit 0
}
trap cleanup EXIT
trap handle_signal INT TERM HUP
case "$PORT" in
''|*[!0-9]*) alert_error "The dashboard port must be a number between 1 and 65535."; exit 1 ;;
esac
if [ "$PORT" -lt 1 ] || [ "$PORT" -gt 65535 ]; then
alert_error "The dashboard port must be a number between 1 and 65535."
exit 1
fi
PYTHON=""
if [ -n "${MACOS_INSPECTOR_PYTHON:-}" ]; then
candidates=("$MACOS_INSPECTOR_PYTHON")
else
candidates=("${ROOT}/.venv/bin/python3" "${ROOT}/venv/bin/python3" "$(command -v python3 2>/dev/null || true)" /opt/homebrew/bin/python3 /usr/local/bin/python3 /usr/bin/python3)
fi
for candidate in "${candidates[@]}"; do
if [ -x "$candidate" ] && "$candidate" -c 'import sys; raise SystemExit(0 if sys.version_info >= (3, 10) else 1)' >/dev/null 2>&1; then
PYTHON="$candidate"
break
fi
done
if [ -z "$PYTHON" ]; then
open_setup_help
alert_error "A compatible Python 3.10 or newer was not found. Open docs/START_HERE.html for setup instructions. If MACOS_INSPECTOR_PYTHON is set, it must point to a compatible executable. No software was installed or downloaded."
exit 1
fi
if dashboard_ready; then
open_dashboard
exit 0
fi
/bin/mkdir -p "$STATE_DIR" "$OUTPUT_DIR"
/bin/chmod 700 "$STATE_DIR" "$OUTPUT_DIR"
/usr/bin/touch "$LOG_FILE"
/bin/chmod 600 "$LOG_FILE"
/usr/bin/env HOME="${HOME:-/Users/Shared}" \
PATH="${PATH:-/usr/bin:/bin:/usr/sbin:/sbin}" \
TMPDIR="${TMPDIR:-/private/tmp}" PYTHONUNBUFFERED=1 \
PYTHONPATH="${ROOT}/src${PYTHONPATH:+:${PYTHONPATH}}" \
"$PYTHON" -m macos_inspector --web --no-open-browser \
--host "$HOST" --port "$PORT" --output "$OUTPUT_DIR" \
>"$LOG_FILE" 2>&1 &
SERVER_PID=$!
print -- "$SERVER_PID" >"$PID_FILE"
PID_FILE_CREATED=1
/bin/chmod 600 "$PID_FILE"
attempt=0
while [ "$attempt" -lt 80 ]; do
if dashboard_ready; then
open_dashboard
print -- "macOS Inspector is running at ${URL}"
print -- "Keep this window open. Closing it stops the local dashboard."
if wait "$SERVER_PID"; then
server_status=0
else
server_status=$?
fi
SERVER_PID=""
/bin/rm -f "$PID_FILE"
PID_FILE_CREATED=0
if [ "$server_status" -ne 0 ]; then
alert_error "The dashboard stopped unexpectedly. Details: ${LOG_FILE}"
fi
exit "$server_status"
fi
if ! /bin/kill -0 "$SERVER_PID" 2>/dev/null; then
break
fi
/bin/sleep 0.25
attempt=$((attempt + 1))
done
alert_error "The dashboard could not start. Details: ${LOG_FILE}"
exit 1