Introspector/Probe/Packages/Apt.pm

filter: Statementstmt 100% 62 / 62 Branchbran 83.3% 10 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 22 / 22 Podpod 0.0% 0 / 1 totaltotal 96.9% 94 / 97 CC 7 SCAR 4.5
CC
7
Cov
97%
_check_apt_state
3.0
_gather_upgradable
3.0
_gather_sources
2.0
CRAP 1.6
« Introspector/Probe/Processes.pm ↑ Summary Introspector/Probe/Puppet.pm »
1 package System::Introspector::Probe::Packages::Apt;
2 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use System::Introspector::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
6 handle_from_command
7 transform_exceptions
8 output_from_file
9 files_from_dir
10 1 );
11  
12 1 has apt_lists_dir => (is => 'ro', builder => 1);
13 1 ▶has apt_update_after => (is => 'ro', default => sub { 86400 });
Subroutine __ANON__: called CC=1 SCAR=0.0
14 1 has apt_update => (is => 'ro');
15  
16 1 has apt_sources => (is => 'ro', builder => 1);
17 1 has apt_sources_dir => (is => 'ro', builder => 1);
18  
19 2 ▶sub _build_apt_lists_dir { '/var/lib/apt/lists' }
Subroutine _build_apt_lists_dir: called CC=1 SCAR=0.0
20 1 ▶sub _build_apt_sources { '/etc/apt/sources.list' }
Subroutine _build_apt_sources: called CC=1 SCAR=0.0
21 1 ▶sub _build_apt_sources_dir { '/etc/apt/sources.list.d' }
Subroutine _build_apt_sources_dir: called CC=1 SCAR=0.0
22  
23 2 ▶sub gather {
Subroutine gather: called CC=1 SCAR=0.0
Pod undocumented
24 2 my ($self) = @_;
25 return {
26 update => {
27 last => $self->_last_apt_update,
28 2 ▶ run => transform_exceptions {
Subroutine __ANON__: called CC=1 SCAR=0.0
29 2 return { result => $self->_check_apt_state };
30 },
31 },
32 2 ▶ installed => transform_exceptions {
Subroutine __ANON__: called CC=1 SCAR=0.0
33 2 return { packages => $self->_gather_installed };
34 },
35 2 ▶ upgradable => transform_exceptions {
Subroutine __ANON__: called CC=1 SCAR=0.0
36 2 return { actions => $self->_gather_upgradable };
37 },
38 2 ▶ sources => transform_exceptions {
Subroutine __ANON__: called CC=1 SCAR=0.0
39 2 return { config => $self->_gather_sources };
40 },
41 2 };
42 }
43  
44 3 ▶sub _last_apt_update {
Subroutine _last_apt_update: called CC=1 SCAR=0.0
45 3 my ($self) = @_;
46 3 return scalar( (stat($self->apt_lists_dir))[9] );
47 }
48  
49 2 ▶sub _check_apt_state {
Subroutine _check_apt_state: called CC=3 SCAR=11.0
50 2 my ($self) = @_;
51 2 ▶ return 'disabled' unless $self->apt_update;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->apt_update 1 1 2
52 1 my $threshold = $self->apt_update_after;
53 1 my $last_change = $self->_last_apt_update;
54 1 ▶ return 'no'if ($last_change + $threshold) > time;
Branch: 1 branch
BranchTrueFalseTotal
if $last_change + $threshold > time 0 1 1
55 1 handle_from_command 'apt-get update';
56 1 return 'yes';
57 }
58  
59 2 ▶sub _open_dpkg_query_pipe {
Subroutine _open_dpkg_query_pipe: called CC=1 SCAR=0.0
60 2 my ($self) = @_;
61 2 return handle_from_command 'dpkg-query --show';
62 }
63  
64 2 ▶sub _open_apt_get_upgrade_simulation_pipe {
Subroutine _open_apt_get_upgrade_simulation_pipe: called CC=1 SCAR=0.0
65 2 my ($self) = @_;
66 2 return handle_from_command 'apt-get -s upgrade';
67 }
68  
69 2 ▶sub _gather_sources {
Subroutine _gather_sources: called CC=2 SCAR=7.0
70 2 my ($self) = @_;
71 2 my $sources_dir = $self->apt_sources_dir;
72 return {
73 'sources_list' => $self->_fetch_source_list($self->apt_sources),
74 2 ▶ 'sources_list_dir' => (-e $sources_dir) ? transform_exceptions {
Subroutine __ANON__: called CC=1 SCAR=0.0
75 return +{ files => +{ map {
76 2 ($_, $self->_fetch_source_list("$sources_dir/$_"));
77 } files_from_dir $sources_dir } };
78 } : {},
79 2 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
-e $sources_dir ? : 2 0 2
80 }
81  
82 5 ▶sub _fetch_source_list {
Subroutine _fetch_source_list: called CC=1 SCAR=0.0
83 5 my ($self, $file) = @_;
84 5 ▶ return transform_exceptions {
Subroutine __ANON__: called CC=1 SCAR=0.0
85 return {
86 5 file_name => $file,
87 body => scalar(output_from_file $file),
88 };
89 5 };
90 }
91  
92 2 ▶sub _gather_upgradable {
Subroutine _gather_upgradable: called CC=3 SCAR=11.0
93 2 my ($self) = @_;
94 2 my $pipe = $self->_open_apt_get_upgrade_simulation_pipe;
95 2 my %action;
96 2 ▶ while (defined( my $line = <$pipe> )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $pipe)) 12 2 14
97 12 chomp $line;
98 12 ▶ if ($line =~ m{^(inst|remv)\s+(\S+)\s+(.+)$}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(inst|remv)\s+(\S+)\s+(.+)$/i) 2 10 12
99 2 $action{ lc($1) }{ $2 } = $3;
100 }
101 }
102 2 return \%action;
103 }
104  
105 2 ▶sub _gather_installed {
Subroutine _gather_installed: called CC=2 SCAR=6.9
106 2 my ($self) = @_;
107 2 my $pipe = $self->_open_dpkg_query_pipe;
108 2 my %package;
109 2 ▶ while (defined( my $line = <$pipe> )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $pipe)) 338 2 340
110 338 chomp $line;
111 338 my ($package, $version) = split m{\s+}, $line;
112 338 $package{ $package } = {
113 version => $version,
114 };
115 }
116 2 return \%package;
117 }
118  
119 1 1;
120  
121 __END__
« Introspector/Probe/Processes.pm ↑ Summary Introspector/Probe/Puppet.pm »