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

Last change on this file since 24192 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
RevLine 
[1778]1#!/usr/bin/perl -w
[1184]2
[1778]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
[1198]28# This perl script may be called directly or by running build.bat on
29# windows (build.bat is in bin\windows)
[1184]30
[1762]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
[23311]35# I think that currently (Nov 2010) its only officially used from the collector
36# and depositor
37
[1454]38package build;
39
[17253]40use strict;
41no strict 'refs';
42
[1438]43use FileHandle;
[1454]44use File::Copy;
[24192]45use Config; # for getting the perlpath in the recommended way
[1438]46
[1184]47BEGIN {
[1198]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'};
[1184]53 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
[1438]54
55 STDOUT->autoflush(1);
56 STDERR->autoflush(1);
[1184]57}
58
[1778]59use lib qq($ENV{'GSDLHOME'}/perllib/cpan);
60use Mail::Sendmail;
[1184]61use parsargv;
62use util;
[1454]63use cfgread;
[22589]64use colcfg;
65use dbutil;
[1184]66
[23313]67# set up greenstone environment
68&util::setup_greenstone_env($ENV{'GSDLHOME'}, $ENV{'GSDLOS'});
69# is the following needed with the previous line??
[1678]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
[17253]79# all the input option variables
[22589]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);
[17253]81
[1454]82&parse_args (\@ARGV);
[1198]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
[1454]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
[1198]103if ($maxdocs == -1) {
104 $maxdocs = "";
105} else {
106 $maxdocs = "-maxdocs $maxdocs";
107}
108
[1454]109my $cdir = $collectdir;
[22589]110my $gs_mode;
[17253]111if (defined $site && $site =~ /\w/)
[14925]112{
113 die "GSDL3HOME not set." unless $ENV{'GSDL3HOME'};
114 $cdir = &util::filename_cat ($ENV{'GSDL3HOME'}, "sites", $site, "collect") unless $collectdir =~ /\w/;
[22589]115 $gs_mode = "gs3";
[14925]116}
117else
118{
119 $cdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect") unless $collectdir =~ /\w/;
[22589]120 $gs_mode = "gs2";
[14925]121}
122
[1454]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");
[22589]127my $etcdir = &util::filename_cat ($cdir, $collection, "etc");
[1198]128my $bindir = &util::filename_cat ($ENV{'GSDLHOME'}, "bin");
129
[1431]130my $use_out = 0;
[1424]131my $outfile = $out;
132if ($out !~ /^(STDERR|STDOUT)$/i) {
133 open (OUT, ">$out") || die "Couldn't open output file $out\n";
134 $out = "OUT";
[1452]135
136 # delete any existing .final file
137 &util::rm ("$outfile.final") if -e "$outfile.final";
138
[1431]139 $use_out = 1;
[1424]140}
141$out->autoflush(1);
142
[1454]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
[1762]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
[1184]160&main();
161
[2785]162if ($use_out) {
163 close OUT;
[1424]164
[2785]165 # if we've created a build log we'll copy it to the collection's etc
166 # directory
[22589]167 my ($final_etcdir);
[2785]168 if ($dontinstall) {
[22589]169 $final_etcdir = &util::filename_cat($collectdir, "etc", "build.log");
[2785]170 } else {
[22589]171 $final_etcdir = &util::filename_cat($ENV{'GSDLHOME'}, "collect", $collection, "etc", "build.log");
[2785]172 }
173
[22589]174 &util::cp($outfile, $final_etcdir);
[2785]175}
176
[1184]177sub print_usage {
[2359]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";
[12003]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";
[2359]190 print STDOUT " -append Add new files to existing collection\n";
[12003]191 print STDOUT " -manifest Use manifest.xml file to determine which files to process.\n";
[2359]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 " .
[14925]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";
[2359]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";
[2785]220 print STDOUT " -statsfile name Filename or handle to print import statistics to.\n";
221 print STDOUT " The default is STDERR\n";
[2566]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";
[2359]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";
[2469]237 print STDOUT " [Type \"build | more\" if this help text scrolled off your screen]";
[2359]238 print STDOUT "\n" unless $ENV{'GSDLOS'} =~ /^windows$/i;
[1184]239}
240
241sub main {
[1454]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
[1424]248 # do the download thing if we have any -download options
249 if (scalar (@download)) {
250 # remove any existing import data
[1431]251 if (&has_content ($importdir)) {
[23311]252 #print $out "build: WARNING: removing contents of $importdir\n";
253 #&util::rm_r ($importdir);
[1431]254 }
[1454]255
[17253]256 foreach my $download_dir (@download) {
[1507]257
258 # remove any leading or trailing whitespace from filenames (just in case)
259 $download_dir =~ s/^\s+//;
260 $download_dir =~ s/\s+$//;
[1424]261
[1709]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)
[1743]278
[24192]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";
[1753]281 $download_cmd .= " -r -N -l inf -R \"*\\?*\"";
[1709]282 $download_cmd .= " -o \"$outfile.download\"" if $use_out;
283 $download_cmd .= " \"$download_dir\"";
284 system ($download_cmd);
[1424]285
[1709]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 {
[1743]301 $file = &util::filename_cat ($importdir, $file);
[1709]302 if (-d $file) {
303 if (opendir (DIR, $file)) {
[1743]304 my @files2 = readdir DIR;
[1709]305 closedir DIR;
[1743]306 foreach my $file2 (@files2) {
307 if ($file2 =~ /^robots.txt$/i) {
308 &util::rm (&util::filename_cat ($file, $file2));
[1709]309 last;
310 }
311 }
312 }
313 }
314 }
315 }
316 }
[1424]317
[1709]318 # if using output directory append the file download output to it
319 &append_file ($out, "$outfile.download");
320
[1424]321 } else {
322 # we assume anything not beginning with http:// or ftp://
323 # is a file or directory on the local file system.
[1485]324 $download_dir =~ s/^file:(\/\/)?//;
[1507]325 $download_dir =~ s/^\s+//; # may be whitespace between "file://" and the rest
[1424]326
327 if (-e $download_dir) {
328 # copy download_dir and all it contains to the import directory
[24192]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";
[1454]331 $download_cmd .= " -collectdir \"$collectdir\"" if $collectdir =~ /\w/;
[14925]332 $download_cmd .= " -site \"$site\"" if $site =~ /\w/;
[1431]333 $download_cmd .= " -out \"$outfile.download\"" if $use_out;
334 $download_cmd .= " \"" . $download_dir . "\" " . $collection;
[14925]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
[1431]343 # if using output directory append the file download output to it
344 &append_file ($out, "$outfile.download");
[1424]345 } else {
[1507]346 print $out "WARNING: '$download_dir' does not exist\n";
[1424]347 }
348 }
349 }
350 }
351
[14925]352 `echo $importdir ; ls $importdir `;
353
[22589]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);
[21567]366 if (-e $archiveinf_doc_file_path) {
[1198]367 if (&has_content ($importdir)) {
[1184]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
[1424]376 print $out "build: no import material was found, building directly\n";
377 print $out " from archives\n";
[1184]378 &gsdl_build();
379 }
380 } else {
[1198]381 if (&has_content ($importdir)) {
[1184]382 if ($buildtype eq "build") {
[1424]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";
[1184]387 }
388 &gsdl_import();
[21567]389 if (&has_content ($archivedir, "^archiveinf-doc\..*\$")) {
[2381]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 }
[1184]401 } else {
402 # no import or archives
[2381]403 my $msg = "build: ERROR: The collection could not be built as it contains no data.\n";
[1762]404 print $out $msg;
405 &log_event ($msg);
[1438]406 &final_out (1) if $use_out;
[1184]407 die "\n";
408 }
409 }
[1454]410
411 if ($collectdir ne "" && !$dontinstall) {
[14925]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
[1454]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) {
[1762]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);
[1454]447 &final_out (4) if $use_out;
448 die "\n";
449 }
450 if (!&File::Copy::move ($olddir, $newdir)) {
[1762]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);
[1454]455 &final_out (5) if $use_out;
456 die "\n";
457 }
458 }
459 }
460
[1762]461 &log_event ("The $collection collection was built successfully\n");
[1438]462 &final_out (0) if $use_out;
[1184]463}
464
465sub gsdl_import {
466
[1424]467 print $out "importing the $collection collection\n\n";
[1198]468
[24192]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";
[1431]471 $import_cmd .= " -out \"$outfile.import\"" if $use_out;
[12003]472 if ($append) {
473 $import_cmd .= " -keepold";
[13067]474 if (not $manifest) {
475 # if we are appending, with no manifest, assume incremental
476 $import_cmd .= " -incremental";
477 }
[12003]478 } else {
479 $import_cmd .= " -removeold";
480 }
481
482 $import_cmd .= " -manifest manifest.xml" if ($manifest);
[14925]483 $import_cmd .= " -site \"$site\"" if $site =~ /\w/;
[1454]484 $import_cmd .= " -collectdir \"$collectdir\"" if $collectdir =~ /\w/;
[2785]485 $import_cmd .= " -statsfile \"$statsfile\"" if $statsfile =~ /\w/;
[1431]486 $import_cmd .= " $maxdocs $collection";
[2785]487
[1431]488 system ($import_cmd);
489 # if using output directory append the import output to it
490 &append_file ($out, "$outfile.import");
491
[22589]492 my $archiveinf_doc_file_path = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archivedir);
493
[21567]494 if (-e $archiveinf_doc_file_path) {
[1424]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 }
[1184]500 } else {
[22589]501 print $out "$archiveinf_doc_file_path not found. archives contents:\n";
[19485]502 print $out `ls $archivedir`;
503
[1762]504 my $msg = "build: ERROR: import.pl failed\n";
505 print $out "\n$msg";
506 &log_event ($msg);
[1438]507 &final_out (2) if $use_out;
[1454]508 die "\n";
[1184]509 }
510}
511
512sub gsdl_build {
513
[1424]514 print $out "building the $collection collection\n\n";
[1184]515
[24192]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";
[12003]518
519 my $removeold = 1;
520 if ($append) {
521 if ($indextype eq "lucene") {
[13067]522 $build_cmd .= " -keepold -incremental -builddir $indexdir";
[12003]523 $removeold = 0;
524 }
525 else {
526 $build_cmd .= " -removeold";
527 }
528 }
529 else {
530 $build_cmd .= " -removeold";
531 }
532
[1431]533 $build_cmd .= " -out \"$outfile.build\"" if $use_out;
[14925]534 $build_cmd .= " -site \"$site\"" if $site =~ /\w/;
[1454]535 $build_cmd .= " -collectdir \"$collectdir\"" if $collectdir =~ /\w/;
[1431]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
[19485]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) {
[1424]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 }
[1184]560 } else {
[1762]561 my $msg = "build: ERROR: buildcol.pl failed\n";
562 print $out "\n$msg";
563 &log_event ($msg);
[1454]564 &final_out (3) if $use_out;
565 die "\n";
[1184]566 }
567
[12003]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);
[1184]576 }
[12003]577 else {
[13067]578 # Do nothing. We have built into index dir rather than building dir
[12003]579 }
[1454]580
[13067]581 # remove the cached archives
[1461]582 if ($save_archives && -d "${archivedir}.org") {
[1454]583 &util::rm_r ("${archivedir}.org");
584 }
[1184]585}
586
[2381]587# return 1 if $dir directory contains any files or sub-directories (other
588# than those specified in the $ignore regular expression)
[1184]589sub has_content {
[2381]590 my ($dir, $ignore) = @_;
[1184]591
592 if (!-d $dir) {return 0;}
[2381]593
[1184]594 opendir (DIR, $dir) || return 0;
595 my @files = readdir DIR;
[2488]596 closedir DIR;
[1184]597
598 foreach my $file (@files) {
599 if ($file !~ /^\.{1,2}$/) {
[2381]600 return 1 unless (defined $ignore && $file =~ /$ignore/);
[1184]601 }
602 }
[14925]603
[1184]604 return 0;
605}
[1431]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}
[1438]617
[2774]618# creates a file called $outfile.final and writes an output code to it.
[1438]619# An output code of 0 specifies that there was no error
620sub final_out {
621 my ($exit_code) = @_;
622
[2774]623 if ($use_out && (!-e "$outfile.final")) {
624
625 if (open (FINAL, ">$outfile.final")) {
626 print FINAL $exit_code;
627 close FINAL;
628 }
[1438]629 }
630}
[1454]631
[1762]632sub log_event {
633 my ($msg) = @_;
634
635 return unless ($log_events || $email_events);
636
637 # get the event header
[1778]638 my $eheader = "[Build Event]\n";
639 $eheader .= "Date: " . scalar localtime() . "\n";
[1762]640 if ($event_header ne "" && open (HEADER, $event_header)) {
641 undef $/;
[1778]642 $eheader .= <HEADER>;
[1762]643 $/ = "\n";
644 close HEADER;
645 } else {
646 $eheader .= "Collection: $collection\n";
[1778]647 $eheader .= "GSDLHOME: $ENV{'GSDLHOME'}\n";
648 $eheader .= "Build Location: $collectdir\n";
[1762]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;
[1778]665 print LOG "\n";
[1762]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
[1454]692sub parse_args {
693 my ($argref) = @_;
694
695 if (!parsargv::parse($argref,
696 'optionfile/.*/', \$optionfile,
[12003]697 'indextype/^(mg|mgpp|lucene)$/mg', \$indextype,
[1454]698 'append', \$append,
[12003]699 'manifest', \$manifest,
[1454]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,
[14925]706 'site/.*/', \$site,
[1454]707 'dontinstall', \$dontinstall,
708 'save_archives', \$save_archives,
[1762]709 'out/.*/STDERR', \$out,
[2566]710 'make_writable', \$make_writable,
[1762]711 'log_events', \$log_events,
712 'event_log_file/.*/', \$event_log_file,
713 'email_events/.*/', \$email_events,
714 'mail_server/.*/', \$mail_server,
[2785]715 'statsfile/.*/STDERR', \$statsfile,
[1762]716 'event_header/.*/', \$event_header)) {
[1454]717
718 &print_usage();
719 die "\n";
720 }
721}
[2566]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
[2774]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
[2566]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.