Utils.pm

filter: Statementstmt 33.5% 58 / 173 Branchbran 3.1% 1 / 32 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 53.3% 16 / 30 Podpod 90.0% 9 / 10 totaltotal 31.2% 84 / 269 CC 25 SCAR 25.8
CC
25
Cov
27%
find_module
30.0
_create_module_index
30.0
fetch_options
30.0
CRAP 13.2
↑ Summary Utils/Docs.pm »
1 ########################################################################
2 package DarkPAN::Utils;
3 ########################################################################
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Archive::Tar;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use English qw(no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::Basename qw(fileparse);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Getopt::Long qw(:config no_ignore_case);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use IO::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use List::Util qw(none);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Log::Log4perl::Level;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 Readonly::Scalar our $BASE_URL => q{};
23 2 Readonly::Scalar our $PACKAGES_DETAILS => '02packages.details.txt.gz';
24  
25 2 Readonly::Scalar our $TRUE => 1;
26 2 Readonly::Scalar our $FALSE => 0;
27  
28 2 ▶use parent qw(Class::Accessor::Validated);
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 2 our $VERSION = '1.1.0';
31  
32 2 our @EXPORT_OK = qw(parse_distribution_path);
33  
34 2 our %ATTRIBUTES = (
35 logger => $FALSE,
36 log_level => $FALSE,
37 package => $FALSE, # Archive::Tar of unzip packag
38 module_index => $FALSE, # distribution tarball indexed list of contents
39 darkpan_index => $FALSE, # distribution list (raw)
40 help => $FALSE,
41 base_url => $TRUE,
42 module => $FALSE,
43 );
44  
45 2 __PACKAGE__->setup_accessors( keys %ATTRIBUTES );
46  
47 2 ▶caller or __PACKAGE__->main();
Branch: 1 branch
BranchTrueFalseTotal
caller or __PACKAGE__->main 2 0 2
48  
49 ########################################################################
50 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
51 ########################################################################
52 0 my ( $class, @args ) = @_;
53  
54 0 ▶ my $options = ref $args[0] ? $args[0] : {@args};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 0 0
55  
56 0 ▶ $options->{log_level} //= 'info';
Condition0%0/2
$options->{"log_level"} //= "info"
l!l
00
MC/DC0%0/1
$options->{"log_level"} //= "info"
$options->{"log_level"}
Truth table0%0/2
A
A= $options->{"log_level"}
Aresult
11
00
57  
58 # base_url only required when not constructing from a local package
59 0 local $ATTRIBUTES{base_url} = !$options->{package};
60  
61 0 my $self = $class->SUPER::new($options);
62  
63 0 return $self;
64 }
65  
66 ########################################################################
67 0 ▶sub parse_distribution_path {
Subroutine parse_distribution_path: not called CC=1 SCAR=6.9
Pod documented
68 ########################################################################
69 0 my ($path) = @_;
70  
71 0 my @distribution = ( $path =~ m{(?:.*/)?([^/]*?)-([v\d.]+)[.]tar[.]gz$}xsm );
72  
73 0 return @distribution;
74 }
75  
76 ########################################################################
77 0 ▶sub find_module {
Subroutine find_module: not called CC=5 SCAR=34.0
Pod documented
78 ########################################################################
79 0 my ( $self, $module ) = @_;
80  
81 0 my $module_index = $self->get_module_index;
82  
83 0 my @found;
84  
85 0 foreach my $p ( keys %{$module_index} ) {
86  
87 0 my ($distribution) = parse_distribution_path($p);
88  
89 0 ▶ if ( $distribution eq $module ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($distribution eq $module) 0 0 0
90 0 push @found, $p;
91 0 next;
92 }
93  
94 0 ▶ next if none { $_ eq $module } @{ $module_index->{$p} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $_ eq $module; } , @{$$module_index{$p};}) 0 0 0
95  
96 0 push @found, $p;
97 }
98  
99 return
100 0 ▶ if !@found;
Branch: 1 branch
BranchTrueFalseTotal
unless @found 0 0 0
101  
102 0 return \@found;
103 }
104  
105 ########################################################################
106 0 ▶sub extract_file {
Subroutine extract_file: not called CC=2 SCAR=17.9
Pod documented
107 ########################################################################
108 0 my ( $self, $file ) = @_;
109  
110 0 my $package = $self->get_package;
111  
112 0 my @list = $package->list_files;
113  
114 return
115 0 ▶ if none { $file eq $_ } @list;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $file eq $_; } , @list) 0 0 0
116  
117 0 return $package->get_content($file);
118 }
119  
120 ########################################################################
121 0 ▶sub extract_module {
Subroutine extract_module: not called CC=1 SCAR=6.9
Pod documented
122 ########################################################################
123 0 my ( $self, $package, $module ) = @_;
124  
125 0 $package =~ s{(?:.*\/)?([^\/]+)[.]tar[.]gz$}{$1}xsm;
126  
127 0 my $file = $module;
128  
129 0 $file =~ s/::/\//xsmg;
130  
131 0 return $self->extract_file( sprintf '%s/lib/%s.pm', $package, $file );
132 }
133  
134 ########################################################################
135 0 ▶sub fetch_darkpan_index {
Subroutine fetch_darkpan_index: not called CC=4 SCAR=30.0
Pod documented
136 ########################################################################
137 0 my ($self) = @_;
138  
139 0 ▶ return $self
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_darkpan_index 0 0 0
140 if $self->get_darkpan_index;
141  
142 0 my $file = $PACKAGES_DETAILS;
143  
144 0 my $index_url = sprintf '%s/modules/%s', $self->get_base_url, $file;
145  
146 0 my $rsp = HTTP::Tiny->new->get($index_url);
147  
148 0 my $index = q{};
149  
150 die Dumper( [ rsp => $rsp ] )
151 0 ▶ if !$rsp->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $rsp->{"success"} 0 0 0
152  
153 0 my $index_zipped = $rsp->{content};
154  
155 0 ▶ gunzip( \$index_zipped, \$index )
Branch: 1 branch
BranchTrueFalseTotal
gunzip(\$index_zipped, \$index) or die "unzip failed: $DarkPAN::Utils::GunzipError\n" 0 0 0
156 or die "unzip failed: $GunzipError\n";
157  
158 0 $self->set_darkpan_index($index);
159  
160 0 $self->_create_module_index;
161  
162 0 return $self;
163 }
164  
165 ########################################################################
166 0 ▶sub fetch_package {
Subroutine fetch_package: not called CC=3 SCAR=24.8
Pod documented
167 ########################################################################
168 0 my ( $self, $package_name ) = @_;
169  
170 0 my $logger = $self->get_logger;
171  
172 0 my $package_url = sprintf '%s/authors/id/%s', $self->get_base_url, $package_name;
173  
174 0 my $rsp = HTTP::Tiny->new->get($package_url);
175  
176 die Dumper( [ rsp => $rsp ] )
177 0 ▶ if !$rsp->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $rsp->{"success"} 0 0 0
178  
179 0 my $package_zipped = $rsp->{content};
180 0 my $package = q{};
181  
182 0 gunzip( \$package_zipped, \$package );
183  
184 0 my $tar = Archive::Tar->new;
185  
186 0 my $fh = IO::Scalar->new( \$package );
187  
188 0 $tar->read($fh);
189  
190 0 $self->set_package($tar);
191  
192 0 ▶ if ($logger) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logger) 0 0 0
193 $logger->debug(
194 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
195 0 return Dumper( [ files => $tar->list_files ] );
196 }
197 0 );
198 }
199  
200 0 return $self;
201 }
202  
203 ########################################################################
204 0 ▶sub _create_module_index {
Subroutine _create_module_index: not called CC=5 SCAR=34.0
205 ########################################################################
206 0 my ($self) = @_;
207  
208 0 my $index = $self->get_darkpan_index;
209  
210 0 $index =~ s/^(?:.*)?\n\n//xsm;
211  
212 0 my @modules = split /\n/xsm, $index;
213  
214 0 my %module_index;
215 my %module_versions;
216 0 my %module_zip; # tracks the zip currently representing each module
217  
218 0 foreach (@modules) {
219 0 my ( $module, $version, $zip ) = split /\s+/xsm;
220  
221 0 ▶ if ( $module_versions{$module} && $version gt $module_versions{$module} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module_versions{$module} and $version gt $module_versions{$module}) 0 0 0
Condition0%0/3
$module_versions{$module} and $version gt $module_versions{$module}
!ll&&!rl&&r
000
MC/DC0%0/2
$module_versions{$module} and $version gt $module_versions{$module}
$module_versions{$module} $version gt $module_versions{$module}
Truth table0%0/3
A and B
A= $module_versions{$module}B= $version gt $module_versions{$module}
ABresult
0X0
100
111
222 0 delete $module_index{ $module_zip{$module} }; # delete the OLD zip
223 }
224  
225 0 $module_versions{$module} = $version;
226 0 $module_zip{$module} = $zip; # update the tracker
227 0 ▶ $module_index{$zip} //= [];
Condition0%0/2
$module_index{$zip} //= []
l!l
00
MC/DC0%0/1
$module_index{$zip} //= []
$module_index{$zip}
Truth table0%0/2
A
A= $module_index{$zip}
Aresult
11
00
228 0 push @{ $module_index{$zip} }, $module;
229 }
230  
231 0 $self->set_module_index( \%module_index );
232  
233 0 return $self;
234 }
235  
236 ########################################################################
237 0 ▶sub init_logger {
Subroutine init_logger: not called CC=3 SCAR=24.8
Pod documented
238 ########################################################################
239 0 my ($self) = @_;
240  
241 0 ▶ my $level = $self->get_log_level // 'info';
Condition0%0/2
$self->get_log_level // "info"
l!l
00
MC/DC0%0/1
$self->get_log_level // "info"
$self->get_log_level
Truth table0%0/2
A
A= $self->get_log_level
Aresult
11
00
242  
243 $level = {
244 'trace' => $TRACE,
245 'debug' => $DEBUG,
246 'info' => $INFO,
247 'warn' => $WARN,
248 'error' => $ERROR,
249 'trace' => $TRACE,
250 0 ▶ }->{$level} // $INFO;
Condition0%0/3
{"trace", $DarkPAN::Utils::TRACE, "debug", $DarkPAN::Utils::DEBUG, "info", $DarkPAN::Utils::INFO, "warn", $DarkPAN::Utils::WARN, "error", $DarkPAN::Utils::ERROR, "trace", $DarkPAN::Utils::TRACE}->{$level} // $DarkPAN::Utils::INFO
l!l&&r!l&&!r
000
MC/DC0%0/2
{"trace", $DarkPAN::Utils::TRACE, "debug", $DarkPAN::Utils::DEBUG, "info", $DarkPAN::Utils::INFO, "warn", $DarkPAN::Utils::WARN, "error", $DarkPAN::Utils::ERROR, "trace", $DarkPAN::Utils::TRACE}->{$level} // $DarkPAN::Utils::INFO
{"trace", $DarkPAN::Utils::TRACE, "debug", $DarkPAN::Utils::DEBUG, "info", $DarkPAN::Utils::INFO, "warn", $DarkPAN::Utils::WARN, "error", $DarkPAN::Utils::ERROR, "trace", $DarkPAN::Utils::TRACE}->{$level} $DarkPAN::Utils::INFO
Truth table0%0/3
A // B
A= {"trace", $DarkPAN::Utils::TRACE, "debug", $DarkPAN::Utils::DEBUG, "info", $DarkPAN::Utils::INFO, "warn", $DarkPAN::Utils::WARN, "error", $DarkPAN::Utils::ERROR, "trace", $DarkPAN::Utils::TRACE}->{$level}B= $DarkPAN::Utils::INFO
ABresult
1X1
011
000
251  
252 0 Log::Log4perl->easy_init($level);
253  
254 0 $self->set_logger( Log::Log4perl->get_logger );
255  
256 0 return $self;
257 }
258  
259 ########################################################################
260 0 ▶sub fetch_options {
Subroutine fetch_options: not called CC=5 SCAR=34.0
Pod documented
261 ########################################################################
262  
263 0 my %options = (
264 'log-level' => 'info',
265 'base-url' => $BASE_URL,
266 );
267  
268 0 my @option_specs = qw(
269 help|h
270 package|p=s
271 module|m=s
272 log-level|l=s
273 base-url|u=s
274 );
275  
276 0 my $retval = GetOptions( \%options, @option_specs );
277  
278 0 ▶ if ( !$retval || $options{help} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $retval or $options{"help"}) 0 0 0
Condition0%0/3
not $retval or $options{"help"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $retval or $options{"help"}
not $retval $options{"help"}
Truth table0%0/3
not(A) or B
A= not $retvalB= $options{"help"}
ABresult
1X1
011
000
279 0 pod2usage( -exitval => 1, -verbose => 1 );
280 }
281  
282 0 foreach my $o ( keys %options ) {
283 0 ▶ next if $o !~ /[-]/xsm;
Branch: 1 branch
BranchTrueFalseTotal
unless $o =~ /[-]/msx 0 0 0
284  
285 0 my $value = delete $options{$o};
286 0 $o =~ s/[-]/_/xsm;
287 0 $options{$o} = $value;
288 }
289  
290 0 return \%options;
291 }
292  
293 ########################################################################
294 0 ▶sub main {
Subroutine main: not called CC=3 SCAR=24.8
Pod undocumented
295 ########################################################################
296  
297 0 my $options = fetch_options();
298  
299 0 my $self = DarkPAN::Utils->new($options);
300  
301 0 $self->init_logger;
302  
303 0 my $logger = $self->get_logger;
304  
305 0 $self->fetch_darkpan_index;
306  
307 $logger->trace(
308 Dumper(
309 0 [ packages => [ sort keys %{ $self->get_module_index } ],
310 module_index => $self->get_module_index,
311 ]
312 )
313 );
314  
315 0 my $module = $self->get_module;
316  
317 0 ▶ if ($module) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module) 0 0 0
318 0 my $package = $self->find_module($module);
319  
320 0 ▶ die sprintf "could not find %s\n", $module
Branch: 1 branch
BranchTrueFalseTotal
unless $package 0 0 0
321 if !$package;
322  
323 0 $logger->info( sprintf 'fetching package: %s', $package );
324  
325 0 $self->fetch_package($package);
326  
327 0 my $file = $self->extract_module( $package, $module );
328  
329 0 my $docs = DarkPAN::Module::Docs->new($file);
330  
331 0 print {*STDOUT} $docs->{html};
332 }
333  
334 0 return 0;
335 }
336  
337 2 1;
338  
339 __END__
↑ Summary Utils/Docs.pm »