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