[1145] | 1 | #!perl -w
|
---|
[724] | 2 |
|
---|
| 3 | ###########################################################################
|
---|
| 4 | #
|
---|
| 5 | # webpage_buildcol.pl --
|
---|
| 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) 1999 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 program is a webpage wrapper to the import.pl and buildcol.pl
|
---|
| 29 | # processes
|
---|
| 30 | # Note while it is in the cgi-bin directory, its arguments are supplied as
|
---|
| 31 | # the more traditional command line argument. The program is executed by
|
---|
| 32 | # an already running cgi program.
|
---|
| 33 |
|
---|
| 34 | use File::Basename;
|
---|
[1145] | 35 | use GSDLHOME;
|
---|
[1181] | 36 | use gflock;
|
---|
[724] | 37 |
|
---|
| 38 | my $args;
|
---|
| 39 |
|
---|
| 40 | BEGIN
|
---|
| 41 | {
|
---|
| 42 | my $va_pair;
|
---|
| 43 | foreach $va_pair (@ARGV)
|
---|
| 44 | {
|
---|
| 45 | if ($va_pair =~ m/^(\w+)=(\"?)(.*)(\"?)$/)
|
---|
| 46 | {
|
---|
| 47 | my ($variable,$assignment) = ($1,$3);
|
---|
| 48 | $args->{$variable} = $assignment;
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | require util;
|
---|
| 54 | require webpageutil;
|
---|
| 55 | require cfgread;
|
---|
| 56 |
|
---|
| 57 | sub communicate_single_line
|
---|
| 58 | {
|
---|
| 59 | my ($full_tmpname,$text) = @_;
|
---|
| 60 |
|
---|
| 61 | if (open(TMPOUT,">$full_tmpname"))
|
---|
| 62 | {
|
---|
[1181] | 63 | if (&gflock::lock (TMPOUT)) {
|
---|
[724] | 64 | print TMPOUT $text;
|
---|
| 65 | close(TMPOUT);
|
---|
[1181] | 66 | &gflock::unlock (TMPOUT);
|
---|
| 67 |
|
---|
| 68 | } else {
|
---|
[724] | 69 | # Problem locking file
|
---|
[1175] | 70 | my $mess = "Unable to lock temporary communication file:";
|
---|
| 71 | $mess .= " $full_tmpname";
|
---|
| 72 | print STDERR "$mess\n";
|
---|
| 73 | return;
|
---|
| 74 | }
|
---|
[724] | 75 | }
|
---|
| 76 | else
|
---|
| 77 | {
|
---|
| 78 | my $mess = "Unable to open for writing";
|
---|
| 79 | $mess .= " communication temporary file: $full_tmpname.";
|
---|
| 80 | print STDERR "$mess\n";
|
---|
| 81 | return;
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 |
|
---|
| 86 | sub do_build
|
---|
| 87 | {
|
---|
| 88 | my ($full_dirname,$dirname,$args) = @_;
|
---|
| 89 |
|
---|
| 90 | my $tmpname = $args->{'bc1tmpname'};
|
---|
| 91 | my $full_tmpname = &util::filename_cat($ENV{'GSDLHOME'},"tmp",$tmpname);
|
---|
| 92 | my $cfg_filename = &util::filename_cat($full_dirname,"etc","collect.cfg");
|
---|
| 93 | my $full_importname = &util::filename_cat($full_dirname,"import");
|
---|
| 94 |
|
---|
[1129] | 95 | my $log_filename = &util::filename_cat($ENV{'GSDLHOME'},"etc","$dirname.bld");
|
---|
[724] | 96 | if (!open (LOGOUT, ">$log_filename"))
|
---|
| 97 | {
|
---|
| 98 | my $mess = "Error: Unable to open log file '$log_filename'";
|
---|
[841] | 99 | print LOGOUT "$mess\n";
|
---|
[724] | 100 | communicate_single_line($full_tmpname,$mess);
|
---|
| 101 | return "failure";
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | my $copy_data = $args->{'bc1copydata'};
|
---|
| 105 | my $do_import = $args->{'bc1doimport'};
|
---|
| 106 | my $do_build = $args->{'bc1dobuild'};
|
---|
| 107 |
|
---|
| 108 | my $building_cfg_text
|
---|
| 109 | = &cfgread::read_cfg_file($cfg_filename,undef,undef,"^building");
|
---|
| 110 | my $copy_dir = $building_cfg_text->{'building'}->{'copydir'};
|
---|
| 111 | my $input_dir = $building_cfg_text->{'building'}->{'inputdir'};
|
---|
| 112 |
|
---|
| 113 | if ($copy_data eq "true")
|
---|
| 114 | {
|
---|
| 115 | if ($copy_dir =~ m/^yes$/i)
|
---|
| 116 | {
|
---|
[1175] | 117 | my $download_cmd = "perl " . &util::filename_cat ($ENV{'GSDLHOME'}, "bin", "script");
|
---|
[724] | 118 | my $file_or_url = $building_cfg_text->{'building'}->{'fileorurl'};
|
---|
| 119 |
|
---|
| 120 | if ($file_or_url =~ m/^url$/i)
|
---|
| 121 | {
|
---|
| 122 | # run urlcopy.pl to download files
|
---|
[1175] | 123 | $download_cmd = " urlcopy.pl ";
|
---|
[724] | 124 | my @urls = split("\n",$input_dir);
|
---|
| 125 | my $u;
|
---|
| 126 | foreach $u (@urls)
|
---|
| 127 | {
|
---|
| 128 | $u =~ s/^\s+//;
|
---|
| 129 | $u =~ s/\s+$//;
|
---|
| 130 | $download_cmd .= "\"$u\" ";
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 | else
|
---|
| 134 | {
|
---|
| 135 | # run filecopy.pl to download files
|
---|
| 136 | $input_dir =~ s/^\s+//;
|
---|
| 137 | $input_dir =~ s/\s+$//;
|
---|
[1175] | 138 | $download_cmd = " filecopy.pl $input_dir";
|
---|
[724] | 139 | }
|
---|
| 140 |
|
---|
| 141 | $download_cmd .= " $dirname";
|
---|
| 142 |
|
---|
| 143 | # execute download command and monitor the output generated
|
---|
| 144 | if(!open(DOWNLOADOUT,"$download_cmd 2>&1 |"))
|
---|
| 145 | {
|
---|
| 146 | my $mess = "Error: Unable to open pipe to command '$download_cmd'";
|
---|
[841] | 147 | print LOGOUT "$mess\n";
|
---|
[724] | 148 | communicate_single_line($full_tmpname,$mess);
|
---|
| 149 | close(LOGOUT);
|
---|
| 150 | return "failure";
|
---|
| 151 | }
|
---|
| 152 | while (defined($line=<DOWNLOADOUT>))
|
---|
| 153 | {
|
---|
| 154 | print LOGOUT $line;
|
---|
| 155 | chop $line;
|
---|
| 156 | if ($line =~ m/^Error:/)
|
---|
| 157 | {
|
---|
[841] | 158 | print LOGOUT "$line";
|
---|
[724] | 159 | communicate_single_line($full_tmpname,"$line");
|
---|
| 160 | close(DOWNLOADOUT);
|
---|
| 161 | close(LOGOUT);
|
---|
| 162 | return "failure";
|
---|
| 163 | }
|
---|
| 164 | communicate_single_line($full_tmpname,"Copying data ...<br>$line");
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | close(DOWNLOADOUT);
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[1181] | 171 | if ((defined $copy_dir) && ($copy_dir =~ /^no$/i))
|
---|
[724] | 172 | {
|
---|
| 173 | # link it
|
---|
| 174 | my $tail_dir = &File::Basename::basename($input_dir);
|
---|
| 175 | my $sym_dirname = &util::filename_cat($full_importname,$tail_dir);
|
---|
| 176 |
|
---|
[841] | 177 | &util::rm_r($sym_dirname) if (-e $sym_dirname);
|
---|
[724] | 178 |
|
---|
| 179 | if (&util::soft_link($input_dir,$sym_dirname))
|
---|
| 180 | {
|
---|
| 181 | my $mess = "Source data linked to $input_dir";
|
---|
| 182 | communicate_single_line($full_tmpname,$mess);
|
---|
| 183 | }
|
---|
| 184 | else
|
---|
| 185 | {
|
---|
| 186 | my $mess = "Error: unable to make symbolic link to source:";
|
---|
| 187 | $mess .= " $input_dir";
|
---|
[841] | 188 | print LOGOUT "$mess\n";
|
---|
[724] | 189 | communicate_single_line($full_tmpname,$mess);
|
---|
| 190 | close(LOGOUT);
|
---|
| 191 | return "failure";
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | if ($do_import eq "true")
|
---|
| 196 | {
|
---|
| 197 | # Import operation
|
---|
[1145] | 198 | my $import_cmd = "perl ";
|
---|
| 199 | $import_cmd .= &util::filename_cat($ENV{'GSDLHOME'}, "bin", "script", "import.pl");
|
---|
| 200 | $import_cmd .= " -removeold $dirname";
|
---|
[724] | 201 |
|
---|
| 202 | if(!open(IMPORTOUT,"$import_cmd 2>&1 |"))
|
---|
| 203 | {
|
---|
| 204 | my $mess = "Error: Unable to open pipe to command '$import_cmd'";
|
---|
[841] | 205 | print LOGOUT "$mess\n";
|
---|
[724] | 206 | communicate_single_line($full_tmpname,$mess);
|
---|
| 207 | close(LOGOUT);
|
---|
| 208 | return "failure";
|
---|
| 209 | }
|
---|
| 210 | while (defined($line=<IMPORTOUT>))
|
---|
| 211 | {
|
---|
| 212 | print LOGOUT $line;
|
---|
| 213 | chop $line;
|
---|
| 214 | if ($line =~ m/^Error:/)
|
---|
| 215 | {
|
---|
[841] | 216 | print LOGOUT "$line";
|
---|
[724] | 217 | communicate_single_line($full_tmpname,"$line");
|
---|
| 218 | close(IMPORTOUT);
|
---|
| 219 | close(LOGOUT);
|
---|
| 220 | return "failure";
|
---|
| 221 | }
|
---|
| 222 | communicate_single_line($full_tmpname,"Caching data ...<br>$line");
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | close(IMPORTOUT);
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | if ($do_build eq "true")
|
---|
| 229 | {
|
---|
[841] | 230 | my $full_archivename = &util::filename_cat($full_dirname,"archives");
|
---|
| 231 |
|
---|
[724] | 232 | # Build operation
|
---|
[1145] | 233 | my $build_cmd = "perl " .
|
---|
| 234 | &util::filename_cat($ENV{'GSDLHOME'}, "bin", "script", "buildcol.pl");
|
---|
[841] | 235 | if (($do_import eq "true")
|
---|
| 236 | || (($do_import eq "false") && (-e $full_archivename)))
|
---|
[724] | 237 | {
|
---|
[1145] | 238 | $build_cmd .= " $dirname";
|
---|
[724] | 239 | }
|
---|
| 240 | else
|
---|
| 241 | {
|
---|
| 242 | $build_cmd .= " -archivedir $full_importname";
|
---|
| 243 | $build_cmd .= " -cachedir $full_archivename";
|
---|
| 244 | $build_cmd .= " $dirname";
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | if(!open(BUILDOUT,"$build_cmd 2>&1 |"))
|
---|
| 248 | {
|
---|
| 249 | my $mess = "Error: Unable to open pipe to command '$build_cmd'";
|
---|
[841] | 250 | print LOGOUT "$mess\n";
|
---|
[724] | 251 | communicate_single_line($full_tmpname,$mess);
|
---|
| 252 | close(LOGOUT);
|
---|
| 253 | return "failure";
|
---|
| 254 | }
|
---|
| 255 | while (defined($line=<BUILDOUT>))
|
---|
| 256 | {
|
---|
| 257 | print LOGOUT $line;
|
---|
| 258 | chop $line;
|
---|
| 259 | if ($line =~ m/^Error:/)
|
---|
| 260 | {
|
---|
[841] | 261 | print LOGOUT "$line";
|
---|
[724] | 262 | communicate_single_line($full_tmpname,"$line");
|
---|
| 263 | close(BUILDOUT);
|
---|
| 264 | close(LOGOUT);
|
---|
| 265 | return "failure";
|
---|
| 266 | }
|
---|
| 267 | communicate_single_line($full_tmpname,"Creating indexes ...<br>$line");
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | close(BUILDOUT);
|
---|
| 271 |
|
---|
| 272 | # Make collection live
|
---|
| 273 | #--
|
---|
| 274 | # rm index
|
---|
| 275 | my $full_indexname = &util::filename_cat($full_dirname,"index");
|
---|
| 276 | &util::rm_r($full_indexname);
|
---|
| 277 |
|
---|
| 278 | # move building
|
---|
| 279 | my $full_buildingname = &util::filename_cat($full_dirname,"building");
|
---|
| 280 | &util::mv($full_buildingname,$full_indexname);
|
---|
| 281 |
|
---|
| 282 | my $full_imagesrc = &util::filename_cat($full_dirname,"building_images","imgsrc");
|
---|
| 283 | if (-e $full_imagesrc)
|
---|
| 284 | {
|
---|
| 285 | my $full_imagedst = &util::filename_cat($full_dirname,"index","imgsrc");
|
---|
| 286 | &util::soft_link($full_imagesrc,$full_imagedst);
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | # Recreate 'building' directory ready for next build
|
---|
| 290 | &util::mk_dir($full_buildingname);
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | communicate_single_line($full_tmpname,"Done");
|
---|
| 294 | close(LOGOUT);
|
---|
| 295 | return "success";
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 |
|
---|
| 299 | sub main
|
---|
| 300 | {
|
---|
| 301 | # get arguments
|
---|
| 302 | my $dirname = $args->{'bc1dirname'};
|
---|
| 303 | if (!defined($dirname))
|
---|
| 304 | {
|
---|
| 305 | my $mess = "Directory name for collection missing.";
|
---|
| 306 | print STDERR "$mess\n";
|
---|
| 307 | return;
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | my $tmpname = $args->{'bc1tmpname'};
|
---|
| 311 | if (!defined($tmpname))
|
---|
| 312 | {
|
---|
| 313 | my $mess = "Temporary name for building communication missing.";
|
---|
| 314 | print STDERR "$mess\n";
|
---|
| 315 | return;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | my $full_tmpname
|
---|
| 319 | = &util::filename_cat($ENV{'GSDLHOME'},"tmp",$tmpname);
|
---|
| 320 |
|
---|
| 321 | communicate_single_line($full_tmpname,"Preparing to build.");
|
---|
| 322 |
|
---|
| 323 | # Put lock on config file before any building is done as safe
|
---|
| 324 | # guard against any concurrent operations on this collection.
|
---|
| 325 | #--
|
---|
| 326 |
|
---|
| 327 | my $full_dirname
|
---|
| 328 | = &util::filename_cat($ENV{'GSDLHOME'},"collect",$dirname);
|
---|
| 329 | my $cfg_filename
|
---|
| 330 | = &util::filename_cat($full_dirname,"etc","collect.cfg");
|
---|
| 331 |
|
---|
[1175] | 332 | # do requested stages for building
|
---|
| 333 | my $result = do_build($full_dirname,$dirname,$args);
|
---|
| 334 | return if ($result ne "success");
|
---|
[724] | 335 |
|
---|
[1175] | 336 | my $mess_url = "$args->{'httpbuild'}&bca=mess&bc1dirname=$dirname";
|
---|
| 337 | print "Location: $mess_url&head=_headdone_&mess=_messdonebuildcol_\n\n";
|
---|
| 338 | print "done\n"; # in tmp file
|
---|
[724] | 339 | }
|
---|
| 340 |
|
---|
| 341 | &main();
|
---|