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