tobiko/tools/ci/tox

166 lines
3.7 KiB
Bash
Executable File

#!/bin/bash
set -eu
source $(dirname "$0")/activate
export PYTHON_VERSION=${PYTHON_VERSION:-}
export TOX_BASE_PYTHON=${TOX_BASE_PYTHON:-${CI_TOOLS_DIR}/python}
export TOX_VIRTUAL_ENV=$(realpath "${TOX_VIRTUAL_ENV:-.tox/tox}")
BINDEP=${BINDEP:-}
BINDEP_FILE=${BINDEP_FILE:-$(pwd)/bindep.txt}
function tox {
tox_setup 1>&2
"${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 --seeder pip "${TOX_VIRTUAL_ENV}"
# Activate virtualenv
tox_activate
# Install binary packages
tox_install_bindeps
# 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
}
function tox_install_deps {
tox_install_pip
local options="--upgrade"
if [ "${VIRTUAL_ENV:-}" == "" ]; then
options="${options} --user"
fi
"${TOX_BASE_PYTHON}" -m pip install ${options} virtualenv
}
function tox_install_pip {
if ! "${TOX_BASE_PYTHON}" -m pip --version; then
if which apt-get && ! "${TOX_BASE_PYTHON}" -c 'import distutils.util'; then
# Workaround known get-pip issue #43:
# https://github.com/pypa/get-pip/issues/43
sudo apt-get update
sudo apt-get install -y python3-distutils
fi
curl https://bootstrap.pypa.io/get-pip.py | "${TOX_BASE_PYTHON}" - --user
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}" ]
}
function tox_install_bindeps {
local bindep_file=${BINDEP_FILE:-$1}
if ! [ -r "${bindep_file}" ]; then
exit 0
fi
# process ${BINDEP_FILE}
local missing_packages=( $(bindep -b -f "${bindep_file}") )
if [ "${#missing_packages[@]}" != "0" ]; then
tox_install_packages "${missing_packages[@]}"
bindep -f "${bindep_file}" || true
fi
}
function bindep {
export BINDEP=${BINDEP:-$(get_bindep)}
"${BINDEP}" "$@"
}
function get_bindep {
if ! which bindep; then
tox_pip install bindep > /dev/null
which bindep
fi
}
function tox_install_packages {
local packages=( "$@" )
if which yum; then
if grep -e '^Red Hat Enterprise Linux' /etc/redhat-release; then
# Ensure *-server-opt repos are enabled
tox_enable_yum_repos '.*-server-opt\/'
fi
# install missing packages
sudo yum install -y "${packages[@]}"
elif which apt-get; then
sudo apt-get update
# install missing packages
sudo apt-get install -y "${packages[@]}"
else
echo -e "Don't know how to install packages this platform"
bindep --profiles 1>&2
fi
}
function tox_enable_yum_repos {
local yum_repos_regex=$1
local yum_repos=( $(yum repolist all |
awk '/'${yum_repos_regex}'/{print $1}' |
xargs -n1 dirname) )
if [ "${#yum_repos[@]}" != 0 ]; then
sudo yum-config-manager --enable "${yum_repos[@]}"
fi
}
if [ $(basename "$0") == tox ]; then
tox "$@"
fi