From 27e46c8c395cdb9675356e6526b8e4260b9f179a Mon Sep 17 00:00:00 2001 From: Federico Ressi Date: Tue, 25 Jun 2019 12:30:13 +0200 Subject: [PATCH] Update CI tools with an openstack wrapper Change-Id: I786884c540962fd908b0d323938aa9733ead65ce --- tools/ci/lib | 3 ++ tools/ci/os | 50 ++++++++++++++++++++++ tools/ci/python_prefix | 6 +++ tools/ci/realpath | 8 ++++ tools/ci/tox | 94 ++++++++++++++++++++++++++++++++++-------- 5 files changed, 143 insertions(+), 18 deletions(-) create mode 100644 tools/ci/lib create mode 100755 tools/ci/os create mode 100755 tools/ci/python_prefix create mode 100755 tools/ci/realpath diff --git a/tools/ci/lib b/tools/ci/lib new file mode 100644 index 000000000..be9075464 --- /dev/null +++ b/tools/ci/lib @@ -0,0 +1,3 @@ +CI_TOOLS_DIR=$(dirname "${BASH_SOURCE[0]}") +CI_TOOLS_DIR=$("${CI_TOOLS_DIR}/realpath" "${CI_TOOLS_DIR}") +PATH=${CI_TOOLS_DIR}:${PATH} diff --git a/tools/ci/os b/tools/ci/os new file mode 100755 index 000000000..b9c7e76ee --- /dev/null +++ b/tools/ci/os @@ -0,0 +1,50 @@ +#!/bin/bash + +set -eu + +source $(dirname "$0")/lib + +OS_VIRTUAL_ENV=$(realpath "${OS_VIRTUAL_ENV:-.tox/scenario}") + + +function os { + os_setup + + openstack "$@" +} + + +function os_setup { + if ! os_activate; then + # Cleanup and create virtualenv directory + tox -r -e venv --notest + + os_activate + fi +} + + +function os_activate { + local venv_script=${OS_VIRTUAL_ENV}/bin/activate + if ! [ -r "${venv_script}" ]; then + return 1 + fi + + if ! os_is_active; then + # Activate only once + set +eu + source "${venv_script}" + set -eu + os_is_active + fi +} + + +function os_is_active { + [ "$(python_prefix)" == "${OS_VIRTUAL_ENV}" ] +} + + +if [ $(basename "$0") == os ]; then + os "$@" +fi diff --git a/tools/ci/python_prefix b/tools/ci/python_prefix new file mode 100755 index 000000000..7d78936a5 --- /dev/null +++ b/tools/ci/python_prefix @@ -0,0 +1,6 @@ +#!/usr/bin/env python + +import os +import sys + +print(os.path.realpath(sys.prefix)) diff --git a/tools/ci/realpath b/tools/ci/realpath new file mode 100755 index 000000000..cd1d83c9c --- /dev/null +++ b/tools/ci/realpath @@ -0,0 +1,8 @@ +#!/usr/bin/env python + +import os +import sys + +args = sys.argv[1:] or ['.'] +results = [os.path.realpath(a) for a in args] +print(' '.join(results)) diff --git a/tools/ci/tox b/tools/ci/tox index b26e314ff..c7141d01f 100755 --- a/tools/ci/tox +++ b/tools/ci/tox @@ -1,23 +1,81 @@ #!/bin/bash -set -eux +set -eu -# Prefer python3 on python2 -PYTHON=$(which python3 || which python) +source $(dirname "$0")/lib -# Create a virtualenv for executing Tox and activate it -BASE_VIRTUALENV_DIR=.tox/base -if ! [ -d "${BASE_VIRTUALENV_DIR}" ]; then - "${PYTHON}" -m virtualenv "${BASE_VIRTUALENV_DIR}" - set +eux - source "${BASE_VIRTUALENV_DIR}/bin/activate" - set -eux - curl https://bootstrap.pypa.io/get-pip.py | python - pip install --upgrade setuptools wheel virtualenv tox -else - set +eux - source "${BASE_VIRTUALENV_DIR}/bin/activate" - set -eux + +# Prefer python 3 over python 2 +TOX_BASE_PYTHON=${PYTHON:-$(which python3 || which python)} +TOX_VIRTUAL_ENV=$(realpath "${TOX_VIRTUAL_ENV:-.tox/tox}") + + +function tox { + tox_setup + "${TOX_VIRTUAL_ENV}/bin/tox" "$@" +} + + +function tox_python { + "${TOX_VIRTUAL_ENV}/bin/python" "$@" +} + + +function tox_pip { + "${TOX_VIRTUAL_ENV}/bin/pip" "$@" +} + + +function tox_setup { + if ! tox_activate; then + tox_install_deps + + # Cleanup and create virtualenv directory + rm -fR "${TOX_VIRTUAL_ENV}" + mkdir -p $(dirname "${TOX_VIRTUAL_ENV}") + "${TOX_BASE_PYTHON}" -m virtualenv "${TOX_VIRTUAL_ENV}" + + # Activate virtualenv + if tox_activate; then + # Install/upgrade the last Python packages into the new virutalenv + curl https://bootstrap.pypa.io/get-pip.py | tox_python + tox_pip install --upgrade setuptools wheel virtualenv tox + fi + fi +} + + +function tox_install_deps { + if ! "${TOX_BASE_PYTHON}" -m pip --version; then + curl https://bootstrap.pypa.io/get-pip.py --user | "${TOX_BASE_PYTHON}" + fi + if ! "${TOX_BASE_PYTHON}" -m virtualenv --version; then + "${TOX_BASE_PYTHON}" -m pip install --user virtualenv + fi +} + + +function tox_activate { + local venv_script=${TOX_VIRTUAL_ENV}/bin/activate + if ! [ -r "${venv_script}" ]; then + return 1 + fi + + if ! tox_is_active; then + # Activate only once + set +eu + source "${venv_script}" + set -eu + tox_is_active + fi +} + + +function tox_is_active { + [ "$(python_prefix)" == "${TOX_VIRTUAL_ENV}" ] +} + + +if [ $(basename "$0") == tox ]; then + tox "$@" fi - -tox "$*"