blob: be85c69d869f3908933d73f49cb0ef5bfbed35e6 [file] [log] [blame]
#!/bin/sh -u
# Don't set -e because we don't have robust trapping and printing of errors.
# We use /bin/sh rather than /bin/bash for portability. See discussion here:
# https://groups.google.com/forum/?nomobile=true#!topic/bazel-dev/4Ql_7eDcLC0
# We do lose the ability to set -o pipefail.
# TODO(#7843): integration tests for failure to find python / wrong version
# found / error while trying to print version
FAILURE_HEADER="\
Error occurred while attempting to use the default Python toolchain \
(@bazel_tools//tools/python:autodetecting_toolchain)."
die() {
echo "$FAILURE_HEADER" 1>&2
echo "$1" 1>&2
exit 1
}
# Try the "python%VERSION%" command name first, then fall back on "python".
PYTHON_BIN=`which python%VERSION% || echo ""`
USED_FALLBACK=0
if [ -z "${PYTHON_BIN:-}" ]; then
PYTHON_BIN=`which python || echo ""`
USED_FALLBACK=1
fi
if [ -z "${PYTHON_BIN:-}" ]; then
die "Neither 'python%VERSION%' nor 'python' were found on the target \
platform's PATH, which is:
$PATH
Please ensure an interpreter is available on this platform (and marked \
executable), or else register an appropriate Python toolchain as per the \
documentation for py_runtime_pair \
(https://github.com/bazelbuild/bazel/blob/master/tools/python/toolchain.bzl)."
fi
# Verify that we grabbed an interpreter with the right version.
VERSION_STR=`"$PYTHON_BIN" -V 2>&1` \
|| die "Could not get interpreter version via '$PYTHON_BIN -V'"
if ! echo "$VERSION_STR" | grep -q " %VERSION%\." ; then
die "According to '$PYTHON_BIN -V', version is '$VERSION_STR', but we \
need version %VERSION%. PATH is:
$PATH
Please ensure an interpreter with version %VERSION% is available on this \
platform as 'python%VERSION%' or 'python', or else register an appropriate \
Python toolchain as per the documentation for py_runtime_pair \
(https://github.com/bazelbuild/bazel/blob/master/tools/python/toolchain.bzl)."
fi
exec "$PYTHON_BIN" "$@"