Repository navigation
Expand file tree
/
Copy pathruntime_env_toolchain_interpreter.sh
More file actions
executable file
·81 lines (71 loc) · 3.01 KB
/
Copy pathruntime_env_toolchain_interpreter.sh
File metadata and controls
executable file
·81 lines (71 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/sh
# Don't set -e because we don't have robust trapping and printing of errors.
set -u
# 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.
FAILURE_HEADER="\
Error occurred while attempting to use the deprecated Python toolchain \
(@rules_python//python/runtime_env_toolchain:all)."
die() {
echo "$FAILURE_HEADER" 1>&2
echo "$1" 1>&2
exit 1
}
# We use `which` to locate the Python interpreter command on PATH. `command -v`
# is another option, but it doesn't check whether the file it finds has the
# executable bit.
#
# A tricky situation happens when this wrapper is invoked as part of running a
# tool, e.g. passing a py_binary target to `ctx.actions.run()`. Bazel will unset
# the PATH variable. Then the shell will see there's no PATH and initialize its
# own, sometimes without exporting it. This causes `which` to fail and this
# script to think there's no Python interpreter installed. To avoid this we
# explicitly pass PATH to each `which` invocation. We can't just export PATH
# because that would modify the environment seen by the final user Python
# program.
#
# See also:
#
# https://github.com/bazelbuild/continuous-integration/issues/578
# https://github.com/bazelbuild/bazel/issues/8414
# https://github.com/bazelbuild/bazel/issues/8415
# Try the "python3" command name first, then fall back on "python".
PYTHON_BIN="$(PATH="$PATH" which python3 2> /dev/null)"
if [ -z "${PYTHON_BIN:-}" ]; then
PYTHON_BIN="$(PATH="$PATH" which python 2>/dev/null)"
fi
if [ -z "${PYTHON_BIN:-}" ]; then
die "Neither 'python3' 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/bazel-contrib/rules_python/blob/master/docs/python.md#py_runtime_pair)."
fi
# Because this is a wrapper script that invokes Python, it prevents Python from
# detecting virtualenvs like normal (i.e. using the venv symlink to find the
# real interpreter). To work around this, we have to manually detect the venv,
# then trick the interpreter into understanding we're in a virtual env.
self_dir=$(dirname "$0")
if [ -e "$self_dir/pyvenv.cfg" ] || [ -e "$self_dir/../pyvenv.cfg" ]; then
case "$0" in
/*)
venv_bin="$0"
;;
*)
venv_bin="$PWD/$0"
;;
esac
# PYTHONEXECUTABLE is also used because `exec -a` doesn't fully trick the
# pyenv wrappers.
# NOTE: The PYTHONEXECUTABLE envvar only works for non-Mac starting in Python 3.11
export PYTHONEXECUTABLE="$venv_bin"
# Python looks at argv[0] to determine sys.executable, so use exec -a
# to make it think it's the venv's binary, not the actual one invoked.
# NOTE: exec -a isn't strictly posix-compatible, but very widespread
exec -a "$venv_bin" "$PYTHON_BIN" "$@"
else
exec "$PYTHON_BIN" "$@"
fi