lib/Gentoo/Util/MetaCPAN.pm

filter: Statementstmt 46.8% 74 / 158 Branchbran 9.0% 2 / 22 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 48.6% 18 / 37 Podpod 100% 6 / 6 totaltotal 43.2% 100 / 231 CC 19 SCAR 22.2
CC
19
Cov
41%
__ANON__
30.0
_build_mechua
30.0
next
30.0
CRAP 9.2
« blib/lib/Gentoo/Util/MetaCPAN/Requirement.pm ↑ Summary lib/Gentoo/Util/MetaCPAN/Release.pm »
1 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Gentoo::Util::MetaCPAN;
6  
7 3 our $VERSION = '0.001000'; # TRIAL
8  
9 # ABSTRACT: Gentoo Specific MetaCPAN Utilities.
10  
11 3 our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12  
13 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use MooX::Lsub qw( lsub );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Sub::Exporter::Progressive -setup => { exports => ['mcpan'] };
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶sub _mk_cache {
Subroutine _mk_cache: called CC=1 SCAR=0.0
20 2 my ( $name, %opts ) = @_;
21 2 my $root = path( File::Spec->tmpdir );
22 2 my $child = $root->child('gentoo-metacpan-cache');
23 2 $child->mkpath;
24 2 my $db = $child->child($name);
25 2 require Data::Serializer::Sereal;
26 2 require Sereal;
27 2 my $serial = Data::Serializer::Sereal->new( encoder => Sereal::Encoder->new( { compress => 1, canonincal => 1 } ) );
28 2 $db->mkpath;
29 2 require CHI;
30 2 require CHI::Driver::LMDB;
31 0 return CHI->new(
32 driver => 'LMDB',
33 root_dir => "$db",
34 expires_in => '6 hour',
35 expires_variance => '0.2',
36 namespace => $name,
37 cache_size => '30m',
38 key_serializer => $serial,
39 serializer => $serial,
40 %opts,
41 );
42 }
43  
44 0 ▶lsub 'www_cache' => sub { _mk_cache('web') };
Subroutine __ANON__: not called CC=1 SCAR=1.2
45 0 ▶lsub 'object_cache' => sub { _mk_cache('objects') };
Subroutine _build_object_cache: not called CC=1 SCAR=0.4
Subroutine __ANON__: called CC=1 SCAR=0.4
46  
47 0 ▶lsub 'debug' => sub {
Subroutine _build_debug: not called CC=2 SCAR=17.9
48 0 ▶ return unless defined $ENV{WWW_MECH_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ENV{"WWW_MECH_DEBUG"} 0 0 0
49 0 return $ENV{WWW_MECH_DEBUG};
50 3 };
51 2 ▶lsub 'nocache' => sub {
Subroutine _build_nocache: called CC=2 SCAR=9.2
52 2 ▶ return unless defined $ENV{WWW_MECH_NOCACHE};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ENV{"WWW_MECH_NOCACHE"} 2 0 2
53 0 return $ENV{WWW_MECH_NOCACHE};
54 3 };
55 0 ▶lsub 'mechua' => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
56 0 my ($self) = @_;
57 0 my $mech;
58 0 ▶ if ( $self->nocache ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->nocache) { } 0 0 0
59 0 require LWP::UserAgent;
60 0 $mech = LWP::UserAgent->new();
61 }
62 else {
63 0 require WWW::Mechanize::Cached;
64 0 $mech = WWW::Mechanize::Cached->new(
65 cache => $self->www_cache,
66 timeout => 20_000,
67 autocheck => 1,
68 );
69 }
70 0 ▶ if ( ( $self->debug || 0 ) > 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (($self->debug || 0) > 1) { } 0 0 0
elsif ($self->debug) { } 0 0 0
Condition0%0/2
$self->debug || 0
l!l
00
MC/DC0%0/1
$self->debug || 0
$self->debug
Truth table0%0/2
A
A= $self->debug
Aresult
11
00
71 $mech->add_handler(
72 0 ▶ 'request_send' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 0 *STDERR->printf( "%s\n", $_[0]->as_string );
74 0 return;
75 },
76 0 );
77 $mech->add_handler(
78 0 ▶ 'response_done' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
79 0 *STDERR->printf( "%s\n", $_[0]->content );
80 0 return;
81 },
82 0 );
83 }
84 elsif ( $self->debug ) {
85 $mech->add_handler(
86 0 ▶ 'request_send' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 0 *STDERR->printf( "%s\n", $_[0]->dump );
88 0 return;
89 },
90 0 );
91 $mech->add_handler(
92 0 ▶ 'response_done' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
93 0 *STDERR->printf( "%s\n", $_[0]->dump );
94 0 return;
95 },
96 0 );
97 }
98 0 return $mech;
99 3 };
100 0 ▶lsub 'tinymech' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 0 my ($self) = @_;
102 0 require HTTP::Tiny::Mech;
103 0 HTTP::Tiny::Mech->new( mechua => $self->mechua );
104 3 };
105 0 ▶lsub 'client' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
106 0 my ($self) = @_;
107 0 require MetaCPAN::Client;
108 0 MetaCPAN::Client->new( ua => $self->tinymech );
109 3 };
110  
111 2 ▶sub _cache_object {
Subroutine _cache_object: called CC=2 SCAR=7.6
112 2 my ( $self, $key, $time, $code ) = @_;
113 2 ▶ if ( $self->nocache ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->nocache) 0 2 2
114 0 return $code->();
115 }
116 2 return $self->object_cache->compute( $key, $time, $code );
117 }
118 {
119 ## HACK: This exists because its not supported natively yet.
120 ## no critic (TestingAndDebugging::ProhibitNoWarnings,Subroutines::ProhibitQualifiedSubDeclarations)
121 3 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
122  
123 3 ▶ use MetaCPAN::Client::ResultSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
124  
125 0 ▶ sub MetaCPAN::Client::ResultSet::next {
Subroutine next: not called CC=5 SCAR=34.0
Pod documented
126 0 my $self = shift;
127 my $result =
128 $self->has_scroller
129 ? $self->scroller->next
130 0 ▶ : shift @{ $self->items };
Branch: 1 branch
BranchTrueFalseTotal
$self->has_scroller ? : 0 0 0
131  
132 0 ▶ defined $result or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $result or return 0 0 0
133  
134 0 ▶ my $class = exists $self->{'class'} ? $self->{class} : 'MetaCPAN::Client::' . ucfirst $self->type;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'class'} ? : 0 0 0
135 0 ▶ return $class->new_from_request( $result->{'_source'} || $result->{'fields'} );
Condition0%0/3
$result->{'_source'} || $result->{'fields'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$result->{'_source'} || $result->{'fields'}
$result->{'_source'} $result->{'fields'}
Truth table0%0/3
A || B
A= $result->{'_source'}B= $result->{'fields'}
ABresult
1X1
011
000
136 }
137 }
138  
139 # More hacks because the native MetaCPAN client is a bit broken
140 3 ▶sub _raw_scroll_query {
Subroutine _raw_scroll_query: not called CC=2 SCAR=16.4
141 0 my ( $self, $config ) = @_;
142  
143 0 my $creq = $self->client->request;
144  
145 0 my $class = delete $config->{'class'};
146 0 my $type = delete $config->{type};
147  
148 0 my $scroller = $creq->ssearch( $type, { bogus => 1 }, $config );
149  
150 0 ▶ if ( not $class ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class) 0 0 0
151 0 $class = 'MetaCPAN::Client::' . ucfirst( $config->{type} );
152 }
153 0 my $rs = MetaCPAN::Client::ResultSet->new(
154 type => $type,
155 scroller => $scroller,
156 );
157 0 $rs->{class} = $class;
158 0 return $rs;
159 }
160  
161 0 ▶sub _scroll_to_list {
Subroutine _scroll_to_list: not called CC=2 SCAR=17.9
162 0 my ( undef, $scroll ) = @_;
163 0 my @out;
164 0 ▶ while ( my $item = $scroll->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = $scroll->next) 0 0 0
165 0 push @out, $item;
166 }
167 0 return @out;
168 }
169  
170 1 ▶sub find_release {
Subroutine find_release: called CC=1 SCAR=0.8
Pod documented
171 1 my ( $self, $author, $dist ) = @_;
172 1 require Gentoo::Util::MetaCPAN::Release;
173 1 my $query = {
174 type => 'release',
175 class => 'Gentoo::Util::MetaCPAN::Release',
176 body => {
177 query => {
178 bool => {
179 must => [
180 { term => { name => $dist } }, #
181 { term => { author => $author } }, #
182 ],
183 },
184 },
185 },
186 };
187 my $result = $self->_cache_object(
188 [ 'find_release', $author, $dist ] => undef,
189 0 ▶ => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
190 0 return [ $self->_scroll_to_list( $self->_raw_scroll_query($query) ), ];
191 },
192 1 );
193 0 return @{$result};
194 }
195  
196 0 ▶sub find_files_providing {
Subroutine find_files_providing: not called CC=1 SCAR=6.9
Pod documented
197 0 my ( $self, $module_name ) = @_;
198 0 require Gentoo::Util::MetaCPAN::File;
199  
200 0 my @terms;
201 0 push @terms, { term => { 'module.authorized' => 1 } };
202 0 push @terms, { term => { 'module.indexed' => 1 } };
203 0 push @terms, { term => { 'module.name' => $module_name } };
204  
205 0 my $nested_query = {
206 constant_score => {
207 filter => {
208 and => \@terms,
209 },
210 },
211 };
212 0 my $query = {
213 filtered => {
214 query => {
215 nested => {
216 path => 'module',
217 query => $nested_query,
218 },
219 },
220 },
221 };
222 0 my $config = {
223 type => 'file',
224 class => 'Gentoo::Util::MetaCPAN::File',
225 body => {
226 query => $query,
227 },
228 };
229 my $result = $self->_cache_object(
230 [ 'find_files_providing', $module_name ] => undef,
231 0 ▶ => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
232 0 return [ $self->_scroll_to_list( $self->_raw_scroll_query($config) ) ];
233 },
234 0 );
235 0 return @{$result};
236 }
237  
238 1 ▶sub find_latest_files_providing {
Subroutine find_latest_files_providing: called CC=1 SCAR=0.2
Pod documented
239 1 my ( $self, $module_name ) = @_;
240 1 require Gentoo::Util::MetaCPAN::File;
241  
242 1 my @terms;
243 1 push @terms, { term => { 'module.authorized' => 1 } };
244 1 push @terms, { term => { 'module.indexed' => 1 } };
245 1 push @terms, { term => { 'module.name' => $module_name } };
246  
247 1 my $nested_query = {
248 constant_score => {
249 filter => {
250 and => \@terms,
251 },
252 },
253 };
254 1 my $query = {
255 filtered => {
256 query => {
257 nested => {
258 path => 'module',
259 query => $nested_query,
260 },
261 },
262 },
263 };
264 1 my $config = {
265 type => 'file',
266 class => 'Gentoo::Util::MetaCPAN::File',
267  
268 body => {
269 fields => q[*],
270 script_fields => { latest => { 'metacpan_script' => 'status_is_latest' } },
271 query => $query,
272 },
273 };
274 my $result = $self->_cache_object(
275 [ 'find_latest_files_providing', $module_name ] => undef,
276 0 ▶ => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
277 0 return [ grep { $_->latest } $self->_scroll_to_list( $self->_raw_scroll_query($config) ) ];
278 },
279 1 );
280 0 return @{$result};
281  
282 }
283  
284 0 ▶sub find_releases_providing {
Subroutine find_releases_providing: not called CC=1 SCAR=6.9
Pod documented
285 0 my ( $self, $module_name ) = @_;
286 0 require Gentoo::Util::MetaCPAN::Release;
287  
288 0 my $nested_query = {
289 bool => {
290 must => [
291 { term => { 'authorized' => 1 } }, #
292 { term => { 'indexed' => 1 } }, #
293 { term => { 'name' => $module_name } }, #
294 ],
295 },
296 };
297  
298 0 my $query = {
299 nested => {
300 path => 'module',
301 query => $nested_query,
302 },
303 };
304  
305 0 my $config = {
306 type => 'release',
307 class => 'Gentoo::Util::MetaCPAN::Release',
308 body => {
309 query => { # %{$query},
310 constant_score => { query => $query },
311 },
312 },
313 };
314 my $result = $self->_cache_object(
315 [ 'find_releases_providing', $module_name ] => undef,
316 0 ▶ => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
317 0 return [ $self->_scroll_to_list( $self->_raw_scroll_query($config) ) ];
318 },
319 0 );
320 0 return @{$result};
321 }
322  
323 2 ▶sub mcpan { return __PACKAGE__->new() }
Subroutine mcpan: called CC=1 SCAR=0.0
Pod documented
324  
325 3 ▶no Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
326  
327 3 1;
328  
329 __END__
« blib/lib/Gentoo/Util/MetaCPAN/Requirement.pm ↑ Summary lib/Gentoo/Util/MetaCPAN/Release.pm »