1 package System::Introspector::Probe::Packages::Apt;
5 use System::Introspector::Util qw(
12 has apt_lists_dir => (is => 'ro', builder => 1);
13 has apt_update_after => (is => 'ro', default => sub { 86400 });
14 has apt_update => (is => 'ro');
16 has apt_sources => (is => 'ro', builder => 1);
17 has apt_sources_dir => (is => 'ro', builder => 1);
19 sub _build_apt_lists_dir { '/var/lib/apt/lists' }
20 sub _build_apt_sources { '/etc/apt/sources.list' }
21 sub _build_apt_sources_dir { '/etc/apt/sources.list.d' }
27 last => $self->_last_apt_update,
28 run => transform_exceptions {
29 return { result => $self->_check_apt_state };
32 installed => transform_exceptions {
33 return { packages => $self->_gather_installed };
35 upgradable => transform_exceptions {
36 return { actions => $self->_gather_upgradable };
38 sources => transform_exceptions {
39 return { config => $self->_gather_sources };
44 sub _last_apt_update {
46 return scalar( (stat($self->apt_lists_dir))[9] );
49 sub _check_apt_state {
51 return 'disabled' unless $self->apt_update;
52 my $threshold = $self->apt_update_after;
53 my $last_change = $self->_last_apt_update;
54 return 'no'if ($last_change + $threshold) > time;
55 handle_from_command 'apt-get update';
59 sub _open_dpkg_query_pipe {
61 return handle_from_command 'dpkg-query --show';
64 sub _open_apt_get_upgrade_simulation_pipe {
66 return handle_from_command 'apt-get -s upgrade';
71 my $sources_dir = $self->apt_sources_dir;
73 'sources_list' => $self->_fetch_source_list($self->apt_sources),
74 'sources_list_dir' => (-e $sources_dir) ? transform_exceptions {
75 return +{ files => +{ map {
76 ($_, $self->_fetch_source_list("$sources_dir/$_"));
77 } files_from_dir $sources_dir } };
82 sub _fetch_source_list {
83 my ($self, $file) = @_;
84 return transform_exceptions {
87 body => scalar(output_from_file $file),
92 sub _gather_upgradable {
94 my $pipe = $self->_open_apt_get_upgrade_simulation_pipe;
96 while (defined( my $line = <$pipe> )) {
98 if ($line =~ m{^(inst|remv)\s+(\S+)\s+(.+)$}i) {
99 $action{ lc($1) }{ $2 } = $3;
105 sub _gather_installed {
107 my $pipe = $self->_open_dpkg_query_pipe;
109 while (defined( my $line = <$pipe> )) {
111 my ($package, $version) = split m{\s+}, $line;
112 $package{ $package } = {
125 System::Introspector::Packages::Apt - Gather APT package status
129 Uses C<dpkg-query> to list all installed packages.
135 =item L<System::Introspector>