P5U/Lib/DebianRelease.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 141 CC 7 SCAR 18.6
CC
7
Cov
0%
_build_debian
20.0
author_data
12.0
format_report
6.0
CRAP 6.4
1 package P5U::Lib::DebianRelease;
2  
3 # This is largely based on a script by SHARYANTO
4  
5 use 5.010;
6 use utf8;
7  
8 BEGIN {
9 $P5U::Lib::DebianRelease::AUTHORITY = 'cpan:TOBYINK';
10 $P5U::Lib::DebianRelease::VERSION = '0.100';
11 };
12  
13 use Moo;
14 use IO::Uncompress::Gunzip qw< gunzip $GunzipError >;
15 use JSON 2.00 qw< from_json >;
16 use LWP::Simple 0 qw< get >;
17 use match::smart 0 qw< M >;
18 use Object::AUTHORITY qw/AUTHORITY/;
19 use Type::Utils 0 qw< class_type >;
20 use Types::Standard 0.004 qw< HashRef >;
21  
22 my $json = JSON::->new->allow_nonref;
23  
24 sub dist2deb
25 {
26 my ($dist) = @_;
27 "lib".lc($dist)."-perl";
28 }
29  
30 use namespace::clean;
31  
32 has debian => (
33 is => 'lazy',
34 isa => HashRef,
35 );
36  
37 has cache_file => (
38 is => 'ro',
39 required => 1,
40 isa => class_type { class => 'Path::Tiny' },
41 );
42  
43 sub _build_debian
44 {
45 my $self = shift;
46 my %pkgs;
47 unless ((-f $self->cache_file) && (-M _) < 7)
48 {
49 my $res = get "http://packages.debian.org/unstable/allpackages?format=txt.gz"
50 or die "get failed\n";
51 ($res =~ /^All Debian Package/is)
52 ? $self->cache_file->spew([$res])
53 : gunzip(\$res => $self->cache_file->stringify)
54 or die "gunzip failed: $GunzipError\n";
55 }
56 for ($self->cache_file->lines_utf8)
57 {
58 next unless /^(lib\S+?-perl) \(([^\s\)]+).*\)/;
59 $pkgs{$1} = $2;
60 }
61 \%pkgs
62 }
63  
64 sub author_report
65 {
66 my $self = shift;
67 $self->format_report(
68 $self->author_data(@_)
69 )
70 }
71  
72 sub distribution_report
73 {
74 my $self = shift;
75 $self->format_report(
76 $self->distribution_data(@_)
77 )
78 }
79  
80 sub format_report
81 {
82 my ($self, $data) = @_;
83 join q(),
84 sprintf(
85 "%-40s%15s%15s %s\n",
86 qw(PACKAGE CPAN DEBIAN WARNING)
87 ),
88 map {
89 my ($dist, $cpan, $deb) = @$_;
90 (my $debx = $deb) =~ s/[-].+//;
91 sprintf(
92 "%-40s%15s%15s %s\n",
93 $dist,
94 $cpan,
95 $deb,
96 ($debx eq $cpan ? q[ ] : q[!!]),
97 );
98 }
99 @$data;
100 }
101  
102 sub author_data
103 {
104 my ($self, $author) = @_;
105  
106 my $res = get "http://api.metacpan.org/v0/release/_search?q=author:".
107 uc($author)."%20AND%20status:latest&fields=name&size=1000";
108 $res = $json->decode($res);
109 die "MetaCPAN timed out" if $res->{timed_out};
110  
111 my $pkgs = $self->debian;
112  
113 my %dists;
114 for my $hit (@{ $res->{hits}{hits} })
115 {
116 my $dist = $hit->{fields}{name};
117 $dist =~ s/-(\d.+)//;
118 $dists{$dist} = $1;
119 }
120  
121 # use Data::Dumper;
122 # $Data::Dumper::Sortkeys = 1;
123 # print Dumper $pkgs;
124  
125 my @data;
126 for my $dist (sort keys %dists)
127 {
128 my $pkg = dist2deb($dist);
129 next unless $pkg |M| $pkgs;
130
131 push @data => [
132 $dist,
133 $dists{$dist},
134 $pkgs->{$pkg},
135 ];
136 }
137 \@data;
138 }
139  
140 sub distribution_data
141 {
142 my $self = shift;
143 my $dist = from_json get sprintf('http://api.metacpan.org/v0/release/%s', @_);
144 my $pkg = dist2deb $dist->{distribution};
145
146 [[
147 $dist->{distribution},
148 $dist->{version},
149 ($self->debian->{$pkg} // '(none)'),
150 ]]
151 }
152  
153 1;
154  
155 __END__