source: other-projects/nightly-tasks/diffcol/trunk/task.pl@ 27710

Last change on this file since 27710 was 27710, checked in by ak19, 11 years ago

Now checking out imagemagick binary (tested on linux) to work with image model collections like backdrop. However, the imagemagick binary does not work as it's looking for the libjpeg.so.62 library file, but only libjpeg.so.8.0.2 and its symlinks and an .la file exist.

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