|
0 |
package Linux::Distribution::Packages;
|
|
1 |
|
|
2 |
use 5.006000;
|
|
3 |
use strict;
|
|
4 |
use warnings;
|
|
5 |
|
|
6 |
use base qw(Linux::Distribution);
|
|
7 |
|
|
8 |
our $VERSION = '0.05';
|
|
9 |
|
|
10 |
my %commands = (
|
|
11 |
'debian' => 'dpkg',
|
|
12 |
'gentoo' => 'equery',
|
|
13 |
'fedora' => 'rpm',
|
|
14 |
'redflag' => 'rpm',
|
|
15 |
'redhat' => 'rpm',
|
|
16 |
'slackware' => 'pkgtool',
|
|
17 |
'suse' => 'rpm',
|
|
18 |
'ubuntu' => 'dpkg',
|
|
19 |
);
|
|
20 |
|
|
21 |
our @EXPORT_OK = qw(distribution_packages distribution_write format);
|
|
22 |
|
|
23 |
sub new {
|
|
24 |
my $package = shift;
|
|
25 |
my $options = shift;
|
|
26 |
|
|
27 |
my $self = {
|
|
28 |
'command' => '',
|
|
29 |
'format' => 'native',
|
|
30 |
'_data' => '',
|
|
31 |
'output_file' => ''
|
|
32 |
};
|
|
33 |
|
|
34 |
foreach my $option (keys %{$options}){
|
|
35 |
$self->{$option} = $options->{$option};
|
|
36 |
}
|
|
37 |
|
|
38 |
bless $self, $package;
|
|
39 |
$self->SUPER::new();
|
|
40 |
$self->distribution_name();
|
|
41 |
$self->distribution_packages();
|
|
42 |
return $self;
|
|
43 |
}
|
|
44 |
|
|
45 |
sub distribution_packages {
|
|
46 |
my $self = shift || new();
|
|
47 |
if ($commands{$self->{'DISTRIB_ID'}}){
|
|
48 |
bless $self, 'Linux::Distribution::Packages::' . $commands{$self->{'DISTRIB_ID'}};
|
|
49 |
} else {
|
|
50 |
print "Distribution [ $self->{'DISTRIB_ID'} ] not supported\n";
|
|
51 |
exit;
|
|
52 |
}
|
|
53 |
$self->_retrieve_all();
|
|
54 |
}
|
|
55 |
|
|
56 |
sub distribution_write {
|
|
57 |
my $self = shift;
|
|
58 |
my $options = shift;
|
|
59 |
foreach my $option (keys %{$options}){
|
|
60 |
$self->{$option} = $options->{$option};
|
|
61 |
}
|
|
62 |
my $print_function = '_list_' . $self->{'format'};
|
|
63 |
if ( $self->{'format'} ne 'xml'){
|
|
64 |
$self->_open_output_fh();
|
|
65 |
}
|
|
66 |
$self->$print_function();
|
|
67 |
if ( $self->{'format'} ne 'xml'){
|
|
68 |
$self->_close_output_fh();
|
|
69 |
}
|
|
70 |
return 1;
|
|
71 |
}
|
|
72 |
|
|
73 |
sub format {
|
|
74 |
my $self = shift;
|
|
75 |
$self->{'format'} = shift || 'native';
|
|
76 |
}
|
|
77 |
|
|
78 |
sub _retrieve_all {
|
|
79 |
my $self = shift;
|
|
80 |
$self->_command();
|
|
81 |
$self->{'_data'} = ` $self->{'command'} `;
|
|
82 |
die "Error $? running \'$self->{'command'}\'\n" if $?;
|
|
83 |
}
|
|
84 |
|
|
85 |
sub _list_native {
|
|
86 |
my $self = shift;
|
|
87 |
my $output = $self->{'output_file_handle'};
|
|
88 |
print { $output || *STDOUT } $self->{_data};
|
|
89 |
}
|
|
90 |
|
|
91 |
sub _list_xml {
|
|
92 |
require XML::Writer;
|
|
93 |
my $self = shift;
|
|
94 |
my $writer;
|
|
95 |
|
|
96 |
my $writer_options = {DATA_MODE => 1, DATA_INDENT => 2};
|
|
97 |
my $output;
|
|
98 |
if (defined $self->{'output_file'}){
|
|
99 |
require IO::File;
|
|
100 |
$output = new IO::File(">$self->{'output_file'}");
|
|
101 |
$writer_options->{'OUTPUT'} = $output;
|
|
102 |
}
|
|
103 |
if ($self->{'format'} =~ m/xml/i){
|
|
104 |
$writer = new XML::Writer(%{$writer_options});
|
|
105 |
$writer->startTag('distribution', "name" => $self->{'DISTRIB_ID'}, "release" => $self->distribution_version());
|
|
106 |
}
|
|
107 |
my $hash = $self->_parse($writer);
|
|
108 |
$writer->endTag('distribution');
|
|
109 |
}
|
|
110 |
|
|
111 |
sub _list_csv {
|
|
112 |
my $self = shift;
|
|
113 |
$self->_parse();
|
|
114 |
}
|
|
115 |
|
|
116 |
sub _row_csv {
|
|
117 |
my $self = shift;
|
|
118 |
my $output = $self->{'output_file_handle'};
|
|
119 |
print { $output || *STDOUT } "\'" . join("\',\'", @_) . "\'\n";
|
|
120 |
}
|
|
121 |
|
|
122 |
sub _parse {
|
|
123 |
my $self = shift;
|
|
124 |
my $row_func='_row_' . $self->{'format'};
|
|
125 |
my @data = split '\n', $self->{'_data'};
|
|
126 |
foreach my $row (@data){
|
|
127 |
$self->$row_func($row);
|
|
128 |
}
|
|
129 |
}
|
|
130 |
|
|
131 |
sub _open_output_fh {
|
|
132 |
my $self = shift;
|
|
133 |
if ($self->{'output_file'}){
|
|
134 |
open FH, ">>$self->{'output_file'}";
|
|
135 |
$self->{'output_file_handle'} = *FH;
|
|
136 |
} else {
|
|
137 |
delete $self->{'output_file_handle'};
|
|
138 |
delete $self->{'output_file'};
|
|
139 |
}
|
|
140 |
}
|
|
141 |
|
|
142 |
sub _close_output_fh {
|
|
143 |
my $self = shift;
|
|
144 |
if ($self->{'output_file'}){
|
|
145 |
close $self->{'output_file_handle'};
|
|
146 |
delete $self->{'output_file_handle'};
|
|
147 |
}
|
|
148 |
}
|
|
149 |
|
|
150 |
sub _command {
|
|
151 |
my ( $self, $command ) = @_;
|
|
152 |
# Add options not really yet implemented
|
|
153 |
if ($self->{'options'}){ $command .= ' ' . $self->{'options'}; }
|
|
154 |
$self->{'command'} = $command;
|
|
155 |
}
|
|
156 |
|
|
157 |
return 1;
|
|
158 |
|
|
159 |
package Linux::Distribution::Packages::equery;
|
|
160 |
use base qw(Linux::Distribution::Packages);
|
|
161 |
|
|
162 |
sub _command {
|
|
163 |
my $self = shift;
|
|
164 |
$self->SUPER::_command('equery list');
|
|
165 |
}
|
|
166 |
|
|
167 |
sub _parse {
|
|
168 |
my $self = shift;
|
|
169 |
my @data = split '\n', $self->{_data};
|
|
170 |
my $writer=shift;
|
|
171 |
foreach my $row (@data){
|
|
172 |
my ($dir, $pkg, $ver);
|
|
173 |
next if $row =~ m/.*installed packages.*/;
|
|
174 |
if ($row =~ m/\-(r\d+)$/){
|
|
175 |
($dir, $pkg, $ver) = $row =~ m/(.+)\/(.+)\-(.+(\-(r\d+)))$/;
|
|
176 |
} else {
|
|
177 |
($dir, $pkg, $ver) = $row =~ m/(.+)\/(.+)\-(.+)/;
|
|
178 |
}
|
|
179 |
if ($self->{'format'} =~ m/xml/i){ $writer->emptyTag('package', 'name' => $pkg, 'version' => $ver , 'category' => $dir); next; }
|
|
180 |
my $row_func='_row_' . $self->{'format'};
|
|
181 |
$self->$row_func($dir, $pkg, $ver, '');
|
|
182 |
}
|
|
183 |
}
|
|
184 |
|
|
185 |
return 1;
|
|
186 |
|
|
187 |
package Linux::Distribution::Packages::dpkg;
|
|
188 |
use base qw(Linux::Distribution::Packages);
|
|
189 |
|
|
190 |
sub _command {
|
|
191 |
my $self = shift;
|
|
192 |
$self->SUPER::_command('dpkg --list');
|
|
193 |
}
|
|
194 |
|
|
195 |
sub _parse {
|
|
196 |
my $self = shift;
|
|
197 |
my @data = split '\n', $self->{_data};
|
|
198 |
my $writer=shift;
|
|
199 |
foreach my $row (@data){
|
|
200 |
my ($ii, $desc, $pkg, $ver);
|
|
201 |
next if $row =~ m/^(Desired|\||\+).*/;
|
|
202 |
($ii, $pkg, $ver, $desc) = $row =~ m/^(.+?)\s+(.+?)\s+(.+?)\s+(.+)$/;
|
|
203 |
if ($self->{'format'} =~ m/xml/i){ $writer->emptyTag('package', 'name' => $pkg, 'version' => $ver , 'description' => $desc); next; }
|
|
204 |
my $row_func='_row_' . $self->{'format'};
|
|
205 |
$self->$row_func('', $pkg, $ver, $desc);
|
|
206 |
}
|
|
207 |
}
|
|
208 |
|
|
209 |
return 1;
|
|
210 |
|
|
211 |
|
|
212 |
package Linux::Distribution::Packages::rpm;
|
|
213 |
use base qw(Linux::Distribution::Packages);
|
|
214 |
|
|
215 |
sub _command {
|
|
216 |
my $self = shift;
|
|
217 |
$self->SUPER::_command('rpm -qa');
|
|
218 |
}
|
|
219 |
|
|
220 |
sub _parse {
|
|
221 |
my $self = shift;
|
|
222 |
my @data = split '\n', $self->{_data};
|
|
223 |
my $writer=shift;
|
|
224 |
foreach my $row (@data){
|
|
225 |
my ($pkg, $ver);
|
|
226 |
next if $row =~ m/^(Desired|\||\+).*/;
|
|
227 |
($pkg, $ver) = $row =~ m/^(.+)\-+(.+\-.+)$/;
|
|
228 |
if ($self->{'format'} =~ m/xml/i){ $writer->emptyTag('package', 'name' => $pkg, 'version' => $ver ); next; }
|
|
229 |
my $row_func='_row_' . $self->{'format'};
|
|
230 |
$self->$row_func('', $pkg, $ver, '');
|
|
231 |
}
|
|
232 |
}
|
|
233 |
|
|
234 |
package Linux::Distribution::Packages::pkgtool;
|
|
235 |
use base qw(Linux::Distribution::Packages);
|
|
236 |
|
|
237 |
sub _command {
|
|
238 |
my $self = shift;
|
|
239 |
$self->SUPER::_command('ls /var/log/packages');
|
|
240 |
}
|
|
241 |
|
|
242 |
sub _parse {
|
|
243 |
my $self = shift;
|
|
244 |
my @data = split '\n', $self->{_data};
|
|
245 |
my $writer=shift;
|
|
246 |
foreach my $row (@data){
|
|
247 |
my ($pkg, $ver);
|
|
248 |
($pkg, $ver) = $row =~ m/^(.+)\-(.+)\-.+\-\d+$/;
|
|
249 |
if ($self->{'format'} =~ m/xml/i){ $writer->emptyTag('package', 'name' => $pkg, 'version' => $ver ); next; }
|
|
250 |
my $row_func='_row_' . $self->{'format'};
|
|
251 |
$self->$row_func('', $pkg, $ver, '');
|
|
252 |
}
|
|
253 |
}
|
|
254 |
return 1;
|
|
255 |
|
|
256 |
__END__
|
|
257 |
|
|
258 |
|
|
259 |
=head1 NAME
|
|
260 |
|
|
261 |
Linux::Distribution::Packages - list all packages on various Linux distributions
|
|
262 |
|
|
263 |
=head1 SYNOPSIS
|
|
264 |
|
|
265 |
use Linux::Distribution::Packages qw(distribution_packages distribution_write);
|
|
266 |
|
|
267 |
$linux = new Linux::Distribution::Packages({'format' => 'csv', 'output_file' => 'packages.csv'});
|
|
268 |
$linux->distribution_write();
|
|
269 |
|
|
270 |
# Or you can (re)set the options when you write.
|
|
271 |
$linux->distribution_write({'format' => 'xml', 'output_file' => 'packages.xml'});
|
|
272 |
|
|
273 |
# If you want to reload the package data
|
|
274 |
$linux->distribution_packages();
|
|
275 |
|
|
276 |
=head1 DESCRIPTION
|
|
277 |
|
|
278 |
This is a simple module that uses Linux::Distribution to guess the linux
|
|
279 |
distribution and then uses the correct commands to list all the packages
|
|
280 |
on the system and then output them in one of three formats: native, csv,
|
|
281 |
and xml.
|
|
282 |
|
|
283 |
Distributions currently working: debian, ubuntu, fedora, redhat, suse,
|
|
284 |
gentoo, slackware, redflag.
|
|
285 |
|
|
286 |
The module inherits from Linux::Distribution, so can also use its calls.
|
|
287 |
|
|
288 |
=head2 EXPORT
|
|
289 |
|
|
290 |
None by default.
|
|
291 |
|
|
292 |
=head1 TODO
|
|
293 |
|
|
294 |
* Add the capability to correctly get packages for all recognized distributions.
|
|
295 |
* Seperate out parsing from writing. Parse data to hash and give access to hash.
|
|
296 |
Then write the formatted data from the hash.
|
|
297 |
|
|
298 |
=head1 AUTHORS
|
|
299 |
|
|
300 |
Judith Lebzelter, E<lt>judith@osdl.orgE<gt>
|
|
301 |
Alberto Re, E<lt>alberto@accidia.netE<gt>
|
|
302 |
|
|
303 |
=head1 COPYRIGHT AND LICENSE
|
|
304 |
|
|
305 |
This library is free software; you can redistribute it and/or modify
|
|
306 |
it under the same terms as Perl itself, either Perl version 5.8.5 or,
|
|
307 |
at your option, any later version of Perl 5 you may have available.
|
|
308 |
|
|
309 |
=cut
|
|
310 |
|