tobiko/tools/ci/tox
Federico Ressi e41d31589b Install missing dependencies on Ubuntu
* Install missing binary dependencies on Ubuntu

* Workarounds known Ubuntu get-pip issue #43
  (https://github.com/pypa/get-pip/issues/43)

Change-Id: Ic38646443e8cd0f0bf46017d4c116620a49bb4dd
2020-01-09 12:13:25 +01:00

157 lines
3.5 KiB
Bash
Executable File

#!/bin/bash
set -eu
source $(dirname "$0")/activate
export PYTHON_VERSION=${PYTHON_VERSION:-}
TOX_BASE_PYTHON=${PYTHON:-${CI_TOOLS_DIR}/python}
TOX_VIRTUAL_ENV=$(realpath "${TOX_VIRTUAL_ENV:-.tox/tox}")
BINDEP=${BINDEP:-}
BINDEP_FILE=${BINDEP_FILE:-$(pwd)/bindep.txt}
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
tox_install_bindeps
fi
}
function tox_install_deps {
tox_install_pip
if ! "${TOX_BASE_PYTHON}" -m virtualenv --version; then
"${TOX_BASE_PYTHON}" -m pip install --user virtualenv
fi
}
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
local bindep=${BINDEP:-$(which bindep)}
if ! [ -x "${bindep}" ]; then
tox_pip install bindep
bindep=$(which bindep)
fi
# process ${BINDEP_FILE}
local missing_packages=( $("${bindep}" -b -f "${bindep_file}") )
if [ "${#missing_packages[@]}" != "0" ]; then
tox_install_packages "${missing_packages[@]}"
if ! "${bindep}" -f "${bindep_file}"; then
exit 1
fi
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 "Unsupported Linux distribution" 1>&2
exit 1
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