root/main/trunk/greenstone2/bin/script/build @ 23313

Revision 23313, 25.7 KB (checked in by kjdon, 9 years ago)

call setup_greenstone_env so that we get the correct environment (eg MAGICK_HOME is needed to run greenstone's image magick) - this is not necessary from the command line as setup.bash has already been sourced, but from the collector/depositor it is.

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