buildbot-scripts/travis2bash.sh

192 lines
5.4 KiB
Bash
Raw Normal View History

2021-02-10 13:26:46 +01:00
#!/usr/bin/env bash
# shellcheck disable=SC1003
# pulled from https://raw.githubusercontent.com/jasperes/bash-yaml/master/script/yaml.sh
# Licensed under MIT license
# Based on https://gist.github.com/pkuczynski/8665367
failure=0
parse_yaml() {
local yaml_file=$1
local prefix=$2
local s
local w
local fs
s='[[:space:]]*'
w='[a-zA-Z0-9_.-]*'
fs="$(echo @|tr @ '\034')"
(
sed -e '/- [^\“]'"[^\']"'.*: /s|\([ ]*\)- \([[:space:]]*\)|\1-\'$'\n'' \1\2|g' |
sed -ne '/^--/s|--||g; s|\"|\\\"|g; s/[[:space:]]*$//g;' \
-e "/#.*[\"\']/!s| #.*||g; /^#/s|#.*||g;" \
-e "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)${s}[:-]$s\(.*\)$s\$|\1$fs\2$fs\3|p" |
awk -F"$fs" '{
indent = length($1)/2;
if (length($2) == 0) { conj[indent]="+";} else {conj[indent]="";}
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s%s%s=(\"%s\")\n", "'"$prefix"'",vn, $2, conj[indent-1],$3);
}
}' |
sed -e 's/_=/+=/g' |
awk 'BEGIN {
FS="=";
OFS="="
}
/(-|\.).*=/ {
gsub("-|\\.", "_", $1)
}
{ print }'
) < "$yaml_file"
}
create_variables() {
local yaml_file="$1"
local prefix="$2"
eval "$(parse_yaml "$yaml_file" "$prefix")"
}
if [ ! -e .travis.yml ]; then
echo "No .travis.yml found, exiting"
exit 1
fi
function aptinstall() {
# apt install
# shellcheck disable=SC2154
if [[ "$(declare -p travis_addons_apt_packages)" =~ "declare -a" ]]; then
echo -e "\nsudo apt -y update\n"
sudo apt -y update
for ((i = 0; i < ${#travis_addons_apt_packages[@]}; i++))
do
echo -e "\napt-get -yq --no-install-suggests --no-install-recommends install ${travis_addons_apt_packages[$i]}\n"
# shellcheck disable=SC2086
sudo apt-get -yq --no-install-suggests --no-install-recommends \
install \
${travis_addons_apt_packages[$i]}
done
fi
}
function virtualenv_init() {
# init virtualenv directory
if [ -n "$3" ]; then
echo -e "\nvirtualenv -p \"$1\" \"$3\" ~/.venv-$2\n"
2021-02-14 11:41:17 +01:00
virtualenv -p "$1" "$3" ~/.venv-"$2"
elif [ -n "$2" ]; then
echo -e "\nvirtualenv -p \"$1\" ~/.venv-$2\n"
virtualenv -p "$1" ~/.venv-"$2"
2021-02-10 13:26:46 +01:00
else
echo -e "\nvirtualenv ~/.venv\n"
virtualenv ~/.venv
fi
}
function virtualenv_activate() {
# activate virtualenv
# shellcheck disable=SC1090
if [ -n "$1" ]; then
echo -e "\n. ~/.venv-$1/bin/activate\n"
. ~/.venv-"$1"/bin/activate
2021-02-10 13:26:46 +01:00
else
echo -e "\n. ~/.venv/bin/activate\n"
. ~/.venv/bin/activate
fi
}
function python_run() {
# shellcheck disable=SC2154
if [[ "$(declare -p travis_env)" =~ "declare -a" ]]; then
echo "Setting up environment variables"
for ((i = 0; i < ${#travis_env[@]}; i++))
do
echo -e "\n${travis_env[$i]}\n"
# shellcheck disable=SC2086
export ${travis_env[$i]}
done
fi
# shellcheck disable=SC2154
if [[ "$(declare -p travis_install)" =~ "declare -a" ]]; then
echo "Running \"install\""
if [ -n "$2" ]; then
if [ -e "requirements-${2}.txt" ]; then
echo "Running \"pip install -r requirements-${2}.txt\""
pip install -r "requirements-${2}.txt"
fi
fi
for ((i = 0; i < ${#travis_install[@]}; i++))
do
cd "$rundir" || continue
echo -e "\n${travis_install[$i]}\n"
if ! bash -c "${travis_install[$i]}"; then
failure=1
fi
done
fi
# shellcheck disable=SC2154
if [[ "$(declare -p travis_script)" =~ "declare -a" ]]; then
echo "Running \"script\""
for ((i = 0; i < ${#travis_script[@]}; i++))
do
cd "$rundir" || continue
echo -e "\n${travis_script[$i]}\n"
if ! bash -c "${travis_script[$i]}"; then
failure=1
fi
done
fi
}
create_variables .travis.yml travis_
rundir=$(pwd)
aptinstall
# shellcheck disable=SC2154
if [[ "$(declare -p travis_python)" =~ "declare -a" ]]; then
# shellcheck disable=SC2068
for pv in ${travis_python[@]}; do
# strip quotes
temp="${pv%\"}"
pv="${temp#\"}"
# strip crap
2021-02-14 11:41:17 +01:00
options=$(echo "$pv"|cut -d_ -f2-)
if [[ "$options" == with* ]]; then
options="--$(echo "$options"|cut -d_ -f2-|tr -- '_' '-')"
fi
pv=$(echo "$pv"|cut -d_ -f1)
2021-02-10 13:26:46 +01:00
ppath=/usr/bin/python$pv
if [ -n "$TRAVIS_PYTHON" ] && [ "$TRAVIS_PYTHON" != "$pv" ]; then
echo "skipping python-$pv due to TRAVIS_PYTHON=$TRAVIS_PYTHON"
2021-02-14 09:30:40 +01:00
continue
fi
2021-02-10 13:26:46 +01:00
#if [ ! -e "$ppath" ]; then
sudo apt -y install "python$pv" "python${pv}-dev"
#fi
2021-02-14 11:41:17 +01:00
virtualenv_init "$ppath" "$pv" "$options"
2021-02-10 13:26:46 +01:00
virtualenv_activate "$pv"
python_run "$ppath" "$pv"
deactivate
done
else
virtualenv_init
virtualenv_activate
python_run
deactivate
fi
exit $failure