Codebase list liblinux-distribution-packages-perl / a2dee10
[svn-inject] Installing original source of liblinux-distribution-packages-perl (0.05) Takaki Taniguchi 13 years ago
6 changed file(s) with 392 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 * 2006-20-01
1 Version 0.01.
2
3 * 2006-02-02
4 Version 0.02
5 Add ability to write to file as option. Also can set format as an option.
6
7 * 2006-07-02
8 Version 0.03
9 Add Slackware as a distribution. This was submitted by Alberto Re.
10
11 * 2006-10-02
12 Version 0.04
13 Add Red Flag as a distribution. Alphabetize hash entries for easier find.
14
15 * 2006-19-04
16 Version 0.05
17 Add Fedora as a distribution.
0 Changes
1 Makefile.PL
2 MANIFEST
3 README
4 t/Linux-Distribution-Packages.t
5 lib/Linux/Distribution/Packages.pm
0 use 5.006000;
1 use ExtUtils::MakeMaker;
2 # See lib/ExtUtils/MakeMaker.pm for details of how to influence
3 # the contents of the Makefile that is written.
4 WriteMakefile(
5 NAME => 'Linux::Distribution::Packages',
6 VERSION_FROM => 'lib/Linux/Distribution/Packages.pm', # finds $VERSION
7 PREREQ_PM => { 'Linux::Distribution' => '0.14', 'XML::Writer' => '0' }, # e.g., Module::Name => 1.1
8 PREREQ_FATAL => '1',
9 ($] >= 5.005 ? ## I am not actually sure about this version
10 (ABSTRACT_FROM => 'lib/Linux/Distribution/Packages.pm', # retrieve abstract from module
11 AUTHOR => 'Judith Lebzelter <judith@osdl.org>') : ()),
12 );
0 Linux-Distribution version 0.01
1 ===============================
2
3 This is a simple module that uses Linux::Distribution to guess the linux
4 distribution and then uses the correct commands to list all the packages
5 on the system and then output them in one of three formats: native, csv,
6 and xml.
7
8 INSTALLATION
9
10 To install this module type the following:
11
12 perl Makefile.PL
13 make
14 make test
15 make install
16
17 DEPENDENCIES
18
19 This module dependends on modules Linux::Distribution and XML::Writer.
20
21 COPYRIGHT AND LICENCE
22
23 Copyright (C) 2006 by Judith Lebzelter <judith@osdl.org>
24
25 This library is free software; you can redistribute it and/or modify
26 it under the same terms as Perl itself, either Perl version 5.8.5 or,
27 at your option, any later version of Perl 5 you may have available.
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
0 # Before `make install' is performed this script should be runnable with
1 # `make test'. After `make install' it should work as `perl Linux-Distribution.t'
2
3 #########################
4
5 # change 'tests => 1' to 'tests => last_test_to_print';
6
7 use Test::More tests => 3;
8 use Linux::Distribution::Packages;
9
10 my $linux=new Linux::Distribution::Packages();
11
12 ok( defined($linux) , 'new() works 1' );
13 like( ref $linux, qr/^Linux::Distribution::Packages.*/, 'new() works 2' );
14 ok( $linux->distribution_write(), 'distribution_write() works' );
15