xref: /aosp_15_r20/external/tink/kokoro/testutils/install_python3.sh (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1#!/bin/bash
2
3# Copyright 2022 Google LLC
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16################################################################################
17
18readonly DEFAULT_PYTHON_VERSION=3.7.1
19
20# This scripts installs Python 3 at a given version; if the version is not
21# specified, DEFAULT_PYTHON_VERSION is used.
22#
23# NOTEs:
24#   * If not running on Kokoro, this script will do nothing.
25#   * This script MUST be sourced to update the environment of the calling
26#     script.
27#
28# Usage:
29#   source ./kokoro/testutils/install_python3.sh [version]
30
31#######################################
32# Install Python 3 at a given version.
33# Globals:
34#   DEFAULT_PYTHON_VERSION
35# Arguments:
36#   python_version: Python version to use; default is DEFAULT_PYTHON_VERSION.
37#######################################
38install_python3() {
39  python_version="${1:-${DEFAULT_PYTHON_VERSION}}"
40  # Update the list of Python versions.
41  (
42    cd /home/kbuilder/.pyenv/plugins/python-build/../..
43    git pull
44  )
45  # Install Python.
46  eval "$(pyenv init -)"
47  pyenv install "${python_version}"
48  pyenv global "${python_version}"
49  # Debug output to check we are using the right version.
50  echo "Using python3: $(which python3)"
51  python3 --version
52}
53
54if [[ -n "${KOKORO_ROOT:-}" ]] ; then
55  install_python3 "$@"
56fi
57