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. |
| 28 | for 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 |
| 37 | done |
Albert Larsan | c83ddae | 2022-10-13 08:20:39 | [diff] [blame] | 38 | |
| 39 | python=$(bash -c "compgen -c python" | grep '^python[2-3]\.[0-9]\+$' | head -n1) |
| 40 | if ! [ "$python" = "" ]; then |
| 41 | exec "$python" "$xpy" "$@" |
| 42 | fi |
| 43 | |
Joshua Nelson | 775c3c0 | 2022-07-31 19:02:31 | [diff] [blame] | 44 | echo "$0: error: did not find python installed" >&2 |
| 45 | exit 1 |