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