AUR/Package.pm

filter: Statementstmt 48.9% 72 / 147 Branchbran 8.8% 3 / 34 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 59.3% 19 / 32 Podpod 71.4% 5 / 7 totaltotal 44.3% 99 / 223 SCAR 45.3
CC
19
Cov
41%
download
30.0
pkgbuild
20.0
maintainer
12.0
CRAP 93.2
« AUR/Login.pm ↑ Summary AUR/RPC.pm »
1 package WWW::AUR::Package;
2  
3 5 ▶use warnings 'FATAL' => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use File::Spec qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Carp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶use WWW::AUR::Package::File qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use WWW::AUR::URI qw( pkgbuild_uri pkgfile_uri pkg_uri );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use WWW::AUR::RPC qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use WWW::AUR qw( _path_params _useragent );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 ##############################################################################
17 # CONSTANTS
18 #-----------------------------------------------------------------------------
19  
20 #---CONSTRUCTOR---
21 sub new
22 {
23 5 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=15.2
Pod undocumented
24 5 ▶ Carp::croak( "You must at least supply a name as argument" ) if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 0 5 5
25  
26 5 my $name = shift;
27 5 my %params = @_;
28  
29 5 my $info;
30 5 ▶ if ( $params{info} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'info'}) { } 0 5 5
31 0 $info = $params{info};
32 } else {
33 # this might croak on error
34 5 $info = eval { WWW::AUR::RPC::info( $name ) };
35 5 ▶ Carp::croak( "Failed to find package: $name" ) unless ( $info );
Branch: 1 branch
BranchTrueFalseTotal
unless $info 5 0 5
36 }
37  
38 0 my $self = bless { _path_params( @_ ),
39 pkgfile => "$name.src.tar.gz",
40 info => $info,
41 }, $class;
42  
43 0 return $self;
44 }
45  
46 sub _def_info_accessor
47 {
48 60 ▶ my ($field) = @_;
Subroutine _def_info_accessor: called CC=1 SCAR=0.6
49  
50 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
51 60 *{ "WWW::AUR::Package::$field" } = sub {
52 0 ▶ my ($self) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
53 0 ▶ return $self->{info}{$field} || q{};
Condition0%0/2
$self->{'info'}{$field} || ''
l!l
00
MC/DC0%0/1
$self->{'info'}{$field} || ''
$self->{'info'}{$field}
Truth table0%0/2
A
A= $self->{'info'}{$field}
Aresult
11
00
54 60 };
55 }
56  
57 5 for ( qw{ id name version desc category url urlpath
58 license votes outdated ctime mtime } ) {
59 60 _def_info_accessor( $_ );
60 }
61  
62 sub maintainer_name
63 {
64 0 ▶ my ($self) = @_;
Subroutine maintainer_name: not called CC=1 SCAR=6.9
Pod documented
65 0 return $self->{'info'}{'maintainer'}; # might be undef for orphan
66 }
67  
68 #---PUBLIC METHOD---
69 # Returns a copy of the package info as a hash...
70 sub info
71 {
72 0 ▶ my ($self) = @_;
Subroutine info: not called CC=1 SCAR=6.9
Pod documented
73 0 return %{ $self->{info} };
74 }
75  
76 #---PRIVATE METHOD---
77 sub _download_url
78 {
79 0 ▶ my ($self) = @_;
Subroutine _download_url: not called CC=1 SCAR=6.9
80  
81 0 return pkgfile_uri( $self->name );
82 }
83  
84 #---OBJECT METHOD---
85 sub download_size
86 {
87 0 ▶ my ($self) = @_;
Subroutine download_size: not called CC=2 SCAR=17.9
Pod undocumented
88  
89 0 my $ua = _useragent();
90 0 my $resp = $ua->head( $self->_download_url() );
91
92 0 ▶ return undef unless $resp->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->is_success 0 0 0
93 0 return $resp->header( 'content-length' );
94 }
95  
96 #---OBJECT METHOD---
97 sub download
98 {
99 0 ▶ my ($self, $usercb) = @_;
Subroutine download: not called CC=5 SCAR=34.0
Pod documented
100  
101 0 my $pkgurl = $self->_download_url();
102 my $pkgpath = File::Spec->catfile( $self->{dlpath},
103 0 $self->{pkgfile} );
104  
105 0 make_path( $self->{dlpath} );
106  
107 0 ▶ open my $pkgfile, '>', $pkgpath or die "Failed to open $pkgpath:\n$!";
Branch: 1 branch
BranchTrueFalseTotal
open my $pkgfile, ">", $pkgpath or die "Failed to open ${pkgpath}:\n$!" 0 0 0
108 0 binmode $pkgfile;
109  
110 my $store_chunk = sub {
111 0 ▶ my $chunk = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
112 0 print $pkgfile $chunk;
113 0 };
114  
115 0 ▶ if ( $usercb ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usercb) 0 0 0
116 0 my $total = $self->download_size();
117 0 my $dled = 0;
118  
119 0 my $store = $store_chunk;
120 $store_chunk = sub {
121 0 ▶ my $chunk = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
122 0 $dled += length $chunk;
123 0 $usercb->( $dled, $total );
124 0 $store->( $chunk );
125 0 };
126 }
127  
128 0 my $ua = _useragent();
129 0 my $resp = $ua->get( $self->_download_url(),
130 ':content_cb' => $store_chunk );
131 0 ▶ close $pkgfile or die "close: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $pkgfile or die "close: $!" 0 0 0
132 0 ▶ Carp::croak( 'Failed to download package file:' . $resp->status_line )
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->is_success 0 0 0
133 unless $resp->is_success;
134  
135 0 $self->{pkgfile_obj} = WWW::AUR::Package::File->new
136 ( $pkgpath, _path_params( %$self ));
137  
138 0 return $pkgpath;
139 }
140  
141 #---PUBLIC METHOD---
142 # Purpose: Returns an object representing the package maintainer.
143 sub maintainer
144 {
145 0 ▶ my $self = shift;
Subroutine maintainer: not called CC=3 SCAR=24.8
Pod documented
146 0 my $mname = $self->maintainer_name();
147 0 ▶ return undef unless defined $mname;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mname 0 0 0
148  
149 # Propogate parameters to our new Maintainer object...
150 0 ▶ Carp::croak 'Only a hash of path parameters are allowed as argument'
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 == 0 0 0 0
151 unless @_ % 2 == 0;
152  
153 0 require WWW::AUR::Maintainer;
154  
155 # Propogate parameters to our new Maintainer object...
156 # Path parameters given as arguments override the path params the
157 # package object was given...
158 0 my %params = ( _path_params( %$self ), _path_params( @_ ) );
159 0 my $mobj = WWW::AUR::Maintainer->new( $mname, %params );
160 0 return $mobj;
161 }
162  
163 sub _def_file_wrapper
164 {
165 30 ▶ my ($name) = @_;
Subroutine _def_file_wrapper: called CC=1 SCAR=1.0
166  
167 5 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
168 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
169 30 my $file_method = *{ $WWW::AUR::Package::File::{$name} }{ 'CODE' };
170 30 *{ $name } = sub {
171 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
172 0 ▶ return undef unless $self->{'pkgfile_obj'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pkgfile_obj"} 0 0 0
173 0 my $ret = eval { $file_method->( $self->{'pkgfile_obj'}, @_ ) };
174 0 ▶ die if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
175 0 return $ret;
176 30 };
177 }
178  
179 5 _def_file_wrapper( $_ ) for qw{ extract src_pkg_path
180 src_dir_path make_src_path build
181 bin_pkg_path };
182  
183 # Wrap the Package::File methods to call download first if we have to...
184 sub _def_dl_wrapper
185 {
186 10 ▶ my ($name) = @_;
Subroutine _def_dl_wrapper: called CC=1 SCAR=0.4
187  
188 5 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
189 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
190  
191 10 my $oldcode = *{ $name }{ 'CODE' };
192 10 *{ $name } = sub {
193 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
194 0 ▶ unless ( $self->{'pkgfile_obj'} ) { $self->download(); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"pkgfile_obj"}) 0 0 0
195 0 return $oldcode->( $self, @_ );
196 10 };
197 }
198  
199 5 _def_dl_wrapper( $_ ) for qw/ extract build /;
200  
201 #---PRIVATE METHOD---
202 # Purpose: Download the package's PKGBUILD without saving it to a file.
203 sub _download_pkgbuild
204 {
205 0 ▶ my ($self) = @_;
Subroutine _download_pkgbuild: not called CC=2 SCAR=17.9
206  
207 0 my $name = $self->name;
208 0 my $pkgbuild_uri = pkgbuild_uri( $name );
209  
210 0 my $ua = _useragent();
211 0 my $resp = $ua->get( $pkgbuild_uri );
212  
213 0 ▶ Carp::croak "Failed to download ${name}'s PKGBUILD: "
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->is_success 0 0 0
214 . $resp->status_line() unless $resp->is_success();
215  
216 0 return $resp->content();
217 }
218  
219 sub pkgbuild
220 {
221 0 ▶ my ($self) = @_;
Subroutine pkgbuild: not called CC=4 SCAR=30.0
Pod documented
222  
223 return $self->{pkgfile_obj}->pkgbuild
224 0 ▶ if $self->{pkgfile_obj};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pkgfile_obj"} 0 0 0
225  
226 return $self->{pkgbuild_obj}
227 0 ▶ if $self->{pkgbuild_obj};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pkgbuild_obj"} 0 0 0
228  
229 0 my $pbtext = $self->_download_pkgbuild;
230  
231 0 $self->{pkgbuild_txt} = $pbtext;
232 0 $self->{pkgbuild_obj} = eval { WWW::AUR::PKGBUILD->new( $pbtext ) };
233 0 ▶ Carp::confess if $@; # stack trace
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
234  
235 0 return $self->{pkgbuild_obj};
236 }
237  
238 5 1;
« AUR/Login.pm ↑ Summary AUR/RPC.pm »