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