results2html: Add SHA-1 and SHA-256 to .tar.gz build products.
[pspp] / results2html
1 #! /usr/bin/env perl
2
3 use strict;
4 use warnings;
5
6 use Digest::SHA;
7 use File::Spec;
8 use HTML::Entities qw();
9 use URI::Escape;
10
11 sub encode_entities {
12     return HTML::Entities::encode_entities($_[0], "<&>'\"");
13 }
14
15 my $gitweb_url = 'http://benpfaff.org/cgi-bin/gitweb.cgi?p=pspp;a=blob;f=[FILE];hb=[BRANCH]#l[LINE]';
16
17 open (LOG, '<', "LOG");
18 open (LOG_HTML, '>', "log.html");
19
20 print LOG_HTML <<EOF;
21 <html>
22 <head>
23   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
24   <link rel="stylesheet" href="build.css">
25 </head>
26 <body>
27 EOF
28 print LOG_HTML "<pre>";
29
30 sub read_vars {
31     my ($file) = @_;
32     my %vars = ();
33     if (open (VARS, "<", $file)) {
34         while (<VARS>) {
35             chomp;
36             my ($key, $value) = /^([^=]+)=(.*)/ or next;
37             $vars{$key} = $value;
38         }
39         close (VARS);
40     }
41     return %vars;
42 }
43 my (%vars) = read_vars ('VARS');
44
45 my (@products);
46 my (@steps);
47 my ($new_page) = 0;
48 my ($result) = "failure";
49 my (%dist_files);
50 my ($dist_dir);
51 my @dirstack;
52 while (<LOG>) {
53     my $ln = $.;
54     chomp;
55
56     if (/^\f$/) {
57         $new_page = 1;
58         @dirstack = ();
59         print LOG_HTML "</pre><hr><pre>\n";
60         next;
61     }
62
63     if (/Entering directory `(.*)'$/) {
64         push (@dirstack, $1);
65     } elsif (/Leaving directory `(.*)'$/) {
66         pop (@dirstack);
67     }
68
69     my $log_class;
70     if ($new_page) {
71         $new_page = 0;
72         $log_class = "step";
73         if (my ($name, $product) = /^Saving(?:\s+([^:]*):)?\s+(.*)$/) {
74             my $href = "$product/index.html";
75             $href = $product if ! -e $href;
76
77             my (%p);
78             $p{NAME} = $name if defined ($name);
79             $p{PRODUCT} = $product;
80             $p{HREF} = $href;
81             $p{LN} = $ln;
82             push (@products, \%p);
83             push (@{$steps[$#steps]{CHILDREN}}, \%p);
84
85             if (defined ($name) && $name eq 'source distribution') {
86                 open (DIST, '-|', "zcat $product | tar tf -");
87                 while (my $line = <DIST>) {
88                     chomp $line;
89                     $line =~ s%^([./])*%%; # Trim leading ./
90                     $dist_files{$line} = 1;
91                     if (!defined ($dist_dir)) {
92                         $dist_dir = (split ('/', $line))[0];
93                     }
94                 }
95                 close (DIST);
96             }
97         } else {
98             my (%s);
99             $s{TITLE} = $_;
100             $s{LN} = $ln;
101             push (@steps, \%s);
102         }
103         $result = 'success' if $_ eq 'Success';
104     } else {
105         if (my ($diagnostic) = /(error|warning):/i) {
106             my (%d);
107             $d{DIAGNOSTIC} = lc ($diagnostic);
108             $d{MESSAGE} = $_;
109             $d{LN} = $ln;
110
111             if (@dirstack && defined ($dist_dir)
112                 && (my ($match, $file, $line) = /^(([^\s:]+):(\d+):)/)) {
113                 $file = File::Spec->rel2abs ($file, $dirstack[$#dirstack]);
114                 my (@path) = grep ($_ ne '' && $_ ne '.', split ('/', $file));
115                 for (my $i = 1; $i <= $#path; ) {
116                     if ($path[$i] eq '..') {
117                         splice (@path, $i - 1, 2);
118                         $i-- if $i > 1;
119                     } else {
120                         $i++;
121                     }
122                 }
123
124                 for (my $i = $#path; $i >= 0; $i--) {
125                     if ($path[$i] eq $dist_dir) {
126                         my $dist_file = join ('/', @path[$i..$#path]);
127                         if (exists ($dist_files{$dist_file})) {
128                             $d{MATCH} = length ($match);
129                             $d{LINE} = $line;
130                             $d{FILE} = $dist_file;
131                         }
132                         last;
133                     }
134                 }
135             }
136             push (@{$steps[$#steps]{CHILDREN}}, \%d);
137
138             $log_class = "$diagnostic";
139         }
140     }
141     printf LOG_HTML "<a name=\"%d\"><tt>%4d</tt></a>  ", $ln, $ln;
142     if (defined ($log_class)) {
143         print LOG_HTML "<span class=\"$log_class\">", encode_entities ($_), "</span>\n";
144     } else {
145         print LOG_HTML encode_entities ($_), "\n";
146     }
147 }
148
149 open (INDEX, '>','index.html');
150
151 print INDEX <<EOF;
152 <html>
153 <head>
154   <link rel="stylesheet" href="build.css">
155 </head>
156 <body>
157 EOF
158
159 print INDEX "<h1>Build ", $vars{"build_number"}, ": $result</h1>\n";
160
161 print INDEX "<h2>Build Properties</h2>\n";
162 print INDEX "<table>\n";
163 print INDEX "<tr><th>Name</th><th>Value</th></tr>\n";
164 foreach my $key (sort (keys (%vars))) {
165     print INDEX "<tr>";
166     print INDEX "<td>", encode_entities ($key), "</td>";
167     print INDEX "<td>", encode_entities ($vars{$key}), "</td>";
168     print INDEX "</tr>\n";
169 }
170 print INDEX "</table>\n";
171
172 print INDEX "<h2>Build Products</h2>\n";
173 print INDEX "<ul>\n";
174 foreach my $p (@products) {
175     print INDEX "<li>";
176     print INDEX encode_entities ($p->{NAME}), ": " if defined ($p->{NAME});
177     print INDEX "<a href=\"", encode_entities ($p->{HREF}), "\">";
178     print INDEX encode_entities ($p->{PRODUCT});
179     print INDEX "</a>\n";
180     if ($p->{PRODUCT} =~ /\.tar\.gz$/) {
181         my $sha = Digest::SHA->new(1);
182         $sha->addfile($p->{PRODUCT});
183         print INDEX "<br>sha1: ", $sha->hexdigest, "\n";
184
185         $sha = Digest::SHA->new(256);
186         $sha->addfile($p->{PRODUCT});
187         print INDEX "<br>sha256: ", $sha->hexdigest, "\n";
188     }
189     print INDEX "</li>\n";
190 }
191 print INDEX "</ul>\n";
192
193 sub log_link {
194     my ($ln) = @_;
195     return "<small><a href=\"log.html#$ln\">(log)</a></small>";
196 }
197
198 print INDEX "<h2>Build Summary</h2>\n";
199 print INDEX "<ol>\n";
200 foreach my $s (@steps) {
201     print INDEX "<li>", encode_entities ($s->{TITLE});
202     print INDEX " ", log_link ($s->{LN});
203     foreach my $c (@{$s->{CHILDREN}}) {
204         if (defined ($c->{DIAGNOSTIC})) {
205             print INDEX "<p class=\"$c->{DIAGNOSTIC}\">";
206             if (defined ($c->{MATCH})) {
207                 my $href = $gitweb_url;
208                 $href =~ s/\[FILE\]/uri_escape ($c->{FILE})/ge;
209                 $href =~ s/\[LINE\]/$c->{LINE}/g;
210                 $href =~ s/\[BRANCH\]/uri_escape ($vars{'dist_commit'})/e;
211
212                 print INDEX '<a href="', encode_entities ($href), '">';
213                 print INDEX encode_entities (substr ($c->{MESSAGE},
214                                                      0, $c->{MATCH}));
215                 print INDEX '</a>';
216                 print INDEX encode_entities (substr ($c->{MESSAGE},
217                                                      $c->{MATCH}));
218             } else {
219                 print INDEX encode_entities ($c->{MESSAGE});
220             }
221             print INDEX " ", log_link ($c->{LN});
222             print INDEX "</p>\n";
223         } else {
224             print INDEX "<p>&rarr; <a href=\"", encode_entities ($c->{HREF}), "\">";
225             print INDEX encode_entities ($c->{PRODUCT});
226             print INDEX "</a> ";
227             print INDEX log_link ($c->{LN});
228             print INDEX "</p>\n";
229         }
230     }
231     print INDEX "</li>\n";
232 }
233 print INDEX "</ol>\n";
234
235 print INDEX <<EOF;
236 </body>
237 </html>
238 EOF
239
240 print LOG_HTML <<EOF;
241 </pre>
242 </body>
243 </html>
244 EOF
245
246 open (CSS, '>', "build.css");
247 print CSS <<EOF;
248 body {
249         background: white;
250         color: black;
251         padding: 0em 3em 0em 3em;
252         margin: 0
253 }
254 body>p {
255         margin: 0pt 0pt 0pt 0em
256 }
257 body>p + p {
258         text-indent: 1.5em;
259 }
260 H1 {
261         font-size: 150%;
262         margin-left: -1.33em
263 }
264 H2 {
265         font-size: 125%;
266         font-weight: bold;
267         margin-left: -.8em
268 }
269 H3 {
270         font-size: 100%;
271         font-weight: bold;
272         margin-left: -.5em }
273 H4 {
274         font-size: 100%;
275         margin-left: 0em
276 }
277 H1, H2, H3, H4, H5, H6 {
278         font-family: sans-serif;
279         color: blue
280 }
281 html {
282         margin: 0
283 }
284
285 a:link {
286         color: blue;
287         text-decoration: none;
288 }
289 a:visited {
290         color: gray;
291         text-decoration: none;
292 }
293 a:active {
294         color: black;
295         text-decoration: none;
296 }
297 a:hover {
298         text-decoration: underline
299 }
300
301 ol>li>p {
302         margin-top: 0;
303         margin-bottom: 0
304 }
305 .step {
306         font-weight: bold
307 }
308 .warning {
309         background: yellow
310 }
311 .error {
312         background: orange
313 }
314 EOF