Add progress meter to travis builds
[dbsrgits/DBIx-Class.git] / maint / travis-ci_scripts / common.bash
CommitLineData
b58ecb01 1#!/bin/bash
2
3set -e
4
b9970637 5TEST_STDERR_LOG=/tmp/dbictest.stderr
851b6c2d 6TIMEOUT_CMD="/usr/bin/timeout --kill-after=9.5m --signal=TERM 9m"
b9970637 7
b58ecb01 8echo_err() { echo "$@" 1>&2 ; }
9
10if [[ "$TRAVIS" != "true" ]] ; then
11 echo_err "Running this script makes no sense outside of travis-ci"
12 exit 1
13fi
14
15tstamp() { echo -n "[$(date '+%H:%M:%S')]" ; }
16
17run_or_err() {
18 echo_err -n "$(tstamp) $1 ... "
19
20 LASTEXIT=0
21 START_TIME=$SECONDS
707f8405 22
23 PRMETER_PIDFILE="$(tempfile)_$SECONDS"
24 # the double bash is to hide the job control messages
25 bash -c "bash -c 'echo \$\$ >> $PRMETER_PIDFILE; while true; do sleep 10; echo -n \"\${SECONDS}s ... \"; done' &"
26
003e97c5 27 # the tee is a handy debugging tool when stumpage is exceedingly strong
28 #LASTOUT=$( bash -c "$2" 2>&1 | tee /dev/stderr) || LASTEXIT=$?
b58ecb01 29 LASTOUT=$( bash -c "$2" 2>&1 ) || LASTEXIT=$?
707f8405 30
31 # stop progress meter
32 for p in $(cat "$PRMETER_PIDFILE"); do kill $p ; done
33
b58ecb01 34 DELTA_TIME=$(( $SECONDS - $START_TIME ))
35
36 if [[ "$LASTEXIT" != "0" ]] ; then
4242985f 37 echo_err "FAILED !!! (after ${DELTA_TIME}s)"
38 echo_err "Command executed:"
39 echo_err "$2"
40 echo_err "STDOUT+STDERR:"
41 echo_err "$LASTOUT"
42
b58ecb01 43 return $LASTEXIT
44 else
45 echo_err "done (took ${DELTA_TIME}s)"
46 fi
47}
48
003e97c5 49apt_install() {
50 # flatten
51 pkgs="$@"
52
53 # Need to do this at every step, the sources list may very well have changed
54 run_or_err "Updating APT available package list" "sudo apt-get update"
55
56 run_or_err "Installing Debian APT packages: $pkgs" "sudo apt-get install --allow-unauthenticated --no-install-recommends -y $pkgs"
57}
58
b58ecb01 59extract_prereqs() {
71dd2ecc 60 # once --verbose is set, --no-verbose can't disable it
61 # do this by hand
b2435630 62 local PERL_CPANM_OPT="$( echo $PERL_CPANM_OPT | sed 's/--verbose\s*//' )"
71dd2ecc 63
b58ecb01 64 # hack-hack-hack
65 LASTEXIT=0
66 COMBINED_OUT="$( { stdout="$(cpanm --quiet --scandeps --format tree "$@")" ; } 2>&1; echo "!!!STDERRSTDOUTSEPARATOR!!!$stdout")" \
67 || LASTEXIT=$?
68
69 OUT=${COMBINED_OUT#*!!!STDERRSTDOUTSEPARATOR!!!}
777447e9 70 ERR=${COMBINED_OUT%!!!STDERRSTDOUTSEPARATOR!!!*}
b58ecb01 71
50e644e5 72 if [[ "$LASTEXIT" != "0" ]] ; then
4242985f 73 echo_err "Error occured (exit code $LASTEXIT) retrieving dependencies of $@:"
74 echo_err "$ERR"
75 echo_err "$OUT"
b58ecb01 76 exit 1
77 fi
78
777447e9 79 # throw away warnings, up-to-date diag, ascii art, convert to modnames
80 PQ=$(perl -p -e '
81 s/^.*?is up to date.*$//;
82 s/^\!.*//;
83 s/^[^a-z]+//i;
84 s/\-[^\-]+$/ /; # strip version part
85 s/\-/::/g
86 ' <<< "$OUT")
f207111d 87
88 # throw away what was in $@
89 for m in "$@" ; do
90 PQ=$( perl -p -e 's/(?:\s|^)\Q'"$m"'\E(?:\s|$)/ /mg' <<< "$PQ")
91 done
92
93 # RV
94 echo "$PQ"
b58ecb01 95}
96
97parallel_installdeps_notest() {
98 if [[ -z "$@" ]] ; then return; fi
99
4242985f 100 # one module spec per line
101 MODLIST="$(printf '%s\n' "$@")"
b58ecb01 102
23905f5f 103 # We want to trap the output of each process and serially append them to
104 # each other as opposed to just dumping a jumbled up mass-log that would
105 # need careful unpicking by a human
106 #
107 # While cpanm does maintain individual buildlogs in more recent versions,
108 # we are not terribly interested in trying to figure out which log is which
109 # dist. The verbose-output + trap STDIO technique is vastly superior in this
110 # particular case
10e248bf 111 #
112 # Explanation of inline args:
113 #
114 # [09:38] <T> you need a $0
115 # [09:38] <G> hence the _
116 # [09:38] <G> bash -c '...' _
117 # [09:39] <T> I like -- because it's the magic that gnu getopts uses for somethign else
118 # [09:39] <G> or --, yes
119 # [09:39] <T> ribasushi: you could put "giant space monkey penises" instead of "--" and it would work just as well
120 #
4242985f 121 run_or_err "Installing (without testing) $(echo $MODLIST)" \
122 "echo \\
123\"$MODLIST\" \\
124 | xargs -d '\\n' -n 1 -P $NUMTHREADS bash -c \\
e6b373aa 125 'OUT=\$($TIMEOUT_CMD cpanm --notest \"\$@\" 2>&1 ) || (LASTEXIT=\$?; echo \"\$OUT\"; exit \$LASTEXIT)' \\
4242985f 126 'giant space monkey penises'
10e248bf 127 "
b58ecb01 128}
129
579472df 130installdeps() {
131 if [[ -z "$@" ]] ; then return; fi
132
133 echo_err "$(tstamp) Processing dependencies: $@"
134
135 local -x HARNESS_OPTIONS
136
137 HARNESS_OPTIONS="j$NUMTHREADS"
138
139 echo_err -n "Attempting install of $# modules under parallel ($HARNESS_OPTIONS) testing ... "
140
141 LASTEXIT=0
142 START_TIME=$SECONDS
e6b373aa 143 LASTOUT=$( _dep_inst_with_test "$@" ) || LASTEXIT=$?
579472df 144 DELTA_TIME=$(( $SECONDS - $START_TIME ))
145
146 if [[ "$LASTEXIT" = "0" ]] ; then
147 echo_err "done (took ${DELTA_TIME}s)"
148 else
e6b373aa 149 local errlog="after ${DELTA_TIME}s Exit:$LASTEXIT Log:$(/usr/bin/nopaste -q -s Shadowcat -d "Parallel testfail" <<< "$LASTOUT")"
a9fc70ee 150 echo_err -n "failed ($errlog) retrying with sequential testing ... "
151 POSTMORTEM="$POSTMORTEM$(
152 echo
77a86bbf 153 echo "Depinstall under $HARNESS_OPTIONS parallel testing failed $errlog"
154 echo "============================================================="
155 echo "Attempted installation of: $@"
156 echo "============================================================="
a9fc70ee 157 )"
579472df 158
159 HARNESS_OPTIONS=""
160 LASTEXIT=0
161 START_TIME=$SECONDS
e6b373aa 162 LASTOUT=$( _dep_inst_with_test "$@" ) || LASTEXIT=$?
579472df 163 DELTA_TIME=$(( $SECONDS - $START_TIME ))
164
165 if [[ "$LASTEXIT" = "0" ]] ; then
166 echo_err "done (took ${DELTA_TIME}s)"
167 else
168 echo_err "FAILED !!! (after ${DELTA_TIME}s)"
169 echo_err "STDOUT+STDERR:"
170 echo_err "$LASTOUT"
171 exit 1
172 fi
173 fi
174
175 INSTALLDEPS_OUT="${INSTALLDEPS_OUT}${LASTOUT}"
176}
177
e6b373aa 178_dep_inst_with_test() {
179 if [[ "$DEVREL_DEPS" == "true" ]] ; then
180 # --dev is already part of CPANM_OPT
181 $TIMEOUT_CMD cpanm "$@" 2>&1
182 else
183 $TIMEOUT_CMD cpan "$@" 2>&1
579472df 184
e6b373aa 185 # older perls do not have a CPAN which can exit with error on failed install
186 for m in "$@"; do
187 if ! perl -e '
579472df 188
647da28e 189my $mod = (
579472df 190 $ARGV[0] =~ m{ \/ .*? ([^\/]+) $ }x
191 ? do { my @p = split (/\-/, $1); pop @p; join "::", @p }
192 : $ARGV[0]
647da28e 193);
194
195$mod = q{List::Util} if $mod eq q{Scalar::List::Utils};
196
197eval qq{require($mod)} or ( print $@ and exit 1)
579472df 198
e6b373aa 199 ' "$m" 2> /dev/null ; then
200 echo -e "$m installation seems to have failed"
201 return 1
202 fi
203 done
204 fi
579472df 205}
206
b58ecb01 207CPAN_is_sane() { perl -MCPAN\ 1.94_56 -e 1 &>/dev/null ; }
f207111d 208
209CPAN_supports_BUILDPL() { perl -MCPAN\ 1.9205 -e1 &>/dev/null; }