source: trunk/gsdl/bin/script/build@ 1452

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

* empty log message *

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 8.2 KB
Line 
1#!/usr/bin/perl
2
3# This perl script may be called directly or by running build.bat on
4# windows (build.bat is in bin\windows)
5
6use FileHandle;
7
8BEGIN {
9
10 die "GSDLHOME not set - did you remember to source setup.bash (unix) or " .
11 "run setup.bat (windows)?\n" unless defined $ENV{'GSDLHOME'};
12 die "GSDLOS not set - did you remember to source setup.bash (unix) or " .
13 "run setup.bat (windows)?\n" unless defined $ENV{'GSDLOS'};
14 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
15
16 STDOUT->autoflush(1);
17 STDERR->autoflush(1);
18}
19
20use parsargv;
21use util;
22
23if (!parsargv::parse(\@ARGV,
24 'append', \$append,
25 'remove_archives', \$remove_archives,
26 'remove_import', \$remove_import,
27 'buildtype/^(build|import)$/import', \$buildtype,
28 'maxdocs/^\-?\d+/-1', \$maxdocs,
29 'download/.+', \@download,
30 'out/.*/STDERR', \$out)) {
31
32 &print_usage();
33 die "\n";
34}
35
36my ($collection) = @ARGV;
37
38if (!defined $collection || $collection !~ /\w/) {
39 print STDERR "You must specify a collection to build\n";
40 &print_usage();
41 die "\n";
42}
43
44if ($maxdocs == -1) {
45 $maxdocs = "";
46} else {
47 $maxdocs = "-maxdocs $maxdocs";
48}
49
50my $collectdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $collection);
51my $importdir = &util::filename_cat ($collectdir, "import");
52my $archivedir = &util::filename_cat ($collectdir, "archives");
53my $buildingdir = &util::filename_cat ($collectdir, "building");
54my $indexdir = &util::filename_cat ($collectdir, "index");
55my $bindir = &util::filename_cat ($ENV{'GSDLHOME'}, "bin");
56
57my $use_out = 0;
58my $outfile = $out;
59if ($out !~ /^(STDERR|STDOUT)$/i) {
60 open (OUT, ">$out") || die "Couldn't open output file $out\n";
61 $out = "OUT";
62
63 # delete any existing .final file
64 &util::rm ("$outfile.final") if -e "$outfile.final";
65
66 $use_out = 1;
67}
68$out->autoflush(1);
69
70&main();
71
72close OUT if $use_out;
73
74sub print_usage {
75 print STDERR "\n usage: $0 [options] collection-name\n\n";
76 print STDERR " options:\n";
77 print STDERR " -append Add new files to existing collection\n";
78 print STDERR " -remove_archives Remove archives directory after successfully\n";
79 print STDERR " building the collection.\n";
80 print STDERR " -remove_import Remove import directory after successfully\n";
81 print STDERR " importing the collection.\n";
82 print STDERR " -buildtype build|import If 'build' attempt to build directly\n";
83 print STDERR " from archives directory (bypassing import\n";
84 print STDERR " stage). Defaults to 'import'\n";
85 print STDERR " -maxdocs number Maximum number of documents to build\n";
86 print STDERR " -download directory Directory (or file) to get import documents from.\n";
87 print STDERR " There may be multiple download directories and they\n";
88 print STDERR " may be of type http://, ftp://, or file://\n.";
89 print STDERR " Note that any existing import directory will be\n";
90 print STDERR " deleted to make way for the downloaded data if\n";
91 print STDERR " a -download option is supplied\n";
92 print STDERR " -out Filename or handle to print output status to.\n";
93 print STDERR " The default is STDERR\n\n";
94}
95
96sub main {
97
98 # do the download thing if we have any -download options
99 if (scalar (@download)) {
100 # remove any existing import data
101 if (&has_content ($importdir)) {
102 print $out "build: WARNING: removing contents of $importdir\n";
103 &util::rm_r ($importdir);
104 }
105
106 foreach $download_dir (@download) {
107
108 if ($download_dir =~ /^http:\/\//) {
109 # http download
110
111 } elsif ($download_dir =~ /^ftp:\/\//) {
112 # ftp download
113
114 } else {
115 # we assume anything not beginning with http:// or ftp://
116 # is a file or directory on the local file system.
117 $download_dir =~ s/^file:\/\///;
118
119 if (-e $download_dir) {
120 # copy download_dir and all it contains to the import directory
121 my $download_cmd = "perl " . &util::filename_cat ($bindir, "script", "filecopy.pl");
122 $download_cmd .= " -out \"$outfile.download\"" if $use_out;
123 $download_cmd .= " \"" . $download_dir . "\" " . $collection;
124 system ($download_cmd);
125 # if using output directory append the file download output to it
126 &append_file ($out, "$outfile.download");
127 } else {
128 print $out "WARNING: $download_dir does not exist\n";
129 }
130 }
131 }
132 }
133
134 if (-e &util::filename_cat ($archivedir, "archives.inf")) {
135 if (&has_content ($importdir)) {
136 if ($buildtype eq "build") {
137 &gsdl_build();
138 } else {
139 &gsdl_import();
140 &gsdl_build();
141 }
142 } else {
143 # there are archives but no import, build directly from archives
144 print $out "build: no import material was found, building directly\n";
145 print $out " from archives\n";
146 &gsdl_build();
147 }
148 } else {
149 if (&has_content ($importdir)) {
150 if ($buildtype eq "build") {
151 print $out "build: can't build directly from archives as no\n";
152 print $out " imported archives exist (did you forget to\n";
153 print $out " move the contents of $collection/import to\n";
154 print $out " collection/archives?)\n";
155 }
156 &gsdl_import();
157 &gsdl_build();
158 } else {
159 # no import or archives
160 print $out "build: ERROR: The $collection collection has no import\n";
161 print $out " or archives data. Try downloading an unbuilt version\n";
162 print $out " of the collection from www.nzdl.org\n";
163 &final_out (1) if $use_out;
164 die "\n";
165 }
166 }
167 &final_out (0) if $use_out;
168}
169
170sub gsdl_import {
171
172 print $out "importing the $collection collection\n\n";
173
174 my $import_cmd = "perl " . &util::filename_cat ($bindir, "script", "import.pl");
175 $import_cmd .= " -out \"$outfile.import\"" if $use_out;
176 $import_cmd .= " -removeold" unless $append;
177 $import_cmd .= " $maxdocs $collection";
178 system ($import_cmd);
179 # if using output directory append the import output to it
180 &append_file ($out, "$outfile.import");
181
182 if (-e &util::filename_cat ($archivedir, "archives.inf")) {
183 print $out "$collection collection imported successfully\n\n";
184 if ($remove_import) {
185 print $out "removing import directory ($importdir)\n";
186 &util::rm_r ($importdir);
187 }
188 } else {
189 &final_out (2) if $use_out;
190 die "\nimport.pl failed\n";
191 }
192}
193
194sub gsdl_build {
195
196 print $out "building the $collection collection\n\n";
197
198 my $build_cmd = "perl " . &util::filename_cat ($bindir, "script", "buildcol.pl");
199 $build_cmd .= " -out \"$outfile.build\"" if $use_out;
200 $build_cmd .= " $maxdocs $collection";
201 system ($build_cmd);
202 # if using output directory append the buildcol output to it
203 &append_file ($out, "$outfile.build");
204
205 if (-e &util::filename_cat ($buildingdir, "text", "$collection.ldb") ||
206 -e &util::filename_cat ($buildingdir, "text", "$collection.bdb")) {
207 print $out "$collection collection built successfully\n\n";
208 if ($remove_archives) {
209 print $out "removing archives directory ($archivedir)\n";
210 &util::rm_r ($archivedir);
211 }
212 } else {
213 &final_out (2) if $use_out;
214 die "\nbuildcol.pl failed\n";
215 }
216
217 # replace old indexes with new ones
218 if (&has_content ($indexdir)) {
219 print $out "removing old indexes\n";
220 &util::rm_r ($indexdir);
221 }
222 rmdir ($indexdir) if -d $indexdir;
223 rename ($buildingdir, $indexdir);
224}
225
226sub has_content {
227 my ($dir) = @_;
228
229 if (!-d $dir) {return 0;}
230
231 opendir (DIR, $dir) || return 0;
232 my @files = readdir DIR;
233 close DIR;
234
235 foreach my $file (@files) {
236 if ($file !~ /^\.{1,2}$/) {
237 return 1;
238 }
239 }
240 return 0;
241}
242
243sub append_file {
244 my ($handle, $file) = @_;
245
246 open (FILE, $file) || return;
247 undef $/;
248 print $handle <FILE>;
249 $/ = "\n";
250 close FILE;
251 &util::rm ($file);
252}
253
254# creates a file called $outfile.final (should only be called if -out option
255# is used and isn't STDERR or STDOUT) and writes an output code to it.
256# An output code of 0 specifies that there was no error
257sub final_out {
258 my ($exit_code) = @_;
259
260 if (open (FINAL, ">$outfile.final")) {
261 print FINAL $exit_code;
262 close FINAL;
263 }
264}
Note: See TracBrowser for help on using the repository browser.