PiFlash/State.pm

filter: Statementstmt 70.4% 81 / 115 Branchbran 50.0% 25 / 50 Conditioncond 6.9% 2 / 29 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 77.7% 14 / 18 Podpod 0.0% 0 / 9 totaltotal 51.4% 122 / 237 CC 39 SCAR 44.0
CC
39
Cov
56%
odump
272.0
read_config
10.1
has
6.0
CRAP 81.4
« PiFlash/Plugin.pm ↑ Summary PiFlash/Object.pm »
1 # PiFlash::State - store program-site state information for PiFlash
2 # by Ian Kluft
3 #
4 # the information stored here includes configuration,command-line arguments, system hardware inspection results, etc
5 #
6  
7 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use v5.14.0; # require 2011 or newer version of Perl
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # State class to hold program state, and print it all out in case of errors
12 # this is a low-level package - it stores state data but at this level has no knowledge of what is being stored in it
13 package PiFlash::State;
14 6 $PiFlash::State::VERSION = '0.4.3';
15 6 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use YAML::XS; # RPM: perl-YAML-LibYAML, DEB: libyaml-libyaml-perl
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # ABSTRACT: PiFlash::State class to store configuration, device info and program state
20  
21  
22  
23 # initialize state as empty
24 ## no critic (ProhibitPackageVars)
25 6 our $state;
26 ## use critic
27  
28 # initialize class' singleton object from parameters
29 # class method
30 sub init
31 1 ▶{
Subroutine init: called CC=5 SCAR=16.2
Pod undocumented
32 ## no critic (ProhibitPackageVars)
33 1 my $class = shift;
34 1 ▶ (defined $PiFlash::State::state) and return; # avoid damaging data if called again
Branch: 1 branch
BranchTrueFalseTotal
defined $PiFlash::State::state and return 0 1 1
35  
36 # global security settings for YAML::XS parser
37 # since PiFlash can run parts as root, we don't want any external code to be run without user authorization
38 1 $YAML::XS::LoadBlessed = 0;
39 1 $YAML::XS::UseCode = 0;
40 1 $YAML::XS::LoadCode = 0;
41  
42 # instantiate the state object as a singleton (only one instance in the system)
43 1 $PiFlash::State::state = {};
44 1 bless $PiFlash::State::state, $class;
45 1 my $self = $PiFlash::State::state;
46  
47 # loop through parameters adding each name as a top-level state hash and accessor functions
48 1 ▶ while (scalar @_ > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 0) 2 1 3
49 2 my $top_level_param = shift;
50  
51 # create top-level hash named for the parameter
52 2 $self->{$top_level_param} = {};
53  
54 # generate class accessor methods named for the parameter
55 {
56 ## no critic (ProhibitNoStrict)
57 6 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 # accessor fieldname()
60 2 ▶ if (! $class->can($top_level_param)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->can($top_level_param)) 2 0 2
61 2 ▶ *{$class."::".$top_level_param} = sub { return $class->accessor($top_level_param, @_); };
Subroutine __ANON__: called CC=1 SCAR=0.0
62 }
63  
64 # accessor has_fieldname()
65 2 ▶ if (! $class->can("has_".$top_level_param)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->can('has_' . $top_level_param)) 2 0 2
66 0 ▶ *{$class."::has_".$top_level_param} = sub { return $class->has($top_level_param, @_); };
Subroutine __ANON__: not called CC=1 SCAR=0.4
67 }
68 }
69 }
70 1 return;
71 }
72  
73 # get top level state
74 sub state
75 32 ▶{
Subroutine state: called CC=3 SCAR=12.5
Pod undocumented
76 32 my ($package, $filename, $line) = caller;
77 32 ▶ ($package eq "PiFlash::State" or $package->isa("PiFlash::State"))
Branch: 1 branch
BranchTrueFalseTotal
$package eq "PiFlash::State" or $package->isa("PiFlash::State") or die "internal-use-only function called by $package at $filename line $line" 32 0 32
Condition33%1/3
$package eq "PiFlash::State" or $package->isa("PiFlash::State")
l!l&&r!l&&!r
3200
MC/DC0%0/2
$package eq "PiFlash::State" or $package->isa("PiFlash::State")
$package eq "PiFlash::State" $package->isa("PiFlash::State")
Truth table33%1/3
A or B
A= $package eq "PiFlash::State"B= $package->isa("PiFlash::State")
ABresult
1X1
011
000
78 or die "internal-use-only function called by $package at $filename line $line";
79 32 return $PiFlash::State::state;
80 }
81  
82 # state value get/set accessor
83 # class method
84 32 ▶sub accessor {
Subroutine accessor: called CC=4 SCAR=13.9
Pod undocumented
85 32 my $class = shift;
86 32 my $top_level_param = shift;
87 32 my $name = shift;
88 32 my $value = shift;
89 32 my $self = $class->state();
90 32 ▶ if (defined $value) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $value) { } 12 20 32
elsif (defined $name) { } 10 10 20
91 # got name & value - set the new value for name
92 12 $self->{$top_level_param}{$name} = $value;
93 12 return;
94 } elsif (defined $name) {
95 # got only name - return the value/ref of name
96 return (exists $self->{$top_level_param}{$name})
97 10 ▶ ? $self->{$top_level_param}{$name}
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$top_level_param}{$name} ? : 10 0 10
98 : undef;
99 } else {
100 # no name or value - return ref to top-level hash (top_level_parameter from init() context)
101 10 return $self->{$top_level_param};
102 }
103 }
104  
105 # check if a top level state has a key
106 # class method
107 sub has
108 0 ▶{
Subroutine has: not called CC=2 SCAR=17.9
Pod undocumented
109 0 my $class = shift;
110 0 my $self = $class->state();
111 0 my $top_level_param = shift;
112 0 my $name = shift;
113 0 ▶ return ((exists $self->{$top_level_param}) and (exists $self->{$top_level_param}{$name}));
Condition0%0/3
exists $self->{$top_level_param} && exists $self->{$top_level_param}{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{$top_level_param} && exists $self->{$top_level_param}{$name}
exists $self->{$top_level_param} exists $self->{$top_level_param}{$name}
Truth table0%0/3
A && B
A= exists $self->{$top_level_param}B= exists $self->{$top_level_param}{$name}
ABresult
0X0
100
111
114 }
115  
116 # return boolean value for verbose mode
117 sub verbose
118 1 ▶{
Subroutine verbose: called CC=2 SCAR=11.6
Pod undocumented
119 1 ▶ return PiFlash::State::cli_opt("verbose") // 0;
Condition0%0/2
cli_opt("verbose") // 0
l!l
00
MC/DC0%0/1
cli_opt("verbose") // 0
cli_opt("verbose")
Truth table0%0/2
A
A= cli_opt("verbose")
Aresult
11
00
120 }
121  
122 # return boolean value for logging mode (recording run data without printing verbose messages, intended for testing)
123 sub logging
124 0 ▶{
Subroutine logging: not called CC=2 SCAR=17.9
Pod undocumented
125 0 ▶ return PiFlash::State::cli_opt("logging") // 0;
Condition0%0/2
cli_opt("logging") // 0
l!l
00
MC/DC0%0/1
cli_opt("logging") // 0
cli_opt("logging")
Truth table0%0/2
A
A= cli_opt("logging")
Aresult
11
00
126 }
127  
128 # dump data structure recursively, part of verbose/logging state output
129 # intended as a lightweight equivalent of Data::Dumper without requiring installation of an extra package
130 # object method
131 sub odump
132 0 ▶{
Subroutine odump: not called CC=16 SCAR=56.1
Pod undocumented
133 0 my ($obj, $level) = @_;
134 0 ▶ if (!defined $obj) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $obj) 0 0 0
135 # bail out for undefined value
136 0 return "";
137 }
138 0 ▶ if (!ref $obj) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $obj) 0 0 0
139 # process plain scalar
140 0 return (" " x $level)."[value]".$obj."\n";
141 }
142 0 ▶ if (ref $obj eq "SCALAR") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj eq "SCALAR") 0 0 0
143 # process scalar reference
144 0 ▶ return (" " x $level).($$obj // "undef")."\n";
Condition0%0/2
$$obj // "undef"
l!l
00
MC/DC0%0/1
$$obj // "undef"
$$obj
Truth table0%0/2
A
A= $$obj
Aresult
11
00
145 }
146 0 ▶ if (ref $obj eq "HASH" or ref $obj eq "PiFlash::State"
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj eq "HASH" or ref $obj eq "PiFlash::State" or ref $obj =~ /^PiFlash::/u and $obj->isa("PiFlash::Object")) 0 0 0
Condition0%0/9
ref $obj eq "HASH" or ref $obj eq "PiFlash::State" or ref $obj =~ /^PiFlash::/u and $obj->isa("PiFlash::Object")
l!l&&r!l&&!r
000
ref $obj eq "HASH" or ref $obj eq "PiFlash::State"
l!l&&r!l&&!r
000
ref $obj =~ /^PiFlash::/u and $obj->isa("PiFlash::Object")
!ll&&!rl&&r
000
MC/DC0%0/4
ref $obj eq "HASH" or ref $obj eq "PiFlash::State" or ref $obj =~ /^PiFlash::/u and $obj->isa("PiFlash::Object")
ref $obj eq "HASH" ref $obj eq "PiFlash::State" ref $obj =~ /^PiFlash::/u $obj->isa("PiFlash::Object")
Truth table0%0/5
A or B or C and D
A= ref $obj eq "HASH"B= ref $obj eq "PiFlash::State"C= ref $obj =~ /^PiFlash::/uD= $obj->isa("PiFlash::Object")
ABCDresult
1XXX1
01XX1
00111
000X0
00100
147 or (ref $obj =~ /^PiFlash::/ and $obj->isa("PiFlash::Object")))
148 {
149 # process hash reference
150 0 my $str = "";
151 0 foreach my $key (sort {lc $a cmp lc $b} keys %$obj) {
152 0 ▶ if (ref $obj->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj->{$key}) { } 0 0 0
153 0 $str .= (" " x $level)."$key:"."\n";
154 0 $str .= odump($obj->{$key}, $level+1);
155 } else {
156 0 ▶ $str .= (" " x $level)."$key: ".($obj->{$key} // "undef")."\n";
Condition0%0/2
$obj->{$key} // "undef"
l!l
00
MC/DC0%0/1
$obj->{$key} // "undef"
$obj->{$key}
Truth table0%0/2
A
A= $obj->{$key}
Aresult
11
00
157 }
158 }
159 0 return $str;
160 }
161 0 ▶ if (ref $obj eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj eq "ARRAY") 0 0 0
162 # process array reference
163 0 my $str = "";
164 0 foreach my $entry (@$obj) {
165 0 ▶ if (ref $entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $entry) { } 0 0 0
166 0 $str .= odump($entry, $level+1);
167 } else {
168 0 $str .= (" " x $level)."$entry\n";
169 }
170 }
171 0 return $str;
172 }
173 0 ▶ if (ref $obj eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj eq "CODE") 0 0 0
174 # process function reference
175 0 return (" " x $level)."[function]$obj"."\n";
176 }
177 # other references/unknown type
178 0 my $type = ref $obj;
179 0 return (" " x $level)."[$type]$obj"."\n";
180 }
181  
182 # die/exception with verbose state dump
183 # class method
184 sub error
185 1 ▶{
Subroutine error: called CC=3 SCAR=16.5
Pod undocumented
186 ## no critic (ProhibitPackageVars)
187 1 my $class = shift;
188 1 my $message = shift;
189 1 ▶ croak "error: ".$message.((verbose() or logging())
Branch: 1 branch
BranchTrueFalseTotal
verbose() || logging() ? : 0 0 0
Condition0%0/3
verbose() or logging()
l!l&&r!l&&!r
000
MC/DC0%0/2
verbose() or logging()
verbose() logging()
Truth table0%0/3
A or B
A= verbose()B= logging()
ABresult
1X1
011
000
190 ? "\nProgram state dump...\n".odump($PiFlash::State::state,0) : "");
191 }
192  
193 # read YAML configuration file
194 sub read_config
195 10 ▶{
Subroutine read_config: called CC=10 SCAR=23.1
Pod undocumented
196 10 my $filepath = shift;
197  
198 # if the provided file name exists and ...
199 10 ▶ if ( -f $filepath) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $filepath) 10 0 10
200 # capture as many YAML documents as can be parsed from the configuration file
201 10 my @yaml_docs = eval { YAML::XS::LoadFile($filepath); };
202 10 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 9 10
203 1 PiFlash::State->error("PiFlash::State::read_config error reading $filepath: $@");
204 }
205  
206 # save the first YAML document as the configuration
207 9 my $yaml_config = shift @yaml_docs;
208 9 ▶ if (ref $yaml_config eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $yaml_config eq 'HASH') { } 8 1 9
209 # if it's a hash, then use all its mappings in PiFlash::State::config
210 8 $PiFlash::State::state->{config} = $yaml_config;
211 } else {
212 # otherwise save the reference in a config entry called config
213 1 PiFlash::State::config("config", $yaml_config);
214 }
215  
216 # if any other YAML documents were parsed, save them as a list in a config called "docs"
217 # these are available for plugins but not currently defined
218 9 ▶ if (@yaml_docs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@yaml_docs) 6 3 9
219 # save the YAML doc structures as a list
220 6 PiFlash::State::config("docs", \@yaml_docs);
221  
222 # the first doc must be the table of contents with a list of metadata about following docs
223 # others after that are categorized by the plugin name in the metadata
224 6 my $toc = $yaml_docs[0];
225 6 ▶ if (ref $toc eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $toc eq "ARRAY") 5 1 6
226 5 PiFlash::State::plugin("docs", {toc => $toc});
227 5 my $docs = PiFlash::State::plugin("docs");
228 5 ▶ for (my $i=1; $i < scalar @yaml_docs; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @yaml_docs) 12 5 17
229 12 ▶ ($i <= scalar @$toc) or next;
Branch: 1 branch
BranchTrueFalseTotal
$i <= scalar @$toc or next 11 1 12
230 11 ▶ if (ref $yaml_docs[$i] eq "HASH" and exists $toc->[$i-1]{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $yaml_docs[$i] eq "HASH" and exists $$toc[$i - 1]{"type"}) 11 0 11
Condition33%1/3
ref $yaml_docs[$i] eq "HASH" and exists $$toc[$i - 1]{"type"}
!ll&&!rl&&r
0011
MC/DC0%0/2
ref $yaml_docs[$i] eq "HASH" and exists $$toc[$i - 1]{"type"}
ref $yaml_docs[$i] eq "HASH" exists $$toc[$i - 1]{"type"}
Truth table33%1/3
A and B
A= ref $yaml_docs[$i] eq "HASH"B= exists $$toc[$i - 1]{"type"}
ABresult
0X0
100
111
231 11 my $type = $toc->[$i-1]{type};
232 11 $docs->{$type} = $yaml_docs[$i];
233 }
234 }
235 }
236 }
237 }
238 }
239  
240 6 1;
241  
242 __END__
« PiFlash/Plugin.pm ↑ Summary PiFlash/Object.pm »