1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
package RPM2;
use 5.00503;
use strict;
use DynaLoader;
use Data::Dumper;
use Cwd qw/realpath/;
use File::Basename;
use File::Spec;
use vars qw/$VERSION/;
$VERSION = '0.62';
use vars qw/@ISA/;
@ISA = qw/DynaLoader/;
bootstrap RPM2 $VERSION;
foreach my $tag (keys %RPM2::constants) {
my $sub = q {
sub [[method]] {
my $self = shift;
return $RPM2::constants{[[tag]]};
}
};
my $method = lc $tag;
$method =~ s/^rpm//;
$sub =~ s/\[\[method\]\]/$method/g;
$sub =~ s/\[\[tag\]\]/$tag/g;
eval $sub;
if ($@) {
die $@;
}
}
sub open_rpm_db {
my $class = shift;
my %params = @_;
my $self = bless { }, "RPM2::DB";
if ($params{-path}) {
$class->add_macro("_dbpath", $params{-path});
$self->{c_db} = RPM2::_open_rpm_db($params{-readwrite} ? 1 : 0);
$class->delete_macro("_dbpath");
}
else {
$self->{c_db} = RPM2::_open_rpm_db($params{-readwrite} ? 1 : 0);
}
return $self;
}
sub open_package {
my $class = shift;
my $file = shift;
my $flags = shift;
if (RPM2->rpm_api_version > 4.0 and not defined $flags) {
$flags = RPM2->vsf_default;
}
$flags ||= 0;
open FH, "<$file"
or die "Can't open $file: $!";
my $hdr = RPM2::_read_package_info(*FH, $flags);
close FH;
my ($filename, $path) = (basename($file), realpath(dirname($file)));
$hdr = RPM2::Header->_new_raw($hdr, File::Spec->catfile($path, $filename));
return $hdr;
}
package RPM2::DB;
sub find_all_iter {
my $self = shift;
return RPM2::PackageIterator->new_iterator($self, "RPMTAG_NAME")
}
sub find_all {
my $self = shift;
return RPM2::PackageIterator->new_iterator($self)->expand_iter();
}
sub find_by_name_iter {
my $self = shift;
my $name = shift;
return RPM2::PackageIterator->new_iterator($self, "RPMTAG_NAME", $name);
}
sub find_by_name {
my $self = shift;
my $name = shift;
return $self->find_by_name_iter($name)->expand_iter;
}
sub find_by_provides_iter {
my $self = shift;
my $name = shift;
return RPM2::PackageIterator->new_iterator($self, "RPMTAG_PROVIDES", $name);
}
sub find_by_provides {
my $self = shift;
my $name = shift;
return $self->find_by_provides_iter($name)->expand_iter;
}
sub find_by_requires_iter {
my $self = shift;
my $name = shift;
return RPM2::PackageIterator->new_iterator($self, "RPMTAG_REQUIRENAME", $name);
}
sub find_by_requires {
my $self = shift;
my $name = shift;
return $self->find_by_requires_iter($name)->expand_iter;
}
sub find_by_file_iter {
my $self = shift;
my $name = shift;
return RPM2::PackageIterator->new_iterator($self, "RPMTAG_BASENAMES", $name);
}
sub find_by_file {
my $self = shift;
my $name = shift;
return $self->find_by_file_iter($name)->expand_iter;
}
package RPM2::Header;
use overload '<=>' => \&op_spaceship,
'bool' => \&op_bool;
sub _new_raw {
my $class = shift;
my $c_header = shift;
my $filename = shift;
my $self = bless { }, $class;
$self->{c_header} = $c_header;
$self->{filename} = $filename if defined $filename;
return $self;
}
sub tag {
my $self = shift;
my $tag = shift;
$tag = uc "RPMTAG_$tag";
die "tag $tag invalid"
unless exists $RPM2::header_tag_map{$tag};
return $self->{c_header}->tag_by_id($RPM2::header_tag_map{$tag});
}
sub tagformat {
my $self = shift;
my $format = shift;
return RPM2::C::Header::_header_sprintf($self->{c_header}, $format);
}
sub compare {
my $h1 = shift;
my $h2 = shift;
return RPM2::C::Header::_header_compare($h1->{c_header}, $h2->{c_header});
}
sub op_bool {
my $self = shift;
return defined($self) && defined($self->{c_header});
}
sub op_spaceship {
my $h1 = shift;
my $h2 = shift;
my $ret = $h1->compare($h2);
# rpmvercmp can return any neg/pos number; normalize here to -1, 0, 1
return 1 if $ret > 0;
return -1 if $ret < 0;
return 0;
}
sub is_source_package {
my $self = shift;
return RPM2::C::Header::_header_is_source($self->{c_header});
}
sub filename {
my $self = shift;
if (exists $self->{filename}) {
return $self->{filename};
}
return;
}
sub as_nvre {
my $self = shift;
my $epoch = $self->tag('epoch');
my $epoch_str = '';
$epoch_str = "$epoch:" if defined $epoch;
my $ret = $epoch_str . join("-", map { $self->tag($_) } qw/name version release/);
return $ret;
}
foreach my $tag (keys %RPM2::header_tag_map) {
$tag =~ s/^RPMTAG_//g;
my $sub = q {
sub [[method]] {
my $self = shift;
return $self->tag("[[tag]]");
}
};
my $method = lc $tag;
$sub =~ s/\[\[method\]\]/$method/g;
$sub =~ s/\[\[tag\]\]/$tag/g;
eval $sub;
if ($@) {
die $@;
}
}
sub files {
my $self = shift;
if (not exists $self->{files}) {
my @base_names = $self->tag('basenames');
my @dir_names = $self->tag('dirnames');
my @dir_indexes = $self->tag('dirindexes');
my @files;
foreach (0 .. $#base_names) {
push @files, $dir_names[$dir_indexes[$_]] . $base_names[$_];
}
$self->{files} = \@files;
}
return @{$self->{files}};
}
package RPM2::PackageIterator;
sub new_iterator {
my $class = shift;
my $db = shift;
my $tag = shift;
my $key = shift;
my $self = bless { db => $db }, $class;
$self->{c_iter} = RPM2::C::DB::_init_iterator($db->{c_db},
$RPM2::header_tag_map{$tag},
$key || "",
defined $key ? length $key : 0);
return $self;
}
sub next {
my $self = shift;
return unless $self->{c_iter};
my $hdr = $self->{c_iter}->_iterator_next();
return unless $hdr;
my $ret = RPM2::Header->_new_raw($hdr);
return $ret;
}
sub expand_iter {
my $self = shift;
my @ret;
while (my $h = $self->next) {
push @ret, $h;
}
return @ret;
}
# make sure c_iter is destroyed before {db} so that we always free an
# iterator before we free the db it came from
sub DESTROY {
my $self = shift;
delete $self->{c_iter};
}
# Preloaded methods go here.
1;
__END__
# Below is stub documentation for your module. You better edit it!
=head1 NAME
RPM2 - Perl bindings for the RPM Package Manager API
=head1 SYNOPSIS
use RPM2;
my $db = RPM2->open_rpm_db();
my $i = $db->find_all_iter();
print "The following packages are installed (aka, 'rpm -qa'):\n";
while (my $pkg = $i->next) {
print $pkg->as_nvre, "\n";
}
$i = $db->find_by_name_iter("kernel");
print "The following kernels are installed (aka, 'rpm -q kernel'):\n";
while (my $pkg = $i->next) {
print $pkg->as_nvre, " ", int($pkg->size()/1024), "k\n";
}
$i = $db->find_by_provides_iter("kernel");
print "The following packages provide 'kernel' (aka, 'rpm -q --whatprovides kernel'):\n";
while (my $pkg = $i->next) {
print $pkg->as_nvre, " ", int($pkg->size()/1024), "k\n";
}
print "The following packages are installed (aka, 'rpm -qa' once more):\n";
foreach my $pkg ($db->find_by_file("/bin/sh")) {
print $pkg->as_nvre, "\n";
}
my $pkg = RPM2->open_package("/tmp/XFree86-4.1.0-15.src.rpm");
print "Package opened: ", $pkg->as_nvre(), ", is source: ", $pkg->is_source_package, "\n";
=head1 DESCRIPTION
The RPM2 module provides an object-oriented interface to querying both
the installed RPM database as well as files on the filesystem.
=head1 CLASS METHODS
Pretty much all use of the class starts here. There are two main
entrypoints into the package -- either through the database of
installed rpms (aka the rpmdb) or through a file on the filesystem
(such as kernel-2.4.9-31.src.rpm or kernel-2.4.9-31.i386.rpm
You can have multiple RPM databases open at once, as well as running
multiple queries on each.
=item open_rpm_db(-path => "/path/to/db")
As it sounds, it opens the RPM database, and returns it as an object.
=item open_package("foo-1.1-14.noarch.rpm")
Opens a specific package (RPM or SRPM). Returns a Header object.
=head1 RPM DB object methods
=item find_all_iter()
Returns an iterator object that iterates over the entire database.
=item find_all()
Returns an list of all of the results of the find_all_iter() method.
=item find_by_file_iter($filename)
Returns an iterator that returns all packages that contain a given file.
=item find_by_file($filename)
Ditto, except it just returns the list
=item find_by_name_iter($package_name)
You get the idea. This one is for iterating by package name.
=item find_by_name($package_name)
Ditto, except it returns a list.
=item find_by_provides_iter($provides_string)
This one iterates over provides.
=item find_by_provides($provides_string)
Ditto, except it returns a list.
=item find_by_requires_iter($requires_string)
This one iterates over requires.
=item find_by_requires($requires_string)
Ditto, except it returns a list.
=head1 RPM Header object methods
stuff goes here
=head1 TODO
Package installation and removal.
Signature validation.
=head1 HISTORY
=over 8
=item 0.01
Initial release
=back
=head1 AUTHOR
Chip Turner E<lt>cturner@redhat.comE<gt>
=head1 SEE ALSO
L<perl>.
The original L<RPM> module.
=cut
|