source: trunk/gsdl/bin/script/downloadinfo.pl@ 12625

Last change on this file since 12625 was 12625, checked in by mdewsnip, 18 years ago

Removed the DTD stuff from the top of the XML output... it's just one more unnecessary thing to maintain.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 6.9 KB
Line 
1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# downloadinfo.pl -- prints out information about a download module
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) 2006 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
29use strict;
30no strict 'subs'; # allow barewords (eg STDERR) as function arguments
31
32BEGIN {
33 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
34 die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
35 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
36 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
37 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/downloaders");
38}
39
40use download;
41use util;
42use gsprintf;
43use printusage;
44use parse2;
45
46my $arguments =
47 [ { 'name' => "collect",
48 'desc' => "{downloadinfo.collect}",
49 'type' => "string",
50 'reqd' => "no" },
51 { 'name' => "xml",
52 'desc' => "{scripts.xml}",
53 'type' => "flag",
54 'reqd' => "no"},
55 { 'name' => "listall",
56 'desc' => "{scripts.listall}",
57 'type' => "flag",
58 'reqd' => "no" },
59 { 'name' => "describeall",
60 'desc' => "{scripts.describeall}",
61 'type' => "flag",
62 'reqd' => "no" },
63 { 'name' => "language",
64 'desc' => "{scripts.language}",
65 'type' => "string",
66 'reqd' => "no" } ];
67
68my $options = { 'name' => "downloadinfo.pl",
69 'desc' => "{downloadinfo.desc}",
70 'args' => $arguments };
71
72sub gsprintf
73{
74 return &gsprintf::gsprintf(@_);
75}
76
77
78sub main {
79
80 my $collect = "";
81 my $xml = 0;
82 my $listall = 0;
83 my $describeall = 0;
84 my ($language, $encoding);
85 my $hashParsingResult = {};
86 my $unparsed_args = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");
87
88 # parse returns -1 on error
89 if ($unparsed_args == -1) {
90 PrintUsage::print_txt_usage($options, "{downloadinfo.params}");
91 die "\n";
92 }
93
94 foreach my $strVariable (keys %$hashParsingResult)
95 {
96 my $value = $hashParsingResult->{$strVariable};
97 # test to make sure the variable name is 'safe'
98 if ($strVariable !~ /^\w+$/) {
99 die "variable name '$strVariable' isn't safe!";
100 }
101 eval "\$$strVariable = \$value";
102 }
103
104 # if language wasn't specified, see if it is set in the environment
105 # (LC_ALL or LANG)
106 if (!$language && ($_=$ENV{'LC_ALL'} or $_=$ENV{'LANG'})) {
107 m/^([^\.]+)\.?(.*)/;
108 $language=$1;
109 $encoding=$2; # might be undef...
110# gsprintf::load_language* thinks "fr" is completely different to "fr_FR"...
111 $language =~ s/_.*$//;
112 }
113
114 # If $language has been set, load the appropriate resource bundle
115 # (Otherwise, the default resource bundle will be loaded automatically)
116 if ($language) {
117 gsprintf::load_language_specific_resource_bundle($language);
118 if ($encoding) {
119 $encoding =~ tr/-/_/;
120 $encoding = lc($encoding);
121 $encoding =~ s/utf_8/utf8/; # special
122 $gsprintf::specialoutputencoding=$encoding;
123 }
124 }
125
126 # If there is not exactly one argument left (download name), then the arguments were wrong
127 # Or if the user specified -h, then we output the usage also
128
129 if((@ARGV && $ARGV[0] =~ /^\-+h/) )
130 {
131 PrintUsage::print_txt_usage($options, "{downloadinfo.params}");
132 die "\n";
133 }
134
135 # If there is not exactly 1 argument left (download name), then the arguments were wrong (apart from if we had listall or describeall set)
136 if ($listall == 0 && $describeall ==0 && $unparsed_args == 0) {
137 gsprintf(STDERR, "{downloadinfo.no_download_name}\n\n");
138 PrintUsage::print_txt_usage($options, "{downloadinfo.params}", 1);
139 die "\n";
140 }
141
142 # we had some arguments that we weren't expecting
143 if ($unparsed_args > 1) {
144 pop(@ARGV); # assume that the last arg is the download name
145 gsprintf(STDERR, "{common.invalid_options}\n\n", join (',', @ARGV));
146 PrintUsage::print_txt_usage($options, "{downloadinfo.params}", 1);
147 die "\n";
148 }
149 my $download_name = shift (@ARGV);
150 if (defined $download_name) {
151 $download_name =~ s/\.pm$//; # allow xxx.pm as the argument
152 }
153
154 if ($collect ne "") {
155 $ENV{'GSDLCOLLECTDIR'} = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $collect);
156 } else {
157 $ENV{'GSDLCOLLECTDIR'} = $ENV{'GSDLHOME'};
158 }
159
160 if ($listall) {
161 my $download_dir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"perllib","downloaders");
162 if (!opendir (INDIR, $download_dir)) {
163 print STDERR "downloadinfo.pl: could not open directory $download_dir\n";
164 }
165 else {
166 my @download_list = grep (/Download\.pm$/, readdir (INDIR));
167 closedir (INDIR);
168
169 if ($xml) {
170 my $intNumDownloads = scalar(@download_list);
171
172 print STDERR "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
173 print STDERR "<DownloadList length=\"$intNumDownloads\">\n";
174 map { print STDERR " <DownloadName>$_</DownloadName>\n"; } @download_list;
175 print STDERR "</DownloadList>\n";
176 }
177 else {
178 print STDERR join(" ",@download_list), "\n";
179 }
180 }
181 }
182 elsif ($describeall) {
183 my $download_dir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"perllib","downloaders");
184 my @download_list;
185 if (!opendir (INDIR, $download_dir)) {
186 print STDERR "downloadinfo.pl: could not open directory $download_dir\n";
187 }
188 else {
189 @download_list = grep (/Download\.pm$/, readdir (INDIR));
190 closedir (INDIR);
191 }
192 #sort (@download_list);
193 if ($xml) {
194 my $num_download = scalar(@download_list);
195 &PrintUsage::print_xml_header("download", 1);
196 print STDERR "<DownloadInfoList length=\"$num_download\">\n";
197 }
198 foreach my $pl (@download_list) {
199 $pl =~ s/\.pm$//;
200 &print_single_download($pl, $xml, 0);
201 }
202 if ($xml) {
203 print STDERR "</DownloadInfoList>\n";
204 }
205 }
206
207 else {
208 &print_single_download($download_name, $xml, 1);
209 }
210}
211
212sub print_single_download {
213 my ($download, $xml, $header) = @_;
214 my @options = ("-gsdlinfo");
215 my $downloadobj = &download::load_download ($download, \@options );
216 if ($xml) {
217 $downloadobj->print_xml_usage($header);
218 }
219 else {
220 gsprintf(STDERR, "{downloadinfo.option_types}:\n\n");
221 gsprintf(STDERR, "{downloadinfo.specific_options}\n\n");
222 gsprintf(STDERR, "{downloadinfo.general_options}\n\n");
223 gsprintf(STDERR, "$download {common.info}:\n\n");
224
225 $downloadobj->print_txt_usage();
226 }
227
228}
229
230&main ();
Note: See TracBrowser for help on using the repository browser.