source: main/tags/2.40/gsdl/bin/script/import.pl@ 21085

Last change on this file since 21085 was 4776, checked in by mdewsnip, 21 years ago

Now uses the PrintUsage module to automatically generate usage text from the $options and $arguments structures.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 16.4 KB
Line 
1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# import.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
29# This program will import a number of files into a particular collection
30
31package import;
32
33BEGIN {
34 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
35 die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
36 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
37 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/plugins");
38 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/classify");
39}
40
41use arcinfo;
42use colcfg;
43use plugin;
44use docprint;
45use util;
46use parsargv;
47use FileHandle;
48use printusage;
49
50my $oidtype_list =
51 [ { 'name' => "hash",
52 'desc' => "Hashes the contents of the file. Document identifier will be the same every time the collection is imported." },
53 { 'name' => "incremental",
54 'desc' => "A simple document count that is significantly faster than \"hash\". It is not guaranteed to always assign the same identifier to a given document though and does not allow further documents to be added to existing xml archives." } ];
55
56my $arguments =
57 [ { 'name' => "archivedir",
58 'desc' => "Where the converted material ends up.",
59 'type' => "string",
60 'reqd' => "no" },
61 { 'name' => "collectdir",
62 'desc' => "Collection directory.",
63 'type' => "string",
64 'deft' => &util::filename_cat ($ENV{'GSDLHOME'}, "collect"),
65 'reqd' => "no" },
66 { 'name' => "debug",
67 'desc' => "Print imported text to STDOUT.",
68 'type' => "flag",
69 'reqd' => "no" },
70 { 'name' => "faillog",
71 'desc' => "Fail log filename. This log receives the filenames of any files which fail to be processed.",
72 'type' => "string",
73 'deft' => &util::filename_cat("<collectdir>", "colname", "etc", "fail.log"),
74 'reqd' => "no" },
75 { 'name' => "groupsize",
76 'desc' => "Number of import documents to group into one XML file.",
77 'type' => "int",
78 'deft' => "1",
79 'reqd' => "no" },
80 { 'name' => "gzip",
81 'desc' => "Use gzip to compress resulting xml documents (don't forget to include ZIPPlug in your plugin list when building from compressed documents).",
82 'type' => "flag",
83 'reqd' => "no" },
84 { 'name' => "importdir",
85 'desc' => "Where the original material lives.",
86 'type' => "string",
87 'reqd' => "no" },
88 { 'name' => "keepold",
89 'desc' => "Will not destroy the current contents of the archives directory (the default).",
90 'type' => "flag",
91 'reqd' => "no" },
92 { 'name' => "maxdocs",
93 'desc' => "Maximum number of documents to import.",
94 'type' => "int",
95 'reqd' => "no" },
96 { 'name' => "OIDtype",
97 'desc' => "The method to use when generating unique identifiers for each document.",
98 'type' => "enum",
99 'list' => $oidtype_list,
100 'deft' => "hash",
101 'reqd' => "no" },
102 { 'name' => "out",
103 'desc' => "Filename or handle to print output status to.",
104 'type' => "string",
105 'deft' => "STDERR",
106 'reqd' => "no" },
107 { 'name' => "removeold",
108 'desc' => "Will remove the old contents of the archives directory -- use with care.",
109 'type' => "flag",
110 'reqd' => "no" },
111 { 'name' => "sortmeta",
112 'desc' => "Sort documents alphabetically by metadata for building. This will be disabled if groupsize > 1.",
113 'type' => "string",
114 'reqd' => "no" },
115 { 'name' => "statsfile",
116 'desc' => "Filename or handle to print import statistics to.",
117 'type' => "string",
118 'deft' => "STDERR",
119 'reqd' => "no" },
120 { 'name' => "verbosity",
121 'desc' => "0=none, 3=lots",
122 'type' => "int",
123 'deft' => "2",
124 'reqd' => "no" } ];
125
126my $options = { 'name' => "import.pl",
127 'desc' => "PERL script used to import files into a GML format ready for building.",
128 'args' => $arguments };
129
130
131sub print_xml_usage
132{
133 &PrintUsage::print_xml_header();
134
135 print STDERR "<Info>\n";
136 print STDERR " <Name>$options->{'name'}</Name>\n";
137 print STDERR " <Desc>$options->{'desc'}</Desc>\n";
138 print STDERR " <Arguments>\n";
139 if (defined($options->{'args'})) {
140 &PrintUsage::print_options_xml($options->{'args'});
141 }
142 print STDERR " </Arguments>\n";
143 print STDERR "</Info>\n";
144}
145
146
147sub print_txt_usage
148{
149 local $programname = $options->{'name'};
150 local $programargs = $options->{'args'};
151
152 # Find the length of the longest option string
153 local $descoffset = 0;
154 if (defined($programargs)) {
155 $descoffset = &PrintUsage::find_longest_option_string($programargs);
156 }
157
158 # Produce the usage information using the data structure above
159 print STDERR " usage: $programname [options] collection-name\n\n";
160
161 # Display the program options, if there are some
162 if (defined($programargs)) {
163 # Calculate the column offset of the option descriptions
164 local $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
165
166 print STDERR " options:\n";
167
168 # Display the program options
169 &PrintUsage::print_options_txt($programargs, $optiondescoffset);
170 }
171}
172
173
174# sub print_usage {
175# print STDOUT "\n";
176# print STDOUT "import.pl: Converts documents in collections -importdir directory into\n";
177# print STDOUT " xml documents which are written to the -archivedir directory.\n\n";
178# print STDOUT " usage: $0 [options] collection-name\n\n";
179# print STDOUT " options:\n";
180# print STDOUT " -verbosity number 0=none, 3=lots\n";
181# print STDOUT " -importdir directory Where the original material lives\n";
182# print STDOUT " -archivedir directory Where the converted material ends up\n";
183# print STDOUT " -keepold Will not destroy the current contents of the\n";
184# print STDOUT " archives directory (the default)\n";
185# print STDOUT " -removeold Will remove the old contents of the archives\n";
186# print STDOUT " directory -- use with care\n";
187# print STDOUT " -gzip Use gzip to compress resulting xml documents\n";
188# print STDOUT " (don't forget to include ZIPPlug in your plugin\n";
189# print STDOUT " list when building from compressed documents)\n";
190# print STDOUT " -maxdocs number Maximum number of documents to import\n";
191# print STDOUT " -groupsize number Number of import documents to group into one XML file\n";
192# print STDOUT " -OIDtype hash|incremental The method to use when generating unique\n";
193# print STDOUT " identifiers for each document. \"hash\" (the\n";
194# print STDOUT " default) hashes the contents of the file and so\n";
195# print STDOUT " will be the same every time the collection is\n";
196# print STDOUT " imported. \"incremental\" is a simple document\n";
197# print STDOUT " count and so will be significantly faster than\n";
198# print STDOUT " \"hash\". It is not guaranteed to always assign\n";
199# print STDOUT " the same identifier to a given document though\n";
200# print STDOUT " and does not allow further documents to be added\n";
201# print STDOUT " to existing xml archives\n";
202# print STDOUT " -sortmeta metadata Sort documents alphabetically by metadata for\n";
203# print STDOUT " building. This will be disabled if groupsize > 1\n";
204# print STDOUT " -debug Print imported text to STDOUT\n";
205# print STDOUT " -collectdir directory Collection directory (defaults to " .
206# &util::filename_cat ($ENV{'GSDLHOME'}, "collect") . ")\n";
207# print STDOUT " -out name Filename or handle to print output status to.\n";
208# print STDOUT " The default is STDERR\n";
209# print STDOUT " -statsfile name Filename or handle to print import statistics to.\n";
210# print STDOUT " The default is STDERR\n";
211# print STDOUT " -faillog name Fail log filename. This log receives the filenames\n";
212# print STDOUT " of any files which fail to be processed (defaults\n";
213# print STDOUT " to " .
214# &util::filename_cat("<collectdir>", "colname", "etc", "fail.log") . ")\n";
215# print STDOUT " [Type \"perl -S import.pl | more\" if this help text scrolled off your screen]";
216# print STDOUT "\n" unless $ENV{'GSDLOS'} =~ /^windows$/i;
217# }
218
219&main();
220
221sub main {
222 my ($verbosity, $importdir, $archivedir, $keepold,
223 $removeold, $gzip, $groupsize, $OIDtype, $debug,
224 $maxdocs, $collection, $configfilename, $collectcfg,
225 $pluginfo, $sortmeta, $archive_info_filename, $statsfile,
226 $archive_info, $processor, $out, $faillog, $collectdir);
227
228 # ***** 11-04-03 - John Thompson *****
229 my $xml = 0;
230 # ************************************
231
232 # note that no defaults are passed for most options as they're set
233 # later (after we check the collect.cfg file)
234 if (!parsargv::parse(\@ARGV,
235 'verbosity/\d+/', \$verbosity,
236 'importdir/.*/', \$importdir,
237 'archivedir/.*/', \$archivedir,
238 'keepold', \$keepold,
239 'removeold', \$removeold,
240 'gzip', \$gzip,
241 'groupsize/\d+/1', \$groupsize,
242 'OIDtype/^(hash|incremental)$/', \$OIDtype,
243 'sortmeta/.*/', \$sortmeta,
244 'debug', \$debug,
245 'maxdocs/^\-?\d+/', \$maxdocs,
246 'collectdir/.*/', \$collectdir,
247 'out/.*/STDERR', \$out,
248 'statsfile/.*/STDERR', \$statsfile,
249 'faillog/.*/', \$faillog,
250 q^xml^, \$xml)) {
251 &print_txt_usage();
252 die "\n";
253 }
254
255 if ($xml) {
256 &print_xml_usage();
257 die "\n";
258 }
259
260 my $close_out = 0;
261 if ($out !~ /^(STDERR|STDOUT)$/i) {
262 open (OUT, ">$out") || die "Couldn't open output file $out\n";
263 $out = 'import::OUT';
264 $close_out = 1;
265 }
266 $out->autoflush(1);
267
268 # set removeold to false if it has been defined
269 $removeold = 0 if ($keepold);
270
271 # get and check the collection name
272 if (($collection = &util::use_collection(@ARGV, $collectdir)) eq "") {
273 &print_txt_usage();
274 die "\n";
275 }
276
277 if ($faillog eq "") {
278 $faillog = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "etc", "fail.log");
279 }
280 open (FAILLOG, ">$faillog") || die "Couldn't open fail log $faillog\n";
281 my $faillogname = $faillog;
282 $faillog = 'import::FAILLOG';
283 $faillog->autoflush(1);
284
285 # check sortmeta
286 $sortmeta = undef unless defined $sortmeta && $sortmeta =~ /\S/;
287 if (defined $sortmeta && $groupsize > 1) {
288 print $out "WARNING: import.pl cannot sort documents when groupsize > 1\n";
289 print $out " sortmeta option will be ignored\n\n";
290 $sortmeta = undef;
291 }
292
293 # dynamically load 'docsave' module so it can pick up on a collection
294 # specific docsave.pm is specified.
295
296 unshift (@INC, "$ENV{'GSDLCOLLECTDIR'}/perllib");
297 require docsave;
298
299
300 # get the list of plugins for this collection and set any options that
301 # were specified in the collect.cfg (all import.pl options except
302 # -collectdir, -out and -faillog may be specified in the collect.cfg (these
303 # options must be known before we read the collect.cfg))
304 my $plugins = [];
305 $configfilename = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "etc", "collect.cfg");
306 if (-e $configfilename) {
307 $collectcfg = &colcfg::read_collect_cfg ($configfilename);
308 if (defined $collectcfg->{'plugin'}) {
309 $plugins = $collectcfg->{'plugin'};
310 }
311
312 if ($verbosity !~ /\d+/) {
313 if (defined $collectcfg->{'verbosity'} && $collectcfg->{'verbosity'} =~ /\d+/) {
314 $verbosity = $collectcfg->{'verbosity'};
315 } else {
316 $verbosity = 2; # the default
317 }
318 }
319 if (defined $collectcfg->{'importdir'} && $importdir eq "") {
320 $importdir = $collectcfg->{'importdir'};
321 }
322 if (defined $collectcfg->{'archivedir'} && $archivedir eq "") {
323 $archivedir = $collectcfg->{'archivedir'};
324 }
325 if (defined $collectcfg->{'removeold'}) {
326 if ($collectcfg->{'removeold'} =~ /^true$/i && !$keepold) {
327 $removeold = 1;
328 }
329 if ($collectcfg->{'removeold'} =~ /^false$/i && !$removeold) {
330 $removeold = 0;
331 }
332 }
333 if (defined $collectcfg->{'keepold'}) {
334 if ($collectcfg->{'keepold'} =~ /^false$/i && !$keepold) {
335 $removeold = 1;
336 }
337 }
338 if (defined $collectcfg->{'gzip'} && !$gzip) {
339 if ($collectcfg->{'gzip'} =~ /^true$/i) {
340 $gzip = 1;
341 }
342 }
343 if ($maxdocs !~ /\-?\d+/) {
344 if (defined $collectcfg->{'maxdocs'} && $collectcfg->{'maxdocs'} =~ /\-?\d+/) {
345 $maxdocs = $collectcfg->{'maxdocs'};
346 } else {
347 $maxdocs = -1; # the default
348 }
349 }
350 if ($groupsize == 1) {
351 if (defined $collectcfg->{'groupsize'} && $collectcfg->{'groupsize'} =~ /\d+/) {
352 $groupsize = $collectcfg->{'groupsize'};
353 }
354 }
355 if ($OIDtype !~ /^(hash|incremental)$/) {
356 if (defined $collectcfg->{'OIDtype'} && $collectcfg->{'OIDtype'} =~ /^(hash|incremental)$/) {
357 $OIDtype = $collectcfg->{'OIDtype'};
358 } else {
359 $OIDtype = "hash"; # the default
360 }
361 }
362 if (defined $collectcfg->{'sortmeta'} && $sortmeta eq "") {
363 $sortmeta = $collectcfg->{'sortmeta'};
364 }
365 if (defined $collectcfg->{'debug'} && $collectcfg->{'debug'} =~ /^true$/i) {
366 $debug = 1;
367 }
368
369 } else {
370 die "Couldn't find the configuration file $configfilename\n";
371 }
372
373 # fill in the default import and archives directories if none
374 # were supplied, turn all \ into / and remove trailing /
375 $importdir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "import") if $importdir eq "";
376 $importdir =~ s/[\\\/]+/\//g;
377 $importdir =~ s/\/$//;
378 $archivedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "archives") if $archivedir eq "";
379 $archivedir =~ s/[\\\/]+/\//g;
380 $archivedir =~ s/\/$//;
381
382 # load all the plugins
383 $pluginfo = &plugin::load_plugins ($plugins, $verbosity, $out, $faillog);
384 if (scalar(@$pluginfo) == 0) {
385 print $out "No plugins were loaded.\n";
386 die "\n";
387 }
388
389 # remove the old contents of the archives directory if needed
390 if ($removeold && -e $archivedir) {
391 print $out "Removing current contents of the archives directory\n";
392 sleep(3); # just in case...
393 &util::rm_r ($archivedir);
394 }
395
396 # read the archive information file
397 if (!$debug) {
398 $archive_info_filename = &util::filename_cat ($archivedir, "archives.inf");
399 $archive_info = new arcinfo ();
400 $archive_info->load_info ($archive_info_filename);
401
402 # create a docsave object to process the documents
403 $processor = new docsave ($collection, $archive_info, $verbosity, $gzip, $groupsize, $out);
404 $processor->setarchivedir ($archivedir);
405 $processor->set_sortmeta ($sortmeta) if defined $sortmeta;
406 $processor->set_OIDtype ($OIDtype);
407 } else {
408 $processor = new docprint ();
409 }
410
411 &plugin::begin($pluginfo, $importdir, $processor, $maxdocs);
412
413 # process the import directory
414 &plugin::read ($pluginfo, $importdir, "", {}, $processor, $maxdocs);
415
416 &plugin::end($pluginfo, $processor);
417
418 # write out the archive information file
419 if (!$debug) {
420 $processor->close_file_output() if $groupsize > 1;
421 $archive_info->save_info($archive_info_filename);
422 }
423
424 # write out import stats
425 my $close_stats = 0;
426 if ($statsfile !~ /^(STDERR|STDOUT)$/i) {
427 if (open (STATS, ">$statsfile")) {
428 $statsfile = 'import::STATS';
429 $close_stats = 1;
430 } else {
431 print $out "WARNING: couldn't open stats file $statsfile\n";
432 print $out " will print stats to STDERR instead\n";
433 $statsfile = 'STDERR';
434 }
435 }
436
437 print $out "\n";
438 print $out "*********************************************\n";
439 print $out "Import Complete\n";
440 print $out "*********************************************\n";
441
442 &plugin::write_stats($pluginfo, $statsfile, $faillogname);
443 if ($close_stats) {
444 close STATS;
445 }
446
447 close OUT if $close_out;
448 close FAILLOG;
449}
450
Note: See TracBrowser for help on using the repository browser.