root/other-projects/nightly-tasks/diffcol/trunk/task.pl @ 27678

Revision 27678, 24.2 KB (checked in by ak19, 6 years ago)

Uploading report to caveat-emptor page now works on windows

  • Property svn:executable set to *
Line 
1#!/usr/bin/perl -w 
2
3# This program is meant to run the nightly diffcol
4# It is meant to be an equivalent for the existing task bash script
5# But it is intended to be expanded to work for Windows and GS3
6# For windows, need to REMEMBER to set the correct shebangs at the top
7
8
9# TODO:
10# Have a caveat mode and a stable mode (as in snapshot/task.pl)
11#
12#} elsif ( $ENV{'TASK_NAME'} =~ "gs2-diffcol-(caveat|stable)" ) {
13#   $major_version = 2;
14#   $prefix="2t";
15#   $rk="tk2"; # test kit
16#} elsif ( $ENV{'TASK_NAME'} =~ "gs3-diffcol-(caveat|stable)" ) {
17#   $major_version = 3;
18#   $prefix="3t";
19#   $rk="tk3"; # test kit
20
21package diffcoltask;
22
23use Cwd;
24use Switch; # for switch(val) { case: ; ...}
25use File::Path; # for rmdir and mkdir type functions
26use File::Copy; # for recursive copying of folders but skipping .svn
27use File::Basename;
28
29use strict;
30no strict 'subs'; # allow barewords (eg STDERR) as function arguments
31
32my $isWin = ($^O =~ m/mswin/i) ? 1 : 0;
33my $sep = $isWin ? "\\" : "/";
34my $pathsep = $isWin ? ";" : ":";
35#my $script_ext = $isWin ? ".bat" : ".bash";
36my $setup_script = "setup"; # needs to become gs3-setup for GS3
37
38# TASK_HOME should be the toplevel diffcol folder
39$ENV{'TASK_HOME'} = getcwd unless defined $ENV{'TASK_HOME'};
40$ENV{'TASK_HOME'} =~ s@\/@\\@g if $isWin;
41##    print STDERR "@@@ TASK_HOME: ".$ENV{'TASK_HOME'}."\n";
42
43
44$ENV{'BIN_DIR'} = &filename_concat($ENV{'TASK_HOME'}, "bin");
45
46# we'll be using BLAT to send mail attachments on Windows
47my $blat = &filename_concat($ENV{'BIN_DIR'}, "blat", "full", "blat.exe");
48if($isWin && ! -e $blat) {
49    print STDERR "*** No blat.exe found in $blat.\n";
50    print STDERR "*** Blat needed to send mail with attachments on Windows.\n";
51    print STDERR "*** Extract the blat zip file found in $ENV{'BIN_DIR'}\n";
52    print STDERR "*** for your bit architecture and name the folder 'blat'\n";
53   
54    $blat = 0;
55}
56
57
58$ENV{'DATA_DIR'} = &filename_concat($ENV{'TASK_HOME'}, "diffcol-data");
59$ENV{'UPLOAD_DIR'} = &filename_concat($ENV{'TASK_HOME'}, "diffcol-reports");
60$ENV{'MONITOR_EMAIL'} = "greenstone_team\@cs.waikato.ac.nz"; # need to escape @ sign
61$ENV{'GSDL_SMTP'} = ""; #"smtp.gmail.com";
62##print STDERR "@@@ email: ".$ENV{'MONITOR_EMAIL'}."\n";
63
64# control if an existing compiled greenstone is used
65# or, if one should be checked out, which revision to checkout from svn
66$ENV{'SVN_OPT_REV'} = "-r head";
67#$ENV{'GSDLHOME'}=
68#$ENV{'GSDL3SRCHOME'}=
69
70
71# if the first arg is a digit, it's the new envi verbosity param. Take it off the array
72my $envi_verbose = shift(@ARGV) if(exists $ARGV[0] && $ARGV[0] =~ m/^\d+$/);
73
74#parse arguments
75my $action = "all";
76if(scalar(@ARGV) > 1) {
77    &printusage();
78    exit 0;
79}
80
81if(scalar(@ARGV) == 0) {
82    $action="all";
83}
84else {   
85    switch ($ARGV[0]) {
86    case qr/^(-h|-help|help)$/i { &printusage; exit 0; }
87    case qr/^(setup_greenstone|run_test|summarise|upload|all)$/ { $action=$ARGV[0]; }
88    else {
89        print STDERR "Bad subcommand.\n";
90        &printusage;
91        exit -1;
92    }
93    }
94}
95
96#check key environment vars are set
97if(!defined $ENV{'UPLOAD_DIR'}) {
98    print STDERR "Please set a UPLOAD_DIR for the test in an environment.sh file\n";
99    #return 1;
100}
101if(!defined $ENV{'DATA_DIR'}) {
102    print STDERR "Please set a DATA_DIR for the test in an environment.sh file\n";
103    #return 1;
104}
105if(!defined $ENV{'MONITOR_EMAIL'}) {
106    print STDERR "Please set a MONITOR_EMAIL for the test in an environment.sh file\n";
107    #return 1;
108}
109
110if($ENV{'DATA_DIR'} eq "/") {
111    print STDERR "DATA_DIR should not be the fs root\n";
112    #return 1;
113}
114
115print STDERR "DATA_DIR: ".$ENV{'DATA_DIR'}."\n";
116print STDERR "UPLOAD_DIR: ".$ENV{'UPLOAD_DIR'}."\n";
117
118#create an id for this test
119my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
120$year += 1900;
121$mon += 1;
122$mon = "0$mon" if ($mon < 10);
123$mday = "0$mday" if ($mday < 10);
124my $dateid="$year-$mon-$mday"; #my $dateid=($year+1900)."-".($mon+1)."-$mday";
125
126print STDERR "Starting test '$dateid'\n";
127
128
129# http://stackoverflow.com/questions/2149368/how-can-i-loop-through-files-in-a-directory-in-perl
130$ENV{'CLASSPATH'} = "";
131my $jar_lib_path = $ENV{'TASK_HOME'}.$sep."lib";
132my @files = <$jar_lib_path/*.jar>; # /full/path/to/diffcol/lib/*jar
133foreach my $file (@files) {
134    $file =~ s@\/@\\@g if $isWin;
135    $ENV{'CLASSPATH'}=$file.$pathsep.$ENV{'CLASSPATH'};
136}
137##print STDERR "**** classpath: ".$ENV{'CLASSPATH'}."\n";
138
139
140#set the location of the full report                                               
141my $xmlout=filename_concat($ENV{'DATA_DIR'}, "full-report-$dateid.xml");
142##print STDERR "XML: $xmlout\n";
143
144# the toplevel folder of the greenstone installation being used
145my $greenstone_home="";
146# gsdl is the checkout folder and can be greenstone2 or greenstone3
147my $gsdl="greenstone2";
148
149
150# Check if using existing compiled-up greenstone installation
151# and set the greenstone_home location accordingly
152
153if(defined $ENV{'GSDL3SRCHOME'} || defined $ENV{'GSDLHOME'}) {
154    print STDERR "Found existing Greenstone home, will use that instead\n";
155    $greenstone_home=$ENV{'GSDLHOME'};
156} else {
157    $greenstone_home=filename_concat($ENV{'DATA_DIR'}, $gsdl);
158}
159##print STDERR "GSHOME: $greenstone_home\n";
160
161#do the requested action
162if($action eq "setup_greenstone") {
163    &setup_greenstone;
164}
165elsif ($action eq "run_test") {
166    &run_test;
167}
168elsif ($action eq "summarise") {
169    &summarise;
170}
171elsif ($action eq "upload") {
172    &upload;
173    &mail_with_report_attached;
174}
175elsif ($action eq "all") {
176    &setup_greenstone;
177    &run_test;
178    &summarise;
179    &upload;
180    &mail_with_report_attached;
181}
182
183##********************************
184
185sub printusage
186{
187    print STDERR "Run as: $0 (help|setup_greenstone|run_test|summarise|upload|all)\n";
188}
189
190#http://stackoverflow.com/questions/7427262/read-a-file-and-save-it-in-variable-using-shell-script
191
192sub setup_greenstone
193{
194    #clean up from previous tests
195    print STDERR "about to clean up any old tests (Ctrl-C to cancel)"; # no newline
196    for my $i ( 1..5 ) {
197    sleep 1; # 1 second
198    print STDERR ".";
199    }
200    print STDERR "\n";
201
202    # http://perldoc.perl.org/File/Path.html
203    print STDERR "cleaning up previous tests\n";
204    &File::Path::remove_tree($ENV{'DATA_DIR'});
205
206    print STDERR "creating the data dir\n";
207    &File::Path::make_path($ENV{'DATA_DIR'}); # works like mkdir -p
208
209    chdir($ENV{'DATA_DIR'});
210
211    # use existing compiled-up greenstone installation, if a GSDLHOME set
212    if(defined $ENV{'GSDL3SRCHOME'} || defined $ENV{'GSDLHOME'}) {
213    print STDERR "Found existing Greenstone home, will use that instead";
214    return;
215    }
216
217    # Else checkout a GS from svn into DATA_DIR
218
219    #svn checkout of main gsdl directory                                       
220    print STDERR "checkout $gsdl:\n";
221    my $cmd = "svn co ".$ENV{'SVN_OPT_REV'}." http://svn.greenstone.org/main/trunk/greenstone2 $gsdl";
222    ##print STDERR "Checkout CMD: $cmd\n";
223
224    # # unlike backticks operator, system() will print the output of the command to the screen as it executes
225    # http://stackoverflow.com/questions/758611/how-to-flush-output-in-backticks-in-perl?rq=1
226    my $status = system "$cmd"; #my $status = `$cmd`;
227    if($status != 0) {
228        print STDERR "@@@ SVN checkout of $gsdl failed\n";
229        exit -1;
230    }   
231    print STDERR "done\n";
232
233    ##print STDERR "$ENV{'DATA_DIR'}$sep$gsdl\n";
234
235    chdir("$ENV{'DATA_DIR'}$sep$gsdl");
236
237    ##print STDERR "@@@ OS:  $^O.|".$Config{'archname64'}."|\n";
238
239   
240    if($isWin) {
241        print STDERR "Compiling $gsdl using makegs2.bat running in auto (silent) mode\n";
242   
243        # we're now in the GS2 folder, call makegs2 with silent param
244        $cmd = "makegs2.bat silent 2>> $ENV{'DATA_DIR'}/compilation-errors"; # STDERR is sent to compilation-errors file
245        $status = system $cmd;
246       
247    } else { # if we're on linux/darwin, need gnome-lib for the correct architecture.
248   
249    print STDERR "setting up gnome-lib-minimal for compilation\n";
250
251    # To get gnome-lib, need to determine bit architecture of the linux/darwin
252    # http://stackoverflow.com/questions/8963400/the-correct-way-to-read-a-data-file-into-an-array
253    # $Config{'archname64'} doesn't work on the Ubuntu and the Sys::Info package seems to not be supported
254    # well on ActivePerl.
255    # But since we know we're on a Linux/Darwin machine at this point, wecan just run `uname -m` and other linux cmds
256
257    my $gnome_lib_file = ($^O =~ m/macos/i) ? "darwin-intel" : "linux"; # assuming all darwin is intel, not ppc!!
258
259    my $bit_arch=`uname -m`;
260    $gnome_lib_file .= "-x64" if($bit_arch =~ m/64$/);
261
262    #svn checkout gnome-lib for this linux/darwin
263    chdir("$ENV{'DATA_DIR'}$sep$gsdl$sep"."ext"); #cd $DATA_DIR/$gsdl/ext
264
265    ##print STDERR "**** gnomelib: $gnome_lib_file\n";
266
267    # checkout and unpack gnome-lib-minimal
268
269    #svn export http://svn.greenstone.org/gs2-extensions/gnome-lib/trunk/gnome-lib-minimal-linux-x64.tar.gz gl.tar.gz
270    $cmd = "svn export http://svn.greenstone.org/gs2-extensions/gnome-lib/trunk/gnome-lib-minimal-".$gnome_lib_file.".tar.gz gl.tar.gz";
271    system $cmd;
272    system ("tar -xvzf gl.tar.gz");
273
274    chdir("gnome-lib-minimal");
275    ##print STDERR "*** ARCH: $bit_arch\n";
276
277    # need to run source devel.bash on gnome-lib followed by configure, make, make install
278    # in one go, in order to preserve the compile environment set up by sourcing devel.bash
279
280    # http://stackoverflow.com/questions/7369145/activating-a-virtualenv-using-a-shell-script-doesnt-seem-to-work
281    # http://ubuntuforums.org/showthread.php?t=1932504 linking /bin/sh to bash instead of dash
282   
283#   $cmd = "bash -c \"source ./devel.bash && cd ../.. && ./configure --enable-apache-httpd && make && make install\"";
284    $cmd = "bash -c \"";   
285
286    $cmd .= "source ./devel.bash";
287    $cmd .= " && cd ../..";
288
289    #configure
290    # $cmd .= " && ./configure";
291    $cmd .= " && echo 'configure $gsdl: ' ";
292        $cmd .= " && echo '<configure>' >> $xmlout";   
293        $cmd .= " && ./configure 2>> $ENV{'DATA_DIR'}/compilation-errors"; # configure
294        $cmd .= " && echo '</configure>' >> $xmlout";
295        $cmd .= " && echo 'done'";
296
297    #make
298    $cmd .= " && echo 'make $gsdl: '";
299    $cmd .= " && echo '<make>' >> $xmlout";
300        $cmd .= " && make 2>> $ENV{'DATA_DIR'}/compilation-errors"; # make
301        $cmd .= " && echo '</make>' >> $xmlout";
302        $cmd .= " && echo 'done'";
303
304    #make install
305    $cmd .= " && echo 'make install $gsdl: '";
306        $cmd .= " && echo '<make-install>' >> $xmlout";
307    $cmd .= " && make install 2>> $ENV{'DATA_DIR'}/compilation-errors"; # make install
308        $cmd .= " && echo '</make-install>' >> $xmlout";
309        $cmd .= " && echo 'done'";
310   
311    $cmd .= "\""; # close off cmd to bash and run it
312    $status = system $cmd;
313    }
314   
315    if($status != 0) {
316        print STDERR "@@@ Compile failed\n";
317        exit -1;
318    }
319   
320    # set the path to the greenstone_home variable                             
321    $greenstone_home="$ENV{'DATA_DIR'}$sep$gsdl";
322
323}
324
325# http://stackoverflow.com/questions/3377879/how-do-i-receive-command-output-immediately
326sub run_test
327{
328    open (my $xml_fh, '>'.$xmlout) || die "Could not open xml file $xmlout for appending: $!\n";
329
330    # perform the requested subcommands, outputting xml information
331    print $xml_fh "<test time=\"$dateid\" id=\"$dateid\">\n";
332   
333    # make sure that diffcol/model-collect is up to date before copying it over to greenstone-home                                                                           
334    print $xml_fh "Updating $ENV{'TASK_HOME'}/model-collect:\n";
335    my $cmd = "svn up $ENV{'TASK_HOME'}/model-collect"; #chdir("$ENV{'TASK_HOME'}/model-collect");
336    my $status = system "$cmd";
337
338    # go to whichever greenstone_home we're using
339    chdir($greenstone_home);
340
341    # get svn info
342    print STDERR "getting svn info: $xmlout\n";
343    print $xml_fh "<svn-info>\n";
344    &run_and_print_cmd("svn info", $xml_fh);
345    print $xml_fh "</svn-info>\n";
346    print STDERR "done\n";
347
348    #make two copies of the model-collect directory in gsdl
349    #one to be rebuilt and one as the basis for comparison
350    #strip both of all .svn directories
351   
352    #copy the model collections to the collect folder to be rebuilt
353    print STDERR "installing test collections and model collections to new $gsdl installation... ";
354   
355    #clean up
356    if(-d "collect") {
357    &File::Path::remove_tree("collect") || die "Error could not delete collect: $!";
358    }
359    &File::Path::remove_tree("model-collect");
360
361    #copy to collect and strip .svn subfolders
362    &File::Path::make_path("collect"); # create the folder and copy contents across
363    &copy_recursively(&filename_concat("$ENV{'TASK_HOME'}","model-collect"), "collect", ".svn");
364
365    #make the model copy
366    &File::Path::make_path("model-collect");
367    &copy_recursively("collect", "model-collect"); # copy contents across
368   
369    print STDERR "done\n";   
370   
371
372    #for each collection, import, build and diff with its model counterpart
373    opendir my($collect_handle), "collect" or die "Could not open dir $greenstone_home/collect: $!";
374    for my $collection (readdir $collect_handle) {
375    next if ($collection eq "." || $collection eq "..");
376    ##next if ($collection ne "Small-HTML"); ## TEMPORARY, FOR TESTING THIS SCRIPT
377
378    #escape the filename (in case of space)
379    $collection =~ s@ @\\ @g;
380    #getting just the basename of the collection would have been necessary had we not cd-ed into $gsdl
381
382    print STDERR "*** Found collection $collection\n";
383    print $xml_fh "<collection-test name=\"$collection\">\n";
384
385    #import
386# Ensure the OIDtype for importing is hash_on_full_filename
387# "to make document identifiers more stable across upgrades of the software,
388# although it means that duplicate documents contained in the collection are
389# no longer detected automatically."
390    print STDERR "$collection - Importing:\n";
391    print $xml_fh "<import>\n";
392    &run_build_script("import.pl -OIDtype hash_on_full_filename $collection -removeold");
393    print $xml_fh "</import>\n";
394    print STDERR "done\n"; 
395
396    #build
397    print STDERR "$collection - Building:\n";
398    print $xml_fh "<build>\n";
399    &run_build_script("buildcol.pl $collection -removeold");
400    print $xml_fh "</build>\n";
401    print STDERR "done\n";
402   
403    #rename the intermediate 'building' directory 'index'
404    print STDERR "$collection - Move \"building\" to \"index\"... ";
405    my $index = &filename_concat("collect", $collection, "index");
406    my $building = &filename_concat("collect", $collection, "building");
407    &File::Path::remove_tree($index);
408    # Renaming Directories, http://www.perlmonks.org/?node_id=177421
409    move($building, $index) or die "copy failed: $!"; # File::Copy::move
410    print STDERR "done\n";
411
412    #diffcol
413    print STDERR "$collection - Diffing:\n";
414    my $diffcol_dir = &filename_concat($ENV{'TASK_HOME'},"diffcol");
415    $cmd = "diffcol.pl -output xml -verbosity 10 $collection"; # need to run with ./diffcol.pl if bash script
416    &run_diff_script($cmd, $xml_fh, $diffcol_dir);
417
418    chdir($greenstone_home); # this is actually where we are
419    print STDERR "done\n";
420    print $xml_fh "</collection-test>\n";
421    }
422    closedir $collect_handle; # close handle to collect dir
423
424    print $xml_fh "</test>\n";
425    close($xml_fh);
426
427    print STDERR "done\n";
428}
429
430##***************************************************************
431# runs setup in greenstone_home before running the diff command
432sub run_diff_script {   
433    my ($cmd, $fh, $diffcol_dir) = @_;
434
435    # we're in greenstone_home now
436    if(!$isWin) {   
437    $cmd = "bash -c \"export GSDLHOME=&& source $setup_script.bash && cd $diffcol_dir && ./$cmd\"";
438
439    } else { # Need to prefix cmd -c/-k as necessary
440    $cmd = "cmd /c \"set GSDLHOME=&& $setup_script.bat && cd $diffcol_dir && perl -S $cmd\"";
441##  print STDERR "@@@@ Going to call command: $cmd\n"; 
442    }
443
444    return &run_and_print_cmd($cmd, $fh);
445}
446
447# runs setup in greenstone_home before running the given build command
448sub run_build_script {   
449    my ($cmd, $fh) = @_;
450
451#    chdir($greenstone_home);
452
453    # we are in $greenstone_home, can directly run the build cmd on the collection
454    if(!$isWin) {
455    $cmd = "bash -c \"export GSDLHOME=&& source $setup_script.bash && $cmd\"";
456
457    } else { # Need to prefix cmd -c/-k as necessary
458    $cmd = "cmd /c \"set GSDLHOME=&& $setup_script.bat && perl -S $cmd\"";
459##  print STDERR "@@@@ Going to call command: $cmd\n";
460   
461    }
462
463    return system($cmd);
464    #return &run_and_print_cmd($cmd, $fh); # doesn't work on cmds chained with bash -c
465}
466
467
468# http://stackoverflow.com/questions/758611/how-to-flush-output-in-backticks-in-perl?rq=1activeperl%20sys::info
469# http://stackoverflow.com/questions/1477500/how-do-i-get-the-output-of-an-external-command-in-perl
470sub run_and_print_cmd {
471    my ($cmd, $fh) = @_;
472   
473    open my $pin, "$cmd|" or die "unable to run cmd $cmd: $!"; # open(my $fh, '-|', 'powercfg -l') or die $!;
474
475    if(defined $fh) { # print cmd output both to the filehandle and to stdout
476    while (my $line = <$pin>) {
477        print $fh $line;
478#       print STDOUT $line; # if also printing cmd output to STDOUT
479    }
480    }
481    else { # no filehandle, so just need to print to stdout
482
483    # unlike backticks operator, system() will print the output of the command to the screen as it executes
484    # http://stackoverflow.com/questions/758611/how-to-flush-output-in-backticks-in-perl?rq=1
485       
486    my $status = system $cmd;
487    if($status != 0) {
488        print STDERR "ERROR ($status) running $cmd: $!\n";
489    }
490    }
491    close($pin);
492}
493
494sub filename_concat {
495    my $first_file = shift(@_);
496    my (@filenames) = @_;
497
498    # If first_file is not null or empty, then add it back into the list
499    if (defined $first_file && $first_file =~ /\S/)
500    {
501    unshift(@filenames, $first_file);
502    }
503
504    my $filename = join($sep, @filenames);
505    $filename =~ s/[\\\/]$//; # remove trailing slashes if any
506    return $filename;
507}
508
509
510# The following code is from
511# http://stackoverflow.com/questions/227613/how-can-i-copy-a-directory-recursively-and-filter-filenames-in-perl
512# It also states that "Perl's File::Copy is a bit broken (it doesn't copy permissions on Unix systems, for example)"
513sub copy_recursively {
514    my ($from_dir, $to_dir, $regex) = @_;
515    opendir my($dh), $from_dir or die "Could not open dir '$from_dir': $!";
516
517#    if(-d !$to_dir) {
518#   mkdir $to_dir or die "mkdir '$to_dir' failed: $!" if not -e $to_dir;
519#    }
520
521    for my $entry (readdir $dh) {
522    next if ($entry eq "." || $entry eq "..");
523        next if (defined $regex && $entry =~ /$regex/);
524        my $source = "$from_dir/$entry";
525        my $destination = "$to_dir/$entry";
526        if (-d $source) {
527            mkdir $destination or die "mkdir '$destination' failed: $!" if not -e $destination;
528            copy_recursively($source, $destination, $regex);
529        } else {
530            copy($source, $destination) or die "copy failed: $!";
531        }
532    }
533    closedir $dh;
534    return;
535}
536
537sub summarise {
538
539    # make a summarised Xml report
540    print STDERR "Summarizing the xml report... ";
541    my $cmd = "java org.apache.xalan.xslt.Process -IN $xmlout -XSL $ENV{'TASK_HOME'}/xsl/xml-report.xsl -OUT $ENV{'DATA_DIR'}/report-$dateid.xml";
542    my $status = system($cmd);
543    print STDERR "done\n";
544
545    # make a summarised HTMl report
546    print STDERR "Creating an html summary report... ";
547    $cmd = "java org.apache.xalan.xslt.Process -IN $ENV{'DATA_DIR'}/report-$dateid.xml -XSL $ENV{'TASK_HOME'}/xsl/html-report.xsl -OUT $ENV{'DATA_DIR'}/report-$dateid.html";
548    $status = system($cmd);
549    print STDERR "done\n";
550}
551
552sub upload {
553    # if the upload dir already existed, clear it of contents
554    if (-d $ENV{'UPLOAD_DIR'}) { #else rm $UPLOAD_DIR/*
555    # don't want to keep previous days reports
556    # else we will have to manually clear them at some point
557    # just generate the set of reports for this run of task.pl upload
558    # and
559    &File::Path::remove_tree($ENV{'UPLOAD_DIR'});
560    }
561    # recreate the upload directory   
562    &File::Path::make_path($ENV{'UPLOAD_DIR'});
563
564    # copy all *.xml and *.html files across to UPLOAD_DIR
565    opendir my($dh), $ENV{'DATA_DIR'} or die "Could not open DATA_DIR: $!";
566    for my $entry (readdir $dh) {
567    next if ($entry !~ m/(\.xml|\.html)$/);
568    # get the absolute path to the files before copying them over
569    $entry = &filename_concat($ENV{'DATA_DIR'}, $entry);
570    copy($entry, $ENV{'UPLOAD_DIR'});
571    }
572    closedir $dh;
573
574
575    # Upload the html file to puka
576    #default identity dir
577    if ( ! exists $ENV{'IDENTITY_DIR'} ) {
578    $ENV{'IDENTITY_DIR'} = "$ENV{'HOME'}${sep}.ssh";
579    }
580    if (! exists $ENV{'SNAPSHOT_MODE'} ) {
581    $ENV{'SNAPSHOT_MODE'} = "caveat";
582    }
583
584    #use the correct key for uploading
585    $ENV{'IDENTITY_FILE'} = "$ENV{'IDENTITY_DIR'}${sep}upload-" . $ENV{'SNAPSHOT_MODE'} . ($^O eq "MSWin32" ? ".ppk" : "");
586    if(-f $ENV{'IDENTITY_FILE'}) {
587    # if you need to touch the file on windows: http://stackoverflow.com/questions/51435/windows-version-of-the-unix-touch-command
588   
589    # the report we want to upload is actually just report-$dateid.html
590    my $command = "cd \"$ENV{'UPLOAD_DIR'}\" && tar -c *.html | "; #&& cat *.html | "; # && tar -c * |
591    $command .= ($^O eq "MSWin32" ? "plink" : "ssh");
592    $command .= " -T -i \"$ENV{'IDENTITY_FILE'}\" nzdl\@puka.cs.waikato.ac.nz";
593    #print "$command\n";
594    my $status = system("$command");
595    if($status != 0) {
596        print STDERR "*** Failed to upload test report to nzdl $status\n";
597    }
598    } else {
599    print STDERR "*** Cannot upload the test report to nzdl from this machine\n";
600    }
601
602    print STDERR "Finished uploading\n";
603}
604
605# Sending emails with perl: http://learn.perl.org/examples/email.html
606# Sending email attachments with perl: http://www.perlmonks.org/?node_id=19430
607# Sadly none of the packages are installed by default and use of MIME::Lite is discouraged
608sub mail_with_report_attached
609{
610    # email out with report attached, if the tests failed                                                     
611    print STDERR "Checking if successful... \n";
612    my $cmd = "java org.apache.xalan.xslt.Process -IN $xmlout -XSL $ENV{'TASK_HOME'}/xsl/passed-or-not.xsl";
613    #my $result = system($cmd);
614    my $result = `$cmd`;
615
616    print STDERR "result: $result\n";
617
618    if($result ne "yes") {
619    my $msg = "$gsdl regression test for $dateid failed";
620    my $subject = "Regression Test Failed"; #"$gsdl regression test for $dateid failed\n";
621    my $attach_file = &filename_concat($ENV{'DATA_DIR'}, "report-$dateid.html");
622
623    if($isWin) {
624        if($blat && $ENV{'GSDL_SMTP'}) {
625            # http://stackoverflow.com/questions/709635/sending-mail-from-batch-file
626            #blat -to user@example.com -server smtp.example.com -f batch_script@example.com -subject "subject" -body "body"
627
628            # need to install blat on windows
629            $cmd = "$blat -to $ENV{'MONITOR_EMAIL'} -server $ENV{'GSDL_SMTP'} -f $ENV{'MONITOR_EMAIL'} -attach $attach_file -subject \"$subject\" -body \"$msg\"";         
630            $result = system($cmd);
631        }
632        else {
633            $result = 0;
634            print STDERR "********************************************\n";
635            print STDERR "Need blat and SMTP set to send mail attachment\n";
636            print STDERR "Inspect report at: $attach_file\n";
637            print STDERR "********************************************\n";
638        }
639    } else { # linux
640        my $status = system("command -v mutt > /dev/null 2>&1;"); #better way of doing "which mutt"
641       
642        if($status != 0) { # mutt doesn't exist, can't send attachments, so send simple email
643        $cmd="echo '$gsdl regression test for $dateid failed.' | mail -s 'Regression Test Failed' $ENV{'MONITOR_EMAIL'}";
644
645        print STDERR "********************************************\n";
646        print STDERR "No mutt installed, unable to mail attachment\n";
647        print STDERR "Inspect report at: $attach_file\n";
648        print STDERR "********************************************\n";
649        } else {
650        #$cmd = "bash -c \"echo '$gsdl regression test for $dateid failed' | mutt -a $attach_file -s 'Regression Test Failed' -- $ENV{'MONITOR_EMAIL'}\"";
651        $cmd = "echo '$gsdl regression test for $dateid failed' | mutt -a $attach_file -s 'Regression Test Failed' -- $ENV{'MONITOR_EMAIL'}";
652        }
653       
654        # run the mail command
655        $result = system($cmd); #&run_and_print_cmd($cmd);
656    }
657
658   
659    if($result != 0) {
660        print STDERR "*** Unable to send email: $?\n";
661    }
662    else {
663        print STDERR "Sent mail with report attached.\n";
664    }
665    } else {
666    print STDERR "********************************************\n";
667    print STDERR "Tests were successful. Not sending mail.\n";
668    print STDERR "********************************************\n";
669    }
670}
671
672# The old version of this program contained the following, consisting of 1 line of active code:
673
674  # Invoke as: sjmc@br:/research/sjm84/envi/bin$ ./envi diffcol summarise
675  # Doing so will call this pl file and pass in "summarise" in ARGV
676  # This pl file will in turn call the task executable in this folder
677  # passing in "summarise" as a parameter.
678#system("/bin/bash -c \"../etc/tasks/diffcol/task @ARGV\"");
679
680  ##system("/bin/bash -c \"./task @ARGV\"");
681  ##print STDERR "/bin/bash -c ../etc/tasks/diffcol/task @ARGV"
682 
Note: See TracBrowser for help on using the browser.