summaryrefslogtreecommitdiff
path: root/Master/tlpkg/tlperl/bin/cpandb
blob: 46c07b939a1e05816e6d5d5abe1d631d2e231ec2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!C:\strawberry\perl\bin\perl.exe 
use strict;
use warnings;
use CPAN::SQLite;
use CPAN::SQLite::Util qw(%chaps);
use Getopt::Long;
my ($CPAN, $setup, $help, $reindex, $index, $query,
    $db_name, $db_dir, $module, $dist, $cpanid, $update);

my $rc = GetOptions('CPAN=s' => \$CPAN,
                    'db_name=s' => \$db_name,
                    'db_dir=s' => \$db_dir,
                    'setup' => \$setup,
                    'update' => \$update,
                    'help' => \$help,
                    'module=s' => \$module,
                    'dist=s' => \$dist,
                    'cpanid=s' => \$cpanid,
                    'reindex=s' => \$reindex);

$query = ($module or $dist or $cpanid);
$index = ($update or $reindex or $setup);

if ($help or not $rc or not ($index or $query)) {
    print <<"END";

Setup, maintain, and search the CPAN::SQLite database
Usage:
   $^X $0 --setup
   $^X $0 --update
   $^X $0 --reindex dist_name

   $^X $0 --module  Mod::Name
   $^X $0 --dist  Distname
   $^X $0 --cpanid  CPANID

   $^X $0 --help
END
    exit(1);
}

if (defined $setup and defined $reindex) {
  die "Must reindex on an exisiting database";
}

if ($index) {
  my $obj = CPAN::SQLite->new(CPAN => $CPAN,
                              setup => $setup,
                              db_name => $db_name,
                              db_dir => $db_dir,
                              reindex => $reindex,
                             );
  $obj->index();
}
else {

  my $max_results = 100;
  my $obj = CPAN::SQLite->new(CPAN => $CPAN,
                              db_name => $db_name,
                              db_dir => $db_dir,
                              max_results => $max_results);
  my $results;

  RESULTS : {
    $module and do {
      $obj->query(mode => 'module', name => $module);
      $results = $obj->{results};
      if (not $results) {
        print qq{\nNo module by name of "$module" was found.\n};
        print qq{Error: $obj->{error}\n} if $obj->{error};
      }
      else {
        my $abs = $results->{mod_abs} || '';
        my $dslip = '';
        if (my $dslip_info = $results->{dslip_info}) {
          foreach my $entry (@$dslip_info) {
            $dslip .= "  $entry->{desc}: $entry->{what}\n";
          }
        }
        my $chapter_desc = $results->{chapter_desc} || '';
        print << "EOI";

Module: $results->{mod_name}
Abstract: $abs
Version: $results->{mod_vers}
Chapter: $chapter_desc
Distribution: $results->{dist_name}
CPAN author: $results->{cpanid}
CPAN file: $results->{dist_file}
Download: $results->{download}
dslip info:
$dslip
EOI
      }
      last RESULTS;
    };
    $dist and do {
      $obj->query(mode => 'dist', name => $dist);
      $results = $obj->{results};
      if (not $results) {
        print qq{\nNo distribution by name of "$dist" was found.\n};
        print qq{Error: $obj->{error}\n} if $obj->{error};
      }
      else {
        my $abs = $results->{dist_abs} || '';
        my $dslip = '';
        if (my $dslip_info = $results->{dslip_info}) {
          foreach my $entry (@$dslip_info) {
            $dslip .= "  $entry->{desc}: $entry->{what}\n";
          }
        }
        print << "EOI";

Distribution: $results->{dist_name}
Abstract: $abs
Version: $results->{dist_vers}
CPAN author: $results->{cpanid}
CPAN file: $results->{dist_file}
Download: $results->{download}
dslip info:
$dslip
EOI
      }
      my $mods = $results->{mods};
      if ($mods and (ref($mods) eq 'ARRAY')) {
	print qq{\nProvided modules:\n};
	foreach my $item(@$mods) {
	  my $abs = $item->{mod_abs} || '';
	  print qq{  $item->{mod_name}: $abs\n};
	}
      }
      last RESULTS;
    };
    $cpanid and do {
      $obj->query(mode => 'author', name => $cpanid);
      $results = $obj->{results};
      if (not $results) {
        print qq{\nNo cpanid by name of "$cpanid" was found.\n};
        print qq{Error: $obj->{error}\n} if $obj->{error};
      }
      else {
        print << "EOI";

CPANID: $results->{cpanid}
Full Name: $results->{fullname}
email: $results->{email}
EOI
      }
      my $dists = $results->{dists};
      if ($dists and (ref($dists) eq 'ARRAY')) {
	print qq{\nAvailable distributions:\n};
	foreach my $item(@$dists) {
	  my $abs = $item->{dist_abs} || '';
	  print qq{  $item->{dist_file}: $abs\n};
	}
      }
      last RESULTS;
    };
  }
}

__END__

=head1 NAME

cpandb - interface to C<CPAN::SQLite>

=head1 DESCRIPTION

This script is an interface to the routines of
L<CPAN::SQLite> for setting up, maintaining and
searching a C<DBD::SQLite> database
of CPAN. Available options can be grouped into
three categories.

=head2 Common options

These are options which are common to both setting up
and maintaining the database or performing queries on it.
These are

=over 3

=item * C<--CPAN  '/path/to/CPAN'>

This specifies the path to where the index files are
to be stored. This could be a local CPAN mirror,
defined here by the presence of a F<MIRRORED.BY> file beneath
this directory, or a local directory in which to store
these files from a remote CPAN mirror. In the latter case,
the index files are fetched from a remote CPAN mirror,
using the same list that C<CPAN.pm> uses, if this is
configured, and are updated if they are more than one
day old.

If the C<CPAN> option is not given, it will default
to C<cpan_home> of L<CPAN>, if this is configured,
with the sources being found under C<keep_source_where>.
A fatal error results if such a directory isn't found.
Updates to these index files are assumed here to be
handled by C<CPAN.pm>.

=item * C<--db_name  'cpan-sqlite'>

This is the name of the database that C<DBD::SQLite>
will use. If not given, this defaults to C<cpandb-sqlite>.

=item * C<--db_dir  '/path/to/db/dir'>

This specifies the path to where the database file is
found. If not given, it defaults to the
C<cpan_home> directory of C<CPAN.pm>, if present, or to
the directory in which the script was invoked.

=back

=head2 Indexing options

These are options which are used for setting up and
maintaining the database. These include

=over 3

=item * C<--setup>

This specifies that the database is to be created and
populated from the CPAN indices; any exisiting database
will be overwritten.

=item * C<--update>

This is used to update an exisiting database,
which must have first been created with the C<setup>
option.

=item * C<--reindex 'dist_name'>

This specifies that the CPAN distribution C<dist_name>
is to be reindexed.

=back

=head2 Querying options

These options are used to query the database. Available
options are

=over 3

=item * C<--module Mod::Name>

This provides information on the specified module name.

=item * C<--dist Dist-Name>

This provides information on the specified distribution name.

=item * C<--cpanid CPANID>

This provides information on the specified CPAN author id

=back

All search terms are assumed to be exact matches in a
case-insensitive manner.

=head1 SEE ALSO

L<CPAN::SQLite>.

=cut