forked from barbie/cpan-testers-data-generator
/
cpanstats-select
239 lines (176 loc) · 6.91 KB
/
cpanstats-select
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
#!/usr/bin/perl
use strict;
$|++;
my $VERSION = '0.09';
#----------------------------------------------------------------------------
=head1 NAME
cpanstats-select - select stats from the CPAN Testers Statistics database.
=head1 SYNOPSIS
perl cpanstats-select \
--config=<file> \
--database=<dbcode> \
\
[--sql=<sql>] \
\
[--nntp|-n=<nntpid>] \
[--grade|-g=<grade>] \
[--disto|-m=<distname>] \
[--dist|-d=<distname>] \
[--version|-v=<distversion>] \
[--date|-y=<YYYYMM>] \
[--tester|-t=<email>] \
[--platform|-o=<platform>] \
[--perl|-p=<perlversion>]
=head1 DESCRIPTION
Using the cpanstats database, which should be in the local directory, extracts
all the required data.
=head1 OPTIONS
=over 4
=item --config
Configuration file contain database access details.
=item --database
Specify the database to use, CPANSTATS or LITESTATS
=back
=cut
# -------------------------------------
# Library Modules
use Config::IniFiles;
use CPAN::Testers::Common::DBUtils;
use Getopt::ArgvFile default=>1;
use Getopt::Long;
# -------------------------------------
# Variables
my (%options,$dbi);
# -------------------------------------
# Program
##### INITIALISE #####
init_options();
##### MAIN #####
# '(id,state,postdate,tester,dist,version,platform,perl) '.
if($options{sql}) {
my @rows = $dbi->get_query('array',$options{sql});
if(@rows) {
for my $row (@rows) {
print join(",",@$row) . "\n";
}
} else {
print "Sorry, no results returned for SQL [$options{sql}]\n";
}
} else {
my $sql = "SELECT * FROM cpanstats WHERE ";
my @where;
# only one of the following at a time
@where = ("dist LIKE '\%$options{distro}\%'") if(defined $options{distro});
@where = ("dist='$options{dist}'") if(defined $options{dist});
@where = ("id=$options{nntp}") if(defined $options{nntp});
push @where, "state='$options{grade}'" if(defined $options{grade});
push @where, "version='$options{version}'" if(defined $options{version});
push @where, "version LIKE '\%$options{distversion}\%'" if(defined $options{distversion});
push @where, "postdate='$options{date}'" if(defined $options{date});
push @where, "tester LIKE '\%$options{tester}\%'" if(defined $options{tester} && $options{tester} ne '-');
push @where, "tester=''" if(defined $options{tester} && $options{tester} eq '-');
push @where, "platform LIKE '$options{platform}\%'" if(defined $options{platform} && $options{platform} ne '-');
push @where, "platform=''" if(defined $options{platform} && $options{platform} eq '-');
push @where, "perl='$options{perl}'" if(defined $options{perl} && $options{perl} ne '-');
push @where, "perl=''" if(defined $options{perl} && $options{perl} eq '-');
if(@where) {
$sql .= join(' AND ',@where);
my @rows = $dbi->get_query('array',$sql);
if(@rows) {
for my $row (@rows) {
print join(",",@$row) . "\n";
}
} else {
print "Sorry, no results returned for SQL [$sql]\n";
}
} else {
print "No SQL arguments given\n";
}
}
# -------------------------------------
# Subroutines
sub init_options {
GetOptions( \%options,
'config=s',
'database=s',
'sql=s',
'nntp|n=s',
'grade|g=s',
'distro|m=s',
'dist|d=s',
'distversion|x=s',
'platform|o=s',
'perl|p=s',
'tester|t=s',
'version|v=s',
'date|y=s',
'help|h',
);
help(1) if($options{help});
$options{grade} = lc $options{grade} if($options{grade});
help(1,"Must specify the configuration file") unless($options{config});
help(1,"Configuration file [$options{config}] not found") unless(-f $options{config});
help(1,"Must specify the database code (CPANSTATS or LITESTATS)")
unless($options{database} && $options{database} =~ /^CPANSTATS|LITESTATS$/);
# load configuration
my $cfg = Config::IniFiles->new( -file => $options{config} );
# configure databases
my $db = $options{database};
die "No configuration for $db database\n" unless($cfg->SectionExists($db));
my %opts = map {$_ => $cfg->val($db,$_);} qw(driver database dbfile dbhost dbport dbuser dbpass);
$dbi = CPAN::Testers::Common::DBUtils->new(%opts);
die "Cannot configure $db database\n" unless($dbi);
}
sub help {
my ($full,$mess) = @_;
print "\n$mess\n\n" if($mess);
if($full) {
print <<HERE;
Usage: $0
--config=<file> - configuration file
--database=<dbcode> - CPANSTATS or LITESTATS
[--nntp|-n=<nntpid>] - NNTP article id
[--grade|-g=<grade>] - report grade
[--dist|-d=<distname>] - distribution name
[--disto|-m=<distname>] - distribution name (partial match)
[--version|-v=<distversion>] - distribution version
[--date|-y=<YYYYMM>] - year/month
[--tester|-t=<email>] - tester email
[--platform|-o=<platform>] - platform (partial match)
[--perl|-p=<perlversion>] - perl version
[--help|-h] - this screen
Notes:
- combine options (except help) to refine your search
- all entries (except distro and paltform) require an exact match
- only one of nntp, dist or distro (in order of preference) is accepted
- use '-' for tester, perl and platform to find blank entries
HERE
}
print "$0 v$VERSION\n";
exit(0);
}
__END__
=back
=head1 BUGS, PATCHES & FIXES
There are no known bugs at the time of this release. However, if you spot a
bug or are experiencing difficulties, that is not explained within the POD
documentation, please send bug reports and patches to the RT Queue (see below).
Fixes are dependant upon their severity and my availablity. Should a fix not
be forthcoming, please feel free to (politely) remind me.
RT Queue -
http://rt.cpan.org/Public/Dist/Display.html?Name=CPAN-Testers-Data-Generator
=head1 SEE ALSO
L<CPAN::WWW::Testers>,
L<CPAN::Testers::WWW::Statistics>
F<http://www.cpantesters.org/>,
F<http://stats.cpantesters.org/>,
F<http://wiki.cpantesters.org/>
=head1 AUTHOR
Barbie, E<lt>barbie@cpan.orgE<gt>
for Miss Barbell Productions L<http://www.missbarbell.co.uk>.
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2005-2008 Barbie for Miss Barbell Productions
All Rights Reserved.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=cut