File Coverage

blib/lib/RPM/Info.pm
Criterion Covered Total %
statement 11 125 8.8
branch 0 38 0.0
condition 0 3 0.0
subroutine 3 9 33.3
pod 7 8 87.5
total 21 183 11.4


line stmt bran cond sub pod time code
1             package RPM::Info;
2             $VERSION = '1.05';
3              
4             =pod
5              
6             =head1 NAME
7              
8             RPM::Info
9              
10             =head1 SYNOPSIS
11              
12             #!/usr/bin/perl -w
13              
14             use RPM::Info;
15              
16             my $rpm = new RPM::Info();
17             my @rpms = ();
18             my %filelist;
19             my %info;
20             my $dir = "";
21             my $info = "";
22             my @rpmreq = ();
23             my $seek = "gimp-1.2.3-360";
24              
25             print "\nVer : ".$rpm->getRpmVer();
26              
27             if ($rpm->getRpms(\@rpms, "gnome") == 0)
28             {
29             foreach (@rpms)
30             {
31             print "\nRPM:-> $_";
32             }
33             }
34              
35             if ($rpm->getRpmFiles(\%filelist, $seek) == 0)
36             {
37             foreach $dir (keys(%filelist))
38             {
39             print "\n\n\nDir : $dir";
40             foreach (@{$filelist{$dir}{'files'}})
41             {
42             print "\nFile : $_";
43             }
44             }
45             }
46              
47             if ($rpm->getRpmInfo(\%info, $seek) == 0)
48             {
49             print "\n\nINFOS:";
50             foreach $info (keys(%info))
51             {
52             print "\n$info : $info{$info}";
53             }
54             }
55              
56             if ($rpm->getRpmRequirements(\@rpmreq, $seek) == 0)
57             {
58             print "\n\nREQUIREMENTS:";
59             foreach (@rpmreq)
60             {
61             print "\n$_";
62             }
63             }
64            
65             if ($rpm->getRpmInfoRaw(\@rpms, "perl") == 0)
66             {
67             foreach (@rpms)
68             {
69             print "\nRPM:-> $_";
70             }
71             }
72            
73             if ($rpm->getRpmDependents(\@rpms, "perl") == 0)
74             {
75             foreach (@rpms)
76             {
77             print "\nRPM:-> $_";
78             }
79             }
80              
81              
82             =cut
83              
84             =head1 DESCRIPTION
85              
86             The RPM::Info module allows to get informations about installed RPM's:
87             it fetches:
88             name,
89             version,
90             requirements,
91             all files / directories containing to a RPM,
92             information like vendor, distributor etc.
93              
94             =head1 AUTHOR
95              
96             Andreas Mahnke
97              
98             =cut
99              
100              
101 1     1   9465 use strict;
  1         3  
  1         2893  
102              
103             =head1 Methods:
104              
105             =head2 new
106              
107             creates a new object of the class
108              
109             =cut
110              
111             sub new
112             {
113 1     1 1 424 my $class = shift;
114 1         12 my $self = {
115             };
116              
117 1         3 bless ($self, $class);
118 1         3 return $self;
119             }
120              
121             =head2 getRpms(result(Array Reference), search pattern(scalar))
122              
123             searches for all installed rpm's containing the search pattern
124             and saves them in an Array
125              
126             if no search pattern is refered, all installed rpm's are saved
127              
128             returns 0 on succes - 1 on failure
129              
130             =cut
131              
132             sub getRpms()
133             {
134 0     0 1 0 my $self = shift;
135 0         0 my $ref = shift;
136 0         0 my $rpmseek = shift;
137              
138 0 0       0 if (! defined $rpmseek)
139             {
140 0         0 @$ref = `rpm -qa`;
141             }
142             else
143             {
144 0         0 @$ref = `rpm -qa | grep $rpmseek`;
145             }
146 0         0 foreach (@$ref)
147             {
148 0         0 chomp $_;
149             }
150 0 0       0 if ($#$ref < 0)
151             {
152 0         0 return 1;
153             }
154              
155 0         0 return 0;
156             }
157              
158             =head2 getRpmFiles((result(Hash Reference), rpmname(scalar))
159              
160             searches for all files and directories which belong to the refered rpm - name
161             and saves them in a Hash of Hashes
162              
163             returns 0 on succes - 1 on failure
164              
165             =cut
166              
167             sub getRpmFiles()
168             {
169 0     0 1 0 my $self = shift;
170 0         0 my $ref = shift;
171 0         0 my $rpmseek = shift;
172 0         0 my $i = 0;
173 0         0 my @dirs = ();
174 0         0 my $line = "";
175 0         0 my $dir = "";
176 0         0 my @output = `rpm -qvl $rpmseek`;
177 0         0 my @line = ();
178 0         0 my @files = ();
179              
180 0         0 undef %$ref;
181              
182 0 0       0 if ($#output < 0)
183             {
184 0         0 return 1;
185             }
186 0         0 foreach (@output)
187             {
188 0         0 chomp $_;
189 0         0 @line = split(/ /,$_);
190 0 0       0 if ($line[0] =~ m/^d/)
191             {
192 0         0 $dirs[$i] = $line[$#line];
193 0         0 $i++;
194             }
195             }
196              
197 0         0 @output = `rpm -ql $rpmseek`;
198 0         0 foreach $dir (@dirs)
199             {
200 0         0 @files = ();
201 0         0 foreach $line (@output)
202             {
203 0         0 chomp $line;
204 0         0 @line = split(/\//,$line);
205 0 0 0     0 if (($line =~ m/^$dir/) && (length($line) != length($dir)))
206             {
207 0         0 push(@files,$line[$#line]);
208             }
209             }
210 0         0 $$ref{$dir}{'files'} = [@files];
211             }
212 0         0 return 0;
213             }
214              
215             =head2 getRpmVer()
216              
217             gets the version of rpm and returns it
218              
219             =cut
220              
221             sub getRpmVer()
222             {
223 1     1 1 150 my $self = shift;
224 1         3123 my $ver = (`rpm --version`);
225 1         141 chomp $ver;
226 1         33 return $ver;
227             }
228              
229             =head2 getRpmInfo((result(Hash Reference), rpmname(scalar))
230              
231             gets Infos about the specified rpm and saves them into
232             a Hash of Hashes
233              
234             returns 0 on succes - 1 on failure
235              
236             =cut
237              
238             sub getRpmInfo()
239             {
240 0     0 1   my $self = shift;
241 0           my $ref = shift;
242 0           my $rpmseek = shift;
243 0           my @output = `rpm -qi $rpmseek`;
244 0           my $line = "";
245 0           my $i = 0;
246              
247 0           undef %$ref;
248              
249 0 0         if ($#output < 0)
250             {
251 0           return 1;
252             }
253            
254 0           LINE:foreach $line (@output)
255             {
256 0 0         if ($line =~ /(Name+).*: ([(\/\w].*.[\w\/)]) .*. (Relocations+).*: ([(\/\w].*.[\w\/)])/)
257             {
258 0           $$ref{'name'} = $2;
259 0           $$ref{'relocations'} =$4;
260 0           next LINE;
261             }
262 0 0         if ($line =~ /(Version+).*: ([(\/\w].*.[\w\/)]) .*. (Vendor+).*: ([(\/\w].*.[\w\/)])/)
263             {
264 0           $$ref{'version'} = $2;
265 0           $$ref{'vendor'} =$4;
266 0           next LINE;
267             }
268 0 0         if ($line =~ /(Release+).*: ([(\/\w].*.[\w\/)]) .*. (Build Date+).*: ([(\/\w].*.[\w\/)])/)
269             {
270 0           $$ref{'release'} = $2;
271 0           $$ref{'build_date'} =$4;
272 0           next LINE;
273             }
274 0 0         if ($line =~ /(Install date+).*: ([(\/\w].*.[\w\/)]) .*. (Build Host+).*: ([(\/\w].*.[\w\/)])/)
275             {
276 0           $$ref{'install_date'} = $2;
277 0           $$ref{'build_host'} =$4;
278 0           next LINE;
279             }
280 0 0         if ($line =~ /(Group+).*: ([(\/\w].*.[\w\/)]) .*. (Source RPM+).*: ([(\/\w].*.[\w\/)])/)
281             {
282 0           $$ref{'group'} = $2;
283 0           $$ref{'source_rpm'} =$4;
284 0           next LINE;
285             }
286 0 0         if ($line =~ /(Size+).*: ([(\/\w].*.[\w\/)]) .*. (License+).*: ([(\/\w].*.[\w\/)])/)
287             {
288 0           $$ref{'size'} = $2;
289 0           $$ref{'license'} =$4;
290 0           next LINE;
291             }
292 0 0         if ($line =~ m/(Summary+).*: ([(\/\w].*.[\w\/)])/)
293             {
294 0           $$ref{'summary'} = $2;
295 0           next LINE;
296             }
297 0 0         if ($line =~ m/(URL+).*: ([(\/\w].*.[\w\/)])/)
298             {
299 0           $$ref{'url'} = $2;
300 0           next LINE;
301             }
302 0 0         if ($line =~ m/(Packager+).*: ([(\/\w].*.[\w\/)])/)
303             {
304 0           $$ref{'packager'} = $2;
305 0           next LINE;
306             }
307 0 0         if ($line =~ m/(Distribution+).*: ([(\/\w].*.[\w\/)])/)
308             {
309 0           $$ref{'distribution'} = $2;
310 0           next LINE;
311             }
312             }
313 0           return 0;
314             }
315              
316             =head2 getRpmRequirements((result(Array Reference), rpmname(scalar))
317              
318             gets all the requirements of the specified rpm and saves them into an array
319              
320             returns 0 on succes - 1 on failure
321              
322             =cut
323              
324             sub getRpmRequirements()
325             {
326 0     0 1   my $self = shift;
327 0           my $ref = shift;
328 0           my $rpmseek = shift;
329            
330 0           @$ref = `rpm -qR $rpmseek`;
331              
332 0           foreach (@$ref)
333             {
334 0           chomp $_;
335             }
336 0 0         if ($#$ref < 0)
337             {
338 0           return 1;
339             }
340              
341 0           return 0;
342             }
343              
344             =head2 getRpmInfoRaw((result(Array Reference), rpmname(scalar))
345              
346             gets Infos about the specified rpm and saves the output
347             line-by-line in an array
348              
349             returns 0 on succes - 1 on failure
350              
351             =cut
352              
353             sub getRpmInfoRaw()
354             {
355 0     0 1   my $self = shift;
356 0           my $ref = shift;
357 0           my $rpmseek = shift;
358 0           @$ref = `rpm -qi $rpmseek`;
359 0           foreach (@$ref)
360             {
361 0           chomp $_;
362             }
363              
364 0 0         if ($#$ref < 0)
365             {
366 0           return 1;
367             }
368 0           return 0;
369             }
370              
371             =head2 getRpmRequirements((result(Array Reference), rpmname(scalar))
372              
373             gets all the rpm names that depend on the specified rpm
374             and saves them into an array
375              
376             returns 0 on succes - 1 on failure
377              
378             =cut
379              
380             sub getRpmDependents()
381             {
382 0     0 0   my $self = shift;
383 0           my $ref = shift;
384 0           my $rpmseek = shift;
385              
386 0           @$ref = `rpm -q --whatrequires $rpmseek`;
387              
388 0           foreach (@$ref)
389             {
390 0           chomp $_;
391             }
392 0 0         if ($#$ref < 0)
393             {
394 0           return 1;
395             }
396 0           return 0;
397             }
398              
399             1;