Joshua Nelson | 775c3c0 | 2022-07-31 19:02:31 | [diff] [blame] | 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 | |
| 8 | set -eu |
| 9 | |
ozkanonur | eea6202 | 2023-05-03 17:32:39 | [diff] [blame] | 10 | # syntax check |
MaxHearnden | ea026f9 | 2023-09-25 19:22:20 | [diff] [blame] | 11 | sh -n "$0" |
ozkanonur | eea6202 | 2023-05-03 17:32:39 | [diff] [blame] | 12 | |
Joshua Nelson | 775c3c0 | 2022-07-31 19:02:31 | [diff] [blame] | 13 | realpath() { |
onur-ozkan | e0fe1d6 | 2023-10-14 14:53:33 | [diff] [blame] | 14 | local path="$1" |
| 15 | if [ -L "$path" ]; then |
| 16 | readlink -f "$path" |
| 17 | elif [ -d "$path" ]; then |
| 18 | (cd -P "$path" && pwd) |
Joshua Nelson | 775c3c0 | 2022-07-31 19:02:31 | [diff] [blame] | 19 | else |
onur-ozkan | e0fe1d6 | 2023-10-14 14:53:33 | [diff] [blame] | 20 | echo "$(realpath "$(dirname "$path")")/$(basename "$path")" |
Joshua Nelson | 775c3c0 | 2022-07-31 19:02:31 | [diff] [blame] | 21 | fi |
| 22 | } |
| 23 | |
| 24 | xpy=$(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. |
jyn | 1cac5fa | 2025-02-13 15:14:06 | [diff] [blame] | 28 | # 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. |
| 30 | case ${OSTYPE:-} in |
| 31 | cygwin*|msys*) SEARCH="py python3 python python2";; |
| 32 | *) SEARCH="python3 python py python2";; |
| 33 | esac |
| 34 | for SEARCH_PYTHON in $SEARCH; do |
Joshua Nelson | 775c3c0 | 2022-07-31 19:02:31 | [diff] [blame] | 35 | 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 |
| 43 | done |
Albert Larsan | c83ddae | 2022-10-13 08:20:39 | [diff] [blame] | 44 | |
ismailarilik | e0b98c7 | 2024-11-06 13:40:02 | [diff] [blame] | 45 | python=$(bash -c "compgen -c python" | grep '^python[2-3]\.[0-9]+$' | head -n1) |
Albert Larsan | c83ddae | 2022-10-13 08:20:39 | [diff] [blame] | 46 | if ! [ "$python" = "" ]; then |
| 47 | exec "$python" "$xpy" "$@" |
| 48 | fi |
| 49 | |
Joshua Nelson | 775c3c0 | 2022-07-31 19:02:31 | [diff] [blame] | 50 | echo "$0: error: did not find python installed" >&2 |
| 51 | exit 1 |