Debian Codegraph @

bin/cpandb

raw
1 #!usr/bin/perl -w
2
3 # $Id: cpandb 85 2022-10-29 05:44:36Z stro $
4
5 use strict;
6 use warnings;
7
8 use CPAN::SQLite;
9 use Getopt::Long;
10
11 our $VERSION = 0.220;
12
13 my ($CPAN, $setup, $help, $reindex, $index, $query,
14 $db_name, $db_dir, $module, $dist, $cpanid, $update);
15
16 my $rc = GetOptions('CPAN=s' => \$CPAN,
17 'db_name=s' => \$db_name,
18 'db_dir=s' => \$db_dir,
19 'setup' => \$setup,
20 'update' => \$update,
21 'help' => \$help,
22 'module=s' => \$module,
23 'dist=s' => \$dist,
24 'cpanid=s' => \$cpanid,
25 'reindex=s' => \$reindex);
26
27 $query = ($module or $dist or $cpanid);
28 $index = ($update or $reindex or $setup);
29
30 if ($help or not $rc or not ($index or $query)) {
31 print <<"END";
32
33 Setup, maintain, and search the CPAN::SQLite database
34 Usage:
35 $^X $0 --setup
36 $^X $0 --update
37 $^X $0 --reindex dist_name
38
39 $^X $0 --module Mod::Name
40 $^X $0 --dist Distname
41 $^X $0 --cpanid CPANID
42
43 $^X $0 --help
44 END
45 exit(1);
46 }
47
48 if (defined $setup and defined $reindex) {
49 die "Must reindex on an existing database";
50 }
51
52 if ($index) {
53 my $obj = CPAN::SQLite->new(CPAN => $CPAN,
54 setup => $setup,
55 db_name => $db_name,
56 db_dir => $db_dir,
57 reindex => $reindex,
58 );
59 $obj->index();
60 }
61 else {
62
63 my $max_results = $ENV{'CPAN_SQLITE_MAX_RESULTS'};
64 my $obj = CPAN::SQLite->new(CPAN => $CPAN,
65 db_name => $db_name,
66 db_dir => $db_dir,
67 max_results => $max_results);
68 my $results;
69
70 RESULTS : {
71 $module and do {
72 $obj->query(mode => 'module', name => $module);
73 $results = $obj->{results};
74 if (not $results) {
75 print qq{\nNo module by name of "$module" was found.\n};
76 print qq{Error: $obj->{error}\n} if $obj->{error};
77 }
78 else {
79 my $abs = $results->{mod_abs} || '';
80 print << "EOI";
81
82 Module: $results->{mod_name}
83 Abstract: $abs
84 Version: $results->{mod_vers}
85 Distribution: $results->{dist_name}
86 CPAN author: $results->{cpanid}
87 CPAN file: $results->{dist_file}
88 Download: $results->{download}
89 EOI
90 }
91 last RESULTS;
92 };
93 $dist and do {
94 $obj->query(mode => 'dist', name => $dist);
95 $results = $obj->{results};
96 if (not $results) {
97 print qq{\nNo distribution by name of "$dist" was found.\n};
98 print qq{Error: $obj->{error}\n} if $obj->{error};
99 }
100 else {
101 my $abs = $results->{dist_abs} || '';
102 print << "EOI";
103
104 Distribution: $results->{dist_name}
105 Abstract: $abs
106 Version: $results->{dist_vers}
107 CPAN author: $results->{cpanid}
108 CPAN file: $results->{dist_file}
109 Download: $results->{download}
110 EOI
111 }
112 my $mods = $results->{mods};
113 if ($mods and (ref($mods) eq 'ARRAY')) {
114 print qq{\nProvided modules:\n};
115 foreach my $item(@$mods) {
116 my $abs = $item->{mod_abs} || '';
117 print qq{ $item->{mod_name}: $abs\n};
118 }
119 }
120 last RESULTS;
121 };
122 $cpanid and do {
123 $obj->query(mode => 'author', name => $cpanid);
124 $results = $obj->{results};
125 if (not $results) {
126 print qq{\nNo cpanid by name of "$cpanid" was found.\n};
127 print qq{Error: $obj->{error}\n} if $obj->{error};
128 }
129 else {
130 print << "EOI";
131
132 CPANID: $results->{cpanid}
133 Full Name: $results->{fullname}
134 email: $results->{email}
135 EOI
136 }
137 my $dists = $results->{dists};
138 if ($dists and (ref($dists) eq 'ARRAY')) {
139 print qq{\nAvailable distributions:\n};
140 foreach my $item(@$dists) {
141 my $abs = $item->{dist_abs} || '';
142 print qq{ $item->{dist_file}: $abs\n};
143 }
144 }
145 last RESULTS;
146 };
147 }
148 }
149
150 __END__
151
152 =head1 NAME
153
154 cpandb - interface to C<CPAN::SQLite>
155
156 =head1 VERSION
157
158 version 0.220
159
160 =head1 DESCRIPTION
161
162 This script is an interface to the routines of
163 L<CPAN::SQLite> for setting up, maintaining and
164 searching a C<DBD::SQLite> database
165 of CPAN. Available options can be grouped into
166 three categories.
167
168 =head2 Common options
169
170 These are options which are common to both setting up
171 and maintaining the database or performing queries on it.
172 These are
173
174 =over 3
175
176 =item * C<--CPAN '/path/to/CPAN'>
177
178 This specifies the path to where the index files are
179 to be stored. This could be a local CPAN mirror,
180 defined here by the presence of a F<MIRRORED.BY> file beneath
181 this directory, or a local directory in which to store
182 these files from a remote CPAN mirror. In the latter case,
183 the index files are fetched from a remote CPAN mirror,
184 using the same list that C<CPAN.pm> uses, if this is
185 configured, and are updated if they are more than one
186 day old.
187
188 If the C<CPAN> option is not given, it will default
189 to C<cpan_home> of L<CPAN::>, if this is configured,
190 with the sources being found under C<keep_source_where>.
191 A fatal error results if such a directory isn't found.
192 Updates to these index files are assumed here to be
193 handled by C<CPAN.pm>.
194
195 =item * C<--db_name 'cpan-sqlite'>
196
197 This is the name of the database that C<DBD::SQLite>
198 will use. If not given, this defaults to C<cpandb-sqlite>.
199
200 =item * C<--db_dir '/path/to/db/dir'>
201
202 This specifies the path to where the database file is
203 found. If not given, it defaults to the
204 C<cpan_home> directory of C<CPAN.pm>, if present, or to
205 the directory in which the script was invoked.
206
207 =back
208
209 =head2 Indexing options
210
211 These are options which are used for setting up and
212 maintaining the database. These include
213
214 =over 3
215
216 =item * C<--setup>
217
218 This specifies that the database is to be created and
219 populated from the CPAN indices; any existing database
220 will be overwritten.
221
222 =item * C<--update>
223
224 This is used to update an existing database,
225 which must have first been created with the C<setup>
226 option.
227
228 =item * C<--reindex 'dist_name'>
229
230 This specifies that the CPAN distribution C<dist_name>
231 is to be reindexed.
232
233 =back
234
235 =head2 Querying options
236
237 These options are used to query the database. Available
238 options are
239
240 =over 3
241
242 =item * C<--module Mod::Name>
243
244 This provides information on the specified module name.
245
246 =item * C<--dist Dist-Name>
247
248 This provides information on the specified distribution name.
249
250 =item * C<--cpanid CPANID>
251
252 This provides information on the specified CPAN author id
253
254 =back
255
256 All search terms are assumed to be exact matches in a
257 case-insensitive manner.
258
259 There's no limit on maximum number of records. If you need to adjust the number
260 because you get too many results, set the CPAN_SQLITE_MAX_RESULTS environment
261 variable.
262
263 =head1 SEE ALSO
264
265 L<CPAN::SQLite>.
266
267 =cut