source: branches/New_Config_Format-branch/gsdl/cgi-bin/webpage_buildcol.pl@ 1279

Last change on this file since 1279 was 1279, checked in by sjboddie, 24 years ago

merged changes to trunk into New_Config_Format branch

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