From 1929871d164eb40956d9a08569b5a2d704227a67 Mon Sep 17 00:00:00 2001 From: Federico Ressi Date: Fri, 15 Nov 2019 12:33:18 +0100 Subject: [PATCH] Allow to pick python version in CI tools Change-Id: I96c1e26af6aaefa26b164ed9dd9ff0e29fb702af --- roles/run-tox/defaults/main.yaml | 1 + roles/run-tox/tasks/main.yaml | 10 +++--- tools/ci/activate | 11 +++++-- tools/ci/python | 56 ++++++++++++++++++++++++++++++++ tools/ci/tox | 5 +-- zuul.d/tobiko-devstack.yaml | 2 ++ 6 files changed, 76 insertions(+), 9 deletions(-) create mode 100755 tools/ci/python diff --git a/roles/run-tox/defaults/main.yaml b/roles/run-tox/defaults/main.yaml index 9cbcb4b27..f4d4523b2 100644 --- a/roles/run-tox/defaults/main.yaml +++ b/roles/run-tox/defaults/main.yaml @@ -1,3 +1,4 @@ tox_dir: /opt/stack/tobiko tox_envlist: scenario tox_extra_args: '' +tox_python_version: '' diff --git a/roles/run-tox/tasks/main.yaml b/roles/run-tox/tasks/main.yaml index 32a555850..1d3bc5043 100644 --- a/roles/run-tox/tasks/main.yaml +++ b/roles/run-tox/tasks/main.yaml @@ -1,6 +1,8 @@ -- name: Run Tobiko - command: tools/ci/tox -e {{tox_envlist}} {{tox_extra_args}} - args: - chdir: "{{tox_dir}}" +- name: Run Tobiko test cases + shell: + chdir: "{{ tox_dir }}" + cmd: | + export PYTHON_VERSION={{ tox_python_version }} + tools/ci/tox -e "{{ tox_envlist }}" {{ tox_extra_args }} become: true become_user: stack diff --git a/tools/ci/activate b/tools/ci/activate index be9075464..627445cce 100644 --- a/tools/ci/activate +++ b/tools/ci/activate @@ -1,3 +1,8 @@ -CI_TOOLS_DIR=$(dirname "${BASH_SOURCE[0]}") -CI_TOOLS_DIR=$("${CI_TOOLS_DIR}/realpath" "${CI_TOOLS_DIR}") -PATH=${CI_TOOLS_DIR}:${PATH} +function tools_dir { + local tools_dir=$(dirname "${BASH_SOURCE[0]}") + PATH=${CI_TOOLS_ORIGINAL_PATH:-${PATH}} "${tools_dir}/realpath" "${tools_dir}" +} + +export CI_TOOLS_DIR=${CI_TOOLS_DIR:-$(tools_dir)} +export CI_TOOLS_ORIGINAL_PATH=${CI_TOOLS_ORIGINAL_PATH:-${PATH}} +export PATH=${CI_TOOLS_DIR}:${PATH} diff --git a/tools/ci/python b/tools/ci/python new file mode 100755 index 000000000..e686d9d49 --- /dev/null +++ b/tools/ci/python @@ -0,0 +1,56 @@ +#!/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)} + "${PYTHON_EXECUTABLE}" "$@" +} + + +function python_executable() { + local version=${PYTHON_VERSION} + + select_python_executables "${version}" \ + $(list_python_executables "${version}") | head -1 +} + + +function list_python_executables() { + PATH=${CI_TOOLS_ORIGINAL_PATH} which $(list_python_names "${version}") +} + + +function list_python_names() { + local version=${1:-} + if [ "${version}" != "" ]; then + echo "python${version}" + fi + echo python3 + echo python +} + + +function select_python_executables() { + local version=${1:-} + local executable + shift + for executable in "$@"; do + "${executable}" -c "${SELECT_PYTHON_EXECUTABLE_SCRIPT}" "${version}" + done +} + + +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]): + print(sys.executable) +END_OF_SCRIPT + + +python "$@" diff --git a/tools/ci/tox b/tools/ci/tox index 91b5e1acc..3593d42f7 100755 --- a/tools/ci/tox +++ b/tools/ci/tox @@ -6,7 +6,8 @@ source $(dirname "$0")/activate # Prefer python 3 over python 2 -TOX_BASE_PYTHON=${PYTHON:-$(which python3 || which python)} +export PYTHON_VERSION=${PYTHON_VERSION:-3} +TOX_BASE_PYTHON=${PYTHON:-${CI_TOOLS_DIR}/python} TOX_VIRTUAL_ENV=$(realpath "${TOX_VIRTUAL_ENV:-.tox/tox}") @@ -47,7 +48,7 @@ function tox_setup { function tox_install_deps { if ! "${TOX_BASE_PYTHON}" -m pip --version; then - curl https://bootstrap.pypa.io/get-pip.py --user | "${TOX_BASE_PYTHON}" + curl https://bootstrap.pypa.io/get-pip.py | "${TOX_BASE_PYTHON}" - --user fi if ! "${TOX_BASE_PYTHON}" -m virtualenv --version; then "${TOX_BASE_PYTHON}" -m pip install --user virtualenv diff --git a/zuul.d/tobiko-devstack.yaml b/zuul.d/tobiko-devstack.yaml index ee976102f..ea07dcd56 100644 --- a/zuul.d/tobiko-devstack.yaml +++ b/zuul.d/tobiko-devstack.yaml @@ -17,6 +17,7 @@ - x/tobiko timeout: 7200 vars: + tox_python_version: 3 devstack_localrc: MULTI_HOST: 0 USE_PYTHON3: true @@ -225,6 +226,7 @@ devstack_localrc: # CentOS has no support for Python3 USE_PYTHON3: false + tox_python_version: 2 - job: