Repository navigation
Expand file tree
/
Copy pathlib.sh
More file actions
191 lines (179 loc) · 7.96 KB
/
Copy pathlib.sh
File metadata and controls
191 lines (179 loc) · 7.96 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# Sourced by every check, right after it sets ROOT:
#
# . "$ROOT/docs/checks/lib.sh"
#
# so that two checks cannot reach into each other.
#
# - A daemon of its own. Checks shared one daemon, and on one evening a
# check left it on the uiautomator backend, which every later check
# inherited, and a sweep's `daemon stop` killed a trace another device
# was recording. MOBIUM_SESSION names a daemon and its socket; a check
# gets one unless its caller chose one (a grid run, mobium test), and it
# is stopped when the check ends, since two daemons driving one device
# invalidate each other's session on it.
# - One check per device: check_lock "$DEV" once the device is known, and
# a second check on the same device is refused, naming the first.
# - CHECK_TMP, a folder of its own, removed at the end.
# - at_exit, in place of `trap ... EXIT`: a shell has one EXIT trap, and a
# check's own would replace this file's. Handlers run last-registered
# first, before the daemon stops, since they often still need it.
# at_exit_clear drops the check's handlers — where it once wrote
# `trap - EXIT` — and keeps this file's.
# - On Windows, under Git Bash: MSYS2_ARG_CONV_EXCL, or a device path such
# as /data/local/tmp/x reaches adb as C:/Program Files/Git/data/local/tmp/x
# — device prefixes only, since a check's own /tmp paths must still be
# converted for mobium.exe, and MSYS_NO_PATHCONV stopped both; and
# PYTHONUTF8, or a check's Python reads Mobium's UTF-8 output as cp1252.
# Both mean nothing anywhere else.
#
# POSIX sh, as every check is.
if [ -z "${CHECK_LIB:-}" ]; then
CHECK_LIB=1
if [ -z "${MOBIUM_SESSION:-}" ]; then
# Short: the session names a socket path, which the OS caps at ~104 bytes.
MOBIUM_SESSION="ck$$"
CHECK_OWN_DAEMON=1
fi
export MOBIUM_SESSION
export MSYS2_ARG_CONV_EXCL="/data/;/sdcard/;/storage/;/system/;/proc/" PYTHONUTF8=1
CHECK_TMP="$(mktemp -d "${TMPDIR:-/tmp}/mobium-check.XXXXXX")"
# Python on Windows ends each printed line with \r\n. Git Bash drops the \r
# inside $(...) but not on a pipe, so `python3 ... | grep -x Button` matched
# nothing for a line that read Button (speech.sh, measured 2026-10-27). A
# sitecustomize of the check's own puts Python's output on \n, as on a Mac.
case "$(uname -s)" in
MINGW*|MSYS*|CYGWIN*)
mkdir -p "$CHECK_TMP/py"
printf 'import sys\nfor s in (sys.stdout, sys.stderr):\n s.reconfigure(newline="\\n")\n' > "$CHECK_TMP/py/sitecustomize.py"
PYTHONPATH="$(cygpath -w "$CHECK_TMP/py")${PYTHONPATH:+;$PYTHONPATH}"
export PYTHONPATH
# On a clean Windows `python3` is the Microsoft Store's placeholder,
# which exits 49 under Git Bash saying Python is not installed, and
# python.org's installer provides `python` alone: clients.sh failed its
# Python client, which passed under `python` (Windows on ARM,
# 2026-10-28). Every check says python3, so the check's own python3
# runs python.
if ! python3 -c '' >/dev/null 2>&1 && python -c '' >/dev/null 2>&1; then
mkdir -p "$CHECK_TMP/bin"
printf '#!/bin/sh\nexec python "$@"\n' > "$CHECK_TMP/bin/python3"
chmod +x "$CHECK_TMP/bin/python3"
PATH="$CHECK_TMP/bin:$PATH"
export PATH
fi ;;
esac
CHECK_LOCK=""
_check_handlers=""
# shows <locator>: whether `$M find` matched it. find answers "No element
# matches" with status 0, so a check that branches on its status cannot fail.
shows() {
! $M find "$1" 2>&1 | grep -q '^No element matches'
}
at_exit() {
# Newest first.
_check_handlers="$1
$_check_handlers"
}
at_exit_clear() {
_check_handlers=""
}
_check_exit() {
_check_status=$?
if [ "$_check_status" = 0 ] && [ -n "$CHECK_SUM" ] && [ "$(cksum < "$CHECK_SELF")" != "$CHECK_SUM" ]; then
echo "$CHECK_SELF changed while it ran, and sh reads a script as it goes: its result means nothing" >&2
_check_status=2
fi
trap - EXIT INT TERM
_check_rest="$_check_handlers"
_check_handlers=""
while [ -n "$_check_rest" ]; do
_check_cmd="${_check_rest%%
*}"
case "$_check_rest" in
*"
"*) _check_rest="${_check_rest#*
}" ;;
*) _check_rest="" ;;
esac
# In a subshell: a handler that fails and exits — a check's own fail —
# ends only itself. One did, restoring TalkBack's log level behind a
# dialog, and the handler after it, the one turning TalkBack off on a
# phone, never ran.
[ -n "$_check_cmd" ] && (eval "$_check_cmd") || true
done
if [ -n "${CHECK_OWN_DAEMON:-}" ] && [ -n "${ROOT:-}" ] && [ -x "$ROOT/bin/mobium" ]; then
"$ROOT/bin/mobium" daemon stop >/dev/null 2>&1 || true
fi
[ -n "$CHECK_LOCK" ] && rm -rf "$CHECK_LOCK"
rm -rf "$CHECK_TMP"
exit "$_check_status"
}
trap _check_exit EXIT
trap 'exit 130' INT TERM
# A check must parse before it runs. Under set -e, macOS's sh (bash 3.2)
# hands an EXIT trap a syntax error as status 0, and this trap exits with
# the status it is given: audio.sh died at line 266 and reported a pass.
# The checksum catches the other way there: a check edited while it ran.
CHECK_SELF="$0"
CHECK_SUM=""
if [ -f "$CHECK_SELF" ]; then
sh -n "$CHECK_SELF" || { echo "$CHECK_SELF does not parse, so it was not run" >&2; exit 2; }
CHECK_SUM=$(cksum < "$CHECK_SELF")
fi
# kill_matching PATTERN ends this machine's processes whose command line
# contains PATTERN, and fails when there were none. pkill -f does it on
# macOS and Linux; Git Bash on Windows has no pkill, and its ps shows no
# arguments, so there the command line is read from Windows itself.
kill_matching() {
case "$(uname -s)" in
MINGW*|MSYS*|CYGWIN*)
PATTERN="$1" powershell.exe -NoProfile -Command '$n = 0; Get-CimInstance Win32_Process | Where-Object { $_.CommandLine -and $_.CommandLine.Contains($env:PATTERN) -and $_.ProcessId -ne $PID } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue; $n++ }; if ($n -eq 0) { exit 1 }' ;;
*) pkill -f "$1" ;;
esac
}
# check_platform android|ios DEV refuses a device of the other kind, by name,
# with exit 2. An Android check given a simulator tried to launch Google's
# Calculator there, and an iOS one given an emulator failed listing its apps
# under the wrong driver: failures that named the wrong cause, in a sweep that
# ran every check on both (CHALLENGES 291). A simulator's UDID is five groups
# and an iPhone's two; anything else is an Android serial.
check_platform() {
case "$2" in
????????-????????????????|*-*-*-*-*) _check_kind=ios ;;
*) _check_kind=android ;;
esac
[ "$_check_kind" = "$1" ] && return 0
if [ "$1" = ios ]; then _check_want="an iOS device"; else _check_want="an Android device"; fi
echo "$(basename "$0") is for $_check_want, and $2 is not one" >&2
exit 2
}
# check_lock DEV takes the device for this check, or refuses: two checks on
# one device fight over its session. A lock whose holder has gone is taken.
#
# A check run by another — third-party-app.sh hands an iPhone to its iOS
# half — shares its caller's lock rather than being refused by it: the
# holder's pid is passed down as CHECK_LOCK_OWNER, and the lock stays the
# caller's to release.
check_lock() {
_check_dir="${MOBIUM_HOME:-$HOME/.mobium}/locks"
mkdir -p "$_check_dir"
_check_name=$(printf '%s' "$1" | tr -c 'A-Za-z0-9._-' '_')
_check_lockdir="$_check_dir/$_check_name"
if [ -n "${CHECK_LOCK_OWNER:-}" ] && [ "$(cat "$_check_lockdir/pid" 2>/dev/null)" = "$CHECK_LOCK_OWNER" ]; then
return 0
fi
if ! mkdir "$_check_lockdir" 2>/dev/null; then
_check_pid=$(cat "$_check_lockdir/pid" 2>/dev/null || true)
if [ -n "$_check_pid" ] && kill -0 "$_check_pid" 2>/dev/null; then
echo "FAIL: $1 is in use by another check ($(cat "$_check_lockdir/check" 2>/dev/null), pid $_check_pid) — run one check per device at a time" >&2
exit 2
fi
rm -rf "$_check_lockdir"
mkdir "$_check_lockdir" || { echo "FAIL: could not lock $1" >&2; exit 2; }
fi
echo $$ > "$_check_lockdir/pid"
basename "$0" > "$_check_lockdir/check"
CHECK_LOCK="$_check_lockdir"
CHECK_LOCK_OWNER=$$
export CHECK_LOCK_OWNER
}
fi