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

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

Added more usage information to all perl programs and removed a few
programs that are no longer useful.

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