source: trunk/gsdl/bin/script/build@ 1778

Last change on this file since 1778 was 1778, checked in by sjboddie, 23 years ago

Implemented the new MailServer, LogEvents, EmailEvents and EmailUserEvents
configuration options.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 18.5 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
35package build;
36
37use FileHandle;
38use File::Copy;
39
40BEGIN {
41
42 die "GSDLHOME not set - did you remember to source setup.bash (unix) or " .
43 "run setup.bat (windows)?\n" unless defined $ENV{'GSDLHOME'};
44 die "GSDLOS not set - did you remember to source setup.bash (unix) or " .
45 "run setup.bat (windows)?\n" unless defined $ENV{'GSDLOS'};
46 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
47
48 STDOUT->autoflush(1);
49 STDERR->autoflush(1);
50}
51
52use lib qq($ENV{'GSDLHOME'}/perllib/cpan);
53use Mail::Sendmail;
54use parsargv;
55use util;
56use cfgread;
57
58# set up path - this allows for paths not to be supplied to system calls
59# and overcomes problems when GSDLHOME contains spaces (double quoting
60# the call doesn't work on win2k and probably other variants of winnt)
61my $path_separator = ":";
62$path_separator = ";" if $ENV{'GSDLOS'} =~ /^windows$/;
63$ENV{'PATH'} = &util::filename_cat($ENV{'GSDLHOME'}, "bin", $ENV{'GSDLOS'}) .
64 $path_separator . &util::filename_cat($ENV{'GSDLHOME'}, "bin", "script") .
65 $path_separator . $ENV{'PATH'};
66
67&parse_args (\@ARGV);
68
69my ($collection) = @ARGV;
70
71if (!defined $collection || $collection !~ /\w/) {
72 print STDERR "You must specify a collection to build\n";
73 &print_usage();
74 die "\n";
75}
76
77if ($optionfile =~ /\w/) {
78 open (OPTIONS, $optionfile) || die "Couldn't open $optionfile\n";
79 my $line = [];
80 my $options = [];
81 while (defined ($line = &cfgread::read_cfg_line ('build::OPTIONS'))) {
82 push (@$options, @$line);
83 }
84 close OPTIONS;
85 &parse_args ($options);
86}
87
88if ($maxdocs == -1) {
89 $maxdocs = "";
90} else {
91 $maxdocs = "-maxdocs $maxdocs";
92}
93
94my $cdir = $collectdir;
95$cdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect") unless $collectdir =~ /\w/;
96my $importdir = &util::filename_cat ($cdir, $collection, "import");
97my $archivedir = &util::filename_cat ($cdir, $collection, "archives");
98my $buildingdir = &util::filename_cat ($cdir, $collection, "building");
99my $indexdir = &util::filename_cat ($cdir, $collection, "index");
100my $bindir = &util::filename_cat ($ENV{'GSDLHOME'}, "bin");
101
102my $use_out = 0;
103my $outfile = $out;
104if ($out !~ /^(STDERR|STDOUT)$/i) {
105 open (OUT, ">$out") || die "Couldn't open output file $out\n";
106 $out = "OUT";
107
108 # delete any existing .final file
109 &util::rm ("$outfile.final") if -e "$outfile.final";
110
111 $use_out = 1;
112}
113$out->autoflush(1);
114
115# delete any .kill file left laying around from a previously aborted build
116if (-e &util::filename_cat ($cdir, $collection, ".kill")) {
117 &util::rm (&util::filename_cat ($cdir, $collection, ".kill"));
118}
119
120# get maintainer email address from main.cfg
121my $maintainer = "NULL";
122my $main_cfg = &util::filename_cat ($ENV{'GSDLHOME'}, "etc", "main.cfg");
123my $cfgdata = &cfgread::read_cfg_file ($main_cfg, "maintainer");
124if (defined $cfgdata->{'maintainer'} && $cfgdata->{'maintainer'} =~ /\w/) {
125 $maintainer = $cfgdata->{'maintainer'};
126}
127# if maintainer is "NULL" email_events should be disabled
128if ($maintainer =~ /^NULL$/i) {
129 $email_events = "";
130}
131
132&main();
133
134close OUT if $use_out;
135
136sub print_usage {
137 print STDERR "\n usage: $0 [options] collection-name\n\n";
138 print STDERR " options:\n";
139 print STDERR " -optionfile file Get options from file, useful on systems where\n";
140 print STDERR " long command lines may cause problems\n";
141 print STDERR " -append Add new files to existing collection\n";
142 print STDERR " -remove_archives Remove archives directory after successfully\n";
143 print STDERR " building the collection.\n";
144 print STDERR " -remove_import Remove import directory after successfully\n";
145 print STDERR " importing the collection.\n";
146 print STDERR " -buildtype build|import If 'build' attempt to build directly\n";
147 print STDERR " from archives directory (bypassing import\n";
148 print STDERR " stage). Defaults to 'import'\n";
149 print STDERR " -maxdocs number Maximum number of documents to build\n";
150 print STDERR " -download directory Directory (or file) to get import documents from.\n";
151 print STDERR " There may be multiple download directories and they\n";
152 print STDERR " may be of type http://, ftp://, or file://\n";
153 print STDERR " Note that any existing import directory will be\n";
154 print STDERR " deleted to make way for the downloaded data if\n";
155 print STDERR " a -download option is supplied\n";
156 print STDERR " -collectdir directory Collection directory (defaults to " .
157 &util::filename_cat ($ENV{'GSDLHOME'}, "collect") . ")\n";
158 print STDERR " -dontinstall Only applicable if -collectdir is set to something\n";
159 print STDERR " other than the default. -dontinstall will suppress the\n";
160 print STDERR " default behaviour which is to install the collection to\n";
161 print STDERR " the gsdl/collect directory once it has been built.\n";
162 print STDERR " -save_archives Create a copy of the existing archives directory called\n";
163 print STDERR " archives.org\n";
164 print STDERR " -out Filename or handle to print output status to.\n";
165 print STDERR " The default is STDERR\n";
166 print STDERR " -log_events Log important events (collection built successfully etc.)\n";
167 print STDERR " to event_log_file\n";
168 print STDERR " -event_log_file file File to append important events to (defaults to\n";
169 print STDERR " " . &util::filename_cat ($ENV{'GSDLHOME'}, "etc", "events.txt") . "\n";
170 print STDERR " -email_events addr Comma separated list of email addresses to mail details\n";
171 print STDERR " of important collection building events\n";
172 print STDERR " -mail_server server The outgoing (SMTP) mail server to be used by email_events.\n";
173 print STDERR " email_events will be disabled if mail_server isn't set\n";
174 print STDERR " -event_header file File containing a header to go on any event messages. If not\n";
175 print STDERR " specified build will create a generic header\n\n";
176}
177
178sub main {
179
180 if ($save_archives && -d $archivedir) {
181 print $out "caching original archives to ${archivedir}.org\n";
182 &util::cp_r ($archivedir, "${archivedir}.org");
183 }
184
185 # do the download thing if we have any -download options
186 if (scalar (@download)) {
187 # remove any existing import data
188 if (&has_content ($importdir)) {
189 print $out "build: WARNING: removing contents of $importdir\n";
190 &util::rm_r ($importdir);
191 }
192
193 foreach $download_dir (@download) {
194
195 # remove any leading or trailing whitespace from filenames (just in case)
196 $download_dir =~ s/^\s+//;
197 $download_dir =~ s/\s+$//;
198
199 if ($download_dir =~ /^(http|ftp):\/\//) {
200 # use wget to mirror http or ftp urls
201 # options used are:
202 # -P = the directory to download documents to
203 # -np = don't ascend to parent directories. this means that only documents
204 # that live in the same directory or below on the same server as
205 # the given url will be downloaded
206 # -nv = not too verbose
207 # -r = recursively mirror
208 # -N = use time-stamping to see if an up-to-date local copy of each
209 # file already exists. this may be useful if wget fails and
210 # is restarted
211 # -l inf = infinite recursion depth
212 # -R "*\?*" = don't download cgi based urls
213 # -o = the output file to write download status to (only used if the -out
214 # option was given to build)
215
216 my $download_cmd = "perl -S gsWget.pl -P \"$importdir\" -np -nv";
217 $download_cmd .= " -r -N -l inf -R \"*\\?*\"";
218 $download_cmd .= " -o \"$outfile.download\"" if $use_out;
219 $download_cmd .= " \"$download_dir\"";
220 system ($download_cmd);
221
222 # note that wget obeys the robot rules. this means that it will have
223 # downloaded a robots.txt file if one was present. since it's unlikely
224 # anyone really wants to include it in a collection we'll delete it.
225 # robots.txt shouldn't be more than two directories deep (I think it will
226 # always be exactly two deep but will look for it in the top directory too)
227 # so that's as far as we'll go looking for it.
228 if (opendir (DIR, $importdir)) {
229 my @files = readdir DIR;
230 closedir DIR;
231 foreach my $file (@files) {
232 next if $file =~ /^\.\.?$/;
233 if ($file =~ /^robots.txt$/i) {
234 &util::rm (&util::filename_cat ($importdir, $file));
235 last;
236 } else {
237 $file = &util::filename_cat ($importdir, $file);
238 if (-d $file) {
239 if (opendir (DIR, $file)) {
240 my @files2 = readdir DIR;
241 closedir DIR;
242 foreach my $file2 (@files2) {
243 if ($file2 =~ /^robots.txt$/i) {
244 &util::rm (&util::filename_cat ($file, $file2));
245 last;
246 }
247 }
248 }
249 }
250 }
251 }
252 }
253
254 # if using output directory append the file download output to it
255 &append_file ($out, "$outfile.download");
256
257 } else {
258 # we assume anything not beginning with http:// or ftp://
259 # is a file or directory on the local file system.
260 $download_dir =~ s/^file:(\/\/)?//;
261 $download_dir =~ s/^\s+//; # may be whitespace between "file://" and the rest
262
263 if (-e $download_dir) {
264 # copy download_dir and all it contains to the import directory
265 my $download_cmd = "perl -S filecopy.pl";
266 $download_cmd .= " -collectdir \"$collectdir\"" if $collectdir =~ /\w/;
267 $download_cmd .= " -out \"$outfile.download\"" if $use_out;
268 $download_cmd .= " \"" . $download_dir . "\" " . $collection;
269 system ($download_cmd);
270 # if using output directory append the file download output to it
271 &append_file ($out, "$outfile.download");
272 } else {
273 print $out "WARNING: '$download_dir' does not exist\n";
274 }
275 }
276 }
277 }
278
279 if (-e &util::filename_cat ($archivedir, "archives.inf")) {
280 if (&has_content ($importdir)) {
281 if ($buildtype eq "build") {
282 &gsdl_build();
283 } else {
284 &gsdl_import();
285 &gsdl_build();
286 }
287 } else {
288 # there are archives but no import, build directly from archives
289 print $out "build: no import material was found, building directly\n";
290 print $out " from archives\n";
291 &gsdl_build();
292 }
293 } else {
294 if (&has_content ($importdir)) {
295 if ($buildtype eq "build") {
296 print $out "build: can't build directly from archives as no\n";
297 print $out " imported archives exist (did you forget to\n";
298 print $out " move the contents of $collection/import to\n";
299 print $out " collection/archives?)\n";
300 }
301 &gsdl_import();
302 &gsdl_build();
303 } else {
304 # no import or archives
305 my $msg = "build: ERROR: The $collection collection has no import or archives data.\n";
306 print $out $msg;
307 &log_event ($msg);
308 &final_out (1) if $use_out;
309 die "\n";
310 }
311 }
312
313 if ($collectdir ne "" && !$dontinstall) {
314 my $install_collectdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect");
315 if (!&util::filenames_equal ($collectdir, $install_collectdir)) {
316
317 # install collection to gsdl/collect
318 print $out "installing the $collection collection\n";
319 my $newdir = &util::filename_cat ($install_collectdir, $collection);
320 my $olddir = &util::filename_cat ($collectdir, $collection);
321 if (-d $newdir) {
322 my $msg = "build: Could not install collection as $newdir\n" .
323 " already exists. Collection will remain at\n$olddir\n";
324
325 print $out $msg;
326 &log_event ($msg);
327 &final_out (4) if $use_out;
328 die "\n";
329 }
330 if (!&File::Copy::move ($olddir, $newdir)) {
331 my $msg = "build: Failed to install collection to $newdir\n" .
332 " Collection will remain at $olddir\n";
333 print $out $msg;
334 &log_event ($msg);
335 &final_out (5) if $use_out;
336 die "\n";
337 }
338 }
339 }
340
341 &log_event ("The $collection collection was built successfully\n");
342 &final_out (0) if $use_out;
343}
344
345sub gsdl_import {
346
347 print $out "importing the $collection collection\n\n";
348
349 my $import_cmd = "perl -S import.pl";
350 $import_cmd .= " -out \"$outfile.import\"" if $use_out;
351 $import_cmd .= " -removeold" unless $append;
352 $import_cmd .= " -collectdir \"$collectdir\"" if $collectdir =~ /\w/;
353 $import_cmd .= " $maxdocs $collection";
354 system ($import_cmd);
355 # if using output directory append the import output to it
356 &append_file ($out, "$outfile.import");
357
358 if (-e &util::filename_cat ($archivedir, "archives.inf")) {
359 print $out "$collection collection imported successfully\n\n";
360 if ($remove_import) {
361 print $out "removing import directory ($importdir)\n";
362 &util::rm_r ($importdir);
363 }
364 } else {
365 my $msg = "build: ERROR: import.pl failed\n";
366 print $out "\n$msg";
367 &log_event ($msg);
368 &final_out (2) if $use_out;
369 die "\n";
370 }
371}
372
373sub gsdl_build {
374
375 print $out "building the $collection collection\n\n";
376
377 my $build_cmd = "perl -S buildcol.pl";
378 $build_cmd .= " -out \"$outfile.build\"" if $use_out;
379 $build_cmd .= " -collectdir \"$collectdir\"" if $collectdir =~ /\w/;
380 $build_cmd .= " $maxdocs $collection";
381 system ($build_cmd);
382 # if using output directory append the buildcol output to it
383 &append_file ($out, "$outfile.build");
384
385 if (-e &util::filename_cat ($buildingdir, "text", "$collection.ldb") ||
386 -e &util::filename_cat ($buildingdir, "text", "$collection.bdb")) {
387 print $out "$collection collection built successfully\n\n";
388 if ($remove_archives) {
389 print $out "removing archives directory ($archivedir)\n";
390 &util::rm_r ($archivedir);
391 }
392 } else {
393 my $msg = "build: ERROR: buildcol.pl failed\n";
394 print $out "\n$msg";
395 &log_event ($msg);
396 &final_out (3) if $use_out;
397 die "\n";
398 }
399
400 # replace old indexes with new ones
401 if (&has_content ($indexdir)) {
402 print $out "removing old indexes\n";
403 &util::rm_r ($indexdir);
404 }
405 rmdir ($indexdir) if -d $indexdir;
406 &File::Copy::move ($buildingdir, $indexdir);
407
408 # remove the cached arhives
409 if ($save_archives && -d "${archivedir}.org") {
410 &util::rm_r ("${archivedir}.org");
411 }
412}
413
414sub has_content {
415 my ($dir) = @_;
416
417 if (!-d $dir) {return 0;}
418
419 opendir (DIR, $dir) || return 0;
420 my @files = readdir DIR;
421 close DIR;
422
423 foreach my $file (@files) {
424 if ($file !~ /^\.{1,2}$/) {
425 return 1;
426 }
427 }
428 return 0;
429}
430
431sub append_file {
432 my ($handle, $file) = @_;
433
434 open (FILE, $file) || return;
435 undef $/;
436 print $handle <FILE>;
437 $/ = "\n";
438 close FILE;
439 &util::rm ($file);
440}
441
442# creates a file called $outfile.final (should only be called if -out option
443# is used and isn't STDERR or STDOUT) and writes an output code to it.
444# An output code of 0 specifies that there was no error
445sub final_out {
446 my ($exit_code) = @_;
447
448 if (open (FINAL, ">$outfile.final")) {
449 print FINAL $exit_code;
450 close FINAL;
451 }
452}
453
454sub log_event {
455 my ($msg) = @_;
456
457 return unless ($log_events || $email_events);
458
459 # get the event header
460 my $eheader = "[Build Event]\n";
461 $eheader .= "Date: " . scalar localtime() . "\n";
462 if ($event_header ne "" && open (HEADER, $event_header)) {
463 undef $/;
464 $eheader .= <HEADER>;
465 $/ = "\n";
466 close HEADER;
467 } else {
468 $eheader .= "Collection: $collection\n";
469 $eheader .= "GSDLHOME: $ENV{'GSDLHOME'}\n";
470 $eheader .= "Build Location: $collectdir\n";
471 }
472
473 if ($log_events) {
474 my $fail = 0;
475 # append the event to the event log file
476 if ($event_log_file eq "" || !open (LOG, ">>$event_log_file")) {
477 # log file defaults to $GSDLHOME/etc/events.txt
478 $event_log_file = &util::filename_cat ($ENV{'GSDLHOME'}, "etc", "events.txt");
479 if (!open (LOG, ">>$event_log_file")) {
480 print $out "build: ERROR: Couldn't open event log file $event_log_file\n";
481 $fail = 1;
482 }
483 }
484 if (!$fail) {
485 print LOG $eheader;
486 print LOG $msg;
487 print LOG "\n";
488 close LOG;
489 }
490 }
491
492 if ($email_events) {
493 # if mail_server isn't set email_events does nothing
494 if ($mail_server eq "") {
495 print $out "build: WARNING: mail_server was not set - email_events option was ignored\n";
496 return;
497 }
498
499 my %mail = ('SMTP' => $mail_server,
500 'To' => $email_events,
501 'From' => $maintainer,
502 'Subject' => 'Greenstone Build Event'
503 );
504 $mail{'Message'} = $eheader . $msg;
505
506 if (!sendmail %mail) {
507 print $out "build: ERROR sending mail to $email_events\n";
508 print $out "'$Mail::Sendmail::error'\n";
509 }
510 }
511}
512
513
514sub parse_args {
515 my ($argref) = @_;
516
517 if (!parsargv::parse($argref,
518 'optionfile/.*/', \$optionfile,
519 'append', \$append,
520 'remove_archives', \$remove_archives,
521 'remove_import', \$remove_import,
522 'buildtype/^(build|import)$/import', \$buildtype,
523 'maxdocs/^\-?\d+/-1', \$maxdocs,
524 'download/.+', \@download,
525 'collectdir/.*/', \$collectdir,
526 'dontinstall', \$dontinstall,
527 'save_archives', \$save_archives,
528 'out/.*/STDERR', \$out,
529 'log_events', \$log_events,
530 'event_log_file/.*/', \$event_log_file,
531 'email_events/.*/', \$email_events,
532 'mail_server/.*/', \$mail_server,
533 'event_header/.*/', \$event_header)) {
534
535 &print_usage();
536 die "\n";
537 }
538}
Note: See TracBrowser for help on using the repository browser.