[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 |
|
---|
[17253] | 37 | use strict;
|
---|
| 38 | no strict 'refs';
|
---|
| 39 |
|
---|
[1438] | 40 | use FileHandle;
|
---|
[1454] | 41 | use File::Copy;
|
---|
[1438] | 42 |
|
---|
[1184] | 43 | BEGIN {
|
---|
[1198] | 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'};
|
---|
[1184] | 49 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
|
---|
[1438] | 50 |
|
---|
| 51 | STDOUT->autoflush(1);
|
---|
| 52 | STDERR->autoflush(1);
|
---|
[1184] | 53 | }
|
---|
| 54 |
|
---|
[1778] | 55 | use lib qq($ENV{'GSDLHOME'}/perllib/cpan);
|
---|
| 56 | use Mail::Sendmail;
|
---|
[1184] | 57 | use parsargv;
|
---|
| 58 | use util;
|
---|
[1454] | 59 | use cfgread;
|
---|
[1184] | 60 |
|
---|
[1678] | 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 |
|
---|
[17253] | 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 |
|
---|
[1454] | 73 | &parse_args (\@ARGV);
|
---|
[1198] | 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 |
|
---|
[1454] | 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 |
|
---|
[1198] | 94 | if ($maxdocs == -1) {
|
---|
| 95 | $maxdocs = "";
|
---|
| 96 | } else {
|
---|
| 97 | $maxdocs = "-maxdocs $maxdocs";
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[1454] | 100 | my $cdir = $collectdir;
|
---|
[17253] | 101 | if (defined $site && $site =~ /\w/)
|
---|
[14925] | 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 |
|
---|
[1454] | 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");
|
---|
[1198] | 115 | my $bindir = &util::filename_cat ($ENV{'GSDLHOME'}, "bin");
|
---|
| 116 |
|
---|
[1431] | 117 | my $use_out = 0;
|
---|
[1424] | 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";
|
---|
[1452] | 122 |
|
---|
| 123 | # delete any existing .final file
|
---|
| 124 | &util::rm ("$outfile.final") if -e "$outfile.final";
|
---|
| 125 |
|
---|
[1431] | 126 | $use_out = 1;
|
---|
[1424] | 127 | }
|
---|
| 128 | $out->autoflush(1);
|
---|
| 129 |
|
---|
[1454] | 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 |
|
---|
[1762] | 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 |
|
---|
[1184] | 147 | &main();
|
---|
| 148 |
|
---|
[2785] | 149 | if ($use_out) {
|
---|
| 150 | close OUT;
|
---|
[1424] | 151 |
|
---|
[2785] | 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 |
|
---|
[1184] | 164 | sub print_usage {
|
---|
[2359] | 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";
|
---|
[12003] | 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";
|
---|
[2359] | 177 | print STDOUT " -append Add new files to existing collection\n";
|
---|
[12003] | 178 | print STDOUT " -manifest Use manifest.xml file to determine which files to process.\n";
|
---|
[2359] | 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 " .
|
---|
[14925] | 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";
|
---|
[2359] | 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";
|
---|
[2785] | 207 | print STDOUT " -statsfile name Filename or handle to print import statistics to.\n";
|
---|
| 208 | print STDOUT " The default is STDERR\n";
|
---|
[2566] | 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";
|
---|
[2359] | 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";
|
---|
[2469] | 224 | print STDOUT " [Type \"build | more\" if this help text scrolled off your screen]";
|
---|
[2359] | 225 | print STDOUT "\n" unless $ENV{'GSDLOS'} =~ /^windows$/i;
|
---|
[1184] | 226 | }
|
---|
| 227 |
|
---|
| 228 | sub main {
|
---|
[1454] | 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 |
|
---|
[1424] | 235 | # do the download thing if we have any -download options
|
---|
| 236 | if (scalar (@download)) {
|
---|
| 237 | # remove any existing import data
|
---|
[1431] | 238 | if (&has_content ($importdir)) {
|
---|
| 239 | print $out "build: WARNING: removing contents of $importdir\n";
|
---|
| 240 | &util::rm_r ($importdir);
|
---|
| 241 | }
|
---|
[1454] | 242 |
|
---|
[17253] | 243 | foreach my $download_dir (@download) {
|
---|
[1507] | 244 |
|
---|
| 245 | # remove any leading or trailing whitespace from filenames (just in case)
|
---|
| 246 | $download_dir =~ s/^\s+//;
|
---|
| 247 | $download_dir =~ s/\s+$//;
|
---|
[1424] | 248 |
|
---|
[1709] | 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)
|
---|
[1743] | 265 |
|
---|
[1709] | 266 | my $download_cmd = "perl -S gsWget.pl -P \"$importdir\" -np -nv";
|
---|
[1753] | 267 | $download_cmd .= " -r -N -l inf -R \"*\\?*\"";
|
---|
[1709] | 268 | $download_cmd .= " -o \"$outfile.download\"" if $use_out;
|
---|
| 269 | $download_cmd .= " \"$download_dir\"";
|
---|
| 270 | system ($download_cmd);
|
---|
[1424] | 271 |
|
---|
[1709] | 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 {
|
---|
[1743] | 287 | $file = &util::filename_cat ($importdir, $file);
|
---|
[1709] | 288 | if (-d $file) {
|
---|
| 289 | if (opendir (DIR, $file)) {
|
---|
[1743] | 290 | my @files2 = readdir DIR;
|
---|
[1709] | 291 | closedir DIR;
|
---|
[1743] | 292 | foreach my $file2 (@files2) {
|
---|
| 293 | if ($file2 =~ /^robots.txt$/i) {
|
---|
| 294 | &util::rm (&util::filename_cat ($file, $file2));
|
---|
[1709] | 295 | last;
|
---|
| 296 | }
|
---|
| 297 | }
|
---|
| 298 | }
|
---|
| 299 | }
|
---|
| 300 | }
|
---|
| 301 | }
|
---|
| 302 | }
|
---|
[1424] | 303 |
|
---|
[1709] | 304 | # if using output directory append the file download output to it
|
---|
| 305 | &append_file ($out, "$outfile.download");
|
---|
| 306 |
|
---|
[1424] | 307 | } else {
|
---|
| 308 | # we assume anything not beginning with http:// or ftp://
|
---|
| 309 | # is a file or directory on the local file system.
|
---|
[1485] | 310 | $download_dir =~ s/^file:(\/\/)?//;
|
---|
[1507] | 311 | $download_dir =~ s/^\s+//; # may be whitespace between "file://" and the rest
|
---|
[1424] | 312 |
|
---|
| 313 | if (-e $download_dir) {
|
---|
| 314 | # copy download_dir and all it contains to the import directory
|
---|
[1678] | 315 | my $download_cmd = "perl -S filecopy.pl";
|
---|
[1454] | 316 | $download_cmd .= " -collectdir \"$collectdir\"" if $collectdir =~ /\w/;
|
---|
[14925] | 317 | $download_cmd .= " -site \"$site\"" if $site =~ /\w/;
|
---|
[1431] | 318 | $download_cmd .= " -out \"$outfile.download\"" if $use_out;
|
---|
| 319 | $download_cmd .= " \"" . $download_dir . "\" " . $collection;
|
---|
[14925] | 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 |
|
---|
[1431] | 328 | # if using output directory append the file download output to it
|
---|
| 329 | &append_file ($out, "$outfile.download");
|
---|
[1424] | 330 | } else {
|
---|
[1507] | 331 | print $out "WARNING: '$download_dir' does not exist\n";
|
---|
[1424] | 332 | }
|
---|
| 333 | }
|
---|
| 334 | }
|
---|
| 335 | }
|
---|
| 336 |
|
---|
[14925] | 337 | `echo $importdir ; ls $importdir `;
|
---|
| 338 |
|
---|
[21567] | 339 | my $archiveinf_doc_file_path = &dbutil::get_infodb_file_path("gdbm", "archiveinf-doc", $archivedir);
|
---|
| 340 | if (-e $archiveinf_doc_file_path) {
|
---|
[1198] | 341 | if (&has_content ($importdir)) {
|
---|
[1184] | 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
|
---|
[1424] | 350 | print $out "build: no import material was found, building directly\n";
|
---|
| 351 | print $out " from archives\n";
|
---|
[1184] | 352 | &gsdl_build();
|
---|
| 353 | }
|
---|
| 354 | } else {
|
---|
[1198] | 355 | if (&has_content ($importdir)) {
|
---|
[1184] | 356 | if ($buildtype eq "build") {
|
---|
[1424] | 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";
|
---|
[1184] | 361 | }
|
---|
| 362 | &gsdl_import();
|
---|
[21567] | 363 | if (&has_content ($archivedir, "^archiveinf-doc\..*\$")) {
|
---|
[2381] | 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 | }
|
---|
[1184] | 375 | } else {
|
---|
| 376 | # no import or archives
|
---|
[2381] | 377 | my $msg = "build: ERROR: The collection could not be built as it contains no data.\n";
|
---|
[1762] | 378 | print $out $msg;
|
---|
| 379 | &log_event ($msg);
|
---|
[1438] | 380 | &final_out (1) if $use_out;
|
---|
[1184] | 381 | die "\n";
|
---|
| 382 | }
|
---|
| 383 | }
|
---|
[1454] | 384 |
|
---|
| 385 | if ($collectdir ne "" && !$dontinstall) {
|
---|
[14925] | 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 |
|
---|
[1454] | 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) {
|
---|
[1762] | 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);
|
---|
[1454] | 421 | &final_out (4) if $use_out;
|
---|
| 422 | die "\n";
|
---|
| 423 | }
|
---|
| 424 | if (!&File::Copy::move ($olddir, $newdir)) {
|
---|
[1762] | 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);
|
---|
[1454] | 429 | &final_out (5) if $use_out;
|
---|
| 430 | die "\n";
|
---|
| 431 | }
|
---|
| 432 | }
|
---|
| 433 | }
|
---|
| 434 |
|
---|
[1762] | 435 | &log_event ("The $collection collection was built successfully\n");
|
---|
[1438] | 436 | &final_out (0) if $use_out;
|
---|
[1184] | 437 | }
|
---|
| 438 |
|
---|
| 439 | sub gsdl_import {
|
---|
| 440 |
|
---|
[1424] | 441 | print $out "importing the $collection collection\n\n";
|
---|
[1198] | 442 |
|
---|
[1678] | 443 | my $import_cmd = "perl -S import.pl";
|
---|
[1431] | 444 | $import_cmd .= " -out \"$outfile.import\"" if $use_out;
|
---|
[12003] | 445 | if ($append) {
|
---|
| 446 | $import_cmd .= " -keepold";
|
---|
[13067] | 447 | if (not $manifest) {
|
---|
| 448 | # if we are appending, with no manifest, assume incremental
|
---|
| 449 | $import_cmd .= " -incremental";
|
---|
| 450 | }
|
---|
[12003] | 451 | } else {
|
---|
| 452 | $import_cmd .= " -removeold";
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | $import_cmd .= " -manifest manifest.xml" if ($manifest);
|
---|
[14925] | 456 | $import_cmd .= " -site \"$site\"" if $site =~ /\w/;
|
---|
[1454] | 457 | $import_cmd .= " -collectdir \"$collectdir\"" if $collectdir =~ /\w/;
|
---|
[2785] | 458 | $import_cmd .= " -statsfile \"$statsfile\"" if $statsfile =~ /\w/;
|
---|
[1431] | 459 | $import_cmd .= " $maxdocs $collection";
|
---|
[2785] | 460 |
|
---|
[1431] | 461 | system ($import_cmd);
|
---|
| 462 | # if using output directory append the import output to it
|
---|
| 463 | &append_file ($out, "$outfile.import");
|
---|
| 464 |
|
---|
[21567] | 465 | my $archiveinf_doc_file_path = &dbutil::get_infodb_file_path("gdbm", "archiveinf-doc", $archivedir);
|
---|
| 466 | if (-e $archiveinf_doc_file_path) {
|
---|
[1424] | 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 | }
|
---|
[1184] | 472 | } else {
|
---|
[19485] | 473 | print $out `ls $archivedir`;
|
---|
| 474 |
|
---|
[1762] | 475 | my $msg = "build: ERROR: import.pl failed\n";
|
---|
| 476 | print $out "\n$msg";
|
---|
| 477 | &log_event ($msg);
|
---|
[1438] | 478 | &final_out (2) if $use_out;
|
---|
[1454] | 479 | die "\n";
|
---|
[1184] | 480 | }
|
---|
| 481 | }
|
---|
| 482 |
|
---|
| 483 | sub gsdl_build {
|
---|
| 484 |
|
---|
[1424] | 485 | print $out "building the $collection collection\n\n";
|
---|
[1184] | 486 |
|
---|
[1678] | 487 | my $build_cmd = "perl -S buildcol.pl";
|
---|
[12003] | 488 |
|
---|
| 489 | my $removeold = 1;
|
---|
| 490 | if ($append) {
|
---|
| 491 | if ($indextype eq "lucene") {
|
---|
[13067] | 492 | $build_cmd .= " -keepold -incremental -builddir $indexdir";
|
---|
[12003] | 493 | $removeold = 0;
|
---|
| 494 | }
|
---|
| 495 | else {
|
---|
| 496 | $build_cmd .= " -removeold";
|
---|
| 497 | }
|
---|
| 498 | }
|
---|
| 499 | else {
|
---|
| 500 | $build_cmd .= " -removeold";
|
---|
| 501 | }
|
---|
| 502 |
|
---|
[1431] | 503 | $build_cmd .= " -out \"$outfile.build\"" if $use_out;
|
---|
[14925] | 504 | $build_cmd .= " -site \"$site\"" if $site =~ /\w/;
|
---|
[1454] | 505 | $build_cmd .= " -collectdir \"$collectdir\"" if $collectdir =~ /\w/;
|
---|
[1431] | 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 |
|
---|
[19485] | 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) {
|
---|
[1424] | 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 | }
|
---|
[1184] | 530 | } else {
|
---|
[1762] | 531 | my $msg = "build: ERROR: buildcol.pl failed\n";
|
---|
| 532 | print $out "\n$msg";
|
---|
| 533 | &log_event ($msg);
|
---|
[1454] | 534 | &final_out (3) if $use_out;
|
---|
| 535 | die "\n";
|
---|
[1184] | 536 | }
|
---|
| 537 |
|
---|
[12003] | 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);
|
---|
[1184] | 546 | }
|
---|
[12003] | 547 | else {
|
---|
[13067] | 548 | # Do nothing. We have built into index dir rather than building dir
|
---|
[12003] | 549 | }
|
---|
[1454] | 550 |
|
---|
[13067] | 551 | # remove the cached archives
|
---|
[1461] | 552 | if ($save_archives && -d "${archivedir}.org") {
|
---|
[1454] | 553 | &util::rm_r ("${archivedir}.org");
|
---|
| 554 | }
|
---|
[1184] | 555 | }
|
---|
| 556 |
|
---|
[2381] | 557 | # return 1 if $dir directory contains any files or sub-directories (other
|
---|
| 558 | # than those specified in the $ignore regular expression)
|
---|
[1184] | 559 | sub has_content {
|
---|
[2381] | 560 | my ($dir, $ignore) = @_;
|
---|
[1184] | 561 |
|
---|
| 562 | if (!-d $dir) {return 0;}
|
---|
[2381] | 563 |
|
---|
[1184] | 564 | opendir (DIR, $dir) || return 0;
|
---|
| 565 | my @files = readdir DIR;
|
---|
[2488] | 566 | closedir DIR;
|
---|
[1184] | 567 |
|
---|
| 568 | foreach my $file (@files) {
|
---|
| 569 | if ($file !~ /^\.{1,2}$/) {
|
---|
[2381] | 570 | return 1 unless (defined $ignore && $file =~ /$ignore/);
|
---|
[1184] | 571 | }
|
---|
| 572 | }
|
---|
[14925] | 573 |
|
---|
[1184] | 574 | return 0;
|
---|
| 575 | }
|
---|
[1431] | 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 | }
|
---|
[1438] | 587 |
|
---|
[2774] | 588 | # creates a file called $outfile.final and writes an output code to it.
|
---|
[1438] | 589 | # An output code of 0 specifies that there was no error
|
---|
| 590 | sub final_out {
|
---|
| 591 | my ($exit_code) = @_;
|
---|
| 592 |
|
---|
[2774] | 593 | if ($use_out && (!-e "$outfile.final")) {
|
---|
| 594 |
|
---|
| 595 | if (open (FINAL, ">$outfile.final")) {
|
---|
| 596 | print FINAL $exit_code;
|
---|
| 597 | close FINAL;
|
---|
| 598 | }
|
---|
[1438] | 599 | }
|
---|
| 600 | }
|
---|
[1454] | 601 |
|
---|
[1762] | 602 | sub log_event {
|
---|
| 603 | my ($msg) = @_;
|
---|
| 604 |
|
---|
| 605 | return unless ($log_events || $email_events);
|
---|
| 606 |
|
---|
| 607 | # get the event header
|
---|
[1778] | 608 | my $eheader = "[Build Event]\n";
|
---|
| 609 | $eheader .= "Date: " . scalar localtime() . "\n";
|
---|
[1762] | 610 | if ($event_header ne "" && open (HEADER, $event_header)) {
|
---|
| 611 | undef $/;
|
---|
[1778] | 612 | $eheader .= <HEADER>;
|
---|
[1762] | 613 | $/ = "\n";
|
---|
| 614 | close HEADER;
|
---|
| 615 | } else {
|
---|
| 616 | $eheader .= "Collection: $collection\n";
|
---|
[1778] | 617 | $eheader .= "GSDLHOME: $ENV{'GSDLHOME'}\n";
|
---|
| 618 | $eheader .= "Build Location: $collectdir\n";
|
---|
[1762] | 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;
|
---|
[1778] | 635 | print LOG "\n";
|
---|
[1762] | 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 |
|
---|
[1454] | 662 | sub parse_args {
|
---|
| 663 | my ($argref) = @_;
|
---|
| 664 |
|
---|
| 665 | if (!parsargv::parse($argref,
|
---|
| 666 | 'optionfile/.*/', \$optionfile,
|
---|
[12003] | 667 | 'indextype/^(mg|mgpp|lucene)$/mg', \$indextype,
|
---|
[1454] | 668 | 'append', \$append,
|
---|
[12003] | 669 | 'manifest', \$manifest,
|
---|
[1454] | 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,
|
---|
[14925] | 676 | 'site/.*/', \$site,
|
---|
[1454] | 677 | 'dontinstall', \$dontinstall,
|
---|
| 678 | 'save_archives', \$save_archives,
|
---|
[1762] | 679 | 'out/.*/STDERR', \$out,
|
---|
[2566] | 680 | 'make_writable', \$make_writable,
|
---|
[1762] | 681 | 'log_events', \$log_events,
|
---|
| 682 | 'event_log_file/.*/', \$event_log_file,
|
---|
| 683 | 'email_events/.*/', \$email_events,
|
---|
| 684 | 'mail_server/.*/', \$mail_server,
|
---|
[2785] | 685 | 'statsfile/.*/STDERR', \$statsfile,
|
---|
[1762] | 686 | 'event_header/.*/', \$event_header)) {
|
---|
[1454] | 687 |
|
---|
| 688 | &print_usage();
|
---|
| 689 | die "\n";
|
---|
| 690 | }
|
---|
| 691 | }
|
---|
[2566] | 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 |
|
---|
[2774] | 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 |
|
---|
[2566] | 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 | }
|
---|