178 lines
4.8 KiB
Bash
178 lines
4.8 KiB
Bash
#!/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 "$1" -a -n "$2" ]; then
|
|
echo -e "\nvirtualenv -p \"$1\" ~/.venv-$2\n"
|
|
virtualenv -p "$1" ~/.venv-"$2"
|
|
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
|
|
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#\"}"
|
|
ppath=/usr/bin/python$pv
|
|
#if [ ! -e "$ppath" ]; then
|
|
sudo apt -y install "python$pv" "python${pv}-dev"
|
|
#fi
|
|
virtualenv_init "$ppath" "$pv"
|
|
virtualenv_activate "$pv"
|
|
python_run "$ppath" "$pv"
|
|
deactivate
|
|
done
|
|
else
|
|
virtualenv_init
|
|
virtualenv_activate
|
|
python_run
|
|
deactivate
|
|
fi
|
|
|
|
exit $failure
|