source: main/trunk/greenstone2/bin/script/build@ 24355

Last change on this file since 24355 was 24192, checked in by ak19, 13 years ago

Sam discovered that using dollar-Config{perlpath} in place of dollar-hat-X is the better way to obtain the path to the perl that is being used. We hope this will not be a relative path on the Mac as dollar-hat-x was on Professor Witten's Mac when we tried it there today.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 26.3 KB
Line 
1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# build --
6# A component of the Greenstone digital library software
7# from the New Zealand Digital Library Project at the
8# University of Waikato, New Zealand.
9#
10# Copyright (C) 2000 New Zealand Digital Library Project
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25#
26###########################################################################
27
28# This perl script may be called directly or by running build.bat on
29# windows (build.bat is in bin\windows)
30
31# Note that this script has grown over time and now has many options for
32# use when called from within the collector. If it appears to
33# over-complicate things a little, that's why.
34
35# I think that currently (Nov 2010) its only officially used from the collector
36# and depositor
37
38package build;
39
40use strict;
41no strict 'refs';
42
43use FileHandle;
44use File::Copy;
45use Config; # for getting the perlpath in the recommended way
46
47BEGIN {
48
49 die "GSDLHOME not set - did you remember to source setup.bash (unix) or " .
50 "run setup.bat (windows)?\n" unless defined $ENV{'GSDLHOME'};
51 die "GSDLOS not set - did you remember to source setup.bash (unix) or " .
52 "run setup.bat (windows)?\n" unless defined $ENV{'GSDLOS'};
53 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
54
55 STDOUT->autoflush(1);
56 STDERR->autoflush(1);
57}
58
59use lib qq($ENV{'GSDLHOME'}/perllib/cpan);
60use Mail::Sendmail;
61use parsargv;
62use util;
63use cfgread;
64use colcfg;
65use dbutil;
66
67# set up greenstone environment
68&util::setup_greenstone_env($ENV{'GSDLHOME'}, $ENV{'GSDLOS'});
69# is the following needed with the previous line??
70# set up path - this allows for paths not to be supplied to system calls
71# and overcomes problems when GSDLHOME contains spaces (double quoting
72# the call doesn't work on win2k and probably other variants of winnt)
73my $path_separator = ":";
74$path_separator = ";" if $ENV{'GSDLOS'} =~ /^windows$/;
75$ENV{'PATH'} = &util::filename_cat($ENV{'GSDLHOME'}, "bin", $ENV{'GSDLOS'}) .
76 $path_separator . &util::filename_cat($ENV{'GSDLHOME'}, "bin", "script") .
77 $path_separator . $ENV{'PATH'};
78
79# all the input option variables
80my ($optionfile, $indextype, $append, $manifest, $remove_archives, $remove_import, $buildtype, $infodbtype, $maxdocs, @download, $collectdir, $site, $dontinstall, $save_archives, $out, $make_writable, $log_events, $event_log_file, $email_events, $mail_server, $statsfile, $event_header);
81
82&parse_args (\@ARGV);
83
84my ($collection) = @ARGV;
85
86if (!defined $collection || $collection !~ /\w/) {
87 print STDERR "You must specify a collection to build\n";
88 &print_usage();
89 die "\n";
90}
91
92if ($optionfile =~ /\w/) {
93 open (OPTIONS, $optionfile) || die "Couldn't open $optionfile\n";
94 my $line = [];
95 my $options = [];
96 while (defined ($line = &cfgread::read_cfg_line ('build::OPTIONS'))) {
97 push (@$options, @$line);
98 }
99 close OPTIONS;
100 &parse_args ($options);
101}
102
103if ($maxdocs == -1) {
104 $maxdocs = "";
105} else {
106 $maxdocs = "-maxdocs $maxdocs";
107}
108
109my $cdir = $collectdir;
110my $gs_mode;
111if (defined $site && $site =~ /\w/)
112{
113 die "GSDL3HOME not set." unless $ENV{'GSDL3HOME'};
114 $cdir = &util::filename_cat ($ENV{'GSDL3HOME'}, "sites", $site, "collect") unless $collectdir =~ /\w/;
115 $gs_mode = "gs3";
116}
117else
118{
119 $cdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect") unless $collectdir =~ /\w/;
120 $gs_mode = "gs2";
121}
122
123my $importdir = &util::filename_cat ($cdir, $collection, "import");
124my $archivedir = &util::filename_cat ($cdir, $collection, "archives");
125my $buildingdir = &util::filename_cat ($cdir, $collection, "building");
126my $indexdir = &util::filename_cat ($cdir, $collection, "index");
127my $etcdir = &util::filename_cat ($cdir, $collection, "etc");
128my $bindir = &util::filename_cat ($ENV{'GSDLHOME'}, "bin");
129
130my $use_out = 0;
131my $outfile = $out;
132if ($out !~ /^(STDERR|STDOUT)$/i) {
133 open (OUT, ">$out") || die "Couldn't open output file $out\n";
134 $out = "OUT";
135
136 # delete any existing .final file
137 &util::rm ("$outfile.final") if -e "$outfile.final";
138
139 $use_out = 1;
140}
141$out->autoflush(1);
142
143# delete any .kill file left laying around from a previously aborted build
144if (-e &util::filename_cat ($cdir, $collection, ".kill")) {
145 &util::rm (&util::filename_cat ($cdir, $collection, ".kill"));
146}
147
148# get maintainer email address from main.cfg
149my $maintainer = "NULL";
150my $main_cfg = &util::filename_cat ($ENV{'GSDLHOME'}, "etc", "main.cfg");
151my $cfgdata = &cfgread::read_cfg_file ($main_cfg, "maintainer");
152if (defined $cfgdata->{'maintainer'} && $cfgdata->{'maintainer'} =~ /\w/) {
153 $maintainer = $cfgdata->{'maintainer'};
154}
155# if maintainer is "NULL" email_events should be disabled
156if ($maintainer =~ /^NULL$/i) {
157 $email_events = "";
158}
159
160&main();
161
162if ($use_out) {
163 close OUT;
164
165 # if we've created a build log we'll copy it to the collection's etc
166 # directory
167 my ($final_etcdir);
168 if ($dontinstall) {
169 $final_etcdir = &util::filename_cat($collectdir, "etc", "build.log");
170 } else {
171 $final_etcdir = &util::filename_cat($ENV{'GSDLHOME'}, "collect", $collection, "etc", "build.log");
172 }
173
174 &util::cp($outfile, $final_etcdir);
175}
176
177sub print_usage {
178 print STDOUT "\n";
179 print STDOUT "build: Builds a Greenstone collection (i.e. runs import.pl and buildcol.pl\n";
180 print STDOUT " then copies the resulting indexes to the correct place).\n\n";
181 print STDOUT " usage: $0 [options] collection-name\n\n";
182 print STDOUT " options:\n";
183 print STDOUT " -optionfile file Get options from file, useful on systems where\n";
184 print STDOUT " long command lines may cause problems\n";
185 print STDOUT " -indextype mg|mgpp|lucene \n";
186 print STDERR " Specify the type of indexer used in this collection\n";
187 print STDERR " If -append is used then -indextype is needed to \n";
188 print STDERR " determine how to run buildcol.pl as well as update\n";
189 print STDERR " 'building' and 'index' according.\n";
190 print STDOUT " -append Add new files to existing collection\n";
191 print STDOUT " -manifest Use manifest.xml file to determine which files to process.\n";
192 print STDOUT " -remove_archives Remove archives directory after successfully\n";
193 print STDOUT " building the collection.\n";
194 print STDOUT " -remove_import Remove import directory after successfully\n";
195 print STDOUT " importing the collection.\n";
196 print STDOUT " -buildtype build|import If 'build' attempt to build directly\n";
197 print STDOUT " from archives directory (bypassing import\n";
198 print STDOUT " stage). Defaults to 'import'\n";
199 print STDOUT " -maxdocs number Maximum number of documents to build\n";
200 print STDOUT " -download directory Directory (or file) to get import documents from.\n";
201 print STDOUT " There may be multiple download directories and they\n";
202 print STDOUT " may be of type http://, ftp://, or file://\n";
203 print STDOUT " Note that any existing import directory will be\n";
204 print STDOUT " deleted to make way for the downloaded data if\n";
205 print STDOUT " a -download option is supplied\n";
206 print STDOUT " -collectdir directory Collection directory (defaults to " .
207 &util::filename_cat($ENV{'GSDLHOME'}). "collect for Greenstone2;\n";
208 print STDOUT" for Greenstone3 use -site option and then collectdir default will be\n";
209 print STDOUT " set to the collect folder within that site.)\n";
210 print STDOUT " -site Specify the site within a Greenstone3 installation to use.\n";
211 print STDOUT " -dontinstall Only applicable if -collectdir is set to something\n";
212 print STDOUT " other than the default. -dontinstall will suppress\n";
213 print STDOUT " the default behaviour which is to install the\n";
214 print STDOUT " collection to the gsdl/collect directory once it has\n";
215 print STDOUT " been built.\n";
216 print STDOUT " -save_archives Create a copy of the existing archives directory\n";
217 print STDOUT " called archives.org\n";
218 print STDOUT " -out Filename or handle to print output status to.\n";
219 print STDOUT " The default is STDERR\n";
220 print STDOUT " -statsfile name Filename or handle to print import statistics to.\n";
221 print STDOUT " The default is STDERR\n";
222 print STDOUT " -make_writable If set build will make the collection and any\n";
223 print STDOUT " temporary files it created globally writable after\n";
224 print STDOUT " it finishes\n";
225 print STDOUT " -log_events Log important events (collection built successfully\n";
226 print STDOUT " etc.) to event_log_file\n";
227 print STDOUT " -event_log_file file File to append important events to (defaults to\n";
228 print STDOUT " " . &util::filename_cat ($ENV{'GSDLHOME'}, "etc", "events.txt") . "\n";
229 print STDOUT " -email_events addr Comma separated list of email addresses to mail\n";
230 print STDOUT " details of important collection building events\n";
231 print STDOUT " -mail_server server The outgoing (SMTP) mail server to be used by\n";
232 print STDOUT " email_events. email_events will be disabled if\n";
233 print STDOUT " mail_server isn't set\n";
234 print STDOUT " -event_header file File containing a header to go on any event\n";
235 print STDOUT " messages. If not specified build will create a\n";
236 print STDOUT " generic header\n\n";
237 print STDOUT " [Type \"build | more\" if this help text scrolled off your screen]";
238 print STDOUT "\n" unless $ENV{'GSDLOS'} =~ /^windows$/i;
239}
240
241sub main {
242
243 if ($save_archives && -d $archivedir) {
244 print $out "caching original archives to ${archivedir}.org\n";
245 &util::cp_r ($archivedir, "${archivedir}.org");
246 }
247
248 # do the download thing if we have any -download options
249 if (scalar (@download)) {
250 # remove any existing import data
251 if (&has_content ($importdir)) {
252 #print $out "build: WARNING: removing contents of $importdir\n";
253 #&util::rm_r ($importdir);
254 }
255
256 foreach my $download_dir (@download) {
257
258 # remove any leading or trailing whitespace from filenames (just in case)
259 $download_dir =~ s/^\s+//;
260 $download_dir =~ s/\s+$//;
261
262 if ($download_dir =~ /^(http|ftp):\/\//) {
263 # use wget to mirror http or ftp urls
264 # options used are:
265 # -P = the directory to download documents to
266 # -np = don't ascend to parent directories. this means that only documents
267 # that live in the same directory or below on the same server as
268 # the given url will be downloaded
269 # -nv = not too verbose
270 # -r = recursively mirror
271 # -N = use time-stamping to see if an up-to-date local copy of each
272 # file already exists. this may be useful if wget fails and
273 # is restarted
274 # -l inf = infinite recursion depth
275 # -R "*\?*" = don't download cgi based urls
276 # -o = the output file to write download status to (only used if the -out
277 # option was given to build)
278
279 #Config{perlpath}, like $^X, is a special variable containing the full path to the current perl executable we are in
280 my $download_cmd = "\"$Config{perlpath}\" -S gsWget.pl -P \"$importdir\" -np -nv";
281 $download_cmd .= " -r -N -l inf -R \"*\\?*\"";
282 $download_cmd .= " -o \"$outfile.download\"" if $use_out;
283 $download_cmd .= " \"$download_dir\"";
284 system ($download_cmd);
285
286 # note that wget obeys the robot rules. this means that it will have
287 # downloaded a robots.txt file if one was present. since it's unlikely
288 # anyone really wants to include it in a collection we'll delete it.
289 # robots.txt shouldn't be more than two directories deep (I think it will
290 # always be exactly two deep but will look for it in the top directory too)
291 # so that's as far as we'll go looking for it.
292 if (opendir (DIR, $importdir)) {
293 my @files = readdir DIR;
294 closedir DIR;
295 foreach my $file (@files) {
296 next if $file =~ /^\.\.?$/;
297 if ($file =~ /^robots.txt$/i) {
298 &util::rm (&util::filename_cat ($importdir, $file));
299 last;
300 } else {
301 $file = &util::filename_cat ($importdir, $file);
302 if (-d $file) {
303 if (opendir (DIR, $file)) {
304 my @files2 = readdir DIR;
305 closedir DIR;
306 foreach my $file2 (@files2) {
307 if ($file2 =~ /^robots.txt$/i) {
308 &util::rm (&util::filename_cat ($file, $file2));
309 last;
310 }
311 }
312 }
313 }
314 }
315 }
316 }
317
318 # if using output directory append the file download output to it
319 &append_file ($out, "$outfile.download");
320
321 } else {
322 # we assume anything not beginning with http:// or ftp://
323 # is a file or directory on the local file system.
324 $download_dir =~ s/^file:(\/\/)?//;
325 $download_dir =~ s/^\s+//; # may be whitespace between "file://" and the rest
326
327 if (-e $download_dir) {
328 # copy download_dir and all it contains to the import directory
329 #Config{perlpath}, like $^X, is a special variable containing the full path to the current perl executable we are in
330 my $download_cmd = "\"$Config{perlpath}\" -S filecopy.pl";
331 $download_cmd .= " -collectdir \"$collectdir\"" if $collectdir =~ /\w/;
332 $download_cmd .= " -site \"$site\"" if $site =~ /\w/;
333 $download_cmd .= " -out \"$outfile.download\"" if $use_out;
334 $download_cmd .= " \"" . $download_dir . "\" " . $collection;
335
336 my $download_status = system ($download_cmd);
337 if ($download_status > 0)
338 {
339 die "Failed to execute: $download_cmd\n";
340 }
341
342
343 # if using output directory append the file download output to it
344 &append_file ($out, "$outfile.download");
345 } else {
346 print $out "WARNING: '$download_dir' does not exist\n";
347 }
348 }
349 }
350 }
351
352 `echo $importdir ; ls $importdir `;
353
354 my $col_cfg_file;
355 if ($gs_mode eq "gs3") {
356 $col_cfg_file = &util::filename_cat($etcdir, "collectionConfig.xml");
357 } else {
358 $col_cfg_file = &util::filename_cat($etcdir, "collect.cfg");
359 }
360
361 my $collect_cfg = &colcfg::read_collection_cfg ($col_cfg_file, $gs_mode);
362 # get the database type for this collection from its configuration file (may be undefined)
363 $infodbtype = $collect_cfg->{'infodbtype'} || &dbutil::get_default_infodb_type();
364
365 my $archiveinf_doc_file_path = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archivedir);
366 if (-e $archiveinf_doc_file_path) {
367 if (&has_content ($importdir)) {
368 if ($buildtype eq "build") {
369 &gsdl_build();
370 } else {
371 &gsdl_import();
372 &gsdl_build();
373 }
374 } else {
375 # there are archives but no import, build directly from archives
376 print $out "build: no import material was found, building directly\n";
377 print $out " from archives\n";
378 &gsdl_build();
379 }
380 } else {
381 if (&has_content ($importdir)) {
382 if ($buildtype eq "build") {
383 print $out "build: can't build directly from archives as no\n";
384 print $out " imported archives exist (did you forget to\n";
385 print $out " move the contents of $collection/import to\n";
386 print $out " collection/archives?)\n";
387 }
388 &gsdl_import();
389 if (&has_content ($archivedir, "^archiveinf-doc\..*\$")) {
390 &gsdl_build();
391 } else {
392 my $msg = "build: ERROR: The collection could not be built as no\n";
393 $msg .= " valid data was imported. Are at least some of\n";
394 $msg .= " the files you imported in a format that can be\n";
395 $msg .= " processed by the specified Greenstone plugins?\n";
396 print $out $msg;
397 &log_event ($msg);
398 &final_out (6) if $use_out;
399 die "\n";
400 }
401 } else {
402 # no import or archives
403 my $msg = "build: ERROR: The collection could not be built as it contains no data.\n";
404 print $out $msg;
405 &log_event ($msg);
406 &final_out (1) if $use_out;
407 die "\n";
408 }
409 }
410
411 if ($collectdir ne "" && !$dontinstall) {
412
413 my $install_collectdir;
414 if (defined $ENV{'GSDL3HOME'})
415 {
416
417 if ((defined $site) && ($site ne ""))
418 {
419 $install_collectdir = &util::filename_cat ($ENV{'GSDL3HOME'}, "sites", $site, "collect");
420 }
421 else
422 {
423 my $msg = "build: ERROR: Need to specify the site within the Greenstone3 installation.";
424 print $out $msg;
425 &log_event ($msg);
426 &final_out (6) if $use_out;
427 die "\n";
428 }
429 }
430 else
431 {
432 $install_collectdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect");
433 }
434
435 if (!&util::filenames_equal ($collectdir, $install_collectdir)) {
436
437 # install collection to gsdl/collect
438 print $out "installing the $collection collection\n";
439 my $newdir = &util::filename_cat ($install_collectdir, $collection);
440 my $olddir = &util::filename_cat ($collectdir, $collection);
441 if (-d $newdir) {
442 my $msg = "build: Could not install collection as $newdir\n" .
443 " already exists. Collection will remain at\n$olddir\n";
444
445 print $out $msg;
446 &log_event ($msg);
447 &final_out (4) if $use_out;
448 die "\n";
449 }
450 if (!&File::Copy::move ($olddir, $newdir)) {
451 my $msg = "build: Failed to install collection to $newdir\n" .
452 " Collection will remain at $olddir\n";
453 print $out $msg;
454 &log_event ($msg);
455 &final_out (5) if $use_out;
456 die "\n";
457 }
458 }
459 }
460
461 &log_event ("The $collection collection was built successfully\n");
462 &final_out (0) if $use_out;
463}
464
465sub gsdl_import {
466
467 print $out "importing the $collection collection\n\n";
468
469 #Config{perlpath}, like $^X, is a special variable containing the full path to the current perl executable we are in
470 my $import_cmd = "\"$Config{perlpath}\" -S import.pl";
471 $import_cmd .= " -out \"$outfile.import\"" if $use_out;
472 if ($append) {
473 $import_cmd .= " -keepold";
474 if (not $manifest) {
475 # if we are appending, with no manifest, assume incremental
476 $import_cmd .= " -incremental";
477 }
478 } else {
479 $import_cmd .= " -removeold";
480 }
481
482 $import_cmd .= " -manifest manifest.xml" if ($manifest);
483 $import_cmd .= " -site \"$site\"" if $site =~ /\w/;
484 $import_cmd .= " -collectdir \"$collectdir\"" if $collectdir =~ /\w/;
485 $import_cmd .= " -statsfile \"$statsfile\"" if $statsfile =~ /\w/;
486 $import_cmd .= " $maxdocs $collection";
487
488 system ($import_cmd);
489 # if using output directory append the import output to it
490 &append_file ($out, "$outfile.import");
491
492 my $archiveinf_doc_file_path = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archivedir);
493
494 if (-e $archiveinf_doc_file_path) {
495 print $out "$collection collection imported successfully\n\n";
496 if ($remove_import) {
497 print $out "removing import directory ($importdir)\n";
498 &util::rm_r ($importdir);
499 }
500 } else {
501 print $out "$archiveinf_doc_file_path not found. archives contents:\n";
502 print $out `ls $archivedir`;
503
504 my $msg = "build: ERROR: import.pl failed\n";
505 print $out "\n$msg";
506 &log_event ($msg);
507 &final_out (2) if $use_out;
508 die "\n";
509 }
510}
511
512sub gsdl_build {
513
514 print $out "building the $collection collection\n\n";
515
516 #Config{perlpath}, like $^X, is a special variable containing the full path to the current perl executable we are in
517 my $build_cmd = "\"$Config{perlpath}\" -S buildcol.pl";
518
519 my $removeold = 1;
520 if ($append) {
521 if ($indextype eq "lucene") {
522 $build_cmd .= " -keepold -incremental -builddir $indexdir";
523 $removeold = 0;
524 }
525 else {
526 $build_cmd .= " -removeold";
527 }
528 }
529 else {
530 $build_cmd .= " -removeold";
531 }
532
533 $build_cmd .= " -out \"$outfile.build\"" if $use_out;
534 $build_cmd .= " -site \"$site\"" if $site =~ /\w/;
535 $build_cmd .= " -collectdir \"$collectdir\"" if $collectdir =~ /\w/;
536 $build_cmd .= " $maxdocs $collection";
537 system ($build_cmd);
538 # if using output directory append the buildcol output to it
539 &append_file ($out, "$outfile.build");
540
541 my @db_exts = ( ".ldb", ".bdb", ".gdb", ".db" );
542 my $build_ok = 0;
543 foreach my $db_ext (@db_exts) {
544 if ($removeold && (-e &util::filename_cat ($buildingdir, "text", "$collection$db_ext"))) {
545 $build_ok = 1;
546 last;
547 }
548 if (($removeold==0) && (-e &util::filename_cat ($indexdir, "text", "$collection$db_ext"))) {
549 $build_ok = 1;
550 last;
551 }
552 }
553
554 if ($build_ok) {
555 print $out "$collection collection built successfully\n\n";
556 if ($remove_archives) {
557 print $out "removing archives directory ($archivedir)\n";
558 &util::rm_r ($archivedir);
559 }
560 } else {
561 my $msg = "build: ERROR: buildcol.pl failed\n";
562 print $out "\n$msg";
563 &log_event ($msg);
564 &final_out (3) if $use_out;
565 die "\n";
566 }
567
568 if ($removeold) {
569 # replace old indexes with new ones
570 if (&has_content ($indexdir)) {
571 print $out "removing old indexes\n";
572 &util::rm_r ($indexdir);
573 }
574 rmdir ($indexdir) if -d $indexdir;
575 &File::Copy::move ($buildingdir, $indexdir);
576 }
577 else {
578 # Do nothing. We have built into index dir rather than building dir
579 }
580
581 # remove the cached archives
582 if ($save_archives && -d "${archivedir}.org") {
583 &util::rm_r ("${archivedir}.org");
584 }
585}
586
587# return 1 if $dir directory contains any files or sub-directories (other
588# than those specified in the $ignore regular expression)
589sub has_content {
590 my ($dir, $ignore) = @_;
591
592 if (!-d $dir) {return 0;}
593
594 opendir (DIR, $dir) || return 0;
595 my @files = readdir DIR;
596 closedir DIR;
597
598 foreach my $file (@files) {
599 if ($file !~ /^\.{1,2}$/) {
600 return 1 unless (defined $ignore && $file =~ /$ignore/);
601 }
602 }
603
604 return 0;
605}
606
607sub append_file {
608 my ($handle, $file) = @_;
609
610 open (FILE, $file) || return;
611 undef $/;
612 print $handle <FILE>;
613 $/ = "\n";
614 close FILE;
615 &util::rm ($file);
616}
617
618# creates a file called $outfile.final and writes an output code to it.
619# An output code of 0 specifies that there was no error
620sub final_out {
621 my ($exit_code) = @_;
622
623 if ($use_out && (!-e "$outfile.final")) {
624
625 if (open (FINAL, ">$outfile.final")) {
626 print FINAL $exit_code;
627 close FINAL;
628 }
629 }
630}
631
632sub log_event {
633 my ($msg) = @_;
634
635 return unless ($log_events || $email_events);
636
637 # get the event header
638 my $eheader = "[Build Event]\n";
639 $eheader .= "Date: " . scalar localtime() . "\n";
640 if ($event_header ne "" && open (HEADER, $event_header)) {
641 undef $/;
642 $eheader .= <HEADER>;
643 $/ = "\n";
644 close HEADER;
645 } else {
646 $eheader .= "Collection: $collection\n";
647 $eheader .= "GSDLHOME: $ENV{'GSDLHOME'}\n";
648 $eheader .= "Build Location: $collectdir\n";
649 }
650
651 if ($log_events) {
652 my $fail = 0;
653 # append the event to the event log file
654 if ($event_log_file eq "" || !open (LOG, ">>$event_log_file")) {
655 # log file defaults to $GSDLHOME/etc/events.txt
656 $event_log_file = &util::filename_cat ($ENV{'GSDLHOME'}, "etc", "events.txt");
657 if (!open (LOG, ">>$event_log_file")) {
658 print $out "build: ERROR: Couldn't open event log file $event_log_file\n";
659 $fail = 1;
660 }
661 }
662 if (!$fail) {
663 print LOG $eheader;
664 print LOG $msg;
665 print LOG "\n";
666 close LOG;
667 }
668 }
669
670 if ($email_events) {
671 # if mail_server isn't set email_events does nothing
672 if ($mail_server eq "") {
673 print $out "build: WARNING: mail_server was not set - email_events option was ignored\n";
674 return;
675 }
676
677 my %mail = ('SMTP' => $mail_server,
678 'To' => $email_events,
679 'From' => $maintainer,
680 'Subject' => 'Greenstone Build Event'
681 );
682 $mail{'Message'} = $eheader . $msg;
683
684 if (!sendmail %mail) {
685 print $out "build: ERROR sending mail to $email_events\n";
686 print $out "'$Mail::Sendmail::error'\n";
687 }
688 }
689}
690
691
692sub parse_args {
693 my ($argref) = @_;
694
695 if (!parsargv::parse($argref,
696 'optionfile/.*/', \$optionfile,
697 'indextype/^(mg|mgpp|lucene)$/mg', \$indextype,
698 'append', \$append,
699 'manifest', \$manifest,
700 'remove_archives', \$remove_archives,
701 'remove_import', \$remove_import,
702 'buildtype/^(build|import)$/import', \$buildtype,
703 'maxdocs/^\-?\d+/-1', \$maxdocs,
704 'download/.+', \@download,
705 'collectdir/.*/', \$collectdir,
706 'site/.*/', \$site,
707 'dontinstall', \$dontinstall,
708 'save_archives', \$save_archives,
709 'out/.*/STDERR', \$out,
710 'make_writable', \$make_writable,
711 'log_events', \$log_events,
712 'event_log_file/.*/', \$event_log_file,
713 'email_events/.*/', \$email_events,
714 'mail_server/.*/', \$mail_server,
715 'statsfile/.*/STDERR', \$statsfile,
716 'event_header/.*/', \$event_header)) {
717
718 &print_usage();
719 die "\n";
720 }
721}
722
723
724END {
725
726 if ($make_writable) {
727 # chmod a+rw new collection
728 my $installed_collection = &util::filename_cat($ENV{'GSDLHOME'}, "collect", $collection);
729 &recursive_chmod($installed_collection);
730
731 # chmod a+rw anything we've left laying about in the tmp directory
732 if (($collectdir ne "") &&
733 (!&util::filenames_equal ($collectdir, &util::filename_cat($ENV{'GSDLHOME'}, "collect")))) {
734 &recursive_chmod($collectdir);
735 }
736 }
737
738 # this will produce a .final file if one doesn't exist yet - that
739 # should only happen if there's been an error somewhere in the perl
740 # code
741 &final_out(7);
742
743 sub recursive_chmod {
744 my ($dir) = @_;
745 return unless -d $dir;
746
747 chmod (0777, $dir);
748
749 opendir (DIR, $dir) || die;
750 my @files = readdir DIR;
751 closedir DIR;
752
753 foreach my $file (@files) {
754 next if $file =~ /^\.\.?$/;
755 $file = &util::filename_cat($dir, $file);
756 if (-d $file) {
757 &recursive_chmod ($file);
758 } else {
759 chmod (0777, $file);
760 }
761 }
762 }
763}
Note: See TracBrowser for help on using the repository browser.