blob: 426b58d0d4ec7de5f50d32ae2d78ba988454b6ec [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.
28for SEARCH_PYTHON in py python3 python python2; do
29 if python=$(command -v $SEARCH_PYTHON) && [ -x "$python" ]; then
30 if [ $SEARCH_PYTHON = py ]; then
31 extra_arg="-3"
32 else
33 extra_arg=""
34 fi
35 exec "$python" $extra_arg "$xpy" "$@"
36 fi
37done
Albert Larsanc83ddae2022-10-13 08:20:3938
39python=$(bash -c "compgen -c python" | grep '^python[2-3]\.[0-9]\+$' | head -n1)
40if ! [ "$python" = "" ]; then
41 exec "$python" "$xpy" "$@"
42fi
43
Joshua Nelson775c3c02022-07-31 19:02:3144echo "$0: error: did not find python installed" >&2
45exit 1