From: Robert 'phaylon' Sedlacek Date: Fri, 15 Jun 2012 01:37:57 +0000 (+0000) Subject: find upgradable packages and optionally update apt index X-Git-Tag: v0.001_001~47 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=025e0d3f36c4b9bc3fc7db95d80625cfa61ee389;p=scpubgit%2FSystem-Introspector.git find upgradable packages and optionally update apt index --- diff --git a/lib/System/Introspector/Probe/Packages/Apt.pm b/lib/System/Introspector/Probe/Packages/Apt.pm index 3dee62b..2b8ccb4 100644 --- a/lib/System/Introspector/Probe/Packages/Apt.pm +++ b/lib/System/Introspector/Probe/Packages/Apt.pm @@ -6,20 +6,68 @@ use System::Introspector::Util qw( transform_exceptions ); +has apt_lists_dir => (is => 'ro', builder => 1); +has apt_update_after => (is => 'ro', default => sub { 86400 }); +has apt_update => (is => 'ro'); + +sub _build_apt_lists_dir { '/var/lib/apt/lists' } + sub gather { my ($self) = @_; return { + update => { + last => $self->_last_apt_update, + run => transform_exceptions { + return { result => $self->_check_apt_state }; + }, + }, installed => transform_exceptions { return { packages => $self->_gather_installed }; }, + upgradable => transform_exceptions { + return { actions => $self->_gather_upgradable }; + }, }; } +sub _last_apt_update { + my ($self) = @_; + return scalar( (stat($self->apt_lists_dir))[9] ); +} + +sub _check_apt_state { + my ($self) = @_; + return 'disabled' unless $self->apt_update; + my $threshold = $self->apt_update_after; + my $last_change = $self->_last_apt_update; + return 'no'if ($last_change + $threshold) > time; + handle_from_command 'apt-get update'; + return 'yes'; +} + sub _open_dpkg_query_pipe { my ($self) = @_; return handle_from_command 'dpkg-query --show'; } +sub _open_apt_get_upgrade_simulation_pipe { + my ($self) = @_; + return handle_from_command 'apt-get -s upgrade'; +} + +sub _gather_upgradable { + my ($self) = @_; + my $pipe = $self->_open_apt_get_upgrade_simulation_pipe; + my %action; + while (defined( my $line = <$pipe> )) { + chomp $line; + if ($line =~ m{^(inst|remv)\s+(\S+)\s+(.+)$}i) { + $action{ lc($1) }{ $2 } = $3; + } + } + return \%action; +} + sub _gather_installed { my ($self) = @_; my $pipe = $self->_open_dpkg_query_pipe;