Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | set -e |
| 4 | set -u |
| 5 | set -o pipefail |
| 6 | |
| 7 | # We disable writing .pyc files here so that the invocation is more |
| 8 | # deterministic. If we get a corrupted .pyc file (for some reason) in the |
| 9 | # .runfiles directory the corresponding Python invocation would crash with an |
| 10 | # EOFError. You can try this by calling truncate(1) on a .pyc file and running |
| 11 | # your Python script. |
| 12 | # In the bazel sandbox none of the .pyc files are preserved anyway. |
| 13 | # Sandboxing also means that Python's entire standard library got cached which |
| 14 | # normally doesn't happen. That can lead to higher memory usage during the |
| 15 | # individual build steps. |
| 16 | export PYTHONDONTWRITEBYTECODE=1 |
| 17 | |
| 18 | # Find the path that contains the Python runtime. It's not always obvious. For |
| 19 | # example in a genrule the Python runtime is in the runfiles folder of the |
| 20 | # tool, not of the genrule. |
| 21 | # TODO(philipp): Is there a better way to do this? |
Philipp Schrader | 9e1b9bd | 2021-12-28 00:15:12 -0800 | [diff] [blame] | 22 | PYTHON_BIN="" |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 23 | for path in ${PYTHONPATH//:/ }; do |
Philipp Schrader | 9e1b9bd | 2021-12-28 00:15:12 -0800 | [diff] [blame] | 24 | if [[ "$path" == *.runfiles/python3_9_x86_64-unknown-linux-gnu ]]; then |
| 25 | PYTHON_BIN="$path"/bin/python3 |
Philipp Schrader | 7520ee6 | 2022-12-10 14:04:40 -0800 | [diff] [blame] | 26 | LD_LIBRARY_PATH=":${path}/lib" |
| 27 | LD_LIBRARY_PATH+=":${path}/../gtk_runtime/lib/x86_64-linux-gnu" |
| 28 | LD_LIBRARY_PATH+=":${path}/../gtk_runtime/usr/lib/x86_64-linux-gnu" |
| 29 | LD_LIBRARY_PATH+=":${path}/../gtk_runtime/usr/lib" |
| 30 | export LD_LIBRARY_PATH |
Philipp Schrader | 9e1b9bd | 2021-12-28 00:15:12 -0800 | [diff] [blame] | 31 | break |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 32 | fi |
| 33 | done |
| 34 | |
Philipp Schrader | 9e1b9bd | 2021-12-28 00:15:12 -0800 | [diff] [blame] | 35 | if [[ -z "$PYTHON_BIN" ]]; then |
Brian Silverman | 6470f44 | 2018-08-05 12:08:16 -0700 | [diff] [blame] | 36 | echo "Could not find Python base path." >&2 |
| 37 | echo "More sophisticated logic may be needed." >&2 |
| 38 | exit 1 |
| 39 | fi |
| 40 | |
Philipp Schrader | 092a62d | 2021-12-05 23:15:05 -0800 | [diff] [blame] | 41 | # Prevent Python from importing the host's installed packages. |
Philipp Schrader | 9e1b9bd | 2021-12-28 00:15:12 -0800 | [diff] [blame] | 42 | exec "$PYTHON_BIN" -sS "$@" |