Add travis2bash

This commit is contained in:
Peter Šurda 2021-01-05 09:52:34 +01:00
parent d05ae4c2b5
commit 6666ec9cb9
Signed by: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87

149
travis2bash.sh Executable file
View File

@ -0,0 +1,149 @@
#!/usr/bin/env bash
# implementation of .travis.yml execution using pure bash
# limited functionality
# shellcheck disable=SC1003
# yaml.sh # 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
# shellcheck disable=SC2086
echo -e "\nsudo apt-get -yq --no-install-suggests --no-install-recommends install ${travis_addons_apt_packages[*]}\n"
sudo apt-get -yq --no-install-suggests --no-install-recommends install \
${travis_addons_apt_packages[*]}
fi
}
function virtualenv_init() {
# init virtualenv directory
if [ -n "$1" ]; then
echo -e "\nvirtualenv -p \"$1\" ~/.venv\n"
virtualenv -p "$1" ~/.venv
else
echo -e "\nvirtualenv ~/.venv\n"
virtualenv ~/.venv
fi
}
function virtualenv_activate() {
# activate virtualenv
# shellcheck disable=SC1090
echo -e "\n. ~/.venv/bin/activate\n"
. ~/.venv/bin/activate
}
function python_run() {
# shellcheck disable=SC2154
if [[ "$(declare -p travis_install)" =~ "declare -a" ]]; then
echo "Running \"install\""
for ((i = 0; i < ${#travis_install[@]}; i++))
do
echo -e "\n${travis_install[$i]}\n"
if ! ${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
echo -e "\n${travis_script[$i]}\n"
if ! ${travis_script[$i]}; then
failure=1
fi
done
fi
}
create_variables .travis.yml travis_
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
virtualenv_init "$ppath"
virtualenv_activate
python_run "$ppath"
deactivate
done
else
virtualenv_init
virtualenv_activate
python_run
deactivate
fi
exit $failure