blib/lib/OrePAN2/Auditor.pm

filter: Statementstmt 76.9% 90 / 117 Branchbran 41.6% 5 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 89.2% 25 / 28 Podpod 66.6% 2 / 3 totaltotal 73.9% 122 / 165 CC 11 SCAR 17.9
CC
11
Cov
72%
run
30.0
_parser_for_url
8.1
_build_outdated_modules
3.0
CRAP 6.0
« blib/lib/OrePAN2/CLI/Inject.pm ↑ Summary blib/lib/OrePAN2/Repository/Cache.pm »
1 package OrePAN2::Auditor;
2  
3 1 ▶use Moo 1.007000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use feature qw( say state );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use version 0.9912;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use List::Compare ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use MooX::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Parse::CPAN::Packages 2.39;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Path::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Type::Params qw( compile );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Type::Tiny::Enum;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Type::Utils qw( class_type );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Types::Standard qw( ArrayRef Bool InstanceOf Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Types::URI -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use LWP::UserAgent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 my $SHOW = Type::Tiny::Enum->new(
21 name => 'Show',
22 values =>
23 [ 'cpan-only-modules', 'darkpan-only-modules', 'outdated-modules' ],
24 );
25  
26 1 option cpan => (
27 is => 'ro',
28 isa => Uri,
29 format => 's',
30 required => 1,
31 coerce => 1,
32 doc => 'the path to a CPAN 02packages file',
33 );
34  
35 1 option darkpan => (
36 is => 'ro',
37 isa => Uri,
38 format => 's',
39 required => 1,
40 coerce => 1,
41 doc => 'the path to your DarkPan 02packages file',
42 );
43  
44 1 option show => (
45 is => 'ro',
46 isa => $SHOW,
47 format => 's',
48 );
49  
50 1 option verbose => (
51 is => 'ro',
52 isa => Bool,
53 default => 0,
54 );
55  
56 has cpan_modules => (
57 is => 'ro',
58 isa => ArrayRef [Str],
59 lazy => 1,
60 default => sub {
61 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
62 1 return $self->_modules_from_parser( $self->_cpan_parser );
63 },
64 1 );
65  
66 has darkpan_modules => (
67 is => 'ro',
68 isa => ArrayRef [Str],
69 lazy => 1,
70 default => sub {
71 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
72 1 return $self->_modules_from_parser( $self->_darkpan_parser );
73 },
74 1 );
75  
76 has cpan_only_modules => (
77 is => 'ro',
78 isa => ArrayRef [Str],
79 lazy => 1,
80 default => sub {
81 1 ▶ return [ shift->_list_compare->get_complement ];
Subroutine __ANON__: called CC=1 SCAR=0.0
82 },
83 1 );
84  
85 has darkpan_only_modules => (
86 is => 'ro',
87 isa => ArrayRef [Str],
88 lazy => 1,
89 default => sub {
90 1 ▶ return [ shift->_list_compare->get_unique ];
Subroutine __ANON__: called CC=1 SCAR=0.0
91 },
92 1 );
93  
94 1 has outdated_modules => (
95 is => 'ro',
96 isa => ArrayRef [Str],
97 lazy => 1,
98 builder => '_build_outdated_modules',
99 );
100  
101 has ua => (
102 is => 'ro',
103 isa => InstanceOf ['LWP::UserAgent'],
104 default => sub {
105 1 ▶ return LWP::UserAgent->new();
Subroutine __ANON__: called CC=1 SCAR=0.0
106 },
107 1 );
108  
109 has _cpan_parser => (
110 is => 'ro',
111 isa => InstanceOf ['Parse::CPAN::Packages'],
112 lazy => 1,
113 default => sub {
114 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
115 1 return $self->_parser_for_url( $self->cpan );
116 },
117 1 );
118  
119 has _darkpan_parser => (
120 is => 'ro',
121 isa => InstanceOf ['Parse::CPAN::Packages'],
122 lazy => 1,
123 default => sub {
124 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
125 1 return $self->_parser_for_url( $self->darkpan );
126 },
127 1 );
128  
129 has _list_compare => (
130 is => 'ro',
131 isa => InstanceOf ['List::Compare'],
132 lazy => 1,
133 default => sub {
134 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
135 1 return List::Compare->new(
136 $self->darkpan_modules,
137 $self->cpan_modules
138 );
139 },
140 1 );
141  
142 sub run {
143 0 ▶ my $self = shift;
Subroutine run: not called CC=5 SCAR=34.0
Pod undocumented
144  
145 0 my $method = $self->show;
146 0 $method =~ s{-}{_}g;
147  
148 0 my $modules = $self->$method;
149  
150 0 ▶ if ( $method eq 'outdated_modules' && $self->verbose ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "outdated_modules" and $self->verbose) 0 0 0
Condition0%0/3
$method eq "outdated_modules" and $self->verbose
!ll&&!rl&&r
000
MC/DC0%0/2
$method eq "outdated_modules" and $self->verbose
$method eq "outdated_modules" $self->verbose
Truth table0%0/3
A and B
A= $method eq "outdated_modules"B= $self->verbose
ABresult
0X0
100
111
151 0 foreach my $module ( @{$modules} ) {
152 0 my @row = (
153 $module,
154 $self->darkpan_module($module)->distribution->distvname,
155 $self->cpan_module($module)->distribution->distvname,
156  
157 sprintf(
158 'https://metacpan.org/changes/distribution/%s',
159 $self->cpan_module($module)->distribution->dist
160 ),
161 );
162 0 say join "\t", @row;
163 }
164 0 return;
165 }
166  
167 0 say $_ for @{$modules};
168 }
169  
170 sub cpan_module {
171 0 ▶ my $self = shift;
Subroutine cpan_module: not called CC=1 SCAR=6.9
Pod documented
172 0 state $check = compile(Str);
173 0 my ($module) = $check->(@_);
174  
175 0 return $self->_cpan_parser->package($module);
176 }
177  
178 sub darkpan_module {
179 0 ▶ my $self = shift;
Subroutine darkpan_module: not called CC=1 SCAR=6.9
Pod documented
180 0 state $check = compile(Str);
181 0 my ($module) = $check->(@_);
182  
183 0 return $self->_darkpan_parser->package($module);
184 }
185  
186 sub _build_outdated_modules {
187 1 ▶ my $self = shift;
Subroutine _build_outdated_modules: called CC=3 SCAR=11.0
188  
189 1 my $darkpan = $self->_darkpan_parser;
190 1 my $cpan = $self->_cpan_parser;
191  
192 1 my @outdated;
193 1 for my $module ( $self->_list_compare->get_intersection ) {
194 1 ▶ if ( version->parse( $darkpan->package($module)->version )
Branch: 1 branch
BranchTrueFalseTotal
if ("version"->parse($darkpan->package($module)->version) < "version"->parse($cpan->package($module)->version)) 1 0 1
195 < version->parse( $cpan->package($module)->version ) ) {
196 1 push @outdated, $module;
197 }
198 }
199 1 return \@outdated;
200 }
201  
202 sub _modules_from_parser {
203 2 ▶ my $self = shift;
Subroutine _modules_from_parser: called CC=1 SCAR=0.0
204 2 my $parser = shift;
205  
206 return [
207 5 map { $_->package }
208 2 sort { $a->package cmp $b->package } $parser->packages
209 ];
210 }
211  
212 sub _parser_for_url {
213 2 ▶ my $self = shift;
Subroutine _parser_for_url: called CC=5 SCAR=20.9
214 2 my $url = shift;
215  
216 2 ▶ $url->scheme('file') if !$url->scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless $url->scheme 1 1 2
217  
218 2 my $res = $self->ua->get($url);
219 2 ▶ croak "could not fetch $url" if !$res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 2 2
220  
221 2 ▶ if ( substr( $url, -3, 3 ) eq 'txt' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($url, -3, 3) eq "txt") 2 0 2
222 2 return Parse::CPAN::Packages->new( $res->content );
223 }
224  
225 # dumb hack to avoid having to uncompress this ourselves
226 0 my @path_segments = $url->path_segments;
227  
228 0 my $err = <<"EOF";
229 Path invalid for $url Please provide full path to 02packages file.
230 EOF
231 0 ▶ croak $err if !@path_segments;
Branch: 1 branch
BranchTrueFalseTotal
unless @path_segments 0 0 0
232  
233 0 my $tempdir = Path::Tiny->tempdir;
234 0 my $child = $tempdir->child( pop @path_segments );
235 0 $child->spew_raw( $res->content );
236  
237 0 return Parse::CPAN::Packages->new( $child->stringify );
238 }
239  
240 1 1;
241  
242 __END__
243  
244 =pod
245  
246 =head1 SYNOPSIS
247  
248 my $auditor = OrePAN2::Auditor->new(
249 cpan => 'https://cpan.metacpan.org/modules/02packages.details.txt',
250 darkpan => '/full/path/to/darkpan/02packages.details.txt'
251 );
252  
253 # ArrayRef of module names
254 my $outdated_modules = $auditor->outdated_modules;
255  
256 =head1 DESCRIPTION
257  
258 BETA BETA BETA
259  
260 L<OrePAN2> is a well established set of code, but this is a very recent
261 edition. Please don't rely on the API remaining stable a this point.
262  
263 If you have a local DarkPAN or MiniCPAN or something which has its own
264 02packages.txt file, it can be helpful to know which files are outdated or
265 which files exist in your DarkPAN, but not on CPAN (or vice versa). This
266 module makes this easy for you.
267  
268 Think of it as a way of diffing 02packages files.
269  
270 =head2 new
271  
272 my $auditor = OrePAN2::Auditor->new(
273 cpan => 'https://cpan.metacpan.org/modules/02packages.details.txt',
274 darkpan => '/full/path/to/darkpan/02packages.details.txt'
275 );
276  
277 The C<cpan> and C<darkpan> args are the only required arguments. These can
278 either be a path on your filesystem or a full URL to the 02packages files which
279 you'd like to diff.
280  
281 =head2 cpan_modules
282  
283 An C<ArrayRef> of module names which exist currently on CPAN.
284  
285 =head2 cpan_only_modules
286  
287 An C<ArrayRef> of module names which exist currently on CPAN but not in your DarkPAN.
288  
289 =head2 darkpan_modules
290  
291 An C<ArrayRef> of module names which exist currently on your DarkPAN.
292  
293 =head2 darkpan_only_modules
294  
295 An C<ArrayRef> of module names which exist currently on your DarkPAN but not in CPAN.
296  
297 =head2 outdated_modules
298  
299 An C<ArrayRef> of module names which exist currently on both your DarkPAN and
300 on CPAN and for which the module in your DarkPAN has a lower version number.
301  
302 =head2 cpan_module( $module_name )
303  
304 my $module = $auditor->cpan_module( 'HTML::Restrict' );
305  
306 Currently returns a L<Parse::CPAN::Packages::Package> object, but this could change.
307  
308 =head2 darkpan_module( $module_name )
309  
310 my $module = $auditor->cpan_module( 'HTML::Restrict' );
311  
312 Currently returns a L<Parse::CPAN::Packages::Package> object, but this could change.
313  
314 =head1 CAVEATS
315  
316 I've tried, for the most part, not to expose the underlying parsers in the API
317 of this module. C<cpan_module> and C<darkpan_module> are currently the
318 exception. I may at some point replace them with OrePAN2 objects which do
319 something similar but with different APIs. I'm exposing them now in order to
320 make this object a little easier to work with and to get some feedback.
321  
322 =cut
« blib/lib/OrePAN2/CLI/Inject.pm ↑ Summary blib/lib/OrePAN2/Repository/Cache.pm »