AUR/PKGBUILD.pm

filter: Statementstmt 90.0% 126 / 140 Branchbran 75.0% 33 / 44 Conditioncond 75.0% 6 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 94.7% 18 / 19 Podpod 66.6% 2 / 3 totaltotal 85.3% 187 / 219 SCAR 34.1
CC
28
Cov
86%
_pkgbuild_fields
11.6
_unquote_bash
11.0
__ANON__
6.0
CRAP 30.2
« AUR/Iterator.pm ↑ Summary AUR.pm »
1 package WWW::AUR::PKGBUILD;
2  
3 6 ▶use warnings 'FATAL' => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Fcntl qw(SEEK_SET);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Carp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 my @ARRAY_FIELDS = qw{ license source noextract
10 md5sums sha1sums sha256sums sha384sums sha512sums
11 groups arch backup depends makedepends conflicts
12 provides replaces options };
13 # We cannot auto-split optdepends because spaces are allowed.
14  
15 sub new
16 {
17 3 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
18 3 my $self = bless {}, $class;
19  
20 3 ▶ if ( @_ ) { $self->read( @_ ); }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 0 3
21 3 return $self;
22 }
23  
24 #---HELPER FUNCTION---
25 sub _unquote_bash
26 {
27 99 ▶ my ($bashtext, $start, $expander) = @_;
Subroutine _unquote_bash: called CC=11 SCAR=24.0
28 99 my $elem;
29  
30 99 ▶ $expander ||= sub { shift };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$expander ||= sub { shift(); }
l!l&&r!l&&!r
9630
MC/DC0%0/2
$expander ||= sub { shift(); }
$expander sub { shift(); }
Truth table66%2/3
A ||= B
A= $expanderB= sub { shift(); }
ABresult
1X1
011
000
31 99 ▶ $start ||= 0;
Condition100%2/2
$start ||= 0
l!l
7326
MC/DC100%1/1
$start ||= 0
$start
Truth table100%2/2
A
A= $start
Aresult
11
00
32 99 ( pos $bashtext ) = $start;
33  
34 # Extract the values of a bash array...
35 99 ▶ if ( $bashtext =~ / \G [(] ([^)]*) [)] /gcx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bashtext =~ / \G [(] ([^)]*) [)] /cgx) 23 76 99
36 23 my $arrtext = $1;
37 23 my @result;
38  
39 ARRAY_LOOP:
40 23 while ( 1 ) {
41 27 my ($elem, $elem_end) = _unquote_bash( $arrtext,
42 pos $arrtext,
43 $expander );
44 27 ▶ push @result, $elem if $elem;
Branch: 1 branch
BranchTrueFalseTotal
if $elem 19 8 27
45  
46 # There should only be spaces leftover.
47 27 ( pos $arrtext ) = $elem_end;
48 27 ▶ last ARRAY_LOOP if ( $elem_end >= length $arrtext ||
Branch: 1 branch
BranchTrueFalseTotal
if $elem_end >= length $arrtext or not $arrtext =~ /\G\s+/g 23 4 27
Condition66%2/3
$elem_end >= length $arrtext or not $arrtext =~ /\G\s+/g
l!l&&r!l&&!r
2304
MC/DC50%1/2
$elem_end >= length $arrtext or not $arrtext =~ /\G\s+/g
$elem_end >= length $arrtext not $arrtext =~ /\G\s+/g
Truth table66%2/3
A or not(B)
A= $elem_end >= length $arrtextB= not $arrtext =~ /\G\s+/g
ABresult
1X1
011
000
49 $arrtext !~ /\G\s+/g );
50 }
51  
52 # Arrays are special, we do not recurse after we find one.
53 23 return \@result, pos $bashtext;
54 }
55  
56 # The rest is for string "parsing"...
57  
58 # Single quoted strings cannot escape the quote (')...
59 76 ▶ if ( $bashtext =~ /\G'([^']*)'/gc ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($bashtext =~ /\G'([^']*)'/cg) { } 22 54 76
elsif ($bashtext =~ /\G"/cg) { } 3 51 54
elsif ($bashtext =~ /\G([^ \n\t'"]+)/cg) { } 8 43 51
60 22 $elem = $1;
61 }
62 # Double quoted strings can...
63 elsif ( $bashtext =~ /\G"/gc ) {
64 3 my $beg = pos $bashtext;
65 # Skip past escaped double-quotes and non-double-quote chars.
66 3 ▶ while ( $bashtext =~ / \G (?: \\" | [^"] ) /gcx ) { ; }
Branch: 1 branch
BranchTrueFalseTotal
if ($bashtext =~ / \G (?: \\" | [^"] ) /cgx) 65 3 68
67  
68 3 $elem = substr $bashtext, $beg, ( pos $bashtext ) - $beg;
69 3 $elem = $expander->( $elem );
70 3 ++( pos $bashtext ); # skip the closing "
71 }
72 # Otherwise regular words are treated as one element...
73 elsif ( $bashtext =~ /\G([^ \n\t'"]+)/gc ) {
74 8 $elem = $expander->( $1 );
75 }
76 # If none of the above matches, then we stop recursion.
77 43 else { return q{}, $start; }
78  
79 # We recurse in order to concatenate adjacent strings.
80 33 my ( $next_elem, $next_end ) = _unquote_bash( $bashtext,
81 pos $bashtext,
82 $expander );
83 33 return ( $elem . $next_elem, $next_end );
84 }
85  
86 # Perform the simplest parameter expansion possible.
87 sub _expand_bash
88 {
89 9 ▶ my ($bashstr, $fields_ref) = @_;
Subroutine _expand_bash: called CC=1 SCAR=0.2
90  
91 my $expand_field = sub {
92 4 ▶ my $name = shift;
Subroutine __ANON__: called CC=2 SCAR=8.1
93 4 ▶ return $fields_ref->{ $name } if defined $fields_ref->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
if defined $fields_ref->{$name} 4 0 4
94 0 return qq{\$$name};
95 # TODO: error reporting?
96 9 };
97  
98 9 $bashstr =~ s{ \$ ([\w_]+) }
99 4 { $expand_field->( $1 ) }gex;
100  
101 # TODO: check for special expansion modifiers
102 9 $bashstr =~ s( \$ \{ ([^}]+) \} )
103 0 ( $expand_field->( $1 ) )gex;
104  
105 9 return $bashstr;
106 }
107  
108 #---HELPER FUNCTION---
109 sub _depstr_to_hash
110 {
111 9 ▶ my ($depstr) = @_;
Subroutine _depstr_to_hash: called CC=2 SCAR=7.0
112 9 my ($pkg, $cmp, $ver) = $depstr =~ / \A ([^=<>]+)
113 (?: ([=<>]=?)
114 (.*) )? \z/xms;
115  
116 9 ▶ Carp::confess "Failed to parse depend string: $_" unless $pkg;
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg 0 9 9
117  
118 9 return +{ 'pkg' => $pkg, 'cmp' => $cmp,
119 'ver' => $ver, 'str' => $depstr };
120 }
121  
122 sub _provides_to_hash
123 {
124 1 ▶ my ($provstr) = @_;
Subroutine _provides_to_hash: called CC=2 SCAR=7.0
125 1 my ($pkg, $ver) = $provstr =~ / \A ([^=]+)
126 (?: = (.*))?
127 /xms;
128 1 ▶ Carp::confess "Failed to parse provides string: $_" unless $pkg;
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg 0 1 1
129 1 return +{ 'pkg' => $pkg, 'ver' => $ver, 'str' => $provstr };
130 }
131  
132 #---HELPER FUNCTION---
133 sub _pkgbuild_fields
134 {
135 3 ▶ my ($pbtext) = @_;
Subroutine _pkgbuild_fields: called CC=11 SCAR=24.5
136  
137 3 my %pbfields;
138 my $expander = sub {
139 9 ▶ _expand_bash( shift, \%pbfields )
Subroutine __ANON__: called CC=1 SCAR=0.0
140 3 };
141  
142 3 ▶ while ( $pbtext =~ / \G .*? \n? ^ ([\w_]+) = /gxms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pbtext =~ / \G .*? \n? ^ ([\w_]+) = /gmsx) 36 3 39
143 36 my $name = $1;
144 36 my ( $value, $endpos ) = _unquote_bash( $pbtext,
145 pos $pbtext,
146 $expander );
147 36 $pbfields{ $name } = $value;
148 36 ( pos $pbtext ) = $endpos;
149 }
150  
151 # Split arrays at whitespace for poorly made PKGBUILDs...
152 # also ensures each field has an arrayref.
153 ARRAY_LOOP:
154 3 for my $arrkey ( @ARRAY_FIELDS ) {
155 51 ▶ unless ( $pbfields{ $arrkey } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pbfields{$arrkey}) 29 22 51
156 29 $pbfields{ $arrkey } = [];
157 29 next ARRAY_LOOP;
158 }
159  
160 22 my $val_ref = $pbfields{ $arrkey };
161  
162 # Force the value into being an array...
163 22 ▶ $val_ref = [ $val_ref ] unless ref $val_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val_ref 0 22 22
164  
165 # Try to filter out common problems people have with defining arrays.
166 # 1) trailing \'s
167 # 2) commented array items (generally a complete line is commented)
168 # 3) depends=('foo=1 bar<2 baz>=3') (a string separated by spaces)
169 # 4) depends=('turbojpegipp >=1.11') (only in the turbovnc-bin pkg)
170 # (These should be done by the parser, eventually)
171 19 $val_ref = [ grep { $_ ne q{\\} } # *1
172 19 map { split } # *3
173 19 map { s{ \A (\w+) \s+
174 19 ([<>=]{1,2}\d+) }{$1$2}x; $_ } # *4
175 19 map { s/\A\s+//; s/\s+\z//; $_ } # trim ws
176 22 grep { length } map { s/\#.*//; $_ } # *2
177 @$val_ref ];
178  
179 22 $pbfields{ $arrkey } = $val_ref;
180 }
181  
182 # optdepends are special, we should only split on newlines
183 3 ▶ if ( $pbfields{'optdepends'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pbfields{'optdepends'}) { } 1 2 3
184 1 my $optdeps = $pbfields{'optdepends'};
185 1 ▶ $optdeps = [ $optdeps ] unless ref $optdeps;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $optdeps 0 1 1
186  
187 # Remember stupid \'s at the end of lines
188 0 $optdeps = [ grep { length } map { s/\#.*//; $_ }
189 0 grep { $_ ne q{\\} }
190 1 map { s/\A\s+//; s/\s+\z//; $_ }
191 @$optdeps ];
192 1 $pbfields{'optdepends'} = $optdeps;
193 }
194 else {
195 2 $pbfields{'optdepends'} = [];
196 }
197  
198 # Convert all depends into hash references...
199 VERSPEC_LOOP:
200 3 for my $depkey ( qw/ makedepends depends conflicts / ) {
201 9 my @deps = @{ $pbfields{ $depkey } };
202 9 ▶ next VERSPEC_LOOP unless @deps;
Branch: 1 branch
BranchTrueFalseTotal
unless @deps 3 6 9
203  
204 6 eval {
205 6 $pbfields{ $depkey } = [ map { _depstr_to_hash($_) } @deps ];
206 };
207 6 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 6 6
208 0 die qq{Error with "$depkey" field:\n$@};
209 }
210 }
211  
212 # Provides has no comparison operator and may have no version...
213 3 ▶ if ( $pbfields{'provides'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pbfields{"provides"}) 3 0 3
214 $pbfields{'provides'} =
215 3 [ map { _provides_to_hash($_) } @{$pbfields{'provides'}} ];
216 }
217  
218 3 return %pbfields;
219 }
220  
221 #---HELPER FUNCTION---
222 sub _slurp
223 {
224 1 ▶ my ($fh) = @_;
Subroutine _slurp: called CC=2 SCAR=7.0
225  
226 # Make sure we start reading from the beginning of the file...
227 1 ▶ seek $fh, SEEK_SET, 0 or die "seek: $!";
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 0 or die "seek: $!" 0 1 1
228  
229 1 local $/;
230 1 return <$fh>;
231 }
232  
233 sub read
234 {
235 3 ▶ my $self = shift;
Subroutine read: called CC=2 SCAR=6.9
Pod documented
236 3 ▶ $self->{'text'} = ( ref $_[0] eq 'GLOB' ? _slurp( shift ) : shift );
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'GLOB' ? : 1 2 3
237  
238 3 my %pbfields = _pkgbuild_fields( $self->{'text'} );
239 3 $self->{'fields'} = \%pbfields;
240 3 return %pbfields;
241 }
242  
243 sub fields
244 {
245 2 ▶ my ($self) = @_;
Subroutine fields: called CC=1 SCAR=0.0
Pod documented
246 2 return %{ $self->{'fields'} }
247 }
248  
249 sub _def_field_acc
250 {
251 150 ▶ my ($name) = @_;
Subroutine _def_field_acc: called CC=1 SCAR=1.2
252  
253 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
254 150 *{ $name } = sub {
255 0 ▶ my ($self) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
256 0 my $val = $self->{'fields'}{$name};
257  
258 0 ▶ return q{} unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
259 0 return $val;
260 }
261 150 }
262  
263 6 _def_field_acc( $_ ) for qw{ pkgname pkgver pkgdesc pkgrel url
264 license install changelog source
265 noextract md5sums sha1sums sha256sums
266 sha384sums sha512sums groups arch
267 backup depends makedepends optdepends
268 conflicts provides replaces options };
269  
270 6 1;
271  
272 __END__
« AUR/Iterator.pm ↑ Summary AUR.pm »