/usr/share/perl5/Linux/Distribution/Packages.pm is in liblinux-distribution-packages-perl 0.05-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | package Linux::Distribution::Packages;
use 5.006000;
use strict;
use warnings;
use base qw(Linux::Distribution);
our $VERSION = '0.05';
my %commands = (
'debian' => 'dpkg',
'gentoo' => 'equery',
'fedora' => 'rpm',
'redflag' => 'rpm',
'redhat' => 'rpm',
'slackware' => 'pkgtool',
'suse' => 'rpm',
'ubuntu' => 'dpkg',
);
our @EXPORT_OK = qw(distribution_packages distribution_write format);
sub new {
my $package = shift;
my $options = shift;
my $self = {
'command' => '',
'format' => 'native',
'_data' => '',
'output_file' => ''
};
foreach my $option (keys %{$options}){
$self->{$option} = $options->{$option};
}
bless $self, $package;
$self->SUPER::new();
$self->distribution_name();
$self->distribution_packages();
return $self;
}
sub distribution_packages {
my $self = shift || new();
if ($commands{$self->{'DISTRIB_ID'}}){
bless $self, 'Linux::Distribution::Packages::' . $commands{$self->{'DISTRIB_ID'}};
} else {
print "Distribution [ $self->{'DISTRIB_ID'} ] not supported\n";
exit;
}
$self->_retrieve_all();
}
sub distribution_write {
my $self = shift;
my $options = shift;
foreach my $option (keys %{$options}){
$self->{$option} = $options->{$option};
}
my $print_function = '_list_' . $self->{'format'};
if ( $self->{'format'} ne 'xml'){
$self->_open_output_fh();
}
$self->$print_function();
if ( $self->{'format'} ne 'xml'){
$self->_close_output_fh();
}
return 1;
}
sub format {
my $self = shift;
$self->{'format'} = shift || 'native';
}
sub _retrieve_all {
my $self = shift;
$self->_command();
$self->{'_data'} = ` $self->{'command'} `;
die "Error $? running \'$self->{'command'}\'\n" if $?;
}
sub _list_native {
my $self = shift;
my $output = $self->{'output_file_handle'};
print { $output || *STDOUT } $self->{_data};
}
sub _list_xml {
require XML::Writer;
my $self = shift;
my $writer;
my $writer_options = {DATA_MODE => 1, DATA_INDENT => 2};
my $output;
if (defined $self->{'output_file'}){
require IO::File;
$output = new IO::File(">$self->{'output_file'}");
$writer_options->{'OUTPUT'} = $output;
}
if ($self->{'format'} =~ m/xml/i){
$writer = new XML::Writer(%{$writer_options});
$writer->startTag('distribution', "name" => $self->{'DISTRIB_ID'}, "release" => $self->distribution_version());
}
my $hash = $self->_parse($writer);
$writer->endTag('distribution');
}
sub _list_csv {
my $self = shift;
$self->_parse();
}
sub _row_csv {
my $self = shift;
my $output = $self->{'output_file_handle'};
print { $output || *STDOUT } "\'" . join("\',\'", @_) . "\'\n";
}
sub _parse {
my $self = shift;
my $row_func='_row_' . $self->{'format'};
my @data = split '\n', $self->{'_data'};
foreach my $row (@data){
$self->$row_func($row);
}
}
sub _open_output_fh {
my $self = shift;
if ($self->{'output_file'}){
open FH, ">>$self->{'output_file'}";
$self->{'output_file_handle'} = *FH;
} else {
delete $self->{'output_file_handle'};
delete $self->{'output_file'};
}
}
sub _close_output_fh {
my $self = shift;
if ($self->{'output_file'}){
close $self->{'output_file_handle'};
delete $self->{'output_file_handle'};
}
}
sub _command {
my ( $self, $command ) = @_;
# Add options not really yet implemented
if ($self->{'options'}){ $command .= ' ' . $self->{'options'}; }
$self->{'command'} = $command;
}
return 1;
package Linux::Distribution::Packages::equery;
use base qw(Linux::Distribution::Packages);
sub _command {
my $self = shift;
$self->SUPER::_command('equery list');
}
sub _parse {
my $self = shift;
my @data = split '\n', $self->{_data};
my $writer=shift;
foreach my $row (@data){
my ($dir, $pkg, $ver);
next if $row =~ m/.*installed packages.*/;
if ($row =~ m/\-(r\d+)$/){
($dir, $pkg, $ver) = $row =~ m/(.+)\/(.+)\-(.+(\-(r\d+)))$/;
} else {
($dir, $pkg, $ver) = $row =~ m/(.+)\/(.+)\-(.+)/;
}
if ($self->{'format'} =~ m/xml/i){ $writer->emptyTag('package', 'name' => $pkg, 'version' => $ver , 'category' => $dir); next; }
my $row_func='_row_' . $self->{'format'};
$self->$row_func($dir, $pkg, $ver, '');
}
}
return 1;
package Linux::Distribution::Packages::dpkg;
use base qw(Linux::Distribution::Packages);
sub _command {
my $self = shift;
$self->SUPER::_command('dpkg --list');
}
sub _parse {
my $self = shift;
my @data = split '\n', $self->{_data};
my $writer=shift;
foreach my $row (@data){
my ($ii, $desc, $pkg, $ver);
next if $row =~ m/^(Desired|\||\+).*/;
($ii, $pkg, $ver, $desc) = $row =~ m/^(.+?)\s+(.+?)\s+(.+?)\s+(.+)$/;
if ($self->{'format'} =~ m/xml/i){ $writer->emptyTag('package', 'name' => $pkg, 'version' => $ver , 'description' => $desc); next; }
my $row_func='_row_' . $self->{'format'};
$self->$row_func('', $pkg, $ver, $desc);
}
}
return 1;
package Linux::Distribution::Packages::rpm;
use base qw(Linux::Distribution::Packages);
sub _command {
my $self = shift;
$self->SUPER::_command('rpm -qa');
}
sub _parse {
my $self = shift;
my @data = split '\n', $self->{_data};
my $writer=shift;
foreach my $row (@data){
my ($pkg, $ver);
next if $row =~ m/^(Desired|\||\+).*/;
($pkg, $ver) = $row =~ m/^(.+)\-+(.+\-.+)$/;
if ($self->{'format'} =~ m/xml/i){ $writer->emptyTag('package', 'name' => $pkg, 'version' => $ver ); next; }
my $row_func='_row_' . $self->{'format'};
$self->$row_func('', $pkg, $ver, '');
}
}
package Linux::Distribution::Packages::pkgtool;
use base qw(Linux::Distribution::Packages);
sub _command {
my $self = shift;
$self->SUPER::_command('ls /var/log/packages');
}
sub _parse {
my $self = shift;
my @data = split '\n', $self->{_data};
my $writer=shift;
foreach my $row (@data){
my ($pkg, $ver);
($pkg, $ver) = $row =~ m/^(.+)\-(.+)\-.+\-\d+$/;
if ($self->{'format'} =~ m/xml/i){ $writer->emptyTag('package', 'name' => $pkg, 'version' => $ver ); next; }
my $row_func='_row_' . $self->{'format'};
$self->$row_func('', $pkg, $ver, '');
}
}
return 1;
__END__
=head1 NAME
Linux::Distribution::Packages - list all packages on various Linux distributions
=head1 SYNOPSIS
use Linux::Distribution::Packages qw(distribution_packages distribution_write);
$linux = new Linux::Distribution::Packages({'format' => 'csv', 'output_file' => 'packages.csv'});
$linux->distribution_write();
# Or you can (re)set the options when you write.
$linux->distribution_write({'format' => 'xml', 'output_file' => 'packages.xml'});
# If you want to reload the package data
$linux->distribution_packages();
=head1 DESCRIPTION
This is a simple module that uses Linux::Distribution to guess the linux
distribution and then uses the correct commands to list all the packages
on the system and then output them in one of three formats: native, csv,
and xml.
Distributions currently working: debian, ubuntu, fedora, redhat, suse,
gentoo, slackware, redflag.
The module inherits from Linux::Distribution, so can also use its calls.
=head2 EXPORT
None by default.
=head1 TODO
* Add the capability to correctly get packages for all recognized distributions.
* Seperate out parsing from writing. Parse data to hash and give access to hash.
Then write the formatted data from the hash.
=head1 AUTHORS
Judith Lebzelter, E<lt>judith@osdl.orgE<gt>
Alberto Re, E<lt>alberto@accidia.netE<gt>
=head1 COPYRIGHT AND LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.5 or,
at your option, any later version of Perl 5 you may have available.
=cut
|