blib/lib/OrePAN2/Auditor.pm

filter: Statementstmt 89.1% 99 / 111 Branchbran 50.0% 5 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 96.4% 27 / 28 Podpod 66.6% 2 / 3 totaltotal 84.7% 133 / 157 CC 10 SCAR 16.9
CC
10
Cov
84%
run
30.0
_parser_for_url
4.0
_build_outdated_modules
3.0
CRAP 5.4
« blib/lib/OrePAN2/Repository/Cache.pm ↑ Summary blib/lib/OrePAN2/Index.pm »
1 package OrePAN2::Auditor;
2  
3 1 ▶use Moo;
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;
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::Fast 0.09 ();
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( signature );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Types::Self qw( Self );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Types::Standard qw( ArrayRef Bool Enum InstanceOf Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Types::URI qw( Uri );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use LWP::UserAgent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use namespace::clean -except => [qw( _options_data _options_config )];
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 option cpan => (
22 is => 'ro',
23 isa => Uri,
24 format => 's',
25 required => 1,
26 coerce => 1,
27 doc => 'the path to a CPAN 02packages file',
28 );
29  
30 1 option darkpan => (
31 is => 'ro',
32 isa => Uri,
33 format => 's',
34 required => 1,
35 coerce => 1,
36 doc => 'the path to your DarkPan 02packages file',
37 );
38  
39 1 option show => (
40 is => 'ro',
41 isa =>
42 Enum [qw( cpan-only-modules darkpan-only-modules outdated-modules )],
43 format => 's',
44 );
45  
46 1 option verbose => (
47 is => 'ro',
48 isa => Bool,
49 default => 0,
50 );
51  
52 has cpan_modules => (
53 is => 'ro',
54 isa => ArrayRef [Str],
55 lazy => 1,
56 default => sub {
57 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
58 1 return $self->_modules_from_parser( $self->_cpan_parser );
59 },
60 1 );
61  
62 has darkpan_modules => (
63 is => 'ro',
64 isa => ArrayRef [Str],
65 lazy => 1,
66 default => sub {
67 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
68 1 return $self->_modules_from_parser( $self->_darkpan_parser );
69 },
70 1 );
71  
72 has cpan_only_modules => (
73 is => 'ro',
74 isa => ArrayRef [Str],
75 lazy => 1,
76 default => sub {
77 1 ▶ return [ shift->_list_compare->get_complement ];
Subroutine __ANON__: called CC=1 SCAR=0.0
78 },
79 1 );
80  
81 has darkpan_only_modules => (
82 is => 'ro',
83 isa => ArrayRef [Str],
84 lazy => 1,
85 default => sub {
86 1 ▶ return [ shift->_list_compare->get_unique ];
Subroutine __ANON__: called CC=1 SCAR=0.0
87 },
88 1 );
89  
90 1 has outdated_modules => (
91 is => 'ro',
92 isa => ArrayRef [Str],
93 lazy => 1,
94 builder => '_build_outdated_modules',
95 );
96  
97 has ua => (
98 is => 'ro',
99 isa => InstanceOf ['LWP::UserAgent'],
100 default => sub {
101 1 ▶ return LWP::UserAgent->new();
Subroutine __ANON__: called CC=1 SCAR=0.0
102 },
103 1 );
104  
105 has _cpan_parser => (
106 is => 'ro',
107 isa => InstanceOf ['Parse::CPAN::Packages::Fast'],
108 lazy => 1,
109 default => sub {
110 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
111 1 return $self->_parser_for_url( $self->cpan );
112 },
113 1 );
114  
115 has _darkpan_parser => (
116 is => 'ro',
117 isa => InstanceOf ['Parse::CPAN::Packages::Fast'],
118 lazy => 1,
119 default => sub {
120 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
121 1 return $self->_parser_for_url( $self->darkpan );
122 },
123 1 );
124  
125 has _list_compare => (
126 is => 'ro',
127 isa => InstanceOf ['List::Compare'],
128 lazy => 1,
129 default => sub {
130 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
131 1 return List::Compare->new(
132 $self->darkpan_modules,
133 $self->cpan_modules
134 );
135 },
136 1 );
137  
138 sub run {
139 0 ▶ my $self = shift;
Subroutine run: not called CC=5 SCAR=34.0
Pod undocumented
140  
141 0 my $method = $self->show;
142 0 $method =~ s{-}{_}g;
143  
144 0 my $modules = $self->$method;
145  
146 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
147 0 foreach my $module ( @{$modules} ) {
148 0 my @row = (
149 $module,
150 $self->darkpan_module($module)->distribution->distvname,
151 $self->cpan_module($module)->distribution->distvname,
152  
153 sprintf(
154 'https://metacpan.org/changes/distribution/%s',
155 $self->cpan_module($module)->distribution->dist
156 ),
157 );
158 0 say join "\t", @row;
159 }
160 0 return;
161 }
162  
163 0 say $_ for @{$modules};
164 }
165  
166 sub cpan_module {
167 1 ▶ state $signature = signature( method => Self, positional => [Str] );
Subroutine cpan_module: called CC=1 SCAR=0.0
Pod documented
168 1 my ( $self, $module ) = $signature->(@_);
169  
170 1 return $self->_cpan_parser->package($module);
171 }
172  
173 sub darkpan_module {
174 1 ▶ state $signature = signature( method => Self, positional => [Str] );
Subroutine darkpan_module: called CC=1 SCAR=0.0
Pod documented
175 1 my ( $self, $module ) = $signature->(@_);
176  
177 1 return $self->_darkpan_parser->package($module);
178 }
179  
180 sub _build_outdated_modules {
181 1 ▶ my $self = shift;
Subroutine _build_outdated_modules: called CC=3 SCAR=11.0
182  
183 1 my $darkpan = $self->_darkpan_parser;
184 1 my $cpan = $self->_cpan_parser;
185  
186 1 my @outdated;
187 1 for my $module ( $self->_list_compare->get_intersection ) {
188 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
189 < version->parse( $cpan->package($module)->version ) ) {
190 1 push @outdated, $module;
191 }
192 }
193 1 return \@outdated;
194 }
195  
196 sub _modules_from_parser {
197 2 ▶ my $self = shift;
Subroutine _modules_from_parser: called CC=1 SCAR=0.0
198 2 my $parser = shift;
199  
200 2 return [ sort { $a cmp $b } $parser->packages ];
201 }
202  
203 sub _parser_for_url {
204 2 ▶ my $self = shift;
Subroutine _parser_for_url: called CC=4 SCAR=13.9
205 2 my $url = shift;
206  
207 2 ▶ $url->scheme('file') if !$url->scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless $url->scheme 1 1 2
208  
209 2 my $res = $self->ua->get($url);
210 2 ▶ croak "could not fetch $url" if !$res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 2 2
211  
212 # dumb hack to avoid having to uncompress this ourselves
213 2 my @path_segments = $url->path_segments;
214  
215 2 my $err = <<"EOF";
216 Path invalid for $url Please provide full path to 02packages file.
217 EOF
218 2 ▶ croak $err if !@path_segments;
Branch: 1 branch
BranchTrueFalseTotal
unless @path_segments 0 2 2
219  
220 2 my $tempdir = Path::Tiny->tempdir;
221 2 my $child = $tempdir->child( pop @path_segments );
222 2 $child->spew_raw( $res->content );
223  
224 2 return Parse::CPAN::Packages::Fast->new( $child->stringify );
225 }
226  
227 1 1;
228  
229 __END__
230  
231 =pod
232  
233 =head1 SYNOPSIS
234  
235 my $auditor = OrePAN2::Auditor->new(
236 cpan => 'https://cpan.metacpan.org/modules/02packages.details.txt',
237 darkpan => '/full/path/to/darkpan/02packages.details.txt'
238 );
239  
240 # ArrayRef of module names
241 my $outdated_modules = $auditor->outdated_modules;
242  
243 =head1 DESCRIPTION
244  
245 If you have a local DarkPAN or MiniCPAN or something which has its own
246 C<02packages.txt> file, it can be helpful to know which files are outdated or
247 which files exist in your DarkPAN, but not on CPAN (or vice versa). This
248 module makes this easy for you.
249  
250 Think of it as a way of diffing C<02packages> files.
251  
252 =head2 new
253  
254 my $auditor = OrePAN2::Auditor->new(
255 cpan => 'https://cpan.metacpan.org/modules/02packages.details.txt',
256 darkpan => '/full/path/to/darkpan/02packages.details.txt'
257 );
258  
259 The C<cpan> and C<darkpan> args are the only required arguments. These can
260 either be a path on your filesystem or a full URL to the 02packages files which
261 you'd like to diff.
262  
263 =head2 cpan_modules
264  
265 An C<ArrayRef> of module names which exist currently on CPAN.
266  
267 =head2 cpan_only_modules
268  
269 An C<ArrayRef> of module names which exist currently on CPAN but not in your DarkPAN.
270  
271 =head2 darkpan_modules
272  
273 An C<ArrayRef> of module names which exist currently on your DarkPAN.
274  
275 =head2 darkpan_only_modules
276  
277 An C<ArrayRef> of module names which exist currently on your DarkPAN but not in CPAN.
278  
279 =head2 outdated_modules
280  
281 An C<ArrayRef> of module names which exist currently on both your DarkPAN and
282 on CPAN and for which the module in your DarkPAN has a lower version number.
283  
284 =head2 cpan_module( $module_name )
285  
286 my $module = $auditor->cpan_module( 'HTML::Restrict' );
287  
288 Returns a L<Parse::CPAN::Packages::Fast::Package> object.
289  
290 =head2 darkpan_module( $module_name )
291  
292 my $module = $auditor->cpan_module( 'HTML::Restrict' );
293  
294 Returns a L<Parse::CPAN::Packages::Fast::Package> object.
295  
296 =cut
« blib/lib/OrePAN2/Repository/Cache.pm ↑ Summary blib/lib/OrePAN2/Index.pm »