Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Commit

Permalink
Refactor install_tink_via_pip
Browse files Browse the repository at this point in the history
 * Modify the script to take as a second parameter the folder where to look for Tink Python's dependencies; this is mostly useful to github.com/tink-crypto/tink-py where Tink Python's dependencies aren't in the same repository.
 * Remove the need to pin `setuptools` in macOS.
 * Install dependencies from `requirements.txt` before installing Tink Python and use `--require-hashes`.
 * Some other minor refactorings.
 * Update call sites accordingly

PiperOrigin-RevId: 538170951
  • Loading branch information
morambro authored and copybara-github committed Jun 6, 2023
1 parent a6b2384 commit fd589d9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 34 deletions.
2 changes: 1 addition & 1 deletion kokoro/gcp_ubuntu_per_language/python/pip/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fi
# Sourcing required to update callers environment.
source ./kokoro/testutils/install_python3.sh
source ./kokoro/testutils/install_protoc.sh
source ./kokoro/testutils/install_tink_via_pip.sh "${PWD}/python"
./kokoro/testutils/install_tink_via_pip.sh "${PWD}/python" "${PWD}"

cd python

Expand Down
2 changes: 1 addition & 1 deletion kokoro/macos_external/python/pip/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fi

./kokoro/testutils/copy_credentials.sh "python/testdata" "all"
source ./kokoro/testutils/install_protoc.sh
source ./kokoro/testutils/install_tink_via_pip.sh "${PWD}/python"
./kokoro/testutils/install_tink_via_pip.sh "${PWD}/python" "${PWD}"

# Get root certificates for gRPC
curl -OLsS https://raw.githubusercontent.com/grpc/grpc/master/etc/roots.pem
Expand Down
2 changes: 1 addition & 1 deletion kokoro/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ run_java_examples_tests() {
run_py_examples_tests() {
use_bazel "$(cat python/examples/.bazelversion)"
## Install Tink and its dependencies via pip for the examples/python tests.
source ./kokoro/testutils/install_tink_via_pip.sh "${PWD}/python"
./kokoro/testutils/install_tink_via_pip.sh "${PWD}/python" "${PWD}"
if [[ "${IS_KOKORO}" == "true" ]]; then
local pip_flags=( --require-hashes )
if [[ "${PLATFORM}" == "darwin" ]]; then
Expand Down
86 changes: 55 additions & 31 deletions kokoro/testutils/install_tink_via_pip.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,73 @@
# This scripts installs Tink for Python and its dependencies using Pip.
# Tink's root folder must be specified.
#
# NOTES:
# * If not running on Kokoro, this script will do nothing.
# * This script *must* be sourced to update the environment of the calling
# script.
# * The required Bazel version *must* be installed before running this script.
#
# Usage:
# source ./kokoro/testutils/install_tink_via_pip.sh <path to tink python root>
# NOTE: If not running on Kokoro, this script will do nothing.

set -eo pipefail

usage() {
cat <<EOF
Usage: $0 <path to tink python root> <tink python deps dir>
-h: Help. Print this usage information.
EOF
exit 1
}

install_tink_via_pip() {
local tink_py_path="${1}"
TINK_PY_ROOT_DIR=
TINK_PY_DEPS_BASE_DIR=

#######################################
# Process command line arguments.
#######################################
process_args() {
# Parse options.
while getopts "h" opt; do
case "${opt}" in
*) usage ;;
esac
done
shift $((OPTIND - 1))
TINK_PY_ROOT_DIR="$1"
if [[ -z "${TINK_PY_ROOT_DIR}" ]]; then
echo "ERROR: The root folder of Tink Python must be specified" >&2
usage
fi
TINK_PY_DEPS_BASE_DIR="$2"
if [[ -z "${TINK_PY_DEPS_BASE_DIR}" ]]; then
echo "ERROR: The folder containing Tink Python's dependencies must be \
specified" >&2
usage
fi
readonly TINK_PY_ROOT_DIR
readonly TINK_PY_DEPS_BASE_DIR
}


main() {
process_args "$@"
if [[ -z "${KOKORO_ROOT:-}" ]] ; then
echo "Not running on Kokoro, skip installing tink-py"
return
fi
(
cd "${tink_py_path}"
readonly local platform="$(uname | tr '[:upper:]' '[:lower:]')"
local setuptools_requirements="setuptools"
cd "${TINK_PY_ROOT_DIR}"
local -r platform="$(uname | tr '[:upper:]' '[:lower:]')"
local -a pip_flags
if [[ "${platform}" == 'darwin' ]]; then
# On MacOS we need to use the --user flag as otherwise pip will complain
# about permissions.
pip_flags=( --user )
# TODO(b/219813176): Remove once Kokoro environment is compatible.
setuptools_requirements="setuptools==60.9.0"
fi
readonly pip_flags

# Set base path to the Tink Python's dependencies.
export TINK_PYTHON_SETUPTOOLS_OVERRIDE_BASE_PATH="${PWD}/.."

# Temporary disable treating unset variables generating errors to avoid old
# versions of bash generating errors when expanding empty pip_flags array.
set +u
# Check if we can build Tink python package.
pip3 install "${pip_flags[@]}" --upgrade pip
pip3 install "${pip_flags[@]}" --upgrade "${setuptools_requirements}"
export TINK_PYTHON_SETUPTOOLS_OVERRIDE_BASE_PATH="${TINK_PY_DEPS_BASE_DIR}"
pip3 install "${pip_flags[@]}" --upgrade pip setuptools
# Install Tink Python requirements.
pip3 install "${pip_flags[@]}" --require-hashes -r requirements.txt
# Install Tink Python
pip3 install "${pip_flags[@]}" .
set -u
)
}

if [[ -n "${KOKORO_ROOT:-}" ]] ; then
if (( "$#" < 1 )); then
echo "Tink Python folder path must be specified" >&2
exit 1
fi
install_tink_via_pip "$@"
fi
main "$@"

0 comments on commit fd589d9

Please sign in to comment.