chiark / gitweb /
* New adt-testreport-onepackage script, with some example config
[autopkgtest.git] / runner / adt-testreport-onepackage
1 #!/bin/bash
2
3 set -e
4
5 mirror=http://mirror.relativity.greenend.org.uk/mirror/ubuntu.good
6 distro=feisty
7 suite=main
8 salt=''
9 tmp=tmp
10 var=var
11 administrator_email=ian@davenant.greenend.org.uk
12 maintainer_email_override="$administrator_email"
13 salutation="Ian"
14 from="$salutation Jackson <ian@davenant.greenend.org.uk>"
15 rsync=rsync
16 disable=true
17 interactive=true
18
19 for config in "$@"; do
20         case "$config" in
21         *=*)    eval "$config"  ;;
22         *)      . "$config"     ;;
23         esac
24 done
25
26 if $disable; then
27         echo >&2 'disabled because config inadequate (no disable=false)'
28         exit 1
29 fi
30
31 exec 3>&1
32 printf >&3 "starting "
33
34 rm -rf "$tmp"
35 mkdir "$tmp"
36
37 >"$tmp"/_log
38
39 if $interactive; then
40         echo '(log diverted to stdout)' >>"$tmp"/_log
41 else
42         exec >>"$tmp"/_log
43 fi
44 exec 4>&1
45
46 progress () {
47         echo "++++++ $1 ++++++"
48 }
49
50 gurl () {
51         progress "fetching $1"
52         curl -fsS "$1" >"$2"
53 }
54
55 gurl "$mirror/dists/$distro/$suite/source/Sources.gz" "$tmp"/_sources.gz
56 zcat "$tmp"/_sources.gz >"$tmp"/_sources-in
57
58 now=`date +%s`
59 >>"$var"/last-info
60
61 progress selecting
62
63 if [ "x$pkg" = x ]; then
64  pkg="`perl -e '
65         use IO::Handle;
66
67         $pre= "[-+.0-9a-z]+";
68         $vre= "[-+.0-9a-zA-Z:~]+";
69
70         sub f1() { $fn=shift @ARGV; open F, $fn or die "$fn $!"; }
71         sub f2() { F->error and die "$fn $!"; close F or die "$fn $!"; }
72
73         f1();
74         while (<F>) {
75                 die unless m/^($pre) ($vre) (\d+)( .*)?$/;
76                 $lastver{$1}= $2;
77                 $lasttime{$1}= $3;
78                 $extras{$1}= $4." ";
79         }
80         f2();
81         f1();
82         $best_score= -1;
83         while (<F>) {
84                 if (m/^Package: ($pre)$/) {
85                         die if length $package or length $version;
86                         $package= $1;
87                 } elsif (m/^Version: ($vre)$/) {
88                         die unless length $package;
89                         die if length $version;
90                         $version= $1;
91                         $score= '$now' - $lasttime{$package};
92                         $score= 1e7 if $score>1e7;
93                         $score *= 5 if $lastver{$package} ne $version;
94                         $score *= 10 unless $extras{$package} =~ m/ nt /;
95                         next if $score < $best_score
96                              or ($score==$best_score and \
97                                  $package gt $best_package);
98                         $best_score= $score;
99                         $best_package= $package;
100                 } elsif (m/^$/) {
101                         die if length $package and !length $version;
102                         $package= $version= "";
103                 }
104         }
105         f2();
106         die unless length $best_package;
107         open L, ">&4" or die $!;
108         printf L "selected %s (age %s, score %d)\n",
109                 $best_package,
110                 exists($lastime{$best_package})
111                 ? '$now' - $lasttime{$best_package}
112                 : "<never-yet>",
113                 $best_score;
114         print "$best_package\n" or die $!;
115  ' "$var"/last-info "$tmp"/_sources-in`"
116 else
117         printf >&4 "package forced: %s\n" "$pkg"
118 fi
119
120 sed -n "/^Package: $pkg\$/,/^\$/p" \
121  <"$tmp"/_sources-in >"$tmp"/_this-stanza
122
123 echo
124 cat "$tmp"/_this-stanza
125
126 getfield () {
127         eval 'p'$1'="`
128                 sed -n '\''s/^'$1': //p'\'' \
129                  <"$tmp"/_this-stanza
130         `"'
131 }
132
133 getfield Directory
134 getfield Version
135
136 leafnames="`
137         sed -n '/^Files:/,/^([^ ].*)?$/{ /^ /{
138                 s/^ [0-9a-z][0-9a-z]*  *[0-9][0-9]* //; p
139                 }}' \
140          <"$tmp"/_this-stanza
141 `"
142
143 printf >&3 "selected \"%s\" " $pkg
144
145 tp="$tmp/$pkg"
146 mkdir "$tp" "$tp/src" "$tp/tmp" "$tp/out"
147
148 for leafname in $leafnames; do
149         df="$tp/src/$leafname"
150         case "$leafname" in
151         */*|.*) echo >&2 "bad leafname: $leafname"; exit 1;;
152         *.dsc) dsc="$df";;
153         esac
154         gurl "$mirror/$pDirectory/$leafname" "$df"
155 done
156
157 if [ "x$maintainer_email_override" = x ]; then
158         getfield Maintainer
159         maintainer_email=pMaintainer
160 else
161         maintainer_email=maintainer_email_override
162 fi
163
164 printf >&3 "adt-run "
165
166 progress "starting test"
167
168 xrc () {
169         printf "+ %s\n" "$*"
170         set +e
171         "$@"
172         rc=$?
173         set -e
174 }
175
176 echo 'fatal: adt-run did not start properly' >"$tmp"/_summary
177
178 xrc adt-run --tmp-dir "$tp"/tmp                         \
179         --output-dir "$tp"/out                          \
180         --log-file "$tp"/log                            \
181         --summary "$tmp"/_summary                       \
182         $adtrun_extra_opts                              \
183         --source "$dsc"                                 \
184  ---                                                    \
185  adt-virt-xenlvm                                        \
186         $adtvirt_extra_opts                             \
187  --                                                     \
188         --distro="$distro"                              \
189  2>&1 3>&- 4>&-
190
191 printf >&3 "%s " $rc
192
193 ourx=0
194 upload=true
195 extras=''
196
197 case "$rc" in
198 0)      summary='all OK';                       email=''                ;;
199 2)      summary='OK (some skipped)';            email=''                ;;
200 8)      summary='package declares no tests';    email=''
201                                         upload=false; extras='nt'       ;;
202 4|6)    summary='test(s) failed!';      email="$maintainer_email"       ;;
203 12)     summary='erroneous package!';   email="$maintainer_email"       ;;
204 16)     summary='testbed failed!';      email="administrator_email"     ;;
205 *)      summary='unexpected failure!';  email="administrator_email"; ourx=20;;
206 esac
207
208 progress "RESULTS $summary"
209
210 if $upload; then
211         progress "bundling"
212         ln "$tmp"/_summary "$tp"/summary
213
214         for odir in tmp out; do
215                 if test -d "$tp"/$odir; then
216                         GZIP=-2 tar -f "$tp"/$odir.tar.gz -C "$tp" -zc $odir
217                         rm -r "$tp"/$odir
218                 fi
219         done
220
221         progress "uploading"
222         printf >&3 "uploading"
223         $rsync -rltH --safe-links --delete "$tp" "$destrsynchead/$destdirtail/"
224         printf >&3 " "
225 fi
226
227 if [ "x$email" != x ]; then
228         progress "contacting $email"
229         eval "email_addr=\$$email"
230         printf >&3 "email \"%s\" " "$email_addr"
231         cat >"$tmp"/_email <<END
232 From: $from
233 To: $email_addr
234 Subject: autopkgtest $distro $pkg: $summary
235
236  Test executed for:  $distro  $pkg
237  Outcome: $summary
238 END
239         sed -e 's/^/  /' "$tmp"/_summary >>"$tmp"/_email
240         cat >>"$tmp"/_email <<END
241
242 This message is automatically generated by the autopkgtest package
243 testing system.  You are receiving it because:
244 END
245         case "$email" in
246                 pMaintainer)
247                         cat >>"$tmp"/_email <<END
248  You are listed in the Maintainer field of the $pkg package in $distro
249   and the test results appear to indicate a problem with the package.
250 END
251                         ;;
252                 maintainer_email_override)
253                         cat >>"$tmp"/_email <<END
254  The test results appear to indicate a problem with the package
255   and reports for package maintainers for $distro are being directed to
256   $maintainer_email_override
257 END
258                         ;;
259                 administrator_email)
260                         cat >>"$tmp"/_email <<END
261  You are the administrator for the autopkgtest installation.
262 END
263                         ;;
264                 *)
265                         echo >&2 "huh email $email is what why?"
266                         exit 1
267                         ;;
268         esac
269         cat >>"$tmp/_email" <<END
270
271 The test log, which is intended to be sufficient to diagnose most
272 failures, can be found below.  However, in case this is not
273 sufficient, another copy can be found along with output files, saved
274 temporary files, and so on, at:
275  $desthttphead/$destdirtail/
276
277 If you have any questions about this service please contact me at:
278  $from
279
280 Regards,
281 $salutation
282
283 -8<-
284 END
285 fi
286
287 printf >>"$var"/log "package=%s rc=%s emailed='%s'\n" \
288         "$pkg" $rc "$email_addr"
289
290 if [ "x$ourx" = x0 ]; then
291         sed -e "/^$pkg /d" <"$var"/last-info >"$var"/last-info.new
292         printf "%s %s %s %s\n" "$pkg" "$pVersion" "$now" "$extras" \
293                 >>"$var"/last-info.new
294         mv "$var"/last-info.new "$var"/last-info
295         progress "tested."
296 else
297         progress "fault ($ourx)."
298 fi
299
300 if [ "x$email" = x ]; then
301         if $interactive; then
302                 cat "$tmp"/_log >&2
303         fi
304 else
305         cat >>"$tmp"/_email 2>&1 "$tmp"/_log ||:
306         if $interactive; then
307                 cat "$tmp"/_email >&2
308         else
309                 sendmail -odq -oem -t -oi <"$tmp"/_email
310         fi
311 fi
312
313 printf >&3 "done %s.\n" $ourx
314 exit $ourx