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