blob: 551cfe6efbf3394eed1d01b06edf050dc76b0af8 [file] [log] [blame]
Joshua Nelson775c3c02022-07-31 19:02:311#!/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
ozkanonureea62022023-05-03 17:32:3910# syntax check
MaxHearndenea026f92023-09-25 19:22:2011sh -n "$0"
ozkanonureea62022023-05-03 17:32:3912
Joshua Nelson775c3c02022-07-31 19:02:3113realpath() {
onur-ozkane0fe1d62023-10-14 14:53:3314 local path="$1"
15 if [ -L "$path" ]; then
16 readlink -f "$path"
17 elif [ -d "$path" ]; then
18 (cd -P "$path" && pwd)
Joshua Nelson775c3c02022-07-31 19:02:3119 else
onur-ozkane0fe1d62023-10-14 14:53:3320 echo "$(realpath "$(dirname "$path")")/$(basename "$path")"
Joshua Nelson775c3c02022-07-31 19:02:3121 fi
22}
23
24xpy=$(dirname "$(realpath "$0")")/x.py
25
26# On Windows, `py -3` sometimes works. We need to try it first because `python3`
27# sometimes tries to launch the app store on Windows.
jyn1cac5fa2025-02-13 15:14:0628# On MacOS, `py` tries to install "Developer command line tools". Try `python3` first.
29# NOTE: running `bash -c ./x` from Windows doesn't set OSTYPE.
30case ${OSTYPE:-} in
31 cygwin*|msys*) SEARCH="py python3 python python2";;
32 *) SEARCH="python3 python py python2";;
33esac
34for SEARCH_PYTHON in $SEARCH; do
Joshua Nelson775c3c02022-07-31 19:02:3135 if python=$(command -v $SEARCH_PYTHON) && [ -x "$python" ]; then
36 if [ $SEARCH_PYTHON = py ]; then
37 extra_arg="-3"
38 else
39 extra_arg=""
40 fi
41 exec "$python" $extra_arg "$xpy" "$@"
42 fi
43done
Albert Larsanc83ddae2022-10-13 08:20:3944
ismailarilike0b98c72024-11-06 13:40:0245python=$(bash -c "compgen -c python" | grep '^python[2-3]\.[0-9]+$' | head -n1)
Albert Larsanc83ddae2022-10-13 08:20:3946if ! [ "$python" = "" ]; then
47 exec "$python" "$xpy" "$@"
48fi
49
Joshua Nelson775c3c02022-07-31 19:02:3150echo "$0: error: did not find python installed" >&2
51exit 1