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