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