1#!/bin/sh
2
3# Modern Linux and macOS systems commonly only have a thing called `python3` and
4# not `python`, while Windows commonly does not have `python3`, so we cannot
5# directly use python in the x.py shebang and have it consistently work. Instead we
6# have a shell script to look for a python to run x.py.
7
8set -eu
9
10# syntax check
11sh -n "$0"
12
13realpath() {
14 local path="$1"
15 if [ -L "$path" ]; then
16 readlink -f "$path"
17 elif [ -d "$path" ]; then
18 # "cd" is not always silent (e.g. when CDPATH is set), so discard its output.
19 (cd -P "$path" >/dev/null && pwd)
20 else
21 echo "$(realpath "$(dirname "$path")")/$(basename "$path")"
22 fi
23}
24
25xpy=$(dirname "$(realpath "$0")")/x.py
26
27# On Windows, `py -3` sometimes works. We need to try it first because `python3`
28# sometimes tries to launch the app store on Windows.
29# On MacOS, `py` tries to install "Developer command line tools". Try `python3` first.
30# NOTE: running `bash -c ./x` from Windows doesn't set OSTYPE.
31case ${OSTYPE:-} in
32 cygwin*|msys*) SEARCH="py python3 python python2 uv";;
33 *) SEARCH="python3 python py python2 uv";;
34esac
35for SEARCH_PYTHON in $SEARCH; do
36 if python=$(command -v $SEARCH_PYTHON) && [ -x "$python" ]; then
37 case $SEARCH_PYTHON in
38 py)
39 extra_arg="-3";;
40 uv)
41 extra_arg="run";;
42 *)
43 extra_arg="";;
44 esac
45 exec "$python" $extra_arg "$xpy" "$@"
46 fi
47done
48
49python=$(bash -c "compgen -c python" | grep -E '^python[2-3](\.[0-9]+)?$' | head -n1)
50if ! [ "$python" = "" ]; then
51 exec "$python" "$xpy" "$@"
52fi
53
54echo "$0: error: did not find python installed" >&2
55exit 1