Query.pm

filter: Statementstmt 26.3% 24 / 91 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 36.8% 7 / 19 Podpod 100% 9 / 9 totaltotal 25.6% 40 / 156 CC 16 SCAR 24.5
CC
16
Cov
19%
_run3_array
42.0
command
12.0
query
6.0
CRAP 11.6
↑ Summary Query/Capability.pm »
1 package RPM::Query;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use base qw{Package::New};
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use List::Util qw{uniq};
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use IPC::Run3 qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use RPM::Query::Package;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use RPM::Query::Capability;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 our $VERSION = '0.05';
11  
12 =head1 NAME
13  
14 RPM::Query - Perl object overlay of the RPM query command
15  
16 =head1 SYNOPSIS
17  
18  
19 use RPM::Query;
20 my $rpm = RPM::Query->new;
21 my $pkg = $rpm->query('perl');
22 my $requires = $pkg->requires;
23 foreach my $capability (@$requires) {
24 printf "Capability: %s\n", $capability->name;
25 my $whatprovides = $capability->whatprovides;
26 foreach my $package (@$whatprovides) { #could be zero or more but normally one
27 printf " Package: %s\n", $package->package_name;
28 }
29 }
30  
31 =head1 DESCRIPTION
32  
33 =head1 METHODS
34  
35 =head2 query
36  
37 Returns an the last object of the passed in package name or undef if not installed.
38  
39 my $package_obj = $rpm->query("my_package") or die("my_package is not installed");
40 my $long_name = $package_obj->package_name;
41  
42 Wrapper around
43  
44 $ rpm --query | tail -n 1
45 perl-5.16.3-299.el7_9.x86_64
46  
47 =cut
48  
49 0 ▶sub query {
Subroutine query: not called CC=2 SCAR=17.9
Pod documented
50 0 my $self = shift;
51 0 ▶ my $name = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $name = shift() or die 0 0 0
52 0 my $array = $self->query_list($name);
53 0 return $array->[-1];
54 }
55  
56 =head2 query_list
57  
58 my $packages_aref = $rpm->query("kernel");
59  
60 Wrapper around
61  
62 $ rpm -q kernel
63 kernel-3.10.0-1160.76.1.el7.x86_64
64 kernel-3.10.0-1160.80.1.el7.x86_64
65 kernel-3.10.0-1160.81.1.el7.x86_64
66 kernel-3.10.0-1160.83.1.el7.x86_64
67 kernel-3.10.0-1160.88.1.el7.x86_64
68  
69 =cut
70  
71 0 ▶sub query_list {
Subroutine query_list: not called CC=2 SCAR=17.9
Pod documented
72 0 my $self = shift;
73 0 ▶ my $name = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $name = shift() or die 0 0 0
74 0 my $array = $self->_run3_array('--query' => $name);
75 0 return [map {RPM::Query::Package->new(package_name=>$_, parent=>$self)} @$array];
76 }
77  
78 =head2 details
79  
80 Returns a HASH data structure of the details of the passed in package name.
81  
82 my $hash = $rpm->details("my_package");
83 my $version = $hash->{'version'};
84  
85 Wrapper around
86  
87 $ rpm --query perl --queryformat '%{name} %{version} ...'
88 perl 5.16.3 ...
89  
90 =cut
91  
92 5 our @QUERY_FORMAT_FIELDS = qw{name version release epoch arch group size license packager url summary description sourcerpm sigmd5 buildtime buildhost installtime distribution vendor}; #rpm info plus
93  
94 0 ▶sub details {
Subroutine details: not called CC=2 SCAR=17.9
Pod documented
95 0 my $self = shift;
96 0 ▶ my $arg = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $arg = shift() or die 0 0 0
97 0 my $format = join('|x1|', map {"%{$_}"} @QUERY_FORMAT_FIELDS); #delimiter is '|x1|'
98 0 my $scalar = $self->_run3_scalar('--query' => $arg, '--queryformat' => $format); #isa ARRAY
99 0 my %hash = ();
100 0 @hash{@QUERY_FORMAT_FIELDS} = split /\|x1\|/, $scalar; #hash slice assignment
101 0 return \%hash;
102 }
103  
104 =head2 verify
105  
106 Returns true if verify is clean
107  
108 Wrapper around
109  
110 $ rpm --verify perl && echo 1 || echo 0
111 1
112  
113 =cut
114  
115 0 ▶sub verify {
Subroutine verify: not called CC=1 SCAR=6.9
Pod documented
116 0 my $self = shift;
117 0 my $name = shift;
118 0 my $error = $self->_run3_error('--verify' => $name);
119 0 return !$error;
120 }
121  
122 =head2 whatprovides
123  
124 Returns a list of packages that provides the capability
125  
126 my $package = $rpm->whatprovides('perl(strict)'); #isa ARRAY of RPM::Query::Package
127  
128 Wrapper around
129  
130 $ rpm --query --whatprovides 'perl(strict)'
131 perl-5.16.3-299.el7_9.x86_64
132  
133 =cut
134  
135 0 ▶sub whatprovides {
Subroutine whatprovides: not called CC=2 SCAR=17.9
Pod documented
136 0 my $self = shift;
137 0 ▶ my $capability = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $capability = shift() or die 0 0 0
138 0 my $array = $self->_run3_array('--query' => '--whatprovides' => $capability);
139 0 return [map {RPM::Query::Package->new(package_name=>$_, parent=>$self)} uniq sort @$array];
140 }
141  
142 =head2 provides
143  
144 Returns a list of capabilities that the installed package provides
145  
146 my $capabilities = $rpm->provides('perl'); #isa ARRAY of RPM::Query::Capability objects
147  
148 Wrapper around
149  
150 $ rpm --query --provides 'perl'
151 perl = 4:5.16.3-299.el7_9
152 perl(AutoLoader) = 5.72
153 perl(B) = 1.35
154 perl(B::Section)
155 ...
156  
157 =cut
158  
159 0 ▶sub provides {
Subroutine provides: not called CC=2 SCAR=17.9
Pod documented
160 0 my $self = shift;
161 0 ▶ my $capability = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $capability = shift() or die 0 0 0
162 0 my $array = $self->_run3_array('--query' => '--provides' => $capability);
163 0 return [map {RPM::Query::Capability->new(capability_name=>$_, parent=>$self)} uniq sort @$array];
164 }
165  
166 =head2 requires
167  
168 Returns a list of capabilities that the package requires
169  
170 my $capabilities = $rpm->requires('perl'); #isa ARRAY of RPM::Query::Capability objects
171  
172 Wrapper around
173  
174 $ rpm --query --requires perl
175 /usr/bin/perl
176 libpthread.so.0()(64bit)
177 perl >= 0:5.000
178 ...
179  
180 =cut
181  
182 0 ▶sub requires {
Subroutine requires: not called CC=2 SCAR=17.9
Pod documented
183 0 my $self = shift;
184 0 ▶ my $capability = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $capability = shift() or die 0 0 0
185 0 my $array = $self->_run3_array('--query' => '--requires' => $capability);
186 0 return [map {RPM::Query::Capability->new(capability_name=>$_, parent=>$self)} uniq sort @$array];
187 }
188  
189 =head2 whatrequires
190  
191 Returns a list of packages that this capability requires
192  
193 my $capabilities = $rpm->whatrequires('perl'); #isa ARRAY of RPM::Query::Package objects
194  
195 Wrapper around
196  
197 $ rpm --query --whatrequires perl
198 perl-podlators-2.5.1-3.el7.noarch
199 perl-Pod-Perldoc-3.20-4.el7.noarch
200 perl-Text-ParseWords-3.29-4.el7.noarch
201 perl-Pod-Usage-1.63-3.el7.noarch
202 perl-threads-shared-1.43-6.el7.x86_64
203 perl-Filter-1.49-3.el7.x86_64
204 perl-Exporter-5.68-3.el7.noarch
205 ...
206  
207 =cut
208  
209 0 ▶sub whatrequires {
Subroutine whatrequires: not called CC=2 SCAR=17.9
Pod documented
210 0 my $self = shift;
211 0 ▶ my $capability = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $capability = shift() or die 0 0 0
212 0 my $array = $self->_run3_array('--query' => '--whatrequires' => $capability);
213 0 return [map {RPM::Query::Package->new(package_name=>$_, parent=>$self)} uniq sort @$array];
214 }
215  
216 0 ▶sub _run3_scalar {
Subroutine _run3_scalar: not called CC=2 SCAR=17.9
217 0 my $self = shift;
218 0 my @argv = @_;
219 0 my $stdout = '';
220 0 my $error = 0;
221 0 IPC::Run3::run3 [$self->command => @argv], \undef, \$stdout, \$error;
222 0 ▶ die("Error: Command returned $error") if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
223 0 $stdout =~ s/\s+\Z//; #RTRIM
224 0 return $stdout;
225 }
226  
227 0 ▶sub _run3_array {
Subroutine _run3_array: not called CC=6 SCAR=37.4
228 0 my $self = shift;
229 0 my @argv = @_;
230 0 my @stdout = ();
231 0 my $error = 0;
232 0 IPC::Run3::run3 [$self->command => @argv], \undef, \@stdout, \$error;
233 0 chomp @stdout;
234 0 ▶ @stdout = () if (@stdout == 1
Branch: 1 branch
BranchTrueFalseTotal
if @stdout == 1 and $stdout[0] =~ /\Ano package provides / || $stdout[0] =~ /\Ano package requires / || $stdout[0] =~ /\Apackage .* is not installed\Z/ 0 0 0
Condition0%0/9
@stdout == 1 and $stdout[0] =~ /\Ano package provides / || $stdout[0] =~ /\Ano package requires / || $stdout[0] =~ /\Apackage .* is not installed\Z/
!ll&&!rl&&r
000
$stdout[0] =~ /\Ano package provides / or $stdout[0] =~ /\Ano package requires / or $stdout[0] =~ /\Apackage .* is not installed\Z/
l!l&&r!l&&!r
000
$stdout[0] =~ /\Ano package provides / or $stdout[0] =~ /\Ano package requires /
l!l&&r!l&&!r
000
MC/DC0%0/4
@stdout == 1 and $stdout[0] =~ /\Ano package provides / || $stdout[0] =~ /\Ano package requires / || $stdout[0] =~ /\Apackage .* is not installed\Z/
@stdout == 1 $stdout[0] =~ /\Ano package provides / $stdout[0] =~ /\Ano package requires / $stdout[0] =~ /\Apackage .* is not installed\Z/
Truth table0%0/5
A and B or C or D
A= @stdout == 1B= $stdout[0] =~ /\Ano package provides /C= $stdout[0] =~ /\Ano package requires /D= $stdout[0] =~ /\Apackage .* is not installed\Z/
ABCDresult
0XXX0
10000
11XX1
101X1
10011
235 and (
236 $stdout[0] =~ m/\Ano package provides /
237 or
238 $stdout[0] =~ m/\Ano package requires /
239 or
240 $stdout[0] =~ m/\Apackage .* is not installed\Z/
241 )
242 );
243 0 ▶ die("Error: Command returned $error") if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
244 0 return \@stdout;
245 }
246  
247 0 ▶sub _run3_error {
Subroutine _run3_error: not called CC=1 SCAR=6.9
248 0 my $self = shift;
249 0 my @argv = @_;
250 0 my $error = 0;
251 0 IPC::Run3::run3 [$self->command => @argv], \undef, \undef, \$error;
252 0 return $error;
253 }
254  
255 =head1 PROPERTIES
256  
257 =head2 command
258  
259 =cut
260  
261 0 ▶sub command {
Subroutine command: not called CC=3 SCAR=24.8
Pod documented
262 0 my $self = shift;
263 0 ▶ $self->{'command'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
264 0 ▶ $self->{'command'} = 'rpm' unless defined $self->{'command'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"command"} 0 0 0
265 0 return $self->{'command'};
266 }
267  
268 =head1 SEE ALSO
269  
270 =head1 AUTHOR
271  
272 Michael R. Davis
273  
274 =head1 COPYRIGHT AND LICENSE
275  
276 MIT License
277  
278 Copyright (c) 2023 Michael R. Davis
279  
280 =cut
281  
282 5 1;
↑ Summary Query/Capability.pm »