File Coverage

lib/DPKG/Log/Analyse.pm
Criterion Covered Total %
statement 17 19 89.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 24 26 92.3


line stmt bran cond sub pod time code
1             package DPKG::Log::Analyse;
2             BEGIN {
3 1     1   598 $DPKG::Log::Analyse::VERSION = '1.20';
4             }
5              
6              
7             =head1 NAME
8              
9             DPKG::Log::Analyse - Analyse a dpkg log
10              
11             =head1 VERSION
12              
13             version 1.20
14              
15             =head1 SYNOPSIS
16              
17             use DPKG::Log;
18              
19             my $analyser = DPKG::Log::Analyse->new('filename' => 'dpkg.log');
20             $analyser->analyse;
21              
22             =head1 DESCRIPTION
23              
24             This module is used to analyse a dpkg log.
25              
26             =head1 METHODS
27              
28             =over 4
29              
30             =cut
31              
32 1     1   10 use strict;
  1         3  
  1         40  
33 1     1   6 use warnings;
  1         3  
  1         32  
34 1     1   25 use 5.010;
  1         3  
  1         31  
35              
36 1     1   5 use Carp;
  1         2  
  1         85  
37 1     1   5 use DPKG::Log;
  1         2  
  1         22  
38 1     1   456 use DPKG::Log::Analyse::Package;
  0            
  0            
39             use Params::Validate qw(:all);
40              
41             =item $analser = DPKG::Log::Analyse->new('filename' => 'dpkg.log')
42              
43             =item $analyser = DPKG::Log::Analyse->new('log_handle' => \$dpkg_log)
44              
45             Returns a new DPKG::Log::Analyse object.
46             Filename parameter can be ommitted, it defaults to /var/log/dpkg.log.
47              
48             Its possible to specify an existing DPKG::Log object instead of a filename.
49             This will be used and overrides any filename setting.
50              
51             =cut
52             sub new {
53             my $package = shift;
54             $package = ref($package) if ref($package);
55              
56             my %params = validate(
57             @_, {
58             'filename' => { 'type' => SCALAR, 'default' => '/var/log/dpkg.log' },
59             'log_handle' => { isa => 'DPKG::Log', default => undef }
60             }
61             );
62            
63             my $self = {
64             packages => {},
65             newly_installed_packages => {},
66             installed_and_removed => {},
67             removed_packages => {},
68             upgraded_packages => {},
69             halfinstalled_packages => {},
70             halfconfigured_packages => {},
71             unpacked_packages => {},
72             installed_and_removed_packages => {},
73             };
74              
75             if ($params{'filename'}) {
76             $self->{'filename'} = $params{'filename'};
77             }
78             if ($params{'log_handle'}) {
79             $self->{dpkg_log} = $params{'log_handle'};
80             } else {
81             $self->{dpkg_log} = DPKG::Log->new('filename' => $self->{'filename'});
82             }
83             $self->{dpkg_log}->parse;
84              
85             bless($self, $package);
86              
87            
88             return $self;
89             }
90              
91             =item $analyser->analyse;
92              
93             Analyse the debian package log.
94              
95             =cut
96             sub analyse {
97             my $self = shift;
98             my $dpkg_log = $self->{dpkg_log};
99              
100             $self->{from} = $dpkg_log->{from};
101             $self->{to} = $dpkg_log->{to};
102              
103             my $analysed_entries=0;
104             foreach my $entry ($dpkg_log->entries) {
105             next if not $entry->associated_package;
106            
107             $analysed_entries++;
108              
109             # Initialize data structure if this is a package
110             my $package = $entry->associated_package;
111             if (not defined $self->{packages}->{$package}) {
112             $self->{packages}->{$package} = DPKG::Log::Analyse::Package->new('package' => $package);
113             }
114              
115             if ($entry->type eq 'action') {
116             my $obj = $self->{packages}->{$package};
117             if ($entry->action eq 'install') {
118             $self->{newly_installed_packages}->{$package} = $obj;
119             $self->{packages}->{$package}->version($entry->available_version);
120             } elsif ($entry->action eq 'upgrade') {
121             $self->{upgraded_packages}->{$package} = $obj;
122             $self->{packages}->{$package}->previous_version($entry->installed_version);
123             $self->{packages}->{$package}->version($entry->available_version);
124             } elsif ($entry->action eq 'remove') {
125             $self->{removed_packages}->{$package} = $obj;
126             $self->{packages}->{$package}->previous_version($entry->installed_version);
127             }
128             } elsif ($entry->type eq 'status') {
129             $self->{packages}->{$package}->status($entry->status);
130             $self->{packages}->{$package}->version($entry->installed_version);
131             }
132             }
133              
134             while (my ($package, $package_obj) = each %{$self->{packages}}) {
135             if ($self->{packages}->{$package}->status eq "half-installed") {
136             $self->{half_installed_packages}->{$package} = \$package_obj;
137             }
138             if ($self->{packages}->{$package}->status eq "half-configured") {
139             $self->{half_configured_packages}->{$package} = \$package_obj;
140             }
141             if ($self->{packages}->{$package}->status eq "unpacked") {
142             $self->{half_configured_packages}->{$package} = \$package_obj;
143             }
144             }
145              
146             # Remove packages from "newly_installed" if installed_version is empty
147             while (my ($package, $package_obj) = each %{$self->{newly_installed_packages}}) {
148             if (not $package_obj->version) {
149             delete($self->{newly_installed_packages}->{$package});
150             $self->{installed_and_removed_packages}->{$package} = $package_obj;
151             }
152             }
153              
154             # Forget about the log object once analysis is done
155             $self->{dpkg_log} = undef;
156              
157             return 1;
158             }
159              
160             =item $analyser->newly_installed_packages
161              
162             Return all packages which were newly installed in the dpkg.log.
163              
164             =cut
165             sub newly_installed_packages {
166             my $self = shift;
167             return $self->{newly_installed_packages};
168             }
169              
170             =item $analyser->upgraded_packages
171              
172              
173             Return all packages which were upgraded in the dpkg.log.
174              
175             =cut
176             sub upgraded_packages {
177             my $self = shift;
178             return $self->{upgraded_packages};
179             }
180              
181             =item $analyser->removed_packages
182              
183              
184             Return all packages which were removed in the dpkg.log.
185              
186             =cut
187             sub removed_packages {
188             my $self = shift;
189             return $self->{removed_packages};
190             }
191              
192             =item $analyser->unpacked_packages
193              
194              
195             Return all packages which are left in state 'unpacked'.
196              
197             =cut
198             sub unpacked_packages {
199             my $self = shift;
200             return $self->{unpacked_packages};
201             }
202              
203             =item $analyser->halfinstalled_packages
204              
205              
206             Return all packages which are left in state 'half-installed'.
207              
208             =cut
209             sub halfinstalled_packages {
210             my $self = shift;
211             return $self->{halfinstalled_packages};
212             }
213              
214             =item $analyser->halfconfigured_packages
215              
216              
217             Return all packages which are left in state 'half-configured'.
218              
219             =cut
220             sub halfconfigured_packages {
221             my $self = shift;
222             return $self->{halfconfigured_packages};
223             }
224              
225             =item $analyser->installed_and_removed_packages
226              
227             Return all packages which got installed and removed.
228              
229             =cut
230             sub installed_and_removed_packages {
231             my $self = shift;
232             return $self->{installed_and_removed_packages};
233             }
234              
235             =back
236              
237             =head1 SEE ALSO
238              
239             L, L
240              
241             =head1 AUTHOR
242              
243             Patrick Schoenfeld .
244              
245             =head1 COPYRIGHT AND LICENSE
246              
247             Copyright (C) 2011 Patrick Schoenfeld
248              
249             This library is free software.
250             You can redistribute it and/or modify it under the same terms as perl itself.
251              
252             =cut
253              
254             1;
255             # vim: expandtab:ts=4:sw=4