[1778] | 1 | #!/usr/bin/perl -w
|
---|
[1184] | 2 |
|
---|
[1778] | 3 | ###########################################################################
|
---|
| 4 | #
|
---|
| 5 | # build --
|
---|
| 6 | # A component of the Greenstone digital library software
|
---|
| 7 | # from the New Zealand Digital Library Project at the
|
---|
| 8 | # University of Waikato, New Zealand.
|
---|
| 9 | #
|
---|
| 10 | # Copyright (C) 2000 New Zealand Digital Library Project
|
---|
| 11 | #
|
---|
| 12 | # This program is free software; you can redistribute it and/or modify
|
---|
| 13 | # it under the terms of the GNU General Public License as published by
|
---|
| 14 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 15 | # (at your option) any later version.
|
---|
| 16 | #
|
---|
| 17 | # This program is distributed in the hope that it will be useful,
|
---|
| 18 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 20 | # GNU General Public License for more details.
|
---|
| 21 | #
|
---|
| 22 | # You should have received a copy of the GNU General Public License
|
---|
| 23 | # along with this program; if not, write to the Free Software
|
---|
| 24 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 25 | #
|
---|
| 26 | ###########################################################################
|
---|
| 27 |
|
---|
[1198] | 28 | # This perl script may be called directly or by running build.bat on
|
---|
| 29 | # windows (build.bat is in bin\windows)
|
---|
[1184] | 30 |
|
---|
[1762] | 31 | # Note that this script has grown over time and now has many options for
|
---|
| 32 | # use when called from within the collector. If it appears to
|
---|
| 33 | # over-complicate things a little, that's why.
|
---|
| 34 |
|
---|
[1454] | 35 | package build;
|
---|
| 36 |
|
---|
[1438] | 37 | use FileHandle;
|
---|
[1454] | 38 | use File::Copy;
|
---|
[1438] | 39 |
|
---|
[1184] | 40 | BEGIN {
|
---|
[1198] | 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'};
|
---|
[1184] | 46 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
|
---|
[1438] | 47 |
|
---|
| 48 | STDOUT->autoflush(1);
|
---|
| 49 | STDERR->autoflush(1);
|
---|
[1184] | 50 | }
|
---|
| 51 |
|
---|
[1778] | 52 | use lib qq($ENV{'GSDLHOME'}/perllib/cpan);
|
---|
| 53 | use Mail::Sendmail;
|
---|
[1184] | 54 | use parsargv;
|
---|
| 55 | use util;
|
---|
[1454] | 56 | use cfgread;
|
---|
[1184] | 57 |
|
---|
[1678] | 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 |
|
---|
[1454] | 67 | &parse_args (\@ARGV);
|
---|
[1198] | 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 |
|
---|
[1454] | 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 |
|
---|
[1198] | 88 | if ($maxdocs == -1) {
|
---|
| 89 | $maxdocs = "";
|
---|
| 90 | } else {
|
---|
| 91 | $maxdocs = "-maxdocs $maxdocs";
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[1454] | 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");
|
---|
[1198] | 100 | my $bindir = &util::filename_cat ($ENV{'GSDLHOME'}, "bin");
|
---|
| 101 |
|
---|
[1431] | 102 | my $use_out = 0;
|
---|
[1424] | 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";
|
---|
[1452] | 107 |
|
---|
| 108 | # delete any existing .final file
|
---|
| 109 | &util::rm ("$outfile.final") if -e "$outfile.final";
|
---|
| 110 |
|
---|
[1431] | 111 | $use_out = 1;
|
---|
[1424] | 112 | }
|
---|
| 113 | $out->autoflush(1);
|
---|
| 114 |
|
---|
[1454] | 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 |
|
---|
[1762] | 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 |
|
---|
[1184] | 132 | &main();
|
---|
| 133 |
|
---|
[2785] | 134 | if ($use_out) {
|
---|
| 135 | close OUT;
|
---|
[1424] | 136 |
|
---|
[2785] | 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 |
|
---|
[1184] | 149 | sub print_usage {
|
---|
[2359] | 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";
|
---|
[12003] | 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";
|
---|
[2359] | 162 | print STDOUT " -append Add new files to existing collection\n";
|
---|
[12003] | 163 | print STDOUT " -manifest Use manifest.xml file to determine which files to process.\n";
|
---|
[2359] | 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 " .
|
---|
[1454] | 179 | &util::filename_cat ($ENV{'GSDLHOME'}, "collect") . ")\n";
|
---|
[2359] | 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";
|
---|
[2785] | 189 | print STDOUT " -statsfile name Filename or handle to print import statistics to.\n";
|
---|
| 190 | print STDOUT " The default is STDERR\n";
|
---|
[2566] | 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";
|
---|
[2359] | 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";
|
---|
[2469] | 206 | print STDOUT " [Type \"build | more\" if this help text scrolled off your screen]";
|
---|
[2359] | 207 | print STDOUT "\n" unless $ENV{'GSDLOS'} =~ /^windows$/i;
|
---|
[1184] | 208 | }
|
---|
| 209 |
|
---|
| 210 | sub main {
|
---|
[1454] | 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 |
|
---|
[1424] | 217 | # do the download thing if we have any -download options
|
---|
| 218 | if (scalar (@download)) {
|
---|
| 219 | # remove any existing import data
|
---|
[1431] | 220 | if (&has_content ($importdir)) {
|
---|
| 221 | print $out "build: WARNING: removing contents of $importdir\n";
|
---|
| 222 | &util::rm_r ($importdir);
|
---|
| 223 | }
|
---|
[1454] | 224 |
|
---|
[1424] | 225 | foreach $download_dir (@download) {
|
---|
[1507] | 226 |
|
---|
| 227 | # remove any leading or trailing whitespace from filenames (just in case)
|
---|
| 228 | $download_dir =~ s/^\s+//;
|
---|
| 229 | $download_dir =~ s/\s+$//;
|
---|
[1424] | 230 |
|
---|
[1709] | 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)
|
---|
[1743] | 247 |
|
---|
[1709] | 248 | my $download_cmd = "perl -S gsWget.pl -P \"$importdir\" -np -nv";
|
---|
[1753] | 249 | $download_cmd .= " -r -N -l inf -R \"*\\?*\"";
|
---|
[1709] | 250 | $download_cmd .= " -o \"$outfile.download\"" if $use_out;
|
---|
| 251 | $download_cmd .= " \"$download_dir\"";
|
---|
| 252 | system ($download_cmd);
|
---|
[1424] | 253 |
|
---|
[1709] | 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 {
|
---|
[1743] | 269 | $file = &util::filename_cat ($importdir, $file);
|
---|
[1709] | 270 | if (-d $file) {
|
---|
| 271 | if (opendir (DIR, $file)) {
|
---|
[1743] | 272 | my @files2 = readdir DIR;
|
---|
[1709] | 273 | closedir DIR;
|
---|
[1743] | 274 | foreach my $file2 (@files2) {
|
---|
| 275 | if ($file2 =~ /^robots.txt$/i) {
|
---|
| 276 | &util::rm (&util::filename_cat ($file, $file2));
|
---|
[1709] | 277 | last;
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
| 283 | }
|
---|
| 284 | }
|
---|
[1424] | 285 |
|
---|
[1709] | 286 | # if using output directory append the file download output to it
|
---|
| 287 | &append_file ($out, "$outfile.download");
|
---|
| 288 |
|
---|
[1424] | 289 | } else {
|
---|
| 290 | # we assume anything not beginning with http:// or ftp://
|
---|
| 291 | # is a file or directory on the local file system.
|
---|
[1485] | 292 | $download_dir =~ s/^file:(\/\/)?//;
|
---|
[1507] | 293 | $download_dir =~ s/^\s+//; # may be whitespace between "file://" and the rest
|
---|
[1424] | 294 |
|
---|
| 295 | if (-e $download_dir) {
|
---|
| 296 | # copy download_dir and all it contains to the import directory
|
---|
[1678] | 297 | my $download_cmd = "perl -S filecopy.pl";
|
---|
[1454] | 298 | $download_cmd .= " -collectdir \"$collectdir\"" if $collectdir =~ /\w/;
|
---|
[1431] | 299 | $download_cmd .= " -out \"$outfile.download\"" if $use_out;
|
---|
| 300 | $download_cmd .= " \"" . $download_dir . "\" " . $collection;
|
---|
[1424] | 301 | system ($download_cmd);
|
---|
[1431] | 302 | # if using output directory append the file download output to it
|
---|
| 303 | &append_file ($out, "$outfile.download");
|
---|
[1424] | 304 | } else {
|
---|
[1507] | 305 | print $out "WARNING: '$download_dir' does not exist\n";
|
---|
[1424] | 306 | }
|
---|
| 307 | }
|
---|
| 308 | }
|
---|
| 309 | }
|
---|
| 310 |
|
---|
[1198] | 311 | if (-e &util::filename_cat ($archivedir, "archives.inf")) {
|
---|
| 312 | if (&has_content ($importdir)) {
|
---|
[1184] | 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
|
---|
[1424] | 321 | print $out "build: no import material was found, building directly\n";
|
---|
| 322 | print $out " from archives\n";
|
---|
[1184] | 323 | &gsdl_build();
|
---|
| 324 | }
|
---|
| 325 | } else {
|
---|
[1198] | 326 | if (&has_content ($importdir)) {
|
---|
[1184] | 327 | if ($buildtype eq "build") {
|
---|
[1424] | 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";
|
---|
[1184] | 332 | }
|
---|
| 333 | &gsdl_import();
|
---|
[2381] | 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 | }
|
---|
[1184] | 346 | } else {
|
---|
| 347 | # no import or archives
|
---|
[2381] | 348 | my $msg = "build: ERROR: The collection could not be built as it contains no data.\n";
|
---|
[1762] | 349 | print $out $msg;
|
---|
| 350 | &log_event ($msg);
|
---|
[1438] | 351 | &final_out (1) if $use_out;
|
---|
[1184] | 352 | die "\n";
|
---|
| 353 | }
|
---|
| 354 | }
|
---|
[1454] | 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) {
|
---|
[1762] | 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);
|
---|
[1454] | 370 | &final_out (4) if $use_out;
|
---|
| 371 | die "\n";
|
---|
| 372 | }
|
---|
| 373 | if (!&File::Copy::move ($olddir, $newdir)) {
|
---|
[1762] | 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);
|
---|
[1454] | 378 | &final_out (5) if $use_out;
|
---|
| 379 | die "\n";
|
---|
| 380 | }
|
---|
| 381 | }
|
---|
| 382 | }
|
---|
| 383 |
|
---|
[1762] | 384 | &log_event ("The $collection collection was built successfully\n");
|
---|
[1438] | 385 | &final_out (0) if $use_out;
|
---|
[1184] | 386 | }
|
---|
| 387 |
|
---|
| 388 | sub gsdl_import {
|
---|
| 389 |
|
---|
[1424] | 390 | print $out "importing the $collection collection\n\n";
|
---|
[1198] | 391 |
|
---|
[1678] | 392 | my $import_cmd = "perl -S import.pl";
|
---|
[1431] | 393 | $import_cmd .= " -out \"$outfile.import\"" if $use_out;
|
---|
[12003] | 394 | if ($append) {
|
---|
| 395 | $import_cmd .= " -keepold";
|
---|
[13067] | 396 | if (not $manifest) {
|
---|
| 397 | # if we are appending, with no manifest, assume incremental
|
---|
| 398 | $import_cmd .= " -incremental";
|
---|
| 399 | }
|
---|
[12003] | 400 | } else {
|
---|
| 401 | $import_cmd .= " -removeold";
|
---|
| 402 | }
|
---|
| 403 |
|
---|
| 404 | $import_cmd .= " -manifest manifest.xml" if ($manifest);
|
---|
[1454] | 405 | $import_cmd .= " -collectdir \"$collectdir\"" if $collectdir =~ /\w/;
|
---|
[2785] | 406 | $import_cmd .= " -statsfile \"$statsfile\"" if $statsfile =~ /\w/;
|
---|
[1431] | 407 | $import_cmd .= " $maxdocs $collection";
|
---|
[2785] | 408 |
|
---|
[1431] | 409 | system ($import_cmd);
|
---|
| 410 | # if using output directory append the import output to it
|
---|
| 411 | &append_file ($out, "$outfile.import");
|
---|
| 412 |
|
---|
[1198] | 413 | if (-e &util::filename_cat ($archivedir, "archives.inf")) {
|
---|
[1424] | 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 | }
|
---|
[1184] | 419 | } else {
|
---|
[1762] | 420 | my $msg = "build: ERROR: import.pl failed\n";
|
---|
| 421 | print $out "\n$msg";
|
---|
| 422 | &log_event ($msg);
|
---|
[1438] | 423 | &final_out (2) if $use_out;
|
---|
[1454] | 424 | die "\n";
|
---|
[1184] | 425 | }
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | sub gsdl_build {
|
---|
| 429 |
|
---|
[1424] | 430 | print $out "building the $collection collection\n\n";
|
---|
[1184] | 431 |
|
---|
[1678] | 432 | my $build_cmd = "perl -S buildcol.pl";
|
---|
[12003] | 433 |
|
---|
| 434 | my $removeold = 1;
|
---|
| 435 | if ($append) {
|
---|
| 436 | if ($indextype eq "lucene") {
|
---|
[13067] | 437 | $build_cmd .= " -keepold -incremental -builddir $indexdir";
|
---|
[12003] | 438 | $removeold = 0;
|
---|
| 439 | }
|
---|
| 440 | else {
|
---|
| 441 | $build_cmd .= " -removeold";
|
---|
| 442 | }
|
---|
| 443 | }
|
---|
| 444 | else {
|
---|
| 445 | $build_cmd .= " -removeold";
|
---|
| 446 | }
|
---|
| 447 |
|
---|
[1431] | 448 | $build_cmd .= " -out \"$outfile.build\"" if $use_out;
|
---|
[1454] | 449 | $build_cmd .= " -collectdir \"$collectdir\"" if $collectdir =~ /\w/;
|
---|
[1431] | 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 |
|
---|
[13067] | 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")))) {
|
---|
[1424] | 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 | }
|
---|
[1184] | 462 | } else {
|
---|
[1762] | 463 | my $msg = "build: ERROR: buildcol.pl failed\n";
|
---|
| 464 | print $out "\n$msg";
|
---|
| 465 | &log_event ($msg);
|
---|
[1454] | 466 | &final_out (3) if $use_out;
|
---|
| 467 | die "\n";
|
---|
[1184] | 468 | }
|
---|
| 469 |
|
---|
[12003] | 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);
|
---|
[1184] | 478 | }
|
---|
[12003] | 479 | else {
|
---|
[13067] | 480 | # Do nothing. We have built into index dir rather than building dir
|
---|
[12003] | 481 | }
|
---|
[1454] | 482 |
|
---|
[13067] | 483 | # remove the cached archives
|
---|
[1461] | 484 | if ($save_archives && -d "${archivedir}.org") {
|
---|
[1454] | 485 | &util::rm_r ("${archivedir}.org");
|
---|
| 486 | }
|
---|
[1184] | 487 | }
|
---|
| 488 |
|
---|
[2381] | 489 | # return 1 if $dir directory contains any files or sub-directories (other
|
---|
| 490 | # than those specified in the $ignore regular expression)
|
---|
[1184] | 491 | sub has_content {
|
---|
[2381] | 492 | my ($dir, $ignore) = @_;
|
---|
[1184] | 493 |
|
---|
| 494 | if (!-d $dir) {return 0;}
|
---|
[2381] | 495 |
|
---|
[1184] | 496 | opendir (DIR, $dir) || return 0;
|
---|
| 497 | my @files = readdir DIR;
|
---|
[2488] | 498 | closedir DIR;
|
---|
[1184] | 499 |
|
---|
| 500 | foreach my $file (@files) {
|
---|
| 501 | if ($file !~ /^\.{1,2}$/) {
|
---|
[2381] | 502 | return 1 unless (defined $ignore && $file =~ /$ignore/);
|
---|
[1184] | 503 | }
|
---|
| 504 | }
|
---|
| 505 | return 0;
|
---|
| 506 | }
|
---|
[1431] | 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 | }
|
---|
[1438] | 518 |
|
---|
[2774] | 519 | # creates a file called $outfile.final and writes an output code to it.
|
---|
[1438] | 520 | # An output code of 0 specifies that there was no error
|
---|
| 521 | sub final_out {
|
---|
| 522 | my ($exit_code) = @_;
|
---|
| 523 |
|
---|
[2774] | 524 | if ($use_out && (!-e "$outfile.final")) {
|
---|
| 525 |
|
---|
| 526 | if (open (FINAL, ">$outfile.final")) {
|
---|
| 527 | print FINAL $exit_code;
|
---|
| 528 | close FINAL;
|
---|
| 529 | }
|
---|
[1438] | 530 | }
|
---|
| 531 | }
|
---|
[1454] | 532 |
|
---|
[1762] | 533 | sub log_event {
|
---|
| 534 | my ($msg) = @_;
|
---|
| 535 |
|
---|
| 536 | return unless ($log_events || $email_events);
|
---|
| 537 |
|
---|
| 538 | # get the event header
|
---|
[1778] | 539 | my $eheader = "[Build Event]\n";
|
---|
| 540 | $eheader .= "Date: " . scalar localtime() . "\n";
|
---|
[1762] | 541 | if ($event_header ne "" && open (HEADER, $event_header)) {
|
---|
| 542 | undef $/;
|
---|
[1778] | 543 | $eheader .= <HEADER>;
|
---|
[1762] | 544 | $/ = "\n";
|
---|
| 545 | close HEADER;
|
---|
| 546 | } else {
|
---|
| 547 | $eheader .= "Collection: $collection\n";
|
---|
[1778] | 548 | $eheader .= "GSDLHOME: $ENV{'GSDLHOME'}\n";
|
---|
| 549 | $eheader .= "Build Location: $collectdir\n";
|
---|
[1762] | 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;
|
---|
[1778] | 566 | print LOG "\n";
|
---|
[1762] | 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 |
|
---|
[1454] | 593 | sub parse_args {
|
---|
| 594 | my ($argref) = @_;
|
---|
| 595 |
|
---|
| 596 | if (!parsargv::parse($argref,
|
---|
| 597 | 'optionfile/.*/', \$optionfile,
|
---|
[12003] | 598 | 'indextype/^(mg|mgpp|lucene)$/mg', \$indextype,
|
---|
[1454] | 599 | 'append', \$append,
|
---|
[12003] | 600 | 'manifest', \$manifest,
|
---|
[1454] | 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,
|
---|
[1762] | 609 | 'out/.*/STDERR', \$out,
|
---|
[2566] | 610 | 'make_writable', \$make_writable,
|
---|
[1762] | 611 | 'log_events', \$log_events,
|
---|
| 612 | 'event_log_file/.*/', \$event_log_file,
|
---|
| 613 | 'email_events/.*/', \$email_events,
|
---|
| 614 | 'mail_server/.*/', \$mail_server,
|
---|
[2785] | 615 | 'statsfile/.*/STDERR', \$statsfile,
|
---|
[1762] | 616 | 'event_header/.*/', \$event_header)) {
|
---|
[1454] | 617 |
|
---|
| 618 | &print_usage();
|
---|
| 619 | die "\n";
|
---|
| 620 | }
|
---|
| 621 | }
|
---|
[2566] | 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 |
|
---|
[2774] | 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 |
|
---|
[2566] | 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 | }
|
---|