source: trunk/gsdl/cgi-bin/webpage_buildcol.pl@ 1305

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

some more tidying up of end-user collection building

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 8.6 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
121 # run filecopy.pl to download files
122 $input_dir =~ s/^\s+//;
123 $input_dir =~ s/\s+$//;
124 $download_cmd = " filecopy.pl \"$input_dir\" \"$dirname\"";
125
126 # execute download command and monitor the output generated
127 if(!open(DOWNLOADOUT,"$download_cmd 2>&1 |"))
128 {
129 my $mess = "Error: Unable to open pipe to command '$download_cmd'";
130 print LOGOUT "$mess\n";
131 communicate_single_line($full_tmpname,$mess);
132 close(LOGOUT);
133 return "failure";
134 }
135 while (defined($line=<DOWNLOADOUT>))
136 {
137 print LOGOUT $line;
138 chop $line;
139 if ($line =~ m/^Error:/)
140 {
141 print LOGOUT "$line";
142 communicate_single_line($full_tmpname,"$line");
143 close(DOWNLOADOUT);
144 close(LOGOUT);
145 return "failure";
146 }
147 communicate_single_line($full_tmpname,"Copying data ...<br>$line");
148 }
149
150 close(DOWNLOADOUT);
151 }
152 }
153
154 if ((defined $copy_dir) && ($copy_dir =~ /^no$/i))
155 {
156 # link it
157 my $tail_dir = &File::Basename::basename($input_dir);
158 my $sym_dirname = &util::filename_cat($full_importname,$tail_dir);
159
160 &util::rm_r($sym_dirname) if (-e $sym_dirname);
161
162 if (&util::soft_link($input_dir,$sym_dirname))
163 {
164 my $mess = "Source data linked to $input_dir";
165 communicate_single_line($full_tmpname,$mess);
166 }
167 else
168 {
169 my $mess = "Error: unable to make symbolic link to source:";
170 $mess .= " $input_dir";
171 print LOGOUT "$mess\n";
172 communicate_single_line($full_tmpname,$mess);
173 close(LOGOUT);
174 return "failure";
175 }
176 }
177
178 if ($do_import eq "true")
179 {
180 # Import operation
181 my $import_cmd = "perl ";
182 $import_cmd .= &util::filename_cat($ENV{'GSDLHOME'}, "bin", "script", "import.pl");
183 $import_cmd .= " -removeold $dirname";
184
185 if(!open(IMPORTOUT,"$import_cmd 2>&1 |"))
186 {
187 my $mess = "Error: Unable to open pipe to command '$import_cmd'";
188 print LOGOUT "$mess\n";
189 communicate_single_line($full_tmpname,$mess);
190 close(LOGOUT);
191 return "failure";
192 }
193 while (defined($line=<IMPORTOUT>))
194 {
195 print LOGOUT $line;
196 chop $line;
197 if ($line =~ m/^Error:/)
198 {
199 print LOGOUT "$line";
200 communicate_single_line($full_tmpname,"$line");
201 close(IMPORTOUT);
202 close(LOGOUT);
203 return "failure";
204 }
205 communicate_single_line($full_tmpname,"Caching data ...<br>$line");
206 }
207
208 close(IMPORTOUT);
209 }
210
211 if ($do_build eq "true")
212 {
213 my $full_archivename = &util::filename_cat($full_dirname,"archives");
214
215 # Build operation
216 my $build_cmd = "perl " .
217 &util::filename_cat($ENV{'GSDLHOME'}, "bin", "script", "buildcol.pl");
218 if (($do_import eq "true")
219 || (($do_import eq "false") && (-e $full_archivename)))
220 {
221 $build_cmd .= " $dirname";
222 }
223 else
224 {
225 $build_cmd .= " -archivedir $full_importname";
226 $build_cmd .= " -cachedir $full_archivename";
227 $build_cmd .= " $dirname";
228 }
229
230 if(!open(BUILDOUT,"$build_cmd 2>&1 |"))
231 {
232 my $mess = "Error: Unable to open pipe to command '$build_cmd'";
233 print LOGOUT "$mess\n";
234 communicate_single_line($full_tmpname,$mess);
235 close(LOGOUT);
236 return "failure";
237 }
238 while (defined($line=<BUILDOUT>))
239 {
240 print LOGOUT $line;
241 chop $line;
242 if ($line =~ m/^Error:/)
243 {
244 print LOGOUT "$line";
245 communicate_single_line($full_tmpname,"$line");
246 close(BUILDOUT);
247 close(LOGOUT);
248 return "failure";
249 }
250 communicate_single_line($full_tmpname,"Creating indexes ...<br>$line");
251 }
252
253 close(BUILDOUT);
254
255 # Make collection live
256 #--
257 # rm index
258 my $full_indexname = &util::filename_cat($full_dirname,"index");
259 &util::rm_r($full_indexname);
260
261 # move building
262 my $full_buildingname = &util::filename_cat($full_dirname,"building");
263 &util::mv($full_buildingname,$full_indexname);
264
265 my $full_imagesrc = &util::filename_cat($full_dirname,"building_images","imgsrc");
266 if (-e $full_imagesrc)
267 {
268 my $full_imagedst = &util::filename_cat($full_dirname,"index","imgsrc");
269 &util::soft_link($full_imagesrc,$full_imagedst);
270 }
271
272 # Recreate 'building' directory ready for next build
273 &util::mk_dir($full_buildingname);
274 }
275
276 communicate_single_line($full_tmpname,"Done");
277 close(LOGOUT);
278 return "success";
279}
280
281
282sub main
283{
284 # get arguments
285 my $dirname = $args->{'bc1dirname'};
286 if (!defined($dirname))
287 {
288 my $mess = "Directory name for collection missing.";
289 print STDERR "$mess\n";
290 return;
291 }
292
293 my $tmpname = $args->{'bc1tmpname'};
294 if (!defined($tmpname))
295 {
296 my $mess = "Temporary name for building communication missing.";
297 print STDERR "$mess\n";
298 return;
299 }
300
301 my $full_tmpname
302 = &util::filename_cat($ENV{'GSDLHOME'},"tmp",$tmpname);
303
304 communicate_single_line($full_tmpname,"Preparing to build.");
305
306 # Put lock on config file before any building is done as safe
307 # guard against any concurrent operations on this collection.
308 #--
309
310 my $full_dirname
311 = &util::filename_cat($ENV{'GSDLHOME'},"collect",$dirname);
312 my $cfg_filename
313 = &util::filename_cat($full_dirname,"etc","collect.cfg");
314
315 # do requested stages for building
316 my $result = do_build($full_dirname,$dirname,$args);
317 return if ($result ne "success");
318
319 my $mess_url = "$args->{'httpbuild'}&bca=mess&bc1dirname=$dirname";
320 print "Location: $mess_url&head=_headdone_&mess=_messdonebuildcol_\n\n";
321 print "done\n"; # in tmp file
322}
323
324&main();
Note: See TracBrowser for help on using the repository browser.