blob: 1a4b3dc5aacdfaa8f76094168537cec3895ce1fa [file] [log] [blame]
Brian Silverman6470f442018-08-05 12:08:16 -07001#!/bin/bash
2
3set -e
4set -u
5set -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.
16export 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 Schrader9e1b9bd2021-12-28 00:15:12 -080022PYTHON_BIN=""
Brian Silverman6470f442018-08-05 12:08:16 -070023for path in ${PYTHONPATH//:/ }; do
Philipp Schrader9e1b9bd2021-12-28 00:15:12 -080024 if [[ "$path" == *.runfiles/python3_9_x86_64-unknown-linux-gnu ]]; then
25 PYTHON_BIN="$path"/bin/python3
26 export LD_LIBRARY_PATH="$path"/lib
27 break
28 elif [[ "$path" == *.runfiles/python_repo ]]; then
29 PYTHON_BIN="$path"/usr/bin/python3
30 LD_LIBRARY_PATH="${path}/lib/x86_64-linux-gnu:${path}/usr/lib:${path}/usr/lib/x86_64-linux-gnu:${path}/../matplotlib_repo/usr/lib"
31 LD_LIBRARY_PATH+=":${path}/usr/lib/lapack:${path}/usr/lib/libblas:${path}/../matplotlib_repo/rpathed3/usr/lib:${path}/usr/lib/x86_64-linux-gnu/lapack:${path}/usr/lib/x86_64-linux-gnu/blas"
32 export LD_LIBRARY_PATH
Brian Silverman6470f442018-08-05 12:08:16 -070033 break
34 fi
35done
36
Philipp Schrader9e1b9bd2021-12-28 00:15:12 -080037if [[ -z "$PYTHON_BIN" ]]; then
Brian Silverman6470f442018-08-05 12:08:16 -070038 echo "Could not find Python base path." >&2
39 echo "More sophisticated logic may be needed." >&2
40 exit 1
41fi
42
Philipp Schrader092a62d2021-12-05 23:15:05 -080043# Prevent Python from importing the host's installed packages.
Philipp Schrader9e1b9bd2021-12-28 00:15:12 -080044exec "$PYTHON_BIN" -sS "$@"