#!/bin/bash source $(dirname "$0")/activate PYTHON_VERSION=${PYTHON_VERSION:-} function python() { # call get_python_exe only the first time it is required export PYTHON_EXECUTABLE=${PYTHON_EXECUTABLE:-$(python_executable)} if [ -x "${PYTHON_EXECUTABLE}" ]; then "${PYTHON_EXECUTABLE}" "$@" else exit 1 fi } function python_executable() { local version=${PYTHON_VERSION:-} local executables=( $( list_python_executables ) ) for executable in "${executables[@]}"; do if "${executable}" -c "${SELECT_PYTHON_EXECUTABLE_SCRIPT}" "${version}"; then echo "${executable}" exit 0 fi done fail 1 "No such Python executable (PYTHON_VERSION=${PYTHON_VERSION})" } function list_python_executables() { PATH=${CI_TOOLS_ORIGINAL_PATH} which $(list_python_names) 2> /dev/null } function list_python_names() { local version=${PYTHON_VERSION} if [ "${version}" != "" ]; then echo "python${version}" fi echo python3.7 echo python3.6 echo python3 echo python } function fail() { local error=$1 shift echo "$@" 1>&2 exit "${error}" } read -r -d '' SELECT_PYTHON_EXECUTABLE_SCRIPT << END_OF_SCRIPT import sys version = ".".join(str(i) for i in sys.version_info[:3]) if version.startswith(sys.argv[1]): exit(0) else: exit(1) END_OF_SCRIPT if [ $(basename "$0") == python ]; then set -euo pipefail python "$@" fi