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? |
| 22 | BASE_PATH="" |
| 23 | for path in ${PYTHONPATH//:/ }; do |
| 24 | if [[ "$path" == *.runfiles/python_repo ]]; then |
| 25 | BASE_PATH="$path" |
| 26 | export LD_LIBRARY_PATH="$path"/lib/x86_64-linux-gnu:"$path"/usr/lib:"$path"/usr/lib/x86_64-linux-gnu |
| 27 | break |
| 28 | fi |
| 29 | done |
| 30 | |
| 31 | if [[ -z "$BASE_PATH" ]]; then |
| 32 | echo "Could not find Python base path." >&2 |
| 33 | echo "More sophisticated logic may be needed." >&2 |
| 34 | exit 1 |
| 35 | fi |
| 36 | |
| 37 | export LD_LIBRARY_PATH="${BASE_PATH}/usr/lib/lapack:${BASE_PATH}/usr/lib/libblas:${BASE_PATH}/usr/lib/x86_64-linux-gnu" |
| 38 | |
Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 39 | exec "$BASE_PATH"/usr/bin/python3 "$@" |