[1184] | 1 | #!/usr/bin/perl
|
---|
| 2 |
|
---|
[1198] | 3 | # This perl script may be called directly or by running build.bat on
|
---|
| 4 | # windows (build.bat is in bin\windows)
|
---|
[1184] | 5 |
|
---|
[1438] | 6 | use FileHandle;
|
---|
| 7 |
|
---|
[1184] | 8 | BEGIN {
|
---|
[1198] | 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'};
|
---|
[1184] | 14 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
|
---|
[1438] | 15 |
|
---|
| 16 | STDOUT->autoflush(1);
|
---|
| 17 | STDERR->autoflush(1);
|
---|
[1184] | 18 | }
|
---|
| 19 |
|
---|
| 20 | use parsargv;
|
---|
| 21 | use util;
|
---|
| 22 |
|
---|
[1198] | 23 | if (!parsargv::parse(\@ARGV,
|
---|
[1431] | 24 | 'append', \$append,
|
---|
[1424] | 25 | 'remove_archives', \$remove_archives,
|
---|
| 26 | 'remove_import', \$remove_import,
|
---|
[1198] | 27 | 'buildtype/^(build|import)$/import', \$buildtype,
|
---|
[1424] | 28 | 'maxdocs/^\-?\d+/-1', \$maxdocs,
|
---|
| 29 | 'download/.+', \@download,
|
---|
| 30 | 'out/.*/STDERR', \$out)) {
|
---|
| 31 |
|
---|
[1198] | 32 | &print_usage();
|
---|
| 33 | die "\n";
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | my ($collection) = @ARGV;
|
---|
| 37 |
|
---|
| 38 | if (!defined $collection || $collection !~ /\w/) {
|
---|
| 39 | print STDERR "You must specify a collection to build\n";
|
---|
| 40 | &print_usage();
|
---|
| 41 | die "\n";
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | if ($maxdocs == -1) {
|
---|
| 45 | $maxdocs = "";
|
---|
| 46 | } else {
|
---|
| 47 | $maxdocs = "-maxdocs $maxdocs";
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | my $collectdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $collection);
|
---|
| 51 | my $importdir = &util::filename_cat ($collectdir, "import");
|
---|
| 52 | my $archivedir = &util::filename_cat ($collectdir, "archives");
|
---|
| 53 | my $buildingdir = &util::filename_cat ($collectdir, "building");
|
---|
| 54 | my $indexdir = &util::filename_cat ($collectdir, "index");
|
---|
| 55 | my $bindir = &util::filename_cat ($ENV{'GSDLHOME'}, "bin");
|
---|
| 56 |
|
---|
[1431] | 57 | my $use_out = 0;
|
---|
[1424] | 58 | my $outfile = $out;
|
---|
| 59 | if ($out !~ /^(STDERR|STDOUT)$/i) {
|
---|
| 60 | open (OUT, ">$out") || die "Couldn't open output file $out\n";
|
---|
| 61 | $out = "OUT";
|
---|
[1452] | 62 |
|
---|
| 63 | # delete any existing .final file
|
---|
| 64 | &util::rm ("$outfile.final") if -e "$outfile.final";
|
---|
| 65 |
|
---|
[1431] | 66 | $use_out = 1;
|
---|
[1424] | 67 | }
|
---|
| 68 | $out->autoflush(1);
|
---|
| 69 |
|
---|
[1184] | 70 | &main();
|
---|
| 71 |
|
---|
[1431] | 72 | close OUT if $use_out;
|
---|
[1424] | 73 |
|
---|
[1184] | 74 | sub print_usage {
|
---|
| 75 | print STDERR "\n usage: $0 [options] collection-name\n\n";
|
---|
| 76 | print STDERR " options:\n";
|
---|
[1431] | 77 | print STDERR " -append Add new files to existing collection\n";
|
---|
[1424] | 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";
|
---|
[1184] | 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";
|
---|
[1424] | 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";
|
---|
[1184] | 94 | }
|
---|
| 95 |
|
---|
| 96 | sub main {
|
---|
[1424] | 97 |
|
---|
| 98 | # do the download thing if we have any -download options
|
---|
| 99 | if (scalar (@download)) {
|
---|
| 100 | # remove any existing import data
|
---|
[1431] | 101 | if (&has_content ($importdir)) {
|
---|
| 102 | print $out "build: WARNING: removing contents of $importdir\n";
|
---|
| 103 | &util::rm_r ($importdir);
|
---|
| 104 | }
|
---|
[1184] | 105 |
|
---|
[1424] | 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");
|
---|
[1431] | 122 | $download_cmd .= " -out \"$outfile.download\"" if $use_out;
|
---|
| 123 | $download_cmd .= " \"" . $download_dir . "\" " . $collection;
|
---|
[1424] | 124 | system ($download_cmd);
|
---|
[1431] | 125 | # if using output directory append the file download output to it
|
---|
| 126 | &append_file ($out, "$outfile.download");
|
---|
[1424] | 127 | } else {
|
---|
| 128 | print $out "WARNING: $download_dir does not exist\n";
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[1198] | 134 | if (-e &util::filename_cat ($archivedir, "archives.inf")) {
|
---|
| 135 | if (&has_content ($importdir)) {
|
---|
[1184] | 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
|
---|
[1424] | 144 | print $out "build: no import material was found, building directly\n";
|
---|
| 145 | print $out " from archives\n";
|
---|
[1184] | 146 | &gsdl_build();
|
---|
| 147 | }
|
---|
| 148 | } else {
|
---|
[1198] | 149 | if (&has_content ($importdir)) {
|
---|
[1184] | 150 | if ($buildtype eq "build") {
|
---|
[1424] | 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";
|
---|
[1184] | 155 | }
|
---|
| 156 | &gsdl_import();
|
---|
| 157 | &gsdl_build();
|
---|
| 158 | } else {
|
---|
| 159 | # no import or archives
|
---|
[1424] | 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";
|
---|
[1438] | 163 | &final_out (1) if $use_out;
|
---|
[1184] | 164 | die "\n";
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
[1438] | 167 | &final_out (0) if $use_out;
|
---|
[1184] | 168 | }
|
---|
| 169 |
|
---|
| 170 | sub gsdl_import {
|
---|
| 171 |
|
---|
[1424] | 172 | print $out "importing the $collection collection\n\n";
|
---|
[1198] | 173 |
|
---|
[1431] | 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 |
|
---|
[1198] | 182 | if (-e &util::filename_cat ($archivedir, "archives.inf")) {
|
---|
[1424] | 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 | }
|
---|
[1184] | 188 | } else {
|
---|
[1438] | 189 | &final_out (2) if $use_out;
|
---|
[1184] | 190 | die "\nimport.pl failed\n";
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | sub gsdl_build {
|
---|
| 195 |
|
---|
[1424] | 196 | print $out "building the $collection collection\n\n";
|
---|
[1184] | 197 |
|
---|
[1431] | 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 |
|
---|
[1198] | 205 | if (-e &util::filename_cat ($buildingdir, "text", "$collection.ldb") ||
|
---|
| 206 | -e &util::filename_cat ($buildingdir, "text", "$collection.bdb")) {
|
---|
[1424] | 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 | }
|
---|
[1184] | 212 | } else {
|
---|
[1438] | 213 | &final_out (2) if $use_out;
|
---|
[1184] | 214 | die "\nbuildcol.pl failed\n";
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | # replace old indexes with new ones
|
---|
| 218 | if (&has_content ($indexdir)) {
|
---|
[1424] | 219 | print $out "removing old indexes\n";
|
---|
[1184] | 220 | &util::rm_r ($indexdir);
|
---|
| 221 | }
|
---|
[1277] | 222 | rmdir ($indexdir) if -d $indexdir;
|
---|
| 223 | rename ($buildingdir, $indexdir);
|
---|
[1184] | 224 | }
|
---|
| 225 |
|
---|
| 226 | sub 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 | }
|
---|
[1431] | 242 |
|
---|
| 243 | sub 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 | }
|
---|
[1438] | 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
|
---|
| 257 | sub final_out {
|
---|
| 258 | my ($exit_code) = @_;
|
---|
| 259 |
|
---|
| 260 | if (open (FINAL, ">$outfile.final")) {
|
---|
| 261 | print FINAL $exit_code;
|
---|
| 262 | close FINAL;
|
---|
| 263 | }
|
---|
| 264 | }
|
---|