summaryrefslogtreecommitdiff
path: root/bin/nightlybuilds/scoreboard_update.pl
blob: 2476f9d396c6cf74fc05693a6aacae2205275515 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
    & eval 'exec perl -S $0 $argv:q'
    if 0;

# $Id$

use Getopt::Std;
use strict;
use English;
use FileHandle;
use File::Path;
use LWP::UserAgent;
use Time::Local;

my @builds = ();

# Hashes based on builds
my %build_latest_file;
my %build_latest_time;
my %build_type;
my %build_list;
my %build_web;

###############################################################################
#
# load_build_list
#
# Reads the list of builds from a file
#
# Arguments:  $ - file to read
#
# Returns:    Nothing
#
###############################################################################
sub load_build_list ($)
{
    my $file = shift;
    my @latest = ();

    print "Loading Build List\n";

    my $filehandle = new FileHandle;

    unless ($filehandle->open ("<$file")) {
        warn 'Cannot open: ', $file, ' ', $_;
        return;
    };

    my @data = <$filehandle>;

    $filehandle->close ();

    my $buildname = undef ;
    my %build;

    foreach (@data) {
	chomp;
	
        # Skip blank lines and comments
	if (m/^\s*$/) { next; };
        if (m/^\#/) { next; }

        # Remove trailing and leading spaces
        s/^\s*//;
        s/\s*$//;
  
	if (m/\[(.*)\]/) {
            $buildname = $1;
            push (@builds, $buildname);
            next;
        }

        if (defined $buildname) {
            if (m/^\s*TYPE\s*=\s*(.*)/) {
                if (defined $build_type{$buildname}) {
                    print "Warning: Redefining TYPE for $buildname\n";
                }
                $build_type{$buildname} = $1;
            }
            if (m/^\s*LIST\s*=\s*(.*)/) {
                if (defined $build_list{$buildname}) {
                    print "Warning: Redefining LIST for $buildname\n";
                }
                $build_list{$buildname} = $1;
            }
            if (m/^\s*WEB\s*=\s*(.*)/) {
                if (defined $build_web{$buildname}) {
                    print "Warning: Redefining WEB for $buildname\n";
                }
                $build_web{$buildname} = $1;
            }
        }
    }

    @builds = sort (@builds);
}


###############################################################################
#
# query_latest
#
# Queries the web servers to figure out the latest build available and saves
# the list in build_latest_file and
# build_latest_time.
#
# @todo Actually since query_latest doesn't depend on the type anymore, we
#       could feasibly remove it.
#
# Arguments:  Nothing
#
# Returns:    Nothing
#
###############################################################################
sub query_latest ()
{
    print "Getting file lists\n";

    foreach my $build (@builds) {
        my @files = load_web_dir ($build_list{$build});

        my $latest_time= 0;
        my $latest_file = "";

        foreach my $file (@files) {
            if ($file =~ m/(....)_(..)_(..)_(..)_(..)\.txt/) {
                my $time = scalar ($1.$2.$3.$4.$5);

                if ($time > $latest_time) {
                    $latest_time = $time;
                    $latest_file = $file;
                }
            }
            elsif ($file =~ m/\Q$build\E\_(...)(..)_(....)\.txt/) {
                my %mon = ('Jan' =>  1, 'Feb' =>  2, 'Mar' =>  3,
                           'Apr' =>  4, 'May' =>  5, 'Jun' =>  6,
                           'Jul' =>  7, 'Aug' =>  8, 'Sep' =>  9,
                           'Oct' => 10, 'Nov' => 11, 'Dec' => 12);

                my $time = scalar (sprintf ('%0.4s%0.2s%0.2s0000', $3, $mon{$1}, $2));

                if ($time > $latest_time) {
                    $latest_time = $time;
                   $latest_file = $file;
                }
            }
            elsif ($file =~ m/\Q$build\E\_(....)_(....)\.txt/) {

                my $time = scalar ("2001".$1.$2);

                if ($time > $latest_time) {
                    $latest_time = $time;
                    $latest_file = $file;
                }
            }
            elsif ($file =~ m/\Q$build\E\_(....)\.txt/) {
                my $time = scalar ("2001".$1."0000");

                if ($time > $latest_time) {
                    $latest_time = $time;
                    $latest_file = $file;
                }
            }
        }

        # Save the latest
        if ($latest_time > 0) {
            $build_latest_file{$build} = $latest_file;
            $build_latest_time{$build} = $latest_time;
        }
    }
}


###############################################################################
#
# load_web_dir
#
# Loads the listing from a web resource, works with both Apache and IIS.
#
# Arguments:  $ - The URI of the directory on the web
#
# Returns:    @ - Listing of the files in that directory
#
###############################################################################
sub load_web_dir ($)
{
    my $address = shift;
    my $web_dir = '';
    my @files = ();

    print "    Loading list from $address\n";

    ### Split web_dir up a bit

    if ($address =~ m/^http:\/\/[\w.]*(.*)/) {
        $web_dir = $1;
    }
    else {
        warn "load_web_dir (): Badly formed http address";
        return ();
    }

    ### Request the web dir page

    my $ua = LWP::UserAgent->new;

    ### We are impatient, so don't wait more than 20 seconds for a
    ### response (the default was 180 seconds)
    $ua->timeout(20);

    my $request = HTTP::Request->new('GET', $address);
    my $response = $ua->request($request);

    if (!$response->is_success ()) {
        print "        ERROR: Could not load web dir\n";
        return ();
    }
    
    ### Pull out the contents based on the server type

    my $content = $response->content ();
    my $server = $response->server ();

    if ($server =~ m/Microsoft-IIS/) {

        ### split it up based on \n and <BR> and <P>

        $content =~ s/<BR>/\n/gi;
        $content =~ s/<P>/\n/gi;
        my @contents = split /\n/, $content;

        ### Now look for files

        foreach my $line (@contents) {
            if ($line =~ m/\"$web_dir([^\"\/]*)\"/) {
                push (@files, $1);
            }
        }
    }
    elsif ($server =~ m/Apache/ || $server =~ m/Boa/) {
        my @contents = split /\n/, $content;

        ### Now look for files
        foreach my $line (@contents) {
            if ($line =~ m/A HREF=\"([^\"\/\?]*)\"/i) {
                push (@files, $1);
            }
        }
    }
    else {
        warn 'load_web_dir (): Unrecognized server: '.$server;
    }

    return @files;
}


###############################################################################
#
# decode_timestamp
#
# Decodes a YYYYMMDDHHMM timestamp
#
# Arguments:  $ - encoded timestamp
#
# Returns:    $ - timestamp description
#
###############################################################################
sub decode_timestamp ($)
{
    my $timestamp = shift; 
    my $description = '';

    if ($timestamp =~ m/(....)(..)(..)(..)(..)/) {
        my %mon = ( 1 => 'Jan',  2 => 'Feb',  3 => 'Mar',
                    4 => 'Apr',  5 => 'May',  6 => 'Jun',
                    7 => 'Jul',  8 => 'Aug',  9 => 'Sep',
                   10 => 'Oct', 11 => 'Nov', 12 => 'Dec');
        $description = 
            sprintf ('%s %s, %s - %s:%s', $mon{int ($2)}, $3, $1, $4, $5);
        
    }
    else {
        warn 'Unable to decode time';
        $description = 'Unknown Time';
    }

    return $description;
}

###############################################################################
#
# update_cache 
#
# Updates the local cache
#
# Arguments:  $ - directory to place files in
#
# Returns:    Nothing
#
###############################################################################
sub update_cache ($)
{
    my $directory = shift;

    print "Updating Local Cache\n";

    if (!-w $directory) {
        warn "Cannot write to $directory";
        return;
    }

    foreach my $build (@builds) {
        ### Check to see if we had problems.  If there is no latest time,
        ### we had problems downloading.
        if (!defined $build_latest_time{$build}) {
            next;
        }

        my $time = $build_latest_time{$build};
        my $oldtime = '';
        my $address = $build_list{$build} . "/" 
                      . $build_latest_file{$build};

        my $filename = $build.'_'.$time.'.txt';

        print "    Looking at $build\n";

	mkpath "$directory/$build";

        if (! -r "$directory/$build/$filename") {
            print "        Downloading\n";
            my $ua = LWP::UserAgent->new;
            my $request = HTTP::Request->new('GET', $address);
            my $response = $ua->request($request, "$directory/$build/$filename");

            if (!$response->is_success ()) {
                warn 'Unable to download '.$address;
                return;
            }
        }

        my @existing = glob ($directory . '/' . $build . '/' . $build . '_*');

        @existing = reverse sort @existing;

        # shift off the ones we want to keep
        shift @existing;  # for now, just the current one 

        foreach my $file (@existing) {
            print "        Removing $file\n";
            unlink $file;
      
        }
    }
}


###############################################################################
#
# timestamp_color
#
# Decodes a YYYYMMDDHHMM timestamp and figures out the color
#
# Arguments:  $ - encoded timestamp
#
# Returns:    $ - color
#
###############################################################################
sub timestamp_color ($)
{
    my $timestamp = shift; 

    if ($timestamp =~ m/(....)(..)(..)(..)(..)/) {
        my $buildtime = timelocal (0, $5, $4, $3, $2 - 1, $1);

	my $nowtime = timelocal (localtime);

        if ($nowtime - $buildtime > (60 * 60 * 24 * 2)) {
            return 'red';
        }

        if ($nowtime - $buildtime > (60 * 60 * 24 * 1)) {
            return 'orange';
        }

        return 'white'        
    }

    warn 'Unable to decode time';

    return 'gray';
}


###############################################################################
#
# determine_type
#
# Figures out whether a log is msvc or makefile
#
# Arguments:  $ - log file
#
# Returns:    Nothing
#
###############################################################################
sub determine_type ($)
{
    my $file = shift;
    my $msvc = 0;

    my $log = new FileHandle;

    unless ($log->open ("<$file")) {
        warn 'Could not open '.$file.' '.$_;
        return 'makefile';
    }

    while (<$log>) {
        if (m/^--------------------Configuration/) {
            $msvc = 1;
            last;
        }
    }
    $log->close ();
    
    if ($msvc == 1) {
        return 'msvc';
    }
    else {        
        return 'makefile';
    }
}


###############################################################################
#
# get_color
#
# Returns a color based on errors/warnings
#
# Arguments:  $ - input file
#             $ - Total type (cvs/compiler/tests)
#
# Returns:    Nothing
#
###############################################################################
sub get_color ($$)
{
    my $file = shift;
    my $type = shift;

    my $errors = 0;
    my $warnings = 0;
    my $found = 0;

    my $results = new FileHandle;

    unless ($results->open ("<$file")) {
        warn 'Could not open '.$file.' '.$_;
        return 'gray';
    }

    while (<$results>) {
        if ($type eq 'cvs') {
            if (m/^CVS Totals.*Modified:(.*) Conflicts:(.*) Unknown/) {
                $found = 1;
                $errors += $2;
                $warnings += $1;
            }
        }
        elsif ($type eq 'compiler') {
            if (m/Compiler Totals:  Errors:(.*)  Warnings:(.*)/) {
                $found = 1;
                $errors += $1;
                $warnings += $2;
            }
        }
        elsif ($type eq 'tests') {
            if (m/Test Failures:(.*)/) {
                $found = 1;
                $errors += $1;
            }
        }
    }
    $results->close ();
    
    if ($errors > 0) {
        return 'red';
    }
    elsif ($warnings > 0) {
        return 'orange';
    }
    elsif ($found == 1) {
        return 'lime';
    }
    else {        
        return 'gray';
    }
}


###############################################################################
#
# update_html
#
# Runs make_pretty on a bunch of files and creates an index.html
#
# Arguments:  $ - directory
#             $ - make_pretty script
#
# Returns:    Nothing
#
###############################################################################
sub update_html ($$)
{
    my $dir = shift;
    my $script = shift;
    my $filename = "$dir/index.html";
    my $localshift = '';

    my $indexhtml = new FileHandle;

    print "Updating HTML\n";

    unless ($indexhtml->open (">$filename")) {
        warn 'Could not create file: '.$filename." ".$_;
        return;
    }

    ### Print Header

    print $indexhtml "<HTML>\n<HEAD>\n<TITLE>Build Scoreboard</TITLE>\n</HEAD>\n";

    ### Start body

    print $indexhtml "<BODY bgcolor=white>\n<H1>Build Scoreboard</H1>\n<HR>\n";

    ### Table

    print $indexhtml '<TABLE border=1><TH>Config<TH>Last Run<TH>CVS<TH>Compilation'.
                     "<TH>Tests\n";

    foreach my $build (@builds) {
        mkpath "$dir/$build/pretty";

        print "    Looking at $build\n";

        if (defined $build_latest_time{$build}) {
            my $time = $build_latest_time{$build};

            my $log = $dir . '/' . $build . '/' . $build . '_' . $time . ".txt";

            my $webfile = "$build/pretty/$build" . "_$time";
            my $newfile = "$dir/$webfile";

            $newfile =~ s/\//\\/g if ($OSNAME eq "MSWin32");
            $log =~ s/\//\\/g if ($OSNAME eq "MSWin32");

            if (!-e $newfile.'.html') {
                print "        Creating HTML for $time\n";

                my $command = 'perl '.$script.' -c makefile < '.$log.' > '.
                              $newfile.'.html';

                system ($command);
            }

            if (!-e $newfile.'_Brief.html') {
                print "        Creating HTML Brief for $time\n";
    
                my $command = 'perl '.$script.' -b -c '
                              .determine_type ($log).
                              ' < '.$log.' > '.
                              $newfile.'_Brief.html';

                system ($command);
            }

            print $indexhtml '<TR><TD>';

            print $indexhtml "<A HREF=\"".$build_web{$build} ."\">" if defined $build_web{$build};
            print $indexhtml $build;
            print $indexhtml "</A> " if defined $build_web{$build};
            
            print $indexhtml '<TD bgcolor=';
            print $indexhtml timestamp_color ($time);
            print $indexhtml '>',decode_timestamp ($time);
            print $indexhtml '<TD bgcolor=';
            print $indexhtml get_color ($newfile.'_Brief.html', 'cvs');
            print $indexhtml '>';
            print $indexhtml "[<A HREF=\"".$webfile.".html#cvs\">Full</A>] ";
            print $indexhtml "[<A HREF=\"".$webfile."_Brief.html#cvs\">Brief</A>]";
            print $indexhtml '<TD bgcolor=';
            print $indexhtml get_color ($newfile.'_Brief.html', 'compiler');
            print $indexhtml '>';
            print $indexhtml "[<A HREF=\"".$webfile.".html#compiler\">Full</A>] ";
            print $indexhtml "[<A HREF=\"".$webfile."_Brief.html#compiler\">Brief</A>]";
            print $indexhtml '<TD bgcolor=';
            print $indexhtml get_color ($newfile.'_Brief.html', 'tests');
            print $indexhtml '>';
            print $indexhtml "[<A HREF=\"".$webfile.".html#tests\">Full</A>] ";
            print $indexhtml "[<A HREF=\"".$webfile."_Brief.html#tests\">Brief</A>]";
            print $indexhtml "\n";

        }
        else {
            print $indexhtml '<TR><TD>';
            print $indexhtml "<A HREF=\"".$build_web{$build} ."\">$build</A> ";
            print $indexhtml '<TD bgcolor=gray>';
            print $indexhtml '<TD bgcolor=gray>';
            print $indexhtml "[Full] [Brief]";
            print $indexhtml '<TD bgcolor=gray>';
            print $indexhtml "[Full] [Brief]";
            print $indexhtml '<TD bgcolor=gray>';
            print $indexhtml "[Full] [Brief]";
            print $indexhtml "\n";
        }

        my @existing = glob ($dir . '/' . $build . '/pretty/' . $build . '_*.html');

        @existing = reverse sort @existing;

        # shift off the ones we want to keep
        # shift only twice to keep the last *.html and *_Brief.html
        shift @existing;  
        shift @existing;  

        foreach my $file (@existing) {
            print "        Removing $file\n";
            unlink $file;
      
        }
    }

    print $indexhtml "</TABLE>\n";

    ### Print timestamp

    print $indexhtml '<BR>Last updated at '.scalar (localtime ())."<BR>\n";

    ### Print the Footer

    print $indexhtml "</BODY>\n</HTML>\n";

    $indexhtml->close ();
}

###############################################################################
###############################################################################

# Getopts

use vars qw/$opt_c $opt_h $opt_m $opt_o/;

if (!getopts ('c:hm:o:') || defined $opt_h) {
    print "scoreboard_update.pl [-c file] [-h] [-o dir] [-m script]\n";
    print "\n";
    print "  input        input directory\n";
    print "  output       output directory\n";
    print "\n";
    print "    -c file    use <file> as the configuration file [def: bin/nightlybuilds/builds.lst]\n";
    print "    -h         display this help\n";
    print "    -m script  make pretty with this script [def:bin/make_pretty.pl\n";
    print "    -o dir     directory to place files [def: html]\n";
    print "    -r         remove files\n";
    exit (1);
}

my $pretty = "bin/make_pretty.pl";
my $file = "bin/nightlybuilds/builds.lst";
my $dir = "html";

if (defined $opt_c) {
    $file = $opt_c;
}

if (defined $opt_o) {
    $dir = $opt_o;
}

if (defined $opt_m) {
    $pretty = $opt_m;
}

# Do the stuff

print 'Running Scoreboard Update at '.scalar (localtime ())."\n";

load_build_list ($file);
query_latest ();
update_cache ($dir);
update_html ($dir, $pretty);

print 'Finished Scoreboard Update at '.scalar (localtime ())."\n";

###############################################################################
###############################################################################