Geoffrey Irving | 4c85a08 | 2016-03-16 12:20:34 -0800 | [diff] [blame] | 1 | #!/usr/bin/env bash |
A. Unique TensorFlower | 122cdce | 2016-06-02 12:32:54 -0800 | [diff] [blame] | 2 | # Copyright 2015 The TensorFlow Authors. All Rights Reserved. |
Vijay Vasudevan | a4806a3 | 2015-12-03 10:26:25 -0800 | [diff] [blame] | 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # ============================================================================== |
Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 16 | |
| 17 | set -e -o errexit |
| 18 | |
Vijay Vasudevan | 8cc567b | 2016-05-26 11:05:13 -0800 | [diff] [blame] | 19 | if [ -d "../org_tensorflow" ]; then |
| 20 | script_path="../org_tensorflow" |
| 21 | else |
| 22 | # Prefix expected paths with ./ locally and external/reponame/ for remote repos. |
| 23 | # TODO(kchodorow): remove once runfiles paths are fixed, see |
| 24 | # https://github.com/bazelbuild/bazel/issues/848. |
| 25 | script_path=$(dirname $(dirname $(dirname "$0"))) |
| 26 | script_path=${script_path:-.} |
| 27 | fi |
| 28 | |
Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 29 | function main { |
A. Unique TensorFlower | 8e0d6f1 | 2017-04-21 06:35:18 -0800 | [diff] [blame^] | 30 | setup_python "$1" |
| 31 | exit 0 |
Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 32 | } |
| 33 | |
Olivia Nordquist | 4959a12 | 2016-08-09 11:16:44 -0800 | [diff] [blame] | 34 | function python_path { |
Patrick Nguyen | c5ab3dd | 2016-10-20 12:09:18 -0800 | [diff] [blame] | 35 | "$PYTHON_BIN_PATH" - <<END |
Olivia Nordquist | 4959a12 | 2016-08-09 11:16:44 -0800 | [diff] [blame] | 36 | from __future__ import print_function |
| 37 | import site |
| 38 | import os |
| 39 | |
| 40 | try: |
| 41 | input = raw_input |
| 42 | except NameError: |
| 43 | pass |
| 44 | |
| 45 | python_paths = [] |
| 46 | if os.getenv('PYTHONPATH') is not None: |
| 47 | python_paths = os.getenv('PYTHONPATH').split(':') |
Olivia Nordquist | 308e34f | 2016-08-23 09:27:27 -0800 | [diff] [blame] | 48 | try: |
Dan Mané | 54a7178 | 2016-09-09 16:07:46 -0800 | [diff] [blame] | 49 | library_paths = site.getsitepackages() |
Olivia Nordquist | 308e34f | 2016-08-23 09:27:27 -0800 | [diff] [blame] | 50 | except AttributeError: |
| 51 | from distutils.sysconfig import get_python_lib |
Dan Mané | 54a7178 | 2016-09-09 16:07:46 -0800 | [diff] [blame] | 52 | library_paths = [get_python_lib()] |
Olivia Nordquist | 308e34f | 2016-08-23 09:27:27 -0800 | [diff] [blame] | 53 | all_paths = set(python_paths + library_paths) |
Olivia Nordquist | 4959a12 | 2016-08-09 11:16:44 -0800 | [diff] [blame] | 54 | |
| 55 | paths = [] |
| 56 | for path in all_paths: |
| 57 | if os.path.isdir(path): |
| 58 | paths.append(path) |
| 59 | |
| 60 | if len(paths) == 1: |
| 61 | print(paths[0]) |
Benoit Steiner | 8b3d8c8 | 2016-08-16 14:01:13 -0800 | [diff] [blame] | 62 | else: |
Patrick Nguyen | c5ab3dd | 2016-10-20 12:09:18 -0800 | [diff] [blame] | 63 | ret_paths = ",".join(paths) |
Benoit Steiner | 8b3d8c8 | 2016-08-16 14:01:13 -0800 | [diff] [blame] | 64 | print(ret_paths) |
Olivia Nordquist | 4959a12 | 2016-08-09 11:16:44 -0800 | [diff] [blame] | 65 | END |
| 66 | } |
| 67 | |
| 68 | function default_python_path { |
Patrick Nguyen | c5ab3dd | 2016-10-20 12:09:18 -0800 | [diff] [blame] | 69 | PYTHON_ARG="$1" "$PYTHON_BIN_PATH" - <<END |
Olivia Nordquist | 4959a12 | 2016-08-09 11:16:44 -0800 | [diff] [blame] | 70 | from __future__ import print_function |
| 71 | import os |
| 72 | |
| 73 | default = os.getenv('PYTHON_ARG') |
| 74 | default = str(default) |
| 75 | print(default) |
| 76 | END |
| 77 | } |
| 78 | |
Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 79 | function setup_python { |
| 80 | PYTHON_BIN_PATH="$1"; |
| 81 | |
A. Unique TensorFlower | 8e0d6f1 | 2017-04-21 06:35:18 -0800 | [diff] [blame^] | 82 | # TODO(ngiraldo): move most of these checks to root configure |
Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 83 | if [ -z "$PYTHON_BIN_PATH" ]; then |
| 84 | echo "PYTHON_BIN_PATH was not provided. Did you run configure?" |
| 85 | exit 1 |
| 86 | fi |
| 87 | if [ ! -x "$PYTHON_BIN_PATH" ] || [ -d "$PYTHON_BIN_PATH" ]; then |
| 88 | echo "PYTHON_BIN_PATH is not executable. Is it the python binary?" |
| 89 | exit 1 |
| 90 | fi |
| 91 | |
Vijay Vasudevan | eb5e56e | 2015-12-03 14:39:42 -0800 | [diff] [blame] | 92 | local python_major_version=$("${PYTHON_BIN_PATH}" -c 'from __future__ import print_function; import sys; print(sys.version_info[0]);') |
| 93 | if [ "$python_major_version" == "" ]; then |
| 94 | echo -e "\n\nERROR: Problem getting python version. Is $PYTHON_BIN_PATH the correct python binary?" |
| 95 | exit 1 |
| 96 | fi |
| 97 | |
A. Unique TensorFlower | 8e0d6f1 | 2017-04-21 06:35:18 -0800 | [diff] [blame^] | 98 | # TODO(ngiraldo): confirm if these checks are really necessary, remove if not |
Xiaoqiang Zheng | e2d51a8 | 2016-10-28 10:29:28 -0800 | [diff] [blame] | 99 | if [ -z "$PYTHON_LIB_PATH" ]; then |
| 100 | local python_lib_path |
| 101 | # Split python_path into an array of paths, this allows path containing spaces |
| 102 | IFS=',' |
| 103 | python_lib_path=($(python_path)) |
| 104 | unset IFS |
Benoit Steiner | a771598 | 2016-11-09 13:14:03 -0800 | [diff] [blame] | 105 | |
| 106 | if [ 1 = "$USE_DEFAULT_PYTHON_LIB_PATH" ]; then |
Xiaoqiang Zheng | e2d51a8 | 2016-10-28 10:29:28 -0800 | [diff] [blame] | 107 | PYTHON_LIB_PATH="$(default_python_path "${python_lib_path[0]}")" |
Benoit Steiner | a771598 | 2016-11-09 13:14:03 -0800 | [diff] [blame] | 108 | echo "Using python library path: $PYTHON_LIB_PATH" |
| 109 | |
Olivia Nordquist | 4959a12 | 2016-08-09 11:16:44 -0800 | [diff] [blame] | 110 | else |
Benoit Steiner | a771598 | 2016-11-09 13:14:03 -0800 | [diff] [blame] | 111 | echo "Found possible Python library paths:" |
| 112 | for x in "${python_lib_path[@]}"; do |
| 113 | echo " $x" |
| 114 | done |
| 115 | set -- "${python_lib_path[@]}" |
| 116 | echo "Please input the desired Python library path to use. Default is ["$1"]" |
| 117 | read b || true |
| 118 | if [ "$b" == "" ]; then |
| 119 | PYTHON_LIB_PATH="$(default_python_path "${python_lib_path[0]}")" |
| 120 | echo "Using python library path: $PYTHON_LIB_PATH" |
| 121 | else |
| 122 | PYTHON_LIB_PATH="$b" |
| 123 | fi |
Olivia Nordquist | 4959a12 | 2016-08-09 11:16:44 -0800 | [diff] [blame] | 124 | fi |
Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 125 | fi |
Benoit Steiner | a771598 | 2016-11-09 13:14:03 -0800 | [diff] [blame] | 126 | |
Xiaoqiang Zheng | e2d51a8 | 2016-10-28 10:29:28 -0800 | [diff] [blame] | 127 | if test -d "$PYTHON_LIB_PATH" -a -x "$PYTHON_LIB_PATH"; then |
| 128 | python_lib="$PYTHON_LIB_PATH" |
| 129 | else |
| 130 | echo -e "\n\nERROR: Invalid python library path: ${PYTHON_LIB_PATH}." |
| 131 | exit 1 |
| 132 | fi |
Olivia Nordquist | 4959a12 | 2016-08-09 11:16:44 -0800 | [diff] [blame] | 133 | |
Patrick Nguyen | c5ab3dd | 2016-10-20 12:09:18 -0800 | [diff] [blame] | 134 | # Convert python path to Windows style before writing into bazel.rc |
| 135 | if is_windows; then |
| 136 | PYTHON_BIN_PATH="$(cygpath -m "$PYTHON_BIN_PATH")" |
| 137 | fi |
Vijay Vasudevan | eb5e56e | 2015-12-03 14:39:42 -0800 | [diff] [blame] | 138 | |
A. Unique TensorFlower | 8e0d6f1 | 2017-04-21 06:35:18 -0800 | [diff] [blame^] | 139 | # TODO(ngiraldo): move all below to root configure |
Vijay Vasudevan | eb5e56e | 2015-12-03 14:39:42 -0800 | [diff] [blame] | 140 | # Write tools/bazel.rc |
| 141 | echo "# Autogenerated by configure: DO NOT EDIT" > tools/bazel.rc |
| 142 | sed -e "s/\$PYTHON_MAJOR_VERSION/$python_major_version/g" \ |
Martin Wicke | bc456e3 | 2017-03-23 12:31:16 -0800 | [diff] [blame] | 143 | -e "s|\$PYTHON_BINARY|\"$PYTHON_BIN_PATH\"|g" \ |
Vijay Vasudevan | eb5e56e | 2015-12-03 14:39:42 -0800 | [diff] [blame] | 144 | tools/bazel.rc.template >> tools/bazel.rc |
Vijay Vasudevan | 019c679 | 2015-12-10 10:15:38 -0800 | [diff] [blame] | 145 | # Write tools/python_bin_path.sh |
Patrick Nguyen | c5ab3dd | 2016-10-20 12:09:18 -0800 | [diff] [blame] | 146 | echo "export PYTHON_BIN_PATH=\"$PYTHON_BIN_PATH\"" > tools/python_bin_path.sh |
Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Martin Wicke | 999b794 | 2016-09-21 13:16:48 -0800 | [diff] [blame] | 149 | PLATFORM="$(uname -s | tr 'A-Z' 'a-z')" |
| 150 | function is_windows() { |
| 151 | # On windows, the shell script is actually running in msys |
| 152 | if [[ "${PLATFORM}" =~ msys_nt* ]]; then |
| 153 | true |
| 154 | else |
| 155 | false |
| 156 | fi |
| 157 | } |
| 158 | |
Vijay Vasudevan | bf6b536 | 2015-12-02 15:04:40 -0800 | [diff] [blame] | 159 | main "$@" |