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 | package build;
|
---|
36 |
|
---|
37 | use FileHandle;
|
---|
38 | use File::Copy;
|
---|
39 |
|
---|
40 | BEGIN {
|
---|
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 |
|
---|
52 | use lib qq($ENV{'GSDLHOME'}/perllib/cpan);
|
---|
53 | use Mail::Sendmail;
|
---|
54 | use parsargv;
|
---|
55 | use util;
|
---|
56 | use 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)
|
---|
61 | my $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 |
|
---|
69 | my ($collection) = @ARGV;
|
---|
70 |
|
---|
71 | if (!defined $collection || $collection !~ /\w/) {
|
---|
72 | print STDERR "You must specify a collection to build\n";
|
---|
73 | &print_usage();
|
---|
74 | die "\n";
|
---|
75 | }
|
---|
76 |
|
---|
77 | if ($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 |
|
---|
88 | if ($maxdocs == -1) {
|
---|
89 | $maxdocs = "";
|
---|
90 | } else {
|
---|
91 | $maxdocs = "-maxdocs $maxdocs";
|
---|
92 | }
|
---|
93 |
|
---|
94 | my $cdir = $collectdir;
|
---|
95 | $cdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect") unless $collectdir =~ /\w/;
|
---|
96 | my $importdir = &util::filename_cat ($cdir, $collection, "import");
|
---|
97 | my $archivedir = &util::filename_cat ($cdir, $collection, "archives");
|
---|
98 | my $buildingdir = &util::filename_cat ($cdir, $collection, "building");
|
---|
99 | my $indexdir = &util::filename_cat ($cdir, $collection, "index");
|
---|
100 | my $bindir = &util::filename_cat ($ENV{'GSDLHOME'}, "bin");
|
---|
101 |
|
---|
102 | my $use_out = 0;
|
---|
103 | my $outfile = $out;
|
---|
104 | if ($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
|
---|
116 | if (-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
|
---|
121 | my $maintainer = "NULL";
|
---|
122 | my $main_cfg = &util::filename_cat ($ENV{'GSDLHOME'}, "etc", "main.cfg");
|
---|
123 | my $cfgdata = &cfgread::read_cfg_file ($main_cfg, "maintainer");
|
---|
124 | if (defined $cfgdata->{'maintainer'} && $cfgdata->{'maintainer'} =~ /\w/) {
|
---|
125 | $maintainer = $cfgdata->{'maintainer'};
|
---|
126 | }
|
---|
127 | # if maintainer is "NULL" email_events should be disabled
|
---|
128 | if ($maintainer =~ /^NULL$/i) {
|
---|
129 | $email_events = "";
|
---|
130 | }
|
---|
131 |
|
---|
132 | &main();
|
---|
133 |
|
---|
134 | if ($use_out) {
|
---|
135 | close OUT;
|
---|
136 |
|
---|
137 | # if we've created a build log we'll copy it to the collection's etc
|
---|
138 | # directory
|
---|
139 | my ($etcdir);
|
---|
140 | if ($dontinstall) {
|
---|
141 | $etcdir = &util::filename_cat($collectdir, "etc", "build.log");
|
---|
142 | } else {
|
---|
143 | $etcdir = &util::filename_cat($ENV{'GSDLHOME'}, "collect", $collection, "etc", "build.log");
|
---|
144 | }
|
---|
145 |
|
---|
146 | &util::cp($outfile, $etcdir);
|
---|
147 | }
|
---|
148 |
|
---|
149 | sub print_usage {
|
---|
150 | print STDOUT "\n";
|
---|
151 | print STDOUT "build: Builds a Greenstone collection (i.e. runs import.pl and buildcol.pl\n";
|
---|
152 | print STDOUT " then copies the resulting indexes to the correct place).\n\n";
|
---|
153 | print STDOUT " usage: $0 [options] collection-name\n\n";
|
---|
154 | print STDOUT " options:\n";
|
---|
155 | print STDOUT " -optionfile file Get options from file, useful on systems where\n";
|
---|
156 | print STDOUT " long command lines may cause problems\n";
|
---|
157 | print STDOUT " -indextype mg|mgpp|lucene \n";
|
---|
158 | print STDERR " Specify the type of indexer used in this collection\n";
|
---|
159 | print STDERR " If -append is used then -indextype is needed to \n";
|
---|
160 | print STDERR " determine how to run buildcol.pl as well as update\n";
|
---|
161 | print STDERR " 'building' and 'index' according.\n";
|
---|
162 | print STDOUT " -append Add new files to existing collection\n";
|
---|
163 | print STDOUT " -manifest Use manifest.xml file to determine which files to process.\n";
|
---|
164 | print STDOUT " -remove_archives Remove archives directory after successfully\n";
|
---|
165 | print STDOUT " building the collection.\n";
|
---|
166 | print STDOUT " -remove_import Remove import directory after successfully\n";
|
---|
167 | print STDOUT " importing the collection.\n";
|
---|
168 | print STDOUT " -buildtype build|import If 'build' attempt to build directly\n";
|
---|
169 | print STDOUT " from archives directory (bypassing import\n";
|
---|
170 | print STDOUT " stage). Defaults to 'import'\n";
|
---|
171 | print STDOUT " -maxdocs number Maximum number of documents to build\n";
|
---|
172 | print STDOUT " -download directory Directory (or file) to get import documents from.\n";
|
---|
173 | print STDOUT " There may be multiple download directories and they\n";
|
---|
174 | print STDOUT " may be of type http://, ftp://, or file://\n";
|
---|
175 | print STDOUT " Note that any existing import directory will be\n";
|
---|
176 | print STDOUT " deleted to make way for the downloaded data if\n";
|
---|
177 | print STDOUT " a -download option is supplied\n";
|
---|
178 | print STDOUT " -collectdir directory Collection directory (defaults to " .
|
---|
179 | &util::filename_cat ($ENV{'GSDLHOME'}, "collect") . ")\n";
|
---|
180 | print STDOUT " -dontinstall Only applicable if -collectdir is set to something\n";
|
---|
181 | print STDOUT " other than the default. -dontinstall will suppress\n";
|
---|
182 | print STDOUT " the default behaviour which is to install the\n";
|
---|
183 | print STDOUT " collection to the gsdl/collect directory once it has\n";
|
---|
184 | print STDOUT " been built.\n";
|
---|
185 | print STDOUT " -save_archives Create a copy of the existing archives directory\n";
|
---|
186 | print STDOUT " called archives.org\n";
|
---|
187 | print STDOUT " -out Filename or handle to print output status to.\n";
|
---|
188 | print STDOUT " The default is STDERR\n";
|
---|
189 | print STDOUT " -statsfile name Filename or handle to print import statistics to.\n";
|
---|
190 | print STDOUT " The default is STDERR\n";
|
---|
191 | print STDOUT " -make_writable If set build will make the collection and any\n";
|
---|
192 | print STDOUT " temporary files it created globally writable after\n";
|
---|
193 | print STDOUT " it finishes\n";
|
---|
194 | print STDOUT " -log_events Log important events (collection built successfully\n";
|
---|
195 | print STDOUT " etc.) to event_log_file\n";
|
---|
196 | print STDOUT " -event_log_file file File to append important events to (defaults to\n";
|
---|
197 | print STDOUT " " . &util::filename_cat ($ENV{'GSDLHOME'}, "etc", "events.txt") . "\n";
|
---|
198 | print STDOUT " -email_events addr Comma separated list of email addresses to mail\n";
|
---|
199 | print STDOUT " details of important collection building events\n";
|
---|
200 | print STDOUT " -mail_server server The outgoing (SMTP) mail server to be used by\n";
|
---|
201 | print STDOUT " email_events. email_events will be disabled if\n";
|
---|
202 | print STDOUT " mail_server isn't set\n";
|
---|
203 | print STDOUT " -event_header file File containing a header to go on any event\n";
|
---|
204 | print STDOUT " messages. If not specified build will create a\n";
|
---|
205 | print STDOUT " generic header\n\n";
|
---|
206 | print STDOUT " [Type \"build | more\" if this help text scrolled off your screen]";
|
---|
207 | print STDOUT "\n" unless $ENV{'GSDLOS'} =~ /^windows$/i;
|
---|
208 | }
|
---|
209 |
|
---|
210 | sub main {
|
---|
211 |
|
---|
212 | if ($save_archives && -d $archivedir) {
|
---|
213 | print $out "caching original archives to ${archivedir}.org\n";
|
---|
214 | &util::cp_r ($archivedir, "${archivedir}.org");
|
---|
215 | }
|
---|
216 |
|
---|
217 | # do the download thing if we have any -download options
|
---|
218 | if (scalar (@download)) {
|
---|
219 | # remove any existing import data
|
---|
220 | if (&has_content ($importdir)) {
|
---|
221 | print $out "build: WARNING: removing contents of $importdir\n";
|
---|
222 | &util::rm_r ($importdir);
|
---|
223 | }
|
---|
224 |
|
---|
225 | foreach $download_dir (@download) {
|
---|
226 |
|
---|
227 | # remove any leading or trailing whitespace from filenames (just in case)
|
---|
228 | $download_dir =~ s/^\s+//;
|
---|
229 | $download_dir =~ s/\s+$//;
|
---|
230 |
|
---|
231 | if ($download_dir =~ /^(http|ftp):\/\//) {
|
---|
232 | # use wget to mirror http or ftp urls
|
---|
233 | # options used are:
|
---|
234 | # -P = the directory to download documents to
|
---|
235 | # -np = don't ascend to parent directories. this means that only documents
|
---|
236 | # that live in the same directory or below on the same server as
|
---|
237 | # the given url will be downloaded
|
---|
238 | # -nv = not too verbose
|
---|
239 | # -r = recursively mirror
|
---|
240 | # -N = use time-stamping to see if an up-to-date local copy of each
|
---|
241 | # file already exists. this may be useful if wget fails and
|
---|
242 | # is restarted
|
---|
243 | # -l inf = infinite recursion depth
|
---|
244 | # -R "*\?*" = don't download cgi based urls
|
---|
245 | # -o = the output file to write download status to (only used if the -out
|
---|
246 | # option was given to build)
|
---|
247 |
|
---|
248 | my $download_cmd = "perl -S gsWget.pl -P \"$importdir\" -np -nv";
|
---|
249 | $download_cmd .= " -r -N -l inf -R \"*\\?*\"";
|
---|
250 | $download_cmd .= " -o \"$outfile.download\"" if $use_out;
|
---|
251 | $download_cmd .= " \"$download_dir\"";
|
---|
252 | system ($download_cmd);
|
---|
253 |
|
---|
254 | # note that wget obeys the robot rules. this means that it will have
|
---|
255 | # downloaded a robots.txt file if one was present. since it's unlikely
|
---|
256 | # anyone really wants to include it in a collection we'll delete it.
|
---|
257 | # robots.txt shouldn't be more than two directories deep (I think it will
|
---|
258 | # always be exactly two deep but will look for it in the top directory too)
|
---|
259 | # so that's as far as we'll go looking for it.
|
---|
260 | if (opendir (DIR, $importdir)) {
|
---|
261 | my @files = readdir DIR;
|
---|
262 | closedir DIR;
|
---|
263 | foreach my $file (@files) {
|
---|
264 | next if $file =~ /^\.\.?$/;
|
---|
265 | if ($file =~ /^robots.txt$/i) {
|
---|
266 | &util::rm (&util::filename_cat ($importdir, $file));
|
---|
267 | last;
|
---|
268 | } else {
|
---|
269 | $file = &util::filename_cat ($importdir, $file);
|
---|
270 | if (-d $file) {
|
---|
271 | if (opendir (DIR, $file)) {
|
---|
272 | my @files2 = readdir DIR;
|
---|
273 | closedir DIR;
|
---|
274 | foreach my $file2 (@files2) {
|
---|
275 | if ($file2 =~ /^robots.txt$/i) {
|
---|
276 | &util::rm (&util::filename_cat ($file, $file2));
|
---|
277 | last;
|
---|
278 | }
|
---|
279 | }
|
---|
280 | }
|
---|
281 | }
|
---|
282 | }
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 | # if using output directory append the file download output to it
|
---|
287 | &append_file ($out, "$outfile.download");
|
---|
288 |
|
---|
289 | } else {
|
---|
290 | # we assume anything not beginning with http:// or ftp://
|
---|
291 | # is a file or directory on the local file system.
|
---|
292 | $download_dir =~ s/^file:(\/\/)?//;
|
---|
293 | $download_dir =~ s/^\s+//; # may be whitespace between "file://" and the rest
|
---|
294 |
|
---|
295 | if (-e $download_dir) {
|
---|
296 | # copy download_dir and all it contains to the import directory
|
---|
297 | my $download_cmd = "perl -S filecopy.pl";
|
---|
298 | $download_cmd .= " -collectdir \"$collectdir\"" if $collectdir =~ /\w/;
|
---|
299 | $download_cmd .= " -out \"$outfile.download\"" if $use_out;
|
---|
300 | $download_cmd .= " \"" . $download_dir . "\" " . $collection;
|
---|
301 | system ($download_cmd);
|
---|
302 | # if using output directory append the file download output to it
|
---|
303 | &append_file ($out, "$outfile.download");
|
---|
304 | } else {
|
---|
305 | print $out "WARNING: '$download_dir' does not exist\n";
|
---|
306 | }
|
---|
307 | }
|
---|
308 | }
|
---|
309 | }
|
---|
310 |
|
---|
311 | if (-e &util::filename_cat ($archivedir, "archives.inf")) {
|
---|
312 | if (&has_content ($importdir)) {
|
---|
313 | if ($buildtype eq "build") {
|
---|
314 | &gsdl_build();
|
---|
315 | } else {
|
---|
316 | &gsdl_import();
|
---|
317 | &gsdl_build();
|
---|
318 | }
|
---|
319 | } else {
|
---|
320 | # there are archives but no import, build directly from archives
|
---|
321 | print $out "build: no import material was found, building directly\n";
|
---|
322 | print $out " from archives\n";
|
---|
323 | &gsdl_build();
|
---|
324 | }
|
---|
325 | } else {
|
---|
326 | if (&has_content ($importdir)) {
|
---|
327 | if ($buildtype eq "build") {
|
---|
328 | print $out "build: can't build directly from archives as no\n";
|
---|
329 | print $out " imported archives exist (did you forget to\n";
|
---|
330 | print $out " move the contents of $collection/import to\n";
|
---|
331 | print $out " collection/archives?)\n";
|
---|
332 | }
|
---|
333 | &gsdl_import();
|
---|
334 | if (&has_content ($archivedir, "^archives.inf\$")) {
|
---|
335 | &gsdl_build();
|
---|
336 | } else {
|
---|
337 | my $msg = "build: ERROR: The collection could not be built as no\n";
|
---|
338 | $msg .= " valid data was imported. Are at least some of\n";
|
---|
339 | $msg .= " the files you imported in a format that can be\n";
|
---|
340 | $msg .= " processed by the specified Greenstone plugins?\n";
|
---|
341 | print $out $msg;
|
---|
342 | &log_event ($msg);
|
---|
343 | &final_out (6) if $use_out;
|
---|
344 | die "\n";
|
---|
345 | }
|
---|
346 | } else {
|
---|
347 | # no import or archives
|
---|
348 | my $msg = "build: ERROR: The collection could not be built as it contains no data.\n";
|
---|
349 | print $out $msg;
|
---|
350 | &log_event ($msg);
|
---|
351 | &final_out (1) if $use_out;
|
---|
352 | die "\n";
|
---|
353 | }
|
---|
354 | }
|
---|
355 |
|
---|
356 | if ($collectdir ne "" && !$dontinstall) {
|
---|
357 | my $install_collectdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect");
|
---|
358 | if (!&util::filenames_equal ($collectdir, $install_collectdir)) {
|
---|
359 |
|
---|
360 | # install collection to gsdl/collect
|
---|
361 | print $out "installing the $collection collection\n";
|
---|
362 | my $newdir = &util::filename_cat ($install_collectdir, $collection);
|
---|
363 | my $olddir = &util::filename_cat ($collectdir, $collection);
|
---|
364 | if (-d $newdir) {
|
---|
365 | my $msg = "build: Could not install collection as $newdir\n" .
|
---|
366 | " already exists. Collection will remain at\n$olddir\n";
|
---|
367 |
|
---|
368 | print $out $msg;
|
---|
369 | &log_event ($msg);
|
---|
370 | &final_out (4) if $use_out;
|
---|
371 | die "\n";
|
---|
372 | }
|
---|
373 | if (!&File::Copy::move ($olddir, $newdir)) {
|
---|
374 | my $msg = "build: Failed to install collection to $newdir\n" .
|
---|
375 | " Collection will remain at $olddir\n";
|
---|
376 | print $out $msg;
|
---|
377 | &log_event ($msg);
|
---|
378 | &final_out (5) if $use_out;
|
---|
379 | die "\n";
|
---|
380 | }
|
---|
381 | }
|
---|
382 | }
|
---|
383 |
|
---|
384 | &log_event ("The $collection collection was built successfully\n");
|
---|
385 | &final_out (0) if $use_out;
|
---|
386 | }
|
---|
387 |
|
---|
388 | sub gsdl_import {
|
---|
389 |
|
---|
390 | print $out "importing the $collection collection\n\n";
|
---|
391 |
|
---|
392 | my $import_cmd = "perl -S import.pl";
|
---|
393 | $import_cmd .= " -out \"$outfile.import\"" if $use_out;
|
---|
394 | if ($append) {
|
---|
395 | $import_cmd .= " -keepold";
|
---|
396 | if (not $manifest) {
|
---|
397 | # if we are appending, with no manifest, assume incremental
|
---|
398 | $import_cmd .= " -incremental";
|
---|
399 | }
|
---|
400 | } else {
|
---|
401 | $import_cmd .= " -removeold";
|
---|
402 | }
|
---|
403 |
|
---|
404 | $import_cmd .= " -manifest manifest.xml" if ($manifest);
|
---|
405 | $import_cmd .= " -collectdir \"$collectdir\"" if $collectdir =~ /\w/;
|
---|
406 | $import_cmd .= " -statsfile \"$statsfile\"" if $statsfile =~ /\w/;
|
---|
407 | $import_cmd .= " $maxdocs $collection";
|
---|
408 |
|
---|
409 | system ($import_cmd);
|
---|
410 | # if using output directory append the import output to it
|
---|
411 | &append_file ($out, "$outfile.import");
|
---|
412 |
|
---|
413 | if (-e &util::filename_cat ($archivedir, "archives.inf")) {
|
---|
414 | print $out "$collection collection imported successfully\n\n";
|
---|
415 | if ($remove_import) {
|
---|
416 | print $out "removing import directory ($importdir)\n";
|
---|
417 | &util::rm_r ($importdir);
|
---|
418 | }
|
---|
419 | } else {
|
---|
420 | my $msg = "build: ERROR: import.pl failed\n";
|
---|
421 | print $out "\n$msg";
|
---|
422 | &log_event ($msg);
|
---|
423 | &final_out (2) if $use_out;
|
---|
424 | die "\n";
|
---|
425 | }
|
---|
426 | }
|
---|
427 |
|
---|
428 | sub gsdl_build {
|
---|
429 |
|
---|
430 | print $out "building the $collection collection\n\n";
|
---|
431 |
|
---|
432 | my $build_cmd = "perl -S buildcol.pl";
|
---|
433 |
|
---|
434 | my $removeold = 1;
|
---|
435 | if ($append) {
|
---|
436 | if ($indextype eq "lucene") {
|
---|
437 | $build_cmd .= " -keepold -incremental -builddir $indexdir";
|
---|
438 | $removeold = 0;
|
---|
439 | }
|
---|
440 | else {
|
---|
441 | $build_cmd .= " -removeold";
|
---|
442 | }
|
---|
443 | }
|
---|
444 | else {
|
---|
445 | $build_cmd .= " -removeold";
|
---|
446 | }
|
---|
447 |
|
---|
448 | $build_cmd .= " -out \"$outfile.build\"" if $use_out;
|
---|
449 | $build_cmd .= " -collectdir \"$collectdir\"" if $collectdir =~ /\w/;
|
---|
450 | $build_cmd .= " $maxdocs $collection";
|
---|
451 | system ($build_cmd);
|
---|
452 | # if using output directory append the buildcol output to it
|
---|
453 | &append_file ($out, "$outfile.build");
|
---|
454 |
|
---|
455 | if (($removeold && (-e &util::filename_cat ($buildingdir, "text", "$collection.ldb") || -e &util::filename_cat ($buildingdir, "text", "$collection.bdb"))) ||
|
---|
456 | ($removeold == 0 && (-e &util::filename_cat ($indexdir, "text", "$collection.ldb") || -e &util::filename_cat ($indexdir, "text", "$collection.bdb")))) {
|
---|
457 | print $out "$collection collection built successfully\n\n";
|
---|
458 | if ($remove_archives) {
|
---|
459 | print $out "removing archives directory ($archivedir)\n";
|
---|
460 | &util::rm_r ($archivedir);
|
---|
461 | }
|
---|
462 | } else {
|
---|
463 | my $msg = "build: ERROR: buildcol.pl failed\n";
|
---|
464 | print $out "\n$msg";
|
---|
465 | &log_event ($msg);
|
---|
466 | &final_out (3) if $use_out;
|
---|
467 | die "\n";
|
---|
468 | }
|
---|
469 |
|
---|
470 | if ($removeold) {
|
---|
471 | # replace old indexes with new ones
|
---|
472 | if (&has_content ($indexdir)) {
|
---|
473 | print $out "removing old indexes\n";
|
---|
474 | &util::rm_r ($indexdir);
|
---|
475 | }
|
---|
476 | rmdir ($indexdir) if -d $indexdir;
|
---|
477 | &File::Copy::move ($buildingdir, $indexdir);
|
---|
478 | }
|
---|
479 | else {
|
---|
480 | # Do nothing. We have built into index dir rather than building dir
|
---|
481 | }
|
---|
482 |
|
---|
483 | # remove the cached archives
|
---|
484 | if ($save_archives && -d "${archivedir}.org") {
|
---|
485 | &util::rm_r ("${archivedir}.org");
|
---|
486 | }
|
---|
487 | }
|
---|
488 |
|
---|
489 | # return 1 if $dir directory contains any files or sub-directories (other
|
---|
490 | # than those specified in the $ignore regular expression)
|
---|
491 | sub has_content {
|
---|
492 | my ($dir, $ignore) = @_;
|
---|
493 |
|
---|
494 | if (!-d $dir) {return 0;}
|
---|
495 |
|
---|
496 | opendir (DIR, $dir) || return 0;
|
---|
497 | my @files = readdir DIR;
|
---|
498 | closedir DIR;
|
---|
499 |
|
---|
500 | foreach my $file (@files) {
|
---|
501 | if ($file !~ /^\.{1,2}$/) {
|
---|
502 | return 1 unless (defined $ignore && $file =~ /$ignore/);
|
---|
503 | }
|
---|
504 | }
|
---|
505 | return 0;
|
---|
506 | }
|
---|
507 |
|
---|
508 | sub append_file {
|
---|
509 | my ($handle, $file) = @_;
|
---|
510 |
|
---|
511 | open (FILE, $file) || return;
|
---|
512 | undef $/;
|
---|
513 | print $handle <FILE>;
|
---|
514 | $/ = "\n";
|
---|
515 | close FILE;
|
---|
516 | &util::rm ($file);
|
---|
517 | }
|
---|
518 |
|
---|
519 | # creates a file called $outfile.final and writes an output code to it.
|
---|
520 | # An output code of 0 specifies that there was no error
|
---|
521 | sub final_out {
|
---|
522 | my ($exit_code) = @_;
|
---|
523 |
|
---|
524 | if ($use_out && (!-e "$outfile.final")) {
|
---|
525 |
|
---|
526 | if (open (FINAL, ">$outfile.final")) {
|
---|
527 | print FINAL $exit_code;
|
---|
528 | close FINAL;
|
---|
529 | }
|
---|
530 | }
|
---|
531 | }
|
---|
532 |
|
---|
533 | sub log_event {
|
---|
534 | my ($msg) = @_;
|
---|
535 |
|
---|
536 | return unless ($log_events || $email_events);
|
---|
537 |
|
---|
538 | # get the event header
|
---|
539 | my $eheader = "[Build Event]\n";
|
---|
540 | $eheader .= "Date: " . scalar localtime() . "\n";
|
---|
541 | if ($event_header ne "" && open (HEADER, $event_header)) {
|
---|
542 | undef $/;
|
---|
543 | $eheader .= <HEADER>;
|
---|
544 | $/ = "\n";
|
---|
545 | close HEADER;
|
---|
546 | } else {
|
---|
547 | $eheader .= "Collection: $collection\n";
|
---|
548 | $eheader .= "GSDLHOME: $ENV{'GSDLHOME'}\n";
|
---|
549 | $eheader .= "Build Location: $collectdir\n";
|
---|
550 | }
|
---|
551 |
|
---|
552 | if ($log_events) {
|
---|
553 | my $fail = 0;
|
---|
554 | # append the event to the event log file
|
---|
555 | if ($event_log_file eq "" || !open (LOG, ">>$event_log_file")) {
|
---|
556 | # log file defaults to $GSDLHOME/etc/events.txt
|
---|
557 | $event_log_file = &util::filename_cat ($ENV{'GSDLHOME'}, "etc", "events.txt");
|
---|
558 | if (!open (LOG, ">>$event_log_file")) {
|
---|
559 | print $out "build: ERROR: Couldn't open event log file $event_log_file\n";
|
---|
560 | $fail = 1;
|
---|
561 | }
|
---|
562 | }
|
---|
563 | if (!$fail) {
|
---|
564 | print LOG $eheader;
|
---|
565 | print LOG $msg;
|
---|
566 | print LOG "\n";
|
---|
567 | close LOG;
|
---|
568 | }
|
---|
569 | }
|
---|
570 |
|
---|
571 | if ($email_events) {
|
---|
572 | # if mail_server isn't set email_events does nothing
|
---|
573 | if ($mail_server eq "") {
|
---|
574 | print $out "build: WARNING: mail_server was not set - email_events option was ignored\n";
|
---|
575 | return;
|
---|
576 | }
|
---|
577 |
|
---|
578 | my %mail = ('SMTP' => $mail_server,
|
---|
579 | 'To' => $email_events,
|
---|
580 | 'From' => $maintainer,
|
---|
581 | 'Subject' => 'Greenstone Build Event'
|
---|
582 | );
|
---|
583 | $mail{'Message'} = $eheader . $msg;
|
---|
584 |
|
---|
585 | if (!sendmail %mail) {
|
---|
586 | print $out "build: ERROR sending mail to $email_events\n";
|
---|
587 | print $out "'$Mail::Sendmail::error'\n";
|
---|
588 | }
|
---|
589 | }
|
---|
590 | }
|
---|
591 |
|
---|
592 |
|
---|
593 | sub parse_args {
|
---|
594 | my ($argref) = @_;
|
---|
595 |
|
---|
596 | if (!parsargv::parse($argref,
|
---|
597 | 'optionfile/.*/', \$optionfile,
|
---|
598 | 'indextype/^(mg|mgpp|lucene)$/mg', \$indextype,
|
---|
599 | 'append', \$append,
|
---|
600 | 'manifest', \$manifest,
|
---|
601 | 'remove_archives', \$remove_archives,
|
---|
602 | 'remove_import', \$remove_import,
|
---|
603 | 'buildtype/^(build|import)$/import', \$buildtype,
|
---|
604 | 'maxdocs/^\-?\d+/-1', \$maxdocs,
|
---|
605 | 'download/.+', \@download,
|
---|
606 | 'collectdir/.*/', \$collectdir,
|
---|
607 | 'dontinstall', \$dontinstall,
|
---|
608 | 'save_archives', \$save_archives,
|
---|
609 | 'out/.*/STDERR', \$out,
|
---|
610 | 'make_writable', \$make_writable,
|
---|
611 | 'log_events', \$log_events,
|
---|
612 | 'event_log_file/.*/', \$event_log_file,
|
---|
613 | 'email_events/.*/', \$email_events,
|
---|
614 | 'mail_server/.*/', \$mail_server,
|
---|
615 | 'statsfile/.*/STDERR', \$statsfile,
|
---|
616 | 'event_header/.*/', \$event_header)) {
|
---|
617 |
|
---|
618 | &print_usage();
|
---|
619 | die "\n";
|
---|
620 | }
|
---|
621 | }
|
---|
622 |
|
---|
623 |
|
---|
624 | END {
|
---|
625 |
|
---|
626 | if ($make_writable) {
|
---|
627 | # chmod a+rw new collection
|
---|
628 | my $installed_collection = &util::filename_cat($ENV{'GSDLHOME'}, "collect", $collection);
|
---|
629 | &recursive_chmod($installed_collection);
|
---|
630 |
|
---|
631 | # chmod a+rw anything we've left laying about in the tmp directory
|
---|
632 | if (($collectdir ne "") &&
|
---|
633 | (!&util::filenames_equal ($collectdir, &util::filename_cat($ENV{'GSDLHOME'}, "collect")))) {
|
---|
634 | &recursive_chmod($collectdir);
|
---|
635 | }
|
---|
636 | }
|
---|
637 |
|
---|
638 | # this will produce a .final file if one doesn't exist yet - that
|
---|
639 | # should only happen if there's been an error somewhere in the perl
|
---|
640 | # code
|
---|
641 | &final_out(7);
|
---|
642 |
|
---|
643 | sub recursive_chmod {
|
---|
644 | my ($dir) = @_;
|
---|
645 | return unless -d $dir;
|
---|
646 |
|
---|
647 | chmod (0777, $dir);
|
---|
648 |
|
---|
649 | opendir (DIR, $dir) || die;
|
---|
650 | my @files = readdir DIR;
|
---|
651 | closedir DIR;
|
---|
652 |
|
---|
653 | foreach my $file (@files) {
|
---|
654 | next if $file =~ /^\.\.?$/;
|
---|
655 | $file = &util::filename_cat($dir, $file);
|
---|
656 | if (-d $file) {
|
---|
657 | &recursive_chmod ($file);
|
---|
658 | } else {
|
---|
659 | chmod (0777, $file);
|
---|
660 | }
|
---|
661 | }
|
---|
662 | }
|
---|
663 | }
|
---|