blib/lib/RPM/Search.pm

filter: Statementstmt 89.6% 69 / 77 Branchbran 70.0% 21 / 30 Conditioncond 47.0% 8 / 17 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 84.2% 16 / 19 Podpod 100% 7 / 7 totaltotal 76.7% 122 / 159 CC 22 SCAR 11.8
CC
22
Cov
79%
search
5.0
find_yum_db
4.5
new
4.5
CRAP 3.2
↑ Summary
1 package RPM::Search;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our $VERSION = '0.01';
11  
12 =head1 NAME
13  
14 RPM::Search - Search all available RPMs
15  
16 =head1 SYNOPSIS
17  
18 # On (recent) RPM based systems
19 my $db = RPM::Search->new();
20  
21 my @names = $db->search(qr/perl-Mo.se/);
22 # or
23 @names = $db->search('perl-CGI-*');
24 # or
25 @names = $db->search('cpanminus');
26  
27 if ( @names ) {
28 my $pkgs = join ", ", @names;
29 `/usr/bin/yum -y install $pkgs`;
30 }
31 else {
32 print "No matching packages\n";
33 }
34  
35 =head1 PURPOSE
36  
37 This module allows one to search the entire collection of RPMs available for
38 a given installed Linux distribution that uses RPMs, not just listing the RPMs
39 already installed on a system.
40  
41 And frankly, have you tried using C<yum search>?
42  
43 Eventually, I plan to use this functionality to make a plugin for
44 C<cpanminus> to suggest vendor-supplied packages instead of building
45 them "from scratch."
46  
47 =head1 ATTRIBUTES
48  
49 These are standard Perlish accessors: pass an argument to set it, pass no
50 argument to get the current value.
51  
52 =over
53  
54 =item cache_base
55  
56 Base location of the yum data (default: none)
57  
58 =back
59  
60 =cut
61  
62 1 ▶sub cache_base {
Subroutine cache_base: called CC=3 SCAR=14.2
Pod documented
63 1 my $self = shift;
64 1 my $param = shift;
65  
66 1 ▶ if ( $param ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param) 0 1 1
67 0 ▶ return unless -d $param;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $param 0 0 0
68 0 $self->{'cache_base'} = $param;
69 }
70  
71 1 return $self->{'cache_base'};
72 }
73  
74 =over
75  
76 =item yum_primary_db
77  
78 Fully qualified path to the primary SQLite database (default: none)
79  
80 =back
81  
82 =cut
83  
84 4 ▶sub yum_primary_db {
Subroutine yum_primary_db: called CC=3 SCAR=11.0
Pod documented
85 4 my $self = shift;
86 4 my $param = shift;
87  
88 4 ▶ if ( $param ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param) 1 3 4
89 1 ▶ return unless -e $param;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $param 0 1 1
90 1 $self->{'yum_primary_db'} = $param;
91 }
92  
93 4 return $self->{'yum_primary_db'};
94 }
95  
96 =over
97  
98 =item dbh
99  
100 DBI handle to the yum SQLite database
101  
102 =back
103  
104 =cut
105  
106 9 ▶sub dbh {
Subroutine dbh: called CC=3 SCAR=11.0
Pod documented
107 9 my $self = shift;
108 9 my $param = shift;
109  
110 9 ▶ if ( $param ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param) 1 8 9
111 1 ▶ return unless ref($param) =~ /DBI/i;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($param) =~ /DBI/i 0 1 1
112 1 $self->{'dbh'} = $param;
113 }
114  
115 9 return $self->{'dbh'};
116 }
117  
118 =head1 METHODS
119  
120 =over
121  
122 =item new()
123  
124 Make a new L<RPM::Search|RPM::Search> object. Will automatically search
125 for an appropriate yum database and open a handle to the data set
126 unless you pass valid arguments to the F<dbh> and/or F<yum_primary_db>
127 attributes at construction time.
128  
129 Returns a new L<RPM::Search|RPM::Search> object.
130  
131 =back
132  
133 =cut
134  
135 1 ▶sub new {
Subroutine new: called CC=4 SCAR=15.0
Pod documented
136  
137 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
138 1 require DBD::SQLite;
139 }
140 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
141 0 die "This module requires DBD::SQLite. Try:\n\tsudo yum -y install perl-DBD-SQLite\n";
142 1 };
143  
144 1 my $class = shift;
145 1 ▶ my $proto = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
146
147 1 my $self = bless { @_ }, $proto;
148  
149 1 ▶ $self->find_yum_db unless $self->yum_primary_db;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->yum_primary_db 1 0 1
150 1 ▶ $self->open_db unless $self->dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dbh 1 0 1
151  
152 1 return $self;
153 }
154  
155  
156  
157  
158 =over
159  
160 =item find_yum_db()
161  
162 This method searches for an appropriate yum database starting at the
163 location passed as a parameter. If no parameter is given, the method
164 will use F<cache_base>. If F<cache_base> is not set, the method will
165 use F</var/cache/yum>.
166  
167 This call populates F<yum_primary_db>.
168  
169 The return value is boolean: true for success, false for failure.
170  
171 =back
172  
173 =cut
174  
175 1 ▶sub find_yum_db {
Subroutine find_yum_db: called CC=4 SCAR=15.1
Pod documented
176 1 my $self = shift;
177 1 ▶ my $base = shift || $self->cache_base() || "/var/cache/yum";
Condition40%2/5
shift() || $self->cache_base || '/var/cache/yum'
l!l
10
shift() or $self->cache_base
l!l&&r!l&&!r
010
MC/DC0%0/2
shift() || $self->cache_base || '/var/cache/yum'
shift() $self->cache_base
Truth table33%1/3
A or B
A= shift()B= $self->cache_base
ABresult
1X1
011
000
178  
179  
180 1 my $path;
181 find( { wanted =>
182 4 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.3
183 4 ▶ $path = $File::Find::name if /primary.*\.sqlite\z/ &&
Branch: 1 branch
BranchTrueFalseTotal
if /primary.*\.sqlite\z/ and not $File::Find::dir =~ /update/ and not $File::Find::dir =~ /extra/ 1 3 4
Condition66%4/6
/primary.*\.sqlite\z/ and not $File::Find::dir =~ /update/ and not $File::Find::dir =~ /extra/
!ll&&!rl&&r
301
/primary.*\.sqlite\z/ and not $File::Find::dir =~ /update/
!ll&&!rl&&r
301
MC/DC33%1/3
/primary.*\.sqlite\z/ and not $File::Find::dir =~ /update/ and not $File::Find::dir =~ /extra/
/primary.*\.sqlite\z/ not $File::Find::dir =~ /update/ not $File::Find::dir =~ /extra/
Truth table50%2/4
A and not(B) and not(C)
A= /primary.*\.sqlite\z/B= not $File::Find::dir =~ /update/C= not $File::Find::dir =~ /extra/
ABCresult
0XX0
10X0
1100
1111
184 $File::Find::dir !~ /update/ &&
185 $File::Find::dir !~ /extra/
186 },
187 1 untaint => 1,
188 },
189 $base);
190  
191 1 ▶ if ( $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path) { } 1 0 1
192 1 $self->yum_primary_db($path);
193 1 return 1;
194 }
195 else {
196 0 warn "Couldn't find any yum primary SQLite databases in $base\n";
197 0 return 0;
198 }
199 }
200  
201 =over
202  
203 =item open_db()
204  
205 This method opens a connection to the yum SQLite database. The DSN
206 is constructed from the passed in parameter. If no parameter is
207 passed in, the method will use F<yum_primary_db>.
208  
209 This method populates F<dbh>.
210  
211 This method causes a fatal error on any failure.
212  
213 =back
214  
215 =cut
216  
217 1 ▶sub open_db {
Subroutine open_db: called CC=2 SCAR=7.8
Pod documented
218 1 my $self = shift;
219 1 ▶ my $dbname = shift || $self->yum_primary_db;
Condition33%1/3
shift() || $self->yum_primary_db
l!l&&r!l&&!r
010
MC/DC0%0/2
shift() || $self->yum_primary_db
shift() $self->yum_primary_db
Truth table33%1/3
A || B
A= shift()B= $self->yum_primary_db
ABresult
1X1
011
000
220  
221 1 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=7.2
222 1 ▶ my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname", undef, undef, {
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = "DBI"->connect("dbi:SQLite:dbname=$dbname", undef, undef, {"RaiseError", 1, "sqlite_unicode", 1}) or die $DBI::err 1 0 1
223 RaiseError => 1,
224 sqlite_unicode => 1,
225 } ) or die $DBI::err;
226 1 $self->dbh($dbh);
227 }
228 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
229 0 die "Couldn't open db $dbname: $_\n";
230 1 };
231 }
232  
233 =over
234  
235 =item search()
236  
237 This method searches the RPM database using an optional pattern parameter.
238 If no pattern is given, the method returns all available package names.
239 (B<Note>: This will be thousands of packages.)
240  
241 The format of the pattern can be one of the following:
242  
243 =over
244  
245 =item *
246  
247 A regular expression using the C<qr//> construct.
248  
249 =item *
250  
251 A SQL-ish wildcard expression using % and _
252  
253 =item *
254  
255 A filesystem like glob expression using ? and *
256  
257 =item *
258  
259 A scalar which must be a literal match for a package name in
260 the database to return any results
261  
262 =back
263  
264 The method returns an array of all matching package names
265 (which may be zero results.) Undef is returned on errors.
266  
267 =back
268  
269 =cut
270  
271 6 ▶sub search {
Subroutine search: called CC=5 SCAR=16.1
Pod documented
272 6 my $self = shift;
273 6 my $pattern = shift;
274  
275 6 my $sql = "SELECT name FROM packages";
276 6 my @bind_params;
277  
278 6 ▶ if ( $pattern ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern) 5 1 6
279 5 $sql .= " WHERE name ";
280  
281 5 ▶ if ( ref($pattern) =~ /regexp/i ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref($pattern) =~ /regexp/i) { } 1 4 5
elsif ($pattern =~ /[%_]/) { } 1 3 4
elsif ($pattern =~ /[\?\*]/) { } 1 2 3
282 1 $sql .= "REGEXP ?";
283 }
284 elsif ( $pattern =~ /[%_]/ ) {
285 1 $sql .= "LIKE ?";
286 }
287 elsif ( $pattern =~ /[\?\*]/ ) {
288 1 $sql .= "GLOB ?";
289 }
290 else {
291 2 $sql .= "=?";
292 }
293 5 push @bind_params, $pattern;
294 }
295  
296 6 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
297 6 return @{ $self->dbh->selectcol_arrayref($sql, undef, @bind_params) } or die $DBI::err;
298 }
299 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
300 0 warn "Couldn't execute query $sql: $_\n";
301 0 return undef;
302 }
303 6 }
304  
305 =head1 AUTHOR
306  
307 Mark Allen, C<mrallen1 at yahoo dot com>
308  
309 =head1 BUGS
310  
311 Please report any bugs or feature requests to C<bug-rpm-search at rt.cpan.org>, or through
312 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=RPM-Search>.
313  
314 I will be notified, and then you'll automatically be notified of progress on your
315 bug as I make changes.
316  
317 =head1 SUPPORT
318  
319 You can find documentation for this module with the perldoc command.
320  
321 perldoc RPM::Search
322  
323 You can also look for information at:
324  
325 =over 4
326  
327 =item * RT: CPAN's request tracker (report bugs here)
328  
329 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=RPM-Search>
330  
331 =item * AnnoCPAN: Annotated CPAN documentation
332  
333 L<http://annocpan.org/dist/RPM-Search>
334  
335 =item * CPAN Ratings
336  
337 L<http://cpanratings.perl.org/d/RPM-Search>
338  
339 =item * MetaCPAN
340  
341 L<https://metacpan.org/module/RPM::Search/>
342  
343 =item * Github
344  
345 L<https://github.com/mrallen1/RPM-Search.git>
346  
347 =back
348  
349 =head1 LICENSE AND COPYRIGHT
350  
351 Copyright 2011 Mark Allen.
352  
353 This program is free software; you can redistribute it and/or modify it
354 under the terms of either: the GNU General Public License as published
355 by the Free Software Foundation; or the Artistic License.
356  
357 See http://dev.perl.org/licenses/ for more information.
358  
359 =cut
360  
361 3 1;
↑ Summary