Ignore:
Timestamp:
2013-06-14T19:47:30+12:00 (11 years ago)
Author:
ak19
Message:

Added main() behaviour and sending mail attachment and fixed issues in previous commit.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • other-projects/nightly-tasks/diffcol/trunk/task.pl

    r27621 r27628  
    66# For windows, need to REMEMBER to set the correct shebangs at the top
    77
     8package diffcoltask;
    89
    910use Cwd;
     11use Switch; # for switch(val) { case: ; ...}
    1012use File::Path; # for rmdir and mkdir type functions
    1113use File::Copy; # for recursive copying of folders but skipping .svn
    1214use File::Basename;
    1315
     16use strict;
     17no strict 'subs'; # allow barewords (eg STDERR) as function arguments
    1418
    1519my $isWin = ($^O =~ m/mswin/i) ? 1 : 0;
     
    2731$ENV{'UPLOAD_DIR'} = filename_concat($ENV{'TASK_HOME'}, "diffcol-reports");
    2832$ENV{'MONITOR_EMAIL'} = "greenstone_team\@cs.waikato.ac.nz"; # need to escape @ sign
     33$ENV{'GSDL_SMTP'} = "";
    2934##print STDERR "@@@ email: ".$ENV{'MONITOR_EMAIL'}."\n";
    3035
     
    3540#export GSDL3SRCHOME=
    3641
     42
     43
     44#parse arguments
     45my $action = "all";
     46if(scalar(@ARGV) > 1) {
     47    &printusage();
     48    exit 0;
     49}
     50
     51if(scalar(@ARGV) == 0) {
     52    $action="all";
     53}
     54else {   
     55    switch ($ARGV[0]) {
     56    case qr/^(-h|-help|help)$/i { &printusage; exit 0; }
     57    case qr/^(setup_greenstone|run_test|summarise|upload|all)$/ { $action=$ARGV[0]; }
     58    else {
     59        print STDERR "Bad subcommand.\n";
     60        &printusage;
     61        exit -1;
     62    }
     63    }
     64}
     65
    3766#check key environment vars are set
    3867if(!defined $ENV{'UPLOAD_DIR'}) {
     
    5685print STDOUT "DATA_DIR: ".$ENV{'DATA_DIR'}."\n";
    5786print STDOUT "UPLOAD_DIR: ".$ENV{'UPLOAD_DIR'}."\n";
    58 
    5987
    6088#create an id for this test
     
    74102##print STDERR "@@@ $jar_lib_path\n";
    75103my @files = <$jar_lib_path/*.jar>; # /full/path/to/diffcol/lib/*jar
    76 foreach $file (@files) {
     104foreach my $file (@files) {
    77105    $ENV{'CLASSPATH'}=$file.$pathsep.$ENV{'CLASSPATH'};
    78106}
     
    101129##print STDERR "GSHOME: $greenstone_home\n";
    102130
    103 #&setup_greenstone;
    104 &run_test;
    105 &summarise;
    106 &upload;
     131#do the requested action
     132if($action eq "setup_greenstone") {
     133    &setup_greenstone;
     134}
     135elsif ($action eq "run_test") {
     136    &run_test;
     137}
     138elsif ($action eq "summarise") {
     139    &summarise;
     140}
     141elsif ($action eq "upload") {
     142    &upload;
     143    &mail_with_report_attached;
     144}
     145elsif ($action eq "all") {
     146    &setup_greenstone;
     147    &run_test;
     148    &summarise;
     149    &upload;
     150    &mail_with_report_attached;
     151}
    107152
    108153##********************************
    109154
     155sub printusage
     156{
     157    print STDERR "Run as: $0 (help|setup_greenstone|run_test|summarise|upload|all)\n";
     158}
    110159
    111160
     
    115164{
    116165    #clean up from previous tests
    117     print STDOUT "about to clean up any old tests (Ctrl-C to cancel)"; # no newline
     166    print STDERR "about to clean up any old tests (Ctrl-C to cancel)"; # no newline
    118167    for my $i ( 1..5 ) {
    119168    sleep 1; # 1 second
    120     print STDOUT ".";
    121     }
    122     print STDOUT "\n";
     169    print STDERR ".";
     170    }
     171    print STDERR "\n";
    123172
    124173    # http://perldoc.perl.org/File/Path.html
     
    298347    print STDOUT "$collection - Importing:\n";
    299348    print $xml_fh "<import>\n";
    300     $cmd = "import.pl -OIDtype hash_on_full_filename $collection -removeold";
    301     &run_build_script($cmd);
     349#   $cmd = "import.pl -OIDtype hash_on_full_filename $collection -removeold";
     350#   &run_build_script($cmd);
     351    $cmd = "bash -c \"export GSDLHOME=&& source $setup_script.bash && import.pl -OIDtype hash_on_full_filename -removeold $collection\"";
     352    system($cmd);
    302353    print $xml_fh "</import>\n";
    303354    print STDOUT "done\n"; 
     
    306357    print STDOUT "$collection - Building:\n";
    307358    print $xml_fh "<build>\n";
    308     $cmd = "buildcol.pl $collection -removeold";
    309     &run_build_script($cmd);
     359#   $cmd = "buildcol.pl $collection -removeold";
     360#   &run_build_script($cmd);
     361    $cmd = "bash -c \"export GSDLHOME=&& source $setup_script.bash && buildcol.pl -removeold $collection\"";
     362    system($cmd);
    310363    print $xml_fh "</build>\n";
    311364    print STDOUT "done\n";
     
    370423    return &run_and_print_cmd($cmd, $fh);
    371424}
    372 #ERROR (36096) running bash -c "export GSDLHOME=&& source setup.bash && buildcol.pl Small-HTML -removeold": Illegal seek
    373425
    374426
     
    475527}
    476528
    477 
    478 # TO DO:
    479529# Sending emails with perl: http://learn.perl.org/examples/email.html
    480530# Sending email attachments with perl: http://www.perlmonks.org/?node_id=19430
    481 
     531# Sadly none of the packages are installed by default and use of MIME::Lite is discouraged
     532sub mail_with_report_attached
     533{
     534    # email out with report attached, if the tests failed                                                     
     535    print STDOUT "Checking if successful... \n";
     536    my $cmd = "java org.apache.xalan.xslt.Process -IN $xmlout -XSL $ENV{'TASK_HOME'}/xsl/passed-or-not.xsl";
     537    #my $result = system($cmd);
     538    my $result = `$cmd`;
     539
     540    print STDOUT "result: $result\n";
     541
     542    if($result ne "yes") {
     543    my $msg = "$gsdl regression test for $dateid failed";
     544    my $subject = "Regression Test Failed"; #"$gsdl regression test for $dateid failed\n";
     545    my $attach_file = &filename_concat($ENV{'DATA_DIR'}, "report-$dateid.html");
     546
     547    if($isWin) {       
     548        # http://stackoverflow.com/questions/709635/sending-mail-from-batch-file
     549        #blat -to [email protected] -server smtp.example.com -f [email protected] -subject "subject" -body "body"
     550
     551        $cmd = "blat -to $ENV{'MONITOR_EMAIL'} -server $ENV{'GSDL_SMTP'} -f $attach_file -subject $subject -body $msg"; # need to install blat on windows
     552
     553    } else {
     554        my $status = system("command -v mutt > /dev/null 2>&1;"); #better way of doing "which mutt"
     555       
     556        if($status != 0) { # mutt doesn't exist, can't send attachments, so send simple email
     557        $cmd="echo '$gsdl regression test for $dateid failed.' | mail -s 'Regression Test Failed' $ENV{'MONITOR_EMAIL'}";
     558
     559        print STDERR "********************************************\n";
     560        print STDERR "No mutt installed, unable to mail attachment\n";
     561        print STDERR "Inspect report at: $attach_file\n";
     562        print STDERR "********************************************\n";
     563        } else {
     564        #$cmd = "bash -c \"echo '$gsdl regression test for $dateid failed' | mutt -a $attach_file -s 'Regression Test Failed' -- $ENV{'MONITOR_EMAIL'}\"";
     565        $cmd = "echo '$gsdl regression test for $dateid failed' | mutt -a $attach_file -s 'Regression Test Failed' -- $ENV{'MONITOR_EMAIL'}";
     566        }
     567    }
     568
     569    # run the mail command
     570    system($cmd); # &run_and_print_cmd($cmd);
     571
     572    print STDOUT "Sent mail with report attached.\n";
     573    } else {
     574    print STDERR "********************************************\n";
     575    print STDOUT "Tests were successful. Not sending mail.\n";
     576    print STDERR "********************************************\n";
     577    }
     578}
    482579
    483580# The old version of this program contained the following, consisting of 1 line of active code:
Note: See TracChangeset for help on using the changeset viewer.