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

Last change on this file since 28615 was 28615, checked in by ak19, 10 years ago

Now gets open-office

  • Property svn:executable set to *
File size: 36.6 KB
RevLine 
[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]21package diffcoltask;
[27556]22
[27621]23use Cwd;
[27628]24use Switch; # for switch(val) { case: ; ...}
[27621]25use File::Path; # for rmdir and mkdir type functions
26use File::Copy; # for recursive copying of folders but skipping .svn
27use File::Basename;
28
[27628]29use strict;
30no strict 'subs'; # allow barewords (eg STDERR) as function arguments
[27621]31
[27764]32
[27621]33my $isWin = ($^O =~ m/mswin/i) ? 1 : 0;
[27764]34my $isMac = ($^O =~ m/macos|darwin/i) ? 1 : 0;
[28571]35
36my $osversion="";
37# Need to get the correct gnome-lib-minimal for the OS
38# darwin11* Lion, darwin12* Mountain Lion, darwin9* and darwin10* are Leopard and Snow Leopard
39if ($^O eq "darwin") {
40 $osversion=`uname -r`; # e.g. 12.x.x
41 #$osversion =~ s@\..*$@@; # e.g.12
[28572]42 $osversion = ($osversion =~ m@^1[1-9](\.)?@i) ? "Lion-" : "";
[28571]43}
44
45
[27621]46my $sep = $isWin ? "\\" : "/";
47my $pathsep = $isWin ? ";" : ":";
48#my $script_ext = $isWin ? ".bat" : ".bash";
49my $setup_script = "setup"; # needs to become gs3-setup for GS3
[27687]50my $use_blat = 0; # if we ever get blat to send mail/attachments on Windows working, set this to 1
[27621]51
[28106]52my $use_local_rebuild = 0; # set to 1 (true) if just diffing and so we needn't copy model-collection over to the test collection again nor rebuild it (This is useful when having built the collection locally once before)
[28072]53my $use_static_model = 0; # set to 1 (true) if working with a non-svn model-collection. Defaults to 1 if $use_local_rebuild is turned on
[27701]54
[28072]55# if use_local_rebuild is on, use_static_model should be on
56if ($use_local_rebuild && !$use_static_model) {
57 $use_static_model = 1;
58}
59
[28172]60my $test_os = $isWin ? "windows" : ($isMac ? "darwin" : "linux");
61my $model_os = "linux"; # default
[28072]62
[27621]63# TASK_HOME should be the toplevel diffcol folder
64$ENV{'TASK_HOME'} = getcwd unless defined $ENV{'TASK_HOME'};
[27701]65if($isWin) {
66 $ENV{'TASK_HOME'} =~ s@\/@\\@g;
67 # need to convert TASK_HOME path name to resolve very subtle bug when running task.pl via
68 # run-gs2-diffcol.bat which uses environment.pl's TASK_HOME setting via envi
69 # At that point TASK_HOME is already defined but ends up lowercase, so that entries in archiveinf-doc
70 # end up sorted differently when db2txt -sort is applied compared to if TASK_HOME had kept its case.
71 require Win32; # for working out Windows Long Filenames from Win 8.3 short filenames
72 $ENV{'TASK_HOME'} = &Win32::GetLongPathName($ENV{'TASK_HOME'});
73}
[27621]74## print STDERR "@@@ TASK_HOME: ".$ENV{'TASK_HOME'}."\n";
75
76
[27678]77$ENV{'BIN_DIR'} = &filename_concat($ENV{'TASK_HOME'}, "bin");
78
79# we'll be using BLAT to send mail attachments on Windows
[27687]80my $blat = $use_blat ? &filename_concat($ENV{'BIN_DIR'}, "blat", "full", "blat.exe") : 0;
81if($isWin && $use_blat && ! -e $blat) {
82 print STDERR "\n***********************************\n";
83 print STDERR "No blat.exe found in $blat.\n";
84 print STDERR "Blat needed to send mail with attachments on Windows.\n";
85 print STDERR "Extract the blat zip file found in $ENV{'BIN_DIR'}\n";
86 print STDERR "for your bit architecture and name the folder 'blat'\n";
87 print STDERR "***********************************\n\n";
[27678]88 $blat = 0;
89}
90
91
[27668]92$ENV{'DATA_DIR'} = &filename_concat($ENV{'TASK_HOME'}, "diffcol-data");
93$ENV{'UPLOAD_DIR'} = &filename_concat($ENV{'TASK_HOME'}, "diffcol-reports");
[27621]94$ENV{'MONITOR_EMAIL'} = "greenstone_team\@cs.waikato.ac.nz"; # need to escape @ sign
[27678]95$ENV{'GSDL_SMTP'} = ""; #"smtp.gmail.com";
[27621]96##print STDERR "@@@ email: ".$ENV{'MONITOR_EMAIL'}."\n";
97
98# control if an existing compiled greenstone is used
99# or, if one should be checked out, which revision to checkout from svn
100$ENV{'SVN_OPT_REV'} = "-r head";
[27656]101#$ENV{'GSDLHOME'}=
102#$ENV{'GSDL3SRCHOME'}=
[27621]103
[27628]104
[27677]105# if the first arg is a digit, it's the new envi verbosity param. Take it off the array
106my $envi_verbose = shift(@ARGV) if(exists $ARGV[0] && $ARGV[0] =~ m/^\d+$/);
[27628]107
108#parse arguments
109my $action = "all";
[28114]110my $subaction = ""; # run_test can take subactions: --just_diff and --no_svn
111my @collections = ();
112
[28172]113# run_test can take any number of args
[28114]114if(scalar(@ARGV) > 1 && $ARGV[0] ne "run_test") {
[28172]115 print STDERR "**** Wrong number of arguments\n";
[28114]116 &printusage();
117 exit -1;
[27628]118}
119
120if(scalar(@ARGV) == 0) {
121 $action="all";
122}
123else {
124 switch ($ARGV[0]) {
[28114]125 case qr/^(-h|--?help|help)$/i { &printusage; exit 0; }
[27628]126 case qr/^(setup_greenstone|run_test|summarise|upload|all)$/ { $action=$ARGV[0]; }
127 else {
[28172]128 print STDERR "**** Bad subcommand.\n";
[27628]129 &printusage;
130 exit -1;
131 }
132 }
[28114]133
[28172]134 # run_test action can take a subaction: nosvn|justdiff. It can also take --modelOS (windows|linux|darwin)
[28114]135 # nosvn: uses the model-collect as static and copies it over to collect, rebuilding what's currently in model-collect instead of copying
136 # it out from the svn model-collect again.
137 # justdiff: same as nosvn, but doesn't copy over model-collection to collect, and doesn't rebuild either of them. Just does the diff part.
138 if($action eq "run_test" && scalar(@ARGV) >= 2) {
139 push(@collections, @ARGV);
140 shift @collections; # remove action from array
141
[28172]142 for (my $i=0; $i < scalar(@ARGV); $i++) {
143 if($ARGV[$i] =~ m@^--@) {
144 shift @collections; # remove subaction/flag from array
[28114]145
[28172]146 $subaction = $ARGV[$i];
[28114]147 if($subaction eq "--justdiff") {
[28172]148 $use_local_rebuild = $use_static_model = 1;
[28114]149 } elsif ($subaction eq "--nosvn") {
[28172]150 $use_static_model = 1;
151 #} elsif ($subaction =~ m/\-\-testOS/i && defined $ARGV[$i+1]) {
152 # $test_os = $ARGV[$i+1];
153 # $i++;
154 # shift @collections; # remove test_os value from array
155 } elsif ($subaction =~ m/\-\-modelOS/i && defined $ARGV[$i+1] && $ARGV[$i+1] =~ m/windows|linux|darwin/i) {
156 $model_os = $ARGV[$i+1];
157 $i++;
158 shift @collections; # remove model_os value from array
159 #print STDERR "Model_os specified: $model_os\n";
[28114]160 } else {
[28172]161 print STDERR "**** Bad subaction/value: ".$ARGV[$i]."\n";
162 &printusage;
163 exit -1;
164 }
165 }
[28114]166 }
167
168# foreach my $col (@collections) {
169# print STDERR "Collection: $col\n";
170# }
171 }
[27628]172}
173
[28114]174
[27621]175#check key environment vars are set
176if(!defined $ENV{'UPLOAD_DIR'}) {
[27629]177 print STDERR "Please set a UPLOAD_DIR for the test in an environment.sh file\n";
[27621]178 #return 1;
179}
180if(!defined $ENV{'DATA_DIR'}) {
[27629]181 print STDERR "Please set a DATA_DIR for the test in an environment.sh file\n";
[27621]182 #return 1;
183}
184if(!defined $ENV{'MONITOR_EMAIL'}) {
[27629]185 print STDERR "Please set a MONITOR_EMAIL for the test in an environment.sh file\n";
[27621]186 #return 1;
187}
188
189if($ENV{'DATA_DIR'} eq "/") {
[27629]190 print STDERR "DATA_DIR should not be the fs root\n";
[27621]191 #return 1;
192}
193
[27629]194print STDERR "DATA_DIR: ".$ENV{'DATA_DIR'}."\n";
195print STDERR "UPLOAD_DIR: ".$ENV{'UPLOAD_DIR'}."\n";
[27621]196
197#create an id for this test
198my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
199$year += 1900;
200$mon += 1;
201$mon = "0$mon" if ($mon < 10);
202$mday = "0$mday" if ($mday < 10);
[27702]203my $dateid="$year.$mon.$mday"; #my $dateid=($year+1900)."-".($mon+1)."-$mday";
[27621]204
[27629]205print STDERR "Starting test '$dateid'\n";
[27621]206
207
208# http://stackoverflow.com/questions/2149368/how-can-i-loop-through-files-in-a-directory-in-perl
209$ENV{'CLASSPATH'} = "";
210my $jar_lib_path = $ENV{'TASK_HOME'}.$sep."lib";
211my @files = <$jar_lib_path/*.jar>; # /full/path/to/diffcol/lib/*jar
[27628]212foreach my $file (@files) {
[27668]213 $file =~ s@\/@\\@g if $isWin;
[27621]214 $ENV{'CLASSPATH'}=$file.$pathsep.$ENV{'CLASSPATH'};
215}
216##print STDERR "**** classpath: ".$ENV{'CLASSPATH'}."\n";
217
218
219#set the location of the full report
220my $xmlout=filename_concat($ENV{'DATA_DIR'}, "full-report-$dateid.xml");
221##print STDERR "XML: $xmlout\n";
222
223# the toplevel folder of the greenstone installation being used
224my $greenstone_home="";
225# gsdl is the checkout folder and can be greenstone2 or greenstone3
226my $gsdl="greenstone2";
227
228
229# Check if using existing compiled-up greenstone installation
230# and set the greenstone_home location accordingly
231
232if(defined $ENV{'GSDL3SRCHOME'} || defined $ENV{'GSDLHOME'}) {
[27629]233 print STDERR "Found existing Greenstone home, will use that instead\n";
[27621]234 $greenstone_home=$ENV{'GSDLHOME'};
235} else {
236 $greenstone_home=filename_concat($ENV{'DATA_DIR'}, $gsdl);
237}
238##print STDERR "GSHOME: $greenstone_home\n";
239
[27628]240#do the requested action
241if($action eq "setup_greenstone") {
242 &setup_greenstone;
243}
244elsif ($action eq "run_test") {
245 &run_test;
246}
247elsif ($action eq "summarise") {
248 &summarise;
249}
250elsif ($action eq "upload") {
251 &upload;
252 &mail_with_report_attached;
253}
254elsif ($action eq "all") {
255 &setup_greenstone;
256 &run_test;
257 &summarise;
258 &upload;
259 &mail_with_report_attached;
260}
[27621]261
262##********************************
263
[27628]264sub printusage
265{
[28172]266# print STDERR "Run as: $0 (help|setup_greenstone|run_test <--modelOS windows|darwin|linux> <--justdiff|--nosvn> <col1 col2 ...> |summarise|upload|all)\n";
267 print STDERR "Run as: $0 (help|setup_greenstone|run_test|summarise|upload|all)\n";
268 print STDERR "where run_test can further take the following optional parameters:\n";
269 print STDERR "\t--modelOS (windows|darwin|linux)\n";
270 print STDERR "\t--justdiff|--nosvn\n";
271 print STDERR "\t<col1 col2 ...>\n";
[27628]272}
[27621]273
274#http://stackoverflow.com/questions/7427262/read-a-file-and-save-it-in-variable-using-shell-script
275
276sub setup_greenstone
277{
278 #clean up from previous tests
[27628]279 print STDERR "about to clean up any old tests (Ctrl-C to cancel)"; # no newline
[27621]280 for my $i ( 1..5 ) {
281 sleep 1; # 1 second
[27628]282 print STDERR ".";
[27621]283 }
[27628]284 print STDERR "\n";
[27621]285
286 # http://perldoc.perl.org/File/Path.html
[27629]287 print STDERR "cleaning up previous tests\n";
[27621]288 &File::Path::remove_tree($ENV{'DATA_DIR'});
289
[27629]290 print STDERR "creating the data dir\n";
[27621]291 &File::Path::make_path($ENV{'DATA_DIR'}); # works like mkdir -p
292
293 chdir($ENV{'DATA_DIR'});
294
295 # use existing compiled-up greenstone installation, if a GSDLHOME set
296 if(defined $ENV{'GSDL3SRCHOME'} || defined $ENV{'GSDLHOME'}) {
[27710]297 print STDERR "Found existing Greenstone home, will use that instead\n";
[27621]298 return;
299 }
300
301 # Else checkout a GS from svn into DATA_DIR
302
303 #svn checkout of main gsdl directory
[27629]304 print STDERR "checkout $gsdl:\n";
[27621]305 my $cmd = "svn co ".$ENV{'SVN_OPT_REV'}." http://svn.greenstone.org/main/trunk/greenstone2 $gsdl";
306 ##print STDERR "Checkout CMD: $cmd\n";
307
308 # # unlike backticks operator, system() will print the output of the command to the screen as it executes
309 # http://stackoverflow.com/questions/758611/how-to-flush-output-in-backticks-in-perl?rq=1
310 my $status = system "$cmd"; #my $status = `$cmd`;
[27668]311 if($status != 0) {
312 print STDERR "@@@ SVN checkout of $gsdl failed\n";
313 exit -1;
314 }
[27629]315 print STDERR "done\n";
[27621]316
317 ##print STDERR "$ENV{'DATA_DIR'}$sep$gsdl\n";
318
319 chdir("$ENV{'DATA_DIR'}$sep$gsdl");
320
321 ##print STDERR "@@@ OS: $^O.|".$Config{'archname64'}."|\n";
[27764]322
[27668]323 if($isWin) {
324 print STDERR "Compiling $gsdl using makegs2.bat running in auto (silent) mode\n";
325
326 # we're now in the GS2 folder, call makegs2 with silent param
327 $cmd = "makegs2.bat silent 2>> $ENV{'DATA_DIR'}/compilation-errors"; # STDERR is sent to compilation-errors file
328 $status = system $cmd;
[28106]329 if($status != 0) {
330 print STDERR "Greenstone compilation on Windows failed\n";
331 exit -1;
332 }
[27668]333
[27710]334 } else { # if we're on linux/darwin, need gnome-lib for the correct architecture. And need imagemagick to build imgs in collections
335
336 my $bit_arch=`uname -m`;
337
338 # imagmagick binary
339 print STDERR "Getting imagemagick binary\n";
[27724]340
341 my $os = $isMac ? "darwin" : "linux";
342 my $imagickzip = "imagemagick-$os";
[27668]343
[27710]344 if($isMac) {
[27962]345# $imagickzip .= "-10.5.tar.gz";
346 # at present, only the Imagemagick binaries created by Max for darwin work on the Macs
347 &File::Path::make_path($ENV{'DATA_DIR'}."$sep$gsdl$sep$os"); # need to ensure gsdl/bin/darwin exists
348 $cmd = "svn export http://svn.greenstone.org/main/trunk/binaries/mac/intel/imagemagick bin/darwin/imagemagick";
349 $status = system($cmd);
[27963]350 if($status != 0) {
351 print STDERR "@@@ Unable to get imagemagick for darwin\n";
352 }
353
354 # need ghostscript mac binary too for pdf to img conversions on mac
355 $cmd = "svn export http://svn.greenstone.org/main/trunk/binaries/mac/intel/ghostscript bin/darwin/ghostscript";
356 $status = system($cmd);
357 if($status != 0) {
358 print STDERR "@@@ Unable to get ghostscript for darwin\n";
359 }
360
[28005]361 # the imagemagick and ghostscript binaries have been set to executable on svn trac now
[27965]362# system("chmod -R u+x $ENV{'DATA_DIR'}/$gsdl/bin/darwin/imagemagick/bin/*");
363# system("chmod -R u+x $ENV{'DATA_DIR'}/$gsdl/bin/darwin/ghostscript/bin/*");
[27710]364 } else { # linux
365 my $extension64 = ($bit_arch =~ m/64$/) ? "-x64" : "";
[27724]366 $imagickzip .= "$extension64.tar.gz";
[27962]367
368 # now these next imagemagick steps (and those near the end of this sub) are just for linux, no longer also for mac
369 $cmd = "svn export http://svn.greenstone.org/gs2-extensions/imagemagick/trunk/$imagickzip ext/$imagickzip";
370 $status = system ($cmd);
371 system("cd ext && tar -xvzf $imagickzip");
[27724]372 }
[27710]373
374 # gnomelib binary
[27629]375 print STDERR "setting up gnome-lib-minimal for compilation\n";
[27621]376
377 # To get gnome-lib, need to determine bit architecture of the linux/darwin
378 # http://stackoverflow.com/questions/8963400/the-correct-way-to-read-a-data-file-into-an-array
379 # $Config{'archname64'} doesn't work on the Ubuntu and the Sys::Info package seems to not be supported
380 # well on ActivePerl.
381 # But since we know we're on a Linux/Darwin machine at this point, wecan just run `uname -m` and other linux cmds
382
[28571]383 # osversion will be "Lion" or ""
384 # and assuming all darwin is intel, not ppc!!
[28572]385 my $gnome_lib_file = $isMac ? "darwin-".$osversion."intel" : "linux";
[27621]386
[28571]387 $gnome_lib_file .= "-x64" if($bit_arch =~ m/64$/ && !$isMac); # linux only case
[27621]388
389 #svn checkout gnome-lib for this linux/darwin
390 chdir("$ENV{'DATA_DIR'}$sep$gsdl$sep"."ext"); #cd $DATA_DIR/$gsdl/ext
391
392 ##print STDERR "**** gnomelib: $gnome_lib_file\n";
393
394 # checkout and unpack gnome-lib-minimal
395
396 #svn export http://svn.greenstone.org/gs2-extensions/gnome-lib/trunk/gnome-lib-minimal-linux-x64.tar.gz gl.tar.gz
397 $cmd = "svn export http://svn.greenstone.org/gs2-extensions/gnome-lib/trunk/gnome-lib-minimal-".$gnome_lib_file.".tar.gz gl.tar.gz";
398 system $cmd;
399 system ("tar -xvzf gl.tar.gz");
400
401 chdir("gnome-lib-minimal");
402 ##print STDERR "*** ARCH: $bit_arch\n";
403
404 # need to run source devel.bash on gnome-lib followed by configure, make, make install
405 # in one go, in order to preserve the compile environment set up by sourcing devel.bash
406
407 # http://stackoverflow.com/questions/7369145/activating-a-virtualenv-using-a-shell-script-doesnt-seem-to-work
408 # http://ubuntuforums.org/showthread.php?t=1932504 linking /bin/sh to bash instead of dash
409
410# $cmd = "bash -c \"source ./devel.bash && cd ../.. && ./configure --enable-apache-httpd && make && make install\"";
411 $cmd = "bash -c \"";
412
413 $cmd .= "source ./devel.bash";
414 $cmd .= " && cd ../..";
415
416 #configure
417 # $cmd .= " && ./configure";
418 $cmd .= " && echo 'configure $gsdl: ' ";
419 $cmd .= " && echo '<configure>' >> $xmlout";
420 $cmd .= " && ./configure 2>> $ENV{'DATA_DIR'}/compilation-errors"; # configure
421 $cmd .= " && echo '</configure>' >> $xmlout";
422 $cmd .= " && echo 'done'";
423
424 #make
425 $cmd .= " && echo 'make $gsdl: '";
426 $cmd .= " && echo '<make>' >> $xmlout";
427 $cmd .= " && make 2>> $ENV{'DATA_DIR'}/compilation-errors"; # make
428 $cmd .= " && echo '</make>' >> $xmlout";
429 $cmd .= " && echo 'done'";
430
431 #make install
432 $cmd .= " && echo 'make install $gsdl: '";
433 $cmd .= " && echo '<make-install>' >> $xmlout";
434 $cmd .= " && make install 2>> $ENV{'DATA_DIR'}/compilation-errors"; # make install
435 $cmd .= " && echo '</make-install>' >> $xmlout";
436 $cmd .= " && echo 'done'";
437
438 $cmd .= "\""; # close off cmd to bash and run it
[27668]439 $status = system $cmd;
[27724]440
[27962]441 if(!$isMac) { # Linux
442 # Moving imagemagick after instead of before compilation, since bin/darwin gets overwritten during compilation
443 move("$ENV{'DATA_DIR'}$sep$gsdl$sep"."ext/imagemagick/$os", "$ENV{'DATA_DIR'}$sep$gsdl$sep"."bin/$os/imagemagick"); # http://www.perlmonks.org/?node_id=586537
444 unlink "$ENV{'DATA_DIR'}/$gsdl"."/ext/$imagickzip" or warn "Could not unlink ext/$imagickzip: $!";
445 &File::Path::remove_tree("$ENV{'DATA_DIR'}$sep$gsdl$sep"."ext/imagemagick"); # the untarred parent folder
446 }
[27724]447
[27621]448 }
[27668]449
450 if($status != 0) {
[28106]451 print STDERR "@@@ Compilation of Greenstone on Linux/Mac failed\n";
[27668]452 exit -1;
453 }
[28605]454
[28606]455 &getIsisGdl("$ENV{'DATA_DIR'}/$gsdl");
[27764]456
[27621]457 # set the path to the greenstone_home variable
458 $greenstone_home="$ENV{'DATA_DIR'}$sep$gsdl";
[27764]459
460}
[27621]461
[27764]462sub getPDFBox
463{
464 # current revision is 27763, but using "head" works
465 my $PDFBOX_TRAC_URL="http://trac.greenstone.org/export/head/gs2-extensions/pdf-box/trunk/pdf-box-java"; # both for .zip and .tar.gz extension
466 #"http://trac.greenstone.org/export/".$ENV{'SVN_OPT_REV'}."/gs2-extensions/pdf-box/trunk/pdf-box-java";
467
468 # now get the PDFBox extension for PDFBox tutorial
469 print STDERR "Getting pdfbox from $PDFBOX_TRAC_URL:\n";
470
471 chdir($greenstone_home);
472 my $cmd = "";
473 if ($isWin) {
474 $cmd = "setup.bat && cd ext && wget $PDFBOX_TRAC_URL.zip && unzip pdf-box-java.zip";
475
476 } elsif ($isMac) { # need to use curl not wget
477 $cmd = "cd ext && curl $PDFBOX_TRAC_URL.tar.gz > pdf-box-java.tar.gz && tar -xzf pdf-box-java.tar.gz";
478 }
479 else { # linux
[27765]480 $cmd = "bash -c \"export GSDLHOME=&& source setup.bash && cd ext && wget $PDFBOX_TRAC_URL.tar.gz && tar -xzf pdf-box-java.tar.gz\"";
[27764]481 }
482 my $status = system $cmd;
483 if($status != 0) {
484 print STDERR "@@@ Failed to set up PDFBox\n";
[27765]485 exit -1; # or proceed to testing other tutorials?
[27764]486 }
[27621]487}
488
[28615]489sub getOpenOfficeExt
490{
491 # current revision is 27763, but using "head" works
492 my $OOEXT_TRAC_URL="http://trac.greenstone.org/export/head/gs2-extensions/open-office/trunk/open-office-java"; # both for .zip and .tar.gz extension
493 #"http://trac.greenstone.org/export/".$ENV{'SVN_OPT_REV'}."/gs2-extensions/open-office/trunk/open-office-java";
494
495 # now get the OpenOffice extension for the AllDocTypes collection
496 print STDERR "Getting open office ext from $OOEXT_TRAC_URL:\n";
497
498 chdir($greenstone_home);
499 my $cmd = "";
500 if ($isWin) {
501 $cmd = "setup.bat && cd ext && wget $OOEXT_TRAC_URL.zip && unzip open-office-java.zip";
502
503 } elsif ($isMac) { # need to use curl not wget
504 $cmd = "cd ext && curl $OOEXT_TRAC_URL.tar.gz > open-office-java.tar.gz && tar -xzf open-office-java.tar.gz";
505 }
506 else { # linux
507 $cmd = "bash -c \"export GSDLHOME=&& source setup.bash && cd ext && wget $OOEXT_TRAC_URL.tar.gz && tar -xzf open-office-java.tar.gz\"";
508 }
509 my $status = system $cmd;
510 if($status != 0) {
511 print STDERR "@@@ Failed to set up the Open Office Extension\n";
512 exit -1; # or proceed to testing other tutorials?
513 }
514}
515
[28605]516sub getIsisGdl {
[28606]517 my $gsfolder = shift(@_);
[28605]518
[28606]519 if(!$isWin) {
[28605]520 my $bit_arch=`uname -m`;
521 if ($bit_arch =~ m/64$/) {
[28606]522 my $cmd = "";
523 if($isMac) {
524 $cmd = "cd $gsfolder/bin/darwin && curl http://www.greenstone.org/caveat-emptor/IsisGdl.macleopard > IsisGdl && chmod u+x IsisGdl";
525 } else { # linux
526 $cmd = "cd $gsfolder/bin/linux && wget http://www.greenstone.org/caveat-emptor/IsisGdl.bin32 && mv IsisGdl.bin32 IsisGdl && chmod u+x IsisGdl";
527 }
[28605]528 my $isis_status = system $cmd;
529 if($isis_status != 0) {
[28606]530 print STDERR "Unable to get IsisGdl from caveat page\n";
[28605]531 }
532 }
533 }
534}
535
[27621]536# http://stackoverflow.com/questions/3377879/how-do-i-receive-command-output-immediately
537sub run_test
[28114]538{
539 my $num_cols = scalar(@collections); # remember the empty case
540
541 if($num_cols == 0) { # deal with all collections
542 push (@collections, "");
[28615]543 # putting the empty string in the array so that the "all collections" case
544 # can be handled similar to how the case of user-specified collections is handled
[28114]545
546 } else { # deal with user specified set of collections
547 # prefix the directory separator to each collection name
548 @collections = map { $sep.$_ } @collections;
549 }
550
[27764]551 my $pdfbox = &filename_concat($greenstone_home, "ext", "pdf-box");
552 if(!-d $pdfbox) {
553 &getPDFBox();
554 }
555
[28615]556 my $openofficeext = &filename_concat($greenstone_home, "ext", "open-office");
557 if(!-d $openofficeext) {
558 &getOpenOfficeExt();
559 }
560
561 #&getIsisGdl("$greenstone_home");
[28605]562
[27668]563 open (my $xml_fh, '>'.$xmlout) || die "Could not open xml file $xmlout for appending: $!\n";
[27621]564
565 # perform the requested subcommands, outputting xml information
566 print $xml_fh "<test time=\"$dateid\" id=\"$dateid\">\n";
[28072]567
[28114]568 my ($cmd, $status);
[27724]569 # make sure that diffcol/model-collect is up to date before copying it over to greenstone-home
[28072]570
571 if(!$use_local_rebuild) {
572 print $xml_fh "Updating $ENV{'TASK_HOME'}/model-collect:\n";
[28114]573 for my $col (@collections) {
574 $cmd = "svn up $ENV{'TASK_HOME'}/model-collect$col"; #chdir("$ENV{'TASK_HOME'}/model-collect");
575 $status = system "$cmd";
576 }
[28072]577 }
[27724]578
[27621]579 # go to whichever greenstone_home we're using
580 chdir($greenstone_home);
581
582 # get svn info
[27629]583 print STDERR "getting svn info: $xmlout\n";
[27621]584 print $xml_fh "<svn-info>\n";
[27668]585 &run_and_print_cmd("svn info", $xml_fh);
[27621]586 print $xml_fh "</svn-info>\n";
[27629]587 print STDERR "done\n";
[27621]588
[28114]589 if(!$use_local_rebuild) {
590
591 #make two copies of the model-collect directory in gsdl
592 #one to be rebuilt and one as the basis for comparison
593 #strip both of all .svn directories
594
595 #copy the model collections to the collect folder to be rebuilt
596 print STDERR "installing test collections and model collections to new $gsdl installation... ";
597
[28072]598 #clean up
599 if(-d "collect") {
[28114]600 for my $col (@collections) {
[28172]601 if(-d "collect$col") {
602 &File::Path::remove_tree("collect$col") || die "Error could not delete collect: $!";
603 }
[28114]604 }
[28072]605 }
[27621]606
[28072]607 if($use_static_model) {
[28114]608 for my $col (@collections) {
609 #copy to collect and strip .svn subfolders
610 &File::Path::make_path("collect$col"); # create the collect folder and copy contents from static model-collection across
611 &copy_recursively("model-collect$col", "collect$col", ".svn");
612 }
[27621]613
[28072]614 } else { # the default situation: where we check out the model-collect from svn
[28114]615 for my $col (@collections) {
616 &File::Path::remove_tree("model-collect$col");
617
618 #copy to collect and strip .svn subfolders
619 &File::Path::make_path("collect$col"); # create the folder and copy contents across
620 &copy_recursively(&filename_concat("$ENV{'TASK_HOME'}","model-collect$col"), "collect$col", ".svn");
621
622 #make the model copy
623 &File::Path::make_path("model-collect$col");
624 &copy_recursively("collect$col", "model-collect$col"); # copy contents across
625 }
[28072]626 }
627
628 print STDERR "done\n";
629 }
630
[28114]631 #for each collection, import, build and diff with its model counterpart
632
633 # if working with all collections, read the list of collections from the folders in collect
634 if($num_cols == 0) {
635 @collections = (); # get rid of the empty string put in the array to represent "all collections"
636
637 opendir my($collect_handle), "collect" or die "Could not open dir $greenstone_home/collect: $!";
638
639 for my $collection (readdir $collect_handle) {
640 next if ($collection eq "." || $collection eq "..");
641 next if ($collection eq "modelcol");
642 push(@collections, $collection);
643 }
644 closedir $collect_handle; # close handle to collect dir
645 }
646
647 for my $collection (@collections) {
[27621]648
[28114]649 # next if ($collection ne "Demo-Lucene"); ## TEMPORARY, FOR TESTING THIS SCRIPT
650 # next if ($collection !~ m/OAI|METS|DSpace|MGPP|Lucene/); ## TEMPORARY, FOR TESTING THIS SCRIPT
651
[27621]652 #escape the filename (in case of space)
653 $collection =~ s@ @\\ @g;
654 #getting just the basename of the collection would have been necessary had we not cd-ed into $gsdl
[28114]655
656 $collection =~ s@^[\\/]@@g; # take the dir-sep prefix away again for user-specified collection names
657
658 if (! -d "collect$sep$collection") {
659 print STDERR "Collection $collection does not exist\n";
660 next;
661 }
662
[27621]663 print STDERR "*** Found collection $collection\n";
664 print $xml_fh "<collection-test name=\"$collection\">\n";
665
[28072]666 if(!$use_local_rebuild) {
667 #import
668 # Ensure the OIDtype for importing is hash_on_full_filename
669 # "to make document identifiers more stable across upgrades of the software,
670 # although it means that duplicate documents contained in the collection are
671 # no longer detected automatically."
672 print STDERR "$collection - Importing:\n";
673 print $xml_fh "<import>\n";
674 &run_build_script("import.pl -removeold $collection"); #-OIDtype hash_on_full_filename
675 print $xml_fh "</import>\n";
676 print STDERR "done\n";
[27621]677
[28072]678 #build
679 print STDERR "$collection - Building:\n";
680 print $xml_fh "<build>\n";
681 &run_build_script("buildcol.pl -removeold $collection");
682 print $xml_fh "</build>\n";
683 print STDERR "done\n";
684
685 #rename the intermediate 'building' directory 'index'
686 print STDERR "$collection - Move \"building\" to \"index\"... ";
687 my $index = &filename_concat("collect", $collection, "index");
688 my $building = &filename_concat("collect", $collection, "building");
689 &File::Path::remove_tree($index);
690 # Renaming Directories, http://www.perlmonks.org/?node_id=177421
691 move($building, $index) or die "copy failed: $!"; # File::Copy::move
692 print STDERR "done\n";
693 }
[27621]694 #diffcol
[27629]695 print STDERR "$collection - Diffing:\n";
[27621]696 my $diffcol_dir = &filename_concat($ENV{'TASK_HOME'},"diffcol");
[28172]697
698 # help diffcol to know on what os the model cols were generated
699 # and what os this test machine is (on which the test cols will be generated)
700 $cmd = "diffcol.pl -testos $test_os -modelos $model_os -output xml -verbosity 10 $collection"; # need to run with ./diffcol.pl if bash script
[27621]701 &run_diff_script($cmd, $xml_fh, $diffcol_dir);
702
703 chdir($greenstone_home); # this is actually where we are
[27629]704 print STDERR "done\n";
[27621]705 print $xml_fh "</collection-test>\n";
706 }
707
708 print $xml_fh "</test>\n";
709 close($xml_fh);
710
[27629]711 print STDERR "done\n";
[27621]712}
713
714##***************************************************************
715# runs setup in greenstone_home before running the diff command
716sub run_diff_script {
717 my ($cmd, $fh, $diffcol_dir) = @_;
718
719 # we're in greenstone_home now
720 if(!$isWin) {
[27667]721 $cmd = "bash -c \"export GSDLHOME=&& source $setup_script.bash && cd $diffcol_dir && ./$cmd\"";
[27621]722
723 } else { # Need to prefix cmd -c/-k as necessary
[27668]724 $cmd = "cmd /c \"set GSDLHOME=&& $setup_script.bat && cd $diffcol_dir && perl -S $cmd\"";
725## print STDERR "@@@@ Going to call command: $cmd\n";
[27621]726 }
727
728 return &run_and_print_cmd($cmd, $fh);
729}
730
731# runs setup in greenstone_home before running the given build command
732sub run_build_script {
733 my ($cmd, $fh) = @_;
734
735# chdir($greenstone_home);
[27687]736 # we are in $greenstone_home already, can directly run the build cmd on the collection
[27621]737 if(!$isWin) {
738 $cmd = "bash -c \"export GSDLHOME=&& source $setup_script.bash && $cmd\"";
739
740 } else { # Need to prefix cmd -c/-k as necessary
[27668]741 $cmd = "cmd /c \"set GSDLHOME=&& $setup_script.bat && perl -S $cmd\"";
[27621]742 }
[27687]743## print STDERR "@@@@ Going to call command: $cmd\n";
[27621]744
[27668]745 return system($cmd);
[27629]746 #return &run_and_print_cmd($cmd, $fh); # doesn't work on cmds chained with bash -c
[27621]747}
748
749
750# http://stackoverflow.com/questions/758611/how-to-flush-output-in-backticks-in-perl?rq=1activeperl%20sys::info
751# http://stackoverflow.com/questions/1477500/how-do-i-get-the-output-of-an-external-command-in-perl
752sub run_and_print_cmd {
753 my ($cmd, $fh) = @_;
754
755 open my $pin, "$cmd|" or die "unable to run cmd $cmd: $!"; # open(my $fh, '-|', 'powercfg -l') or die $!;
756
757 if(defined $fh) { # print cmd output both to the filehandle and to stdout
758 while (my $line = <$pin>) {
759 print $fh $line;
760# print STDOUT $line; # if also printing cmd output to STDOUT
761 }
762 }
763 else { # no filehandle, so just need to print to stdout
764
765 # unlike backticks operator, system() will print the output of the command to the screen as it executes
766 # http://stackoverflow.com/questions/758611/how-to-flush-output-in-backticks-in-perl?rq=1
767
768 my $status = system $cmd;
769 if($status != 0) {
770 print STDERR "ERROR ($status) running $cmd: $!\n";
771 }
772 }
773 close($pin);
774}
775
776sub filename_concat {
777 my $first_file = shift(@_);
778 my (@filenames) = @_;
779
780 # If first_file is not null or empty, then add it back into the list
781 if (defined $first_file && $first_file =~ /\S/)
782 {
783 unshift(@filenames, $first_file);
784 }
785
786 my $filename = join($sep, @filenames);
787 $filename =~ s/[\\\/]$//; # remove trailing slashes if any
788 return $filename;
789}
790
791
792# The following code is from
793# http://stackoverflow.com/questions/227613/how-can-i-copy-a-directory-recursively-and-filter-filenames-in-perl
794# It also states that "Perl's File::Copy is a bit broken (it doesn't copy permissions on Unix systems, for example)"
795sub copy_recursively {
796 my ($from_dir, $to_dir, $regex) = @_;
797 opendir my($dh), $from_dir or die "Could not open dir '$from_dir': $!";
798
799# if(-d !$to_dir) {
800# mkdir $to_dir or die "mkdir '$to_dir' failed: $!" if not -e $to_dir;
801# }
802
803 for my $entry (readdir $dh) {
804 next if ($entry eq "." || $entry eq "..");
805 next if (defined $regex && $entry =~ /$regex/);
806 my $source = "$from_dir/$entry";
807 my $destination = "$to_dir/$entry";
808 if (-d $source) {
809 mkdir $destination or die "mkdir '$destination' failed: $!" if not -e $destination;
810 copy_recursively($source, $destination, $regex);
811 } else {
812 copy($source, $destination) or die "copy failed: $!";
813 }
814 }
815 closedir $dh;
816 return;
817}
818
819sub summarise {
820
821 # make a summarised Xml report
[27629]822 print STDERR "Summarizing the xml report... ";
[27621]823 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";
824 my $status = system($cmd);
[27629]825 print STDERR "done\n";
[27621]826
827 # make a summarised HTMl report
[27629]828 print STDERR "Creating an html summary report... ";
[27621]829 $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";
830 $status = system($cmd);
[27629]831 print STDERR "done\n";
[28005]832
833 # Print whether the tests passed or failed
834 print STDERR "*******************************************\n";
835 print STDERR "Checking if successful... \n";
836 $cmd = "java org.apache.xalan.xslt.Process -IN $xmlout -XSL $ENV{'TASK_HOME'}/xsl/passed-or-not.xsl";
837 $status = `$cmd`; #$status = system($cmd);
838 print STDERR "result: $status\n";
839 print STDERR "*******************************************\n";
[27621]840}
841
842sub upload {
[27657]843 # if the upload dir already existed, clear it of contents
844 if (-d $ENV{'UPLOAD_DIR'}) { #else rm $UPLOAD_DIR/*
845 # don't want to keep previous days reports
846 # else we will have to manually clear them at some point
847 # just generate the set of reports for this run of task.pl upload
848 # and
849 &File::Path::remove_tree($ENV{'UPLOAD_DIR'});
850 }
851 # recreate the upload directory
852 &File::Path::make_path($ENV{'UPLOAD_DIR'});
[27621]853
854 # copy all *.xml and *.html files across to UPLOAD_DIR
855 opendir my($dh), $ENV{'DATA_DIR'} or die "Could not open DATA_DIR: $!";
856 for my $entry (readdir $dh) {
[27687]857 next if ($entry !~ m/(\.xml|\.html?)$/);
858
859 # copy the reports across with different names: with OS prefixed to them. And for the HTML file on Win, rename to HTM
860 # html files uploaded from windows to nzdl are empty for no reason. Uploading as htm seems to work
[27694]861 my $os_entry = $entry;
862 $os_entry =~ s@\[email protected]@ if $isWin;
[28571]863 if($isMac) {
[28572]864 $os_entry = "diffcol-".$^O."-".$osversion."$os_entry"; # darwin-Lion for Lion/Mountain Lion
[28571]865 } else {
866 $os_entry = "diffcol-".$^O."-$os_entry";
867 }
[27687]868
869 # get the absolute path to the original files before copying them over
870 $entry = &filename_concat($ENV{'DATA_DIR'}, $entry);
871
872 # copy them over with their new names
873## print STDERR "@@@@ copying across $entry to $ENV{'UPLOAD_DIR'} as $os_entry\n";
874 copy($entry, "$ENV{'UPLOAD_DIR'}$sep$os_entry"); #copy($entry, "$ENV{'UPLOAD_DIR'}");
[27621]875 }
876 closedir $dh;
877
[27637]878
879 # Upload the html file to puka
880 #default identity dir
881 if ( ! exists $ENV{'IDENTITY_DIR'} ) {
[27725]882 $ENV{'IDENTITY_DIR'} = "$ENV{'HOME'}${sep}.ssh"; # "C:\\Research\\Nightly\\tools\\keys" on windows, see environment.pl
[27637]883 }
884 if (! exists $ENV{'SNAPSHOT_MODE'} ) {
885 $ENV{'SNAPSHOT_MODE'} = "caveat";
886 }
887
[27678]888 #use the correct key for uploading
[27637]889 $ENV{'IDENTITY_FILE'} = "$ENV{'IDENTITY_DIR'}${sep}upload-" . $ENV{'SNAPSHOT_MODE'} . ($^O eq "MSWin32" ? ".ppk" : "");
890 if(-f $ENV{'IDENTITY_FILE'}) {
[27678]891 # if you need to touch the file on windows: http://stackoverflow.com/questions/51435/windows-version-of-the-unix-touch-command
892
[27694]893 # the report we want to upload is actually just os-diffcol-report-$dateid.html
[27687]894 my $command = "cd \"$ENV{'UPLOAD_DIR'}\" && tar -c *.htm* | "; #&& cat *.html | "; # && tar -c * |
[27637]895 $command .= ($^O eq "MSWin32" ? "plink" : "ssh");
896 $command .= " -T -i \"$ENV{'IDENTITY_FILE'}\" nzdl\@puka.cs.waikato.ac.nz";
897 #print "$command\n";
898 my $status = system("$command");
899 if($status != 0) {
[27678]900 print STDERR "*** Failed to upload test report to nzdl $status\n";
[27637]901 }
902 } else {
903 print STDERR "*** Cannot upload the test report to nzdl from this machine\n";
904 }
905
[27629]906 print STDERR "Finished uploading\n";
[27621]907}
908
909# Sending emails with perl: http://learn.perl.org/examples/email.html
910# Sending email attachments with perl: http://www.perlmonks.org/?node_id=19430
[27628]911# Sadly none of the packages are installed by default and use of MIME::Lite is discouraged
912sub mail_with_report_attached
913{
914 # email out with report attached, if the tests failed
[27629]915 print STDERR "Checking if successful... \n";
[27628]916 my $cmd = "java org.apache.xalan.xslt.Process -IN $xmlout -XSL $ENV{'TASK_HOME'}/xsl/passed-or-not.xsl";
917 #my $result = system($cmd);
918 my $result = `$cmd`;
[27621]919
[27629]920 print STDERR "result: $result\n";
[27621]921
[27628]922 if($result ne "yes") {
923 my $msg = "$gsdl regression test for $dateid failed";
924 my $subject = "Regression Test Failed"; #"$gsdl regression test for $dateid failed\n";
925 my $attach_file = &filename_concat($ENV{'DATA_DIR'}, "report-$dateid.html");
926
[27687]927 if($isWin) {
928 if($use_blat && $blat && $ENV{'GSDL_SMTP'}) {
[27678]929 # http://stackoverflow.com/questions/709635/sending-mail-from-batch-file
930 #blat -to [email protected] -server smtp.example.com -f [email protected] -subject "subject" -body "body"
[27628]931
[27678]932 # need to install blat on windows
933 $cmd = "$blat -to $ENV{'MONITOR_EMAIL'} -server $ENV{'GSDL_SMTP'} -f $ENV{'MONITOR_EMAIL'} -attach $attach_file -subject \"$subject\" -body \"$msg\"";
934 $result = system($cmd);
935 }
936 else {
[27687]937 $result = 1; # status from running mail command is 0 if success, 1 if fail
[27678]938 print STDERR "********************************************\n";
[27687]939 if ($use_blat) {
940 print STDERR "Need blat and SMTP set to send mail attachment\n" ;
941 } else {
942 print STDERR "Not set up to send mail on Windows\n";
943 }
[27678]944 print STDERR "Inspect report at: $attach_file\n";
945 print STDERR "********************************************\n";
946 }
947 } else { # linux
[27628]948 my $status = system("command -v mutt > /dev/null 2>&1;"); #better way of doing "which mutt"
949
950 if($status != 0) { # mutt doesn't exist, can't send attachments, so send simple email
951 $cmd="echo '$gsdl regression test for $dateid failed.' | mail -s 'Regression Test Failed' $ENV{'MONITOR_EMAIL'}";
952
953 print STDERR "********************************************\n";
954 print STDERR "No mutt installed, unable to mail attachment\n";
955 print STDERR "Inspect report at: $attach_file\n";
956 print STDERR "********************************************\n";
957 } else {
958 #$cmd = "bash -c \"echo '$gsdl regression test for $dateid failed' | mutt -a $attach_file -s 'Regression Test Failed' -- $ENV{'MONITOR_EMAIL'}\"";
959 $cmd = "echo '$gsdl regression test for $dateid failed' | mutt -a $attach_file -s 'Regression Test Failed' -- $ENV{'MONITOR_EMAIL'}";
960 }
[27678]961
962 # run the mail command
963 $result = system($cmd); #&run_and_print_cmd($cmd);
[27628]964 }
965
[27678]966
[27637]967 if($result != 0) {
968 print STDERR "*** Unable to send email: $?\n";
969 }
970 else {
971 print STDERR "Sent mail with report attached.\n";
972 }
[27628]973 } else {
974 print STDERR "********************************************\n";
[27629]975 print STDERR "Tests were successful. Not sending mail.\n";
[27628]976 print STDERR "********************************************\n";
977 }
978}
979
[27621]980# The old version of this program contained the following, consisting of 1 line of active code:
981
982 # Invoke as: sjmc@br:/research/sjm84/envi/bin$ ./envi diffcol summarise
983 # Doing so will call this pl file and pass in "summarise" in ARGV
984 # This pl file will in turn call the task executable in this folder
985 # passing in "summarise" as a parameter.
986#system("/bin/bash -c \"../etc/tasks/diffcol/task @ARGV\"");
987
988 ##system("/bin/bash -c \"./task @ARGV\"");
989 ##print STDERR "/bin/bash -c ../etc/tasks/diffcol/task @ARGV"
[27694]990
Note: See TracBrowser for help on using the repository browser.