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