BackPANstats.pm

filter: Statementstmt 100% 18 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 100% 24 / 24 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Indexer/Worker.pm ↑ Summary Indexer/Tutorial.pm »
1 package MyCPAN::Indexer::BackPANstats;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use subs qw(get_caller_info);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use vars qw($VERSION $logger);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use parent qw(MyCPAN::Indexer MyCPAN::Indexer::Component MyCPAN::Indexer::Reporter::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 $VERSION = '1.282';
12  
13 =encoding utf8
14  
15 =head1 NAME
16  
17 MyCPAN::Indexer::BackPANstats - Collect various stats about BackPAN activity
18  
19 =head1 SYNOPSIS
20  
21 use MyCPAN::Indexer;
22  
23 =head1 DESCRIPTION
24  
25 This module implements the indexer_class and reporter_class components
26 to allow C<backpan_indexer.pl> to collect stats on BackPAN.
27  
28 It runs through the indexing and prints a report at the end of the run.
29  
30 =cut
31  
32 use Carp qw(croak);
33 use Cwd qw(cwd);
34  
35 use Log::Log4perl;
36  
37 BEGIN {
38 $logger = Log::Log4perl->get_logger( __PACKAGE__ );
39 }
40  
41 __PACKAGE__->run( @ARGV ) unless caller;
42  
43 =head2 Indexer class
44  
45 =over 4
46  
47 =item get_indexer()
48  
49 A stand in for run_components later on.
50  
51 =cut
52  
53 sub get_indexer
54 {
55 my( $self ) = @_;
56  
57 1;
58 }
59  
60 sub class { __PACKAGE__ }
61  
62 =item setup_run_info
63  
64 Like C<setup_run_info> in C<MyCPAN::Indexer>, but it remembers fewer
65 things. The test census really just cares about statements in the test
66 files, so the details about the run aren't as interesting.
67  
68 =cut
69  
70 sub setup_run_info { 1 }
71  
72 =item examine_dist_steps
73  
74 Given a distribution, unpack it, look at it, and report the findings.
75 It does everything except the looking right now, so it merely croaks.
76 Most of this needs to move out of run and into this method.
77  
78 =cut
79  
80 sub examine_dist_steps
81 {
82 my @methods = (
83 # method error message fatal
84 [ 'collect_info', "Could not get info!", 1 ],
85 );
86 }
87  
88 =item check_dist_size
89  
90 We don't care about 0 byte dists, so we always return true so setup_dist_info
91 doesn't bail out.
92  
93 =cut
94  
95 sub check_dist_size { 1 }
96  
97 =item collect_info
98  
99 Given a distribution, unpack it, look at it, and report the findings.
100 It does everything except the looking right now, so it merely croaks.
101 Most of this needs to move out of run and into this method.
102  
103 =cut
104  
105 use CPAN::DistnameInfo;
106 sub collect_info {
107 my $self = shift;
108 my $d = CPAN::DistnameInfo->new( $self->{dist_info}{dist_file} );
109 $self->set_dist_info( 'dist_name', $d->dist );
110 $self->set_dist_info( 'dist_version', $d->version );
111 $self->set_dist_info( 'maturity', $d->maturity );
112  
113 my @gmtime = gmtime( $self->dist_info( 'dist_date' ) );
114 my( $year, $month, $day ) = @gmtime[ 5,4,3 ];
115 $year += 1900;
116 $month += 1;
117  
118 $self->set_dist_info(
119 'yyyymmdd_gmt',
120 sprintf '%4d%02d%02d', $year, $month, $day
121 );
122  
123 $self->set_dist_info(
124 'calendar_quarter',
125 sprintf "%4dQ%d", $year, int( ($month - 1 ) / 3 ) + 1
126 );
127  
128 1;
129 }
130  
131  
132 =back
133  
134 =head2 Reporter class
135  
136 =over 4
137  
138 =item get_reporter( $Notes )
139  
140 C<get_reporter> sets the C<reporter> key in the C<$Notes> hash
141 reference. The value is a code reference that takes the information
142 collected about a distribution and counts the modules used in the test
143 files.
144  
145 See L<MyCPAN::Indexer::Tutorial> for details about what
146 C<get_reporter> expects and should do.
147  
148 $VAR1 = {
149 'dist_date' => 1207928766,
150 'dist_basename' => 'cpan-script-1.54.tar.gz',
151 'maturity' => 'released',
152 'dist_file' => '/Volumes/iPod/BackPAN/authors/id/B/BD/BDFOY/cpan-script-1.54.tar.gz',
153 'dist_size' => 6281,
154 'dist_author' => 'BDFOY',
155 'dist_name' => 'cpan-script',
156 'dist_md5' => '8053fa43edcdce9a90f78f878cbf6caf',
157 'dist_version' => '1.54'
158 };
159 =cut
160  
161 sub check_for_previous_successful_result { 1 }
162 sub check_for_previous_error_result { 0 }
163 sub final_words { sub { 1 } }
164  
165 sub get_reporter {
166 my $self = shift;
167 require JSON::XS;
168 use File::Basename qw(dirname);
169 use File::Path qw(make_path);
170 use Clone qw(clone);
171 use Data::Structure::Util qw(unbless);
172  
173 my $jsonner = JSON::XS->new->pretty;
174  
175 my $reporter = sub {
176 my( $info ) = @_;
177  
178 unless( defined $info ) {
179 $logger->error( "info is undefined!" );
180 return;
181 }
182  
183 my $out_path = $self->get_report_path( $info );
184 my $dir = dirname( $out_path );
185 unless( -d $dir ) {
186 make_path( $dir ) or
187 $logger->fatal( "Could not create directory $dir: $!" );
188 }
189  
190 open my($fh), ">:utf8", $out_path or
191 $logger->fatal( "Could not open $out_path: $!" );
192  
193 {
194 # now that indexer is a component, it has references to all the other
195 # objects, making for a big dump. We don't want the keys starting
196 # with _
197 # Storable doesn't work because it can't handle the CODE refs
198 my $clone = clone( $info ); # hack until we get an info class
199 unbless( $clone );
200 delete $clone->{run_info};
201 my $dist = $clone->{dist_info}{dist_basename};
202  
203 local $SIG{__WARN__} = sub {
204 $logger->warn( "Error writing to YAML output for $dist: @_" );
205 };
206  
207 foreach my $key ( keys %$clone ) {
208 delete $clone->{$key} if $key =~ /^_/;
209 }
210  
211 print { $fh } $jsonner->encode( $clone );
212 }
213  
214 $logger->error( "$out_path is missing!" ) unless -e $out_path;
215  
216 1;
217 };
218  
219 $self->set_note( 'reporter', $reporter );
220 }
221  
222 sub get_report_file_extension { 'json' }
223  
224 =back
225  
226 =head1 TO DO
227  
228 =over 4
229  
230 =item Count the lines in the files
231  
232 =item Code stats? Lines of code, lines of pod, lines of comments
233  
234 =back
235  
236 =head1 SOURCE AVAILABILITY
237  
238 This code is in Github:
239  
240 git://github.com/briandfoy/mycpan-indexer.git
241  
242 =head1 AUTHOR
243  
244 brian d foy, C<< <bdfoy@cpan.org> >>
245  
246 =head1 COPYRIGHT AND LICENSE
247  
248 Copyright © 2010-2018, brian d foy <bdfoy@cpan.org>. All rights reserved.
249  
250 You may redistribute this under the terms of the Artistic License 2.0.
251  
252 =cut
253  
254 1;
« Indexer/Worker.pm ↑ Summary Indexer/Tutorial.pm »