1#!/bin/sh
2#
3# Copyright 2013 Google Inc. All Rights Reserved.
4#
5
6echo Welcome to the Google Cloud SDK!
7
8# <cloud-sdk-sh-preamble>
9#
10#  CLOUDSDK_ROOT_DIR            (a)  installation root dir
11#  CLOUDSDK_PYTHON              (u)  python interpreter path
12#  CLOUDSDK_GSUTIL_PYTHON       (u)  python interpreter path for gsutil
13#  CLOUDSDK_PYTHON_ARGS         (u)  python interpreter arguments
14#  CLOUDSDK_PYTHON_SITEPACKAGES (u)  use python site packages
15#  CLOUDSDK_BQ_PYTHON           (u)  python interpreter for bq
16#
17# (a) always defined by the preamble
18# (u) user definition overrides preamble
19
20# Wrapper around 'which' and 'command -v', tries which first, then falls back
21# to command -v
22_cloudsdk_which() {
23  which "$1" 2>/dev/null || command -v "$1" 2>/dev/null
24}
25
26# Check whether passed in python command reports major version 3.
27_is_python3() {
28  echo "$("$1" -V 2>&1)" | grep -E "Python 3" > /dev/null
29}
30
31# For Python 3, gsutil requires Python 3.5+.
32_py3_interpreter_compat_with_gsutil () {
33  # Some environments (e.g. macOS) don't support grep -P, so we use grep -E.
34  echo "$("$1" -V 2>&1)" | grep -E "Python 3[.]([5-9]|[1-9][0-9])" > /dev/null
35}
36
37order_python() {
38  selected_version=""
39  for python_version in "$@"
40  do
41    if [ -z "$selected_version" ]; then
42      if _cloudsdk_which $python_version > /dev/null && "$python_version" -c "import sys; print(sys.version)" > /dev/null; then
43        selected_version=$python_version
44      fi
45    fi
46  done
47  if [ -z "$selected_version" ]; then
48    selected_version=python
49  fi
50  echo $selected_version
51}
52
53# Determines the real cloud sdk root dir given the script path.
54# Would be easier with a portable "readlink -f".
55_cloudsdk_root_dir() {
56  case $1 in
57  /*)   _cloudsdk_path=$1
58        ;;
59  */*)  _cloudsdk_path=$PWD/$1
60        ;;
61  *)    _cloudsdk_path=$(_cloudsdk_which $1)
62        case $_cloudsdk_path in
63        /*) ;;
64        *)  _cloudsdk_path=$PWD/$_cloudsdk_path ;;
65        esac
66        ;;
67  esac
68  _cloudsdk_dir=0
69  while :
70  do
71    while _cloudsdk_link=$(readlink "$_cloudsdk_path")
72    do
73      case $_cloudsdk_link in
74      /*) _cloudsdk_path=$_cloudsdk_link ;;
75      *)  _cloudsdk_path=$(dirname "$_cloudsdk_path")/$_cloudsdk_link ;;
76      esac
77    done
78    case $_cloudsdk_dir in
79    1)  break ;;
80    esac
81    if [ -d "${_cloudsdk_path}" ]; then
82      break
83    fi
84    _cloudsdk_dir=1
85    _cloudsdk_path=$(dirname "$_cloudsdk_path")
86  done
87  while :
88  do  case $_cloudsdk_path in
89      */)     _cloudsdk_path=$(dirname "$_cloudsdk_path/.")
90              ;;
91      */.)    _cloudsdk_path=$(dirname "$_cloudsdk_path")
92              ;;
93      */bin)  dirname "$_cloudsdk_path"
94              break
95              ;;
96      *)      echo "$_cloudsdk_path"
97              break
98              ;;
99      esac
100  done
101}
102CLOUDSDK_ROOT_DIR=$(_cloudsdk_root_dir "$0")
103
104setup_cloudsdk_python() {
105  # if $CLOUDSDK_PYTHON is not set, look for bundled python else
106  # prefer python3 over python2
107  if [ -z "$CLOUDSDK_PYTHON" ]; then
108    # Is bundled python present?
109    if [ -x "$CLOUDSDK_ROOT_DIR/platform/bundledpythonunix/bin/python3" ];
110    then
111      CLOUDSDK_PYTHON="$CLOUDSDK_ROOT_DIR/platform/bundledpythonunix/bin/python3"
112      CLOUDSDK_PYTHON_SITEPACKAGES=1
113    else
114      CLOUDSDK_PYTHON=$(order_python python3 python2 python2.7 python)
115    fi
116  fi
117}
118
119setup_cloudsdk_python
120
121# $PYTHONHOME can interfere with gcloud. Users should use
122# CLOUDSDK_PYTHON to configure which python gcloud uses.
123unset PYTHONHOME
124
125# if CLOUDSDK_PYTHON_SITEPACKAGES and VIRTUAL_ENV are empty
126case :$CLOUDSDK_PYTHON_SITEPACKAGES:$VIRTUAL_ENV: in
127:::)  # add -S to CLOUDSDK_PYTHON_ARGS if not already there
128      case " $CLOUDSDK_PYTHON_ARGS " in
129      *" -S "*) ;;
130      "  ")     CLOUDSDK_PYTHON_ARGS="-S"
131                ;;
132      *)        CLOUDSDK_PYTHON_ARGS="$CLOUDSDK_PYTHON_ARGS -S"
133                ;;
134      esac
135      unset CLOUDSDK_PYTHON_SITEPACKAGES
136      ;;
137*)    # remove -S from CLOUDSDK_PYTHON_ARGS if already there
138      while :; do
139        case " $CLOUDSDK_PYTHON_ARGS " in
140        *" -S "*) CLOUDSDK_PYTHON_ARGS=${CLOUDSDK_PYTHON_ARGS%%-S*}' '${CLOUDSDK_PYTHON_ARGS#*-S} ;;
141        *) break ;;
142        esac
143      done
144      # if CLOUDSDK_PYTHON_SITEPACKAGES is empty
145      [ -z "$CLOUDSDK_PYTHON_SITEPACKAGES" ] &&
146        CLOUDSDK_PYTHON_SITEPACKAGES=1
147      export CLOUDSDK_PYTHON_SITEPACKAGES
148      ;;
149esac
150
151# Allow users to set the Python interpreter used to launch gsutil, falling
152# back to the CLOUDSDK_PYTHON interpreter otherwise.
153if [ -z "$CLOUDSDK_GSUTIL_PYTHON" ]; then
154  CLOUDSDK_GSUTIL_PYTHON="$CLOUDSDK_PYTHON"
155fi
156
157if [ -z "$CLOUDSDK_BQ_PYTHON" ]; then
158  CLOUDSDK_BQ_PYTHON="$CLOUDSDK_PYTHON"
159fi
160
161export CLOUDSDK_ROOT_DIR
162export CLOUDSDK_PYTHON_ARGS
163export CLOUDSDK_GSUTIL_PYTHON
164export CLOUDSDK_BQ_PYTHON
165
166case $HOSTNAME in
167  *.corp.google.com|*.c.googlers.com) export CLOUDSDK_GOOGLE_AUTH_IS_GOOGLE_DOMAIN=true;;
168esac
169
170# </cloud-sdk-sh-preamble>
171
172if [ -z "$CLOUDSDK_PYTHON" ]; then
173  if [ -z "$( _cloudsdk_which python)" ]; then
174    echo
175    echo "To use the Google Cloud SDK, you must have Python installed and on your PATH."
176    echo "As an alternative, you may also set the CLOUDSDK_PYTHON environment variable"
177    echo "to the location of your Python executable."
178    exit 1
179  fi
180fi
181
182# Warns user if they are running as root.
183if [ $(id -u) = 0 ]; then
184  echo "WARNING: You appear to be running this script as root. This may cause "
185  echo "the installation to be inaccessible to users other than the root user."
186fi
187
188"$CLOUDSDK_PYTHON" $CLOUDSDK_PYTHON_ARGS "${CLOUDSDK_ROOT_DIR}/bin/bootstrapping/install.py" "$@"
189