-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest-wheel.bash
More file actions
executable file
·231 lines (197 loc) · 5.56 KB
/
test-wheel.bash
File metadata and controls
executable file
·231 lines (197 loc) · 5.56 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env bash
set -euo pipefail
venv_python="${PYTHON_WHEEL_TEST_EXECUTABLE:-}"
if [[ -z "$venv_python" ]]; then
echo "PYTHON_WHEEL_TEST_EXECUTABLE is not set; using 'python3' from PATH." >&2
venv_python="python3"
fi
venv=$(mktemp -d)
echo "→ Setting up a virtual environment in $venv using python '$venv_python' ($("$venv_python" --version))..."
"$venv_python" -m venv "$venv"
venv_bin="$venv/bin"
if [[ -d "$venv/Scripts" ]]; then
venv_bin="$venv/Scripts"
fi
# NOTE: Do not source the venv's `activate` script here. On Windows, `venv`
# writes `VIRTUAL_ENV` as an absolute Windows path (e.g. `C:\...`) which breaks
# PATH handling in Git-Bash/MSYS. Instead, prepend the venv bin dir to PATH.
export VIRTUAL_ENV="$venv"
export PATH="$venv_bin:$PATH"
hash -r 2>/dev/null || true
python -m pip install -U pip
pip install pytest
cleanup=true
background_pids=()
failed_pids=()
cleanup_done=false
is_windows_shell=false
case "${OSTYPE:-}" in
msys*|cygwin*)
is_windows_shell=true
;;
esac
if [[ "${OS:-}" == "Windows_NT" ]]; then
is_windows_shell=true
fi
terminate_pid_tree() {
local pid="$1"
if ! kill -0 "$pid" >/dev/null 2>&1; then
return 0
fi
if [[ "$is_windows_shell" == true ]]; then
# Git-Bash/MSYS background jobs are tracked via shell PIDs. Use POSIX
# signals against that PID namespace instead of `taskkill`, which expects
# native Windows PIDs and can silently miss the spawned service.
#
# Keep this non-blocking on Windows: waiting/spinning in the MSYS shell can
# itself fail with `fork: Resource temporarily unavailable` during test
# teardown.
kill -TERM "$pid" >/dev/null 2>&1 || true
return 0
fi
kill -TERM "-$pid" >/dev/null 2>&1 || kill "$pid" >/dev/null 2>&1 || true
local deadline=$((SECONDS + 20))
while kill -0 "$pid" >/dev/null 2>&1; do
if (( SECONDS >= deadline )); then
kill -KILL "-$pid" >/dev/null 2>&1 || kill -9 "$pid" >/dev/null 2>&1 || true
break
fi
sleep 1
done
wait "$pid" 2>/dev/null || true
}
run_command() {
local cmd="$1"
if [[ "$cmd" == *.py ]] && [[ "$cmd" != *[[:space:]]* ]]; then
python "$cmd"
else
$cmd
fi
}
launch_background_command() {
local cmd="$1"
if [[ "$is_windows_shell" == true ]]; then
$cmd &
elif command -v setsid >/dev/null 2>&1; then
setsid $cmd &
else
$cmd &
fi
}
cleanup_background_jobs() {
local pid=""
for pid in "${background_pids[@]:+${background_pids[@]}}"; do
if [[ "$is_windows_shell" == true ]]; then
if kill -0 "$pid" >/dev/null 2>&1; then
terminate_pid_tree "$pid"
else
wait "$pid" 2>/dev/null || true
fi
continue
fi
if kill -0 "$pid" >/dev/null 2>&1; then
terminate_pid_tree "$pid"
else
if wait "$pid"; then
echo "Background task $pid already exited with zero status."
else
local exit_status=$?
echo "Background task $pid exited with nonzero status ($exit_status)."
failed_pids+=("$pid")
fi
fi
done
}
cleanup_virtualenv() {
if [[ "$cleanup" != "true" ]]; then
return 0
fi
if [[ "$is_windows_shell" == true ]]; then
# GitHub's Windows runners clean the workspace after each job anyway.
# Avoid synchronously deleting the temporary venv here: Git-Bash/MSYS can
# spend minutes tearing down a Python tree after the background services
# were killed, which turns integration tests into apparent hangs.
echo "→ Skipping synchronous removal of $venv on Windows"
return 0
fi
echo "→ Removing $venv"
rm -rf "$venv"
}
resolve_wheel_for_install() {
local wheel_source="$1"
python - "$wheel_source" <<'PY'
from pathlib import Path
import sys
wheel_source = Path(sys.argv[1])
if wheel_source.is_file():
if wheel_source.suffix != ".whl":
raise SystemExit(f"Expected a '.whl' file, got '{wheel_source}'.")
print(wheel_source.resolve())
raise SystemExit(0)
if not wheel_source.is_dir():
raise SystemExit(f"Wheel path '{wheel_source}' does not exist.")
wheels = sorted(
wheel_source.glob("*.whl"),
key=lambda wheel: (wheel.stat().st_mtime_ns, wheel.name),
)
if not wheels:
raise SystemExit(f"No wheels found in '{wheel_source}'.")
# Tests often reuse a shared deploy directory, so prefer the newest wheel
# rather than installing every historical artifact that happens to be present.
selected = wheels[-1]
sys.stderr.write(
f"→ Installing newest wheel from {wheel_source}: {selected.name}\n"
)
print(selected.resolve())
PY
}
on_exit() {
if [[ "$cleanup_done" == "true" ]]; then
return 0
fi
cleanup_done=true
set +e
cleanup_background_jobs
cleanup_virtualenv
if [[ ${#failed_pids[@]} -gt 0 ]]; then
echo "The following background processes exited with nonzero status: ${failed_pids[@]:+${failed_pids[@]}}"
return 1
fi
return 0
}
trap on_exit EXIT
while [[ $# -gt 0 ]]; do
case $1 in
-w|--wheels-dir)
selected_wheel="$(resolve_wheel_for_install "$2")"
pip install --no-deps --force-reinstall "$selected_wheel"
shift
shift
;;
-b|--background)
echo "→ Launching background task: $2"
launch_background_command "$2"
background_pids+=("$!")
echo "... started with PID: $!"
sleep 5
shift
shift
;;
-f|--foreground)
echo "→ Starting foreground task: $2"
run_command "$2"
shift
shift
;;
-c|--cleanup)
echo "The temporary virtual will be deleted: $2"
cleanup=$2
shift
shift
;;
esac
done
cleanup_status=0
on_exit || cleanup_status=$?
trap - EXIT
exit "$cleanup_status"