blib/lib/Mac/SysProfile.pm

filter: Statementstmt 16.8% 16 / 95 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 33.3% 4 / 12 Podpod 85.7% 6 / 7 totaltotal 13.2% 26 / 196 CC 34 SCAR 42.3
CC
34
Cov
10%
gettype
156.0
xml
72.0
_structify_val
56.0
CRAP 68.6
↑ Summary
1 package Mac::SysProfile;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Mac::PropertyList ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.05';
10  
11 1 my %conf = (
12 'bin' => 'system_profiler',
13 'lst' => '-listDataTypes',
14 'sfw' => 'SPSoftwareDataType',
15 'sfx' => 'os_overview',
16 'osx' => 'os_version',
17 'drw' => 'kernel_version',
18 'xml' => '-xml',
19 );
20  
21 1 my %types;
22  
23 0 ▶sub new { bless {}, shift }
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
24  
25 0 ▶sub types {
Subroutine types: not called CC=4 SCAR=30.0
Pod documented
26 0 my $pro = shift;
27 0 for (`$conf{bin} $conf{lst}`) {
28 0 ▶ next if m/:/;
Branch: 1 branch
BranchTrueFalseTotal
if /:/ 0 0 0
29 0 chomp;
30 0 ▶ $pro->{$_} = undef unless exists $pro->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $pro->{$_} 0 0 0
31 0 $types{$_} = 1;
32 }
33 0 return [ keys %types ];
34 }
35  
36 0 ▶sub gettype {
Subroutine gettype: not called CC=12 SCAR=50.5
Pod documented
37 0 my ( $pro, $typ, $fre ) = @_;
38  
39 0 ▶ $pro->types() unless exists $types{$typ};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $types{$typ} 0 0 0
40  
41 0 ▶ if ( !exists $types{$typ} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $types{$typ}) 0 0 0
42 0 delete $pro->{$typ};
43 0 return;
44 }
45  
46 0 ▶ return $pro->{$typ} if $pro->{$typ} && !$fre;
Branch: 1 branch
BranchTrueFalseTotal
if $pro->{$typ} and not $fre 0 0 0
Condition0%0/3
$pro->{$typ} and not $fre
!ll&&!rl&&r
000
MC/DC0%0/2
$pro->{$typ} and not $fre
$pro->{$typ} not $fre
Truth table0%0/3
A and not(B)
A= $pro->{$typ}B= not $fre
ABresult
0X0
100
111
47  
48 0 my $data = Mac::PropertyList::parse_plist( $pro->xml($typ) );
49 0 my $items = $data->[0]{_items};
50  
51 # Workaround for types that have multiples (e.g. SPMemoryDataType)
52 0 ▶ if ( defined $items->[0] && exists $items->[0]{'_items'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $items->[0] and exists $items->[0]{"_items"}) 0 0 0
Condition0%0/3
defined $items->[0] and exists $items->[0]{"_items"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $items->[0] and exists $items->[0]{"_items"}
defined $items->[0] exists $items->[0]{"_items"}
Truth table0%0/3
A and B
A= defined $items->[0]B= exists $items->[0]{"_items"}
ABresult
0X0
100
111
53 0 $items = $items->[0]{'_items'};
54 }
55  
56 0 my $hdr;
57 0 foreach my $item ( @{$items} ) {
58 0 my $res;
59  
60 0 my $name = '';
61  
62 # not used to keep from making the already complex structure arbitrarily more complex
63 # would also need done in _structify_val()
64 # my $name = delete $item->{'_name'} || '';
65 # if ( ref($name) ) {
66 # $name = $name->value();
67 # }
68  
69 0 ▶ while ( my ( $key, $value ) = each %{$item} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$item;}) 0 0 0
70 0 my $val = $value->value();
71 0 my $end_val;
72  
73 0 ▶ if ( ref($val) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) { } 0 0 0
74 0 $end_val = $val->value();
75 }
76 else {
77 0 $end_val = $val;
78 }
79  
80 0 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 0 0 0
81 0 $res->{$name}{$key} = _structify_val($end_val);
82 }
83 else {
84 0 $res->{$key} = _structify_val($end_val);
85 }
86 }
87  
88 0 push @{ $pro->{$typ} }, $res;
89 }
90  
91 0 ▶ $pro->{$typ} ||= [];
Condition0%0/2
$pro->{$typ} ||= []
l!l
00
MC/DC0%0/1
$pro->{$typ} ||= []
$pro->{$typ}
Truth table0%0/2
A
A= $pro->{$typ}
Aresult
11
00
92  
93 0 return $pro->{$typ};
94 }
95  
96 # recursive fixer upper: Storable::dclone() does not do it, Mac::PropertyList::plist_as_perl() does not seem to work-patches welcome!
97 0 ▶sub _structify_val {
Subroutine _structify_val: not called CC=7 SCAR=40.3
98 0 my ($val) = @_;
99 0 my $ref = Scalar::Util::reftype($val);
100 0 ▶ return $val if !$ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 0 0 0
101 0 ▶ if ( $ref eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 0 0 0
elsif ($ref eq 'SCALAR') { } 0 0 0
elsif ($ref eq 'HASH') { } 0 0 0
102 0 my @arr;
103 0 for my $item ( @{$val} ) {
104 0 push @arr, _structify_val($item);
105 }
106 0 return \@arr;
107 }
108 elsif ( $ref eq 'SCALAR' ) {
109 0 return $val->value();
110 }
111 elsif ( $ref eq 'HASH' ) {
112 0 my %hsh;
113 0 ▶ while ( my ( $k, $v ) = each %{$val} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$val;}) 0 0 0
114 0 $hsh{$k} = _structify_val($v);
115 }
116 0 return \%hsh;
117 }
118 else {
119 0 die "Do not know $ref";
120 }
121 }
122  
123 0 ▶sub osx {
Subroutine osx: not called CC=4 SCAR=30.0
Pod documented
124 0 my $pro = shift;
125 0 ▶ my $fre = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
126 0 ▶ return $pro->{_osx_version} if $pro->{_osx} && !$fre;
Branch: 1 branch
BranchTrueFalseTotal
if $pro->{"_osx"} and not $fre 0 0 0
Condition0%0/3
$pro->{"_osx"} and not $fre
!ll&&!rl&&r
000
MC/DC0%0/2
$pro->{"_osx"} and not $fre
$pro->{"_osx"} not $fre
Truth table0%0/3
A and not(B)
A= $pro->{"_osx"}B= not $fre
ABresult
0X0
100
111
127 0 $pro->gettype( $conf{sfw}, $fre );
128 0 ( $pro->{_osx_version} ) = $pro->{ $conf{sfw} }->{ $conf{sfx} }->{ $conf{osx} } =~ m/\s(\d+(\.\d+)*)\D/;
129 0 return $pro->{_osx_version};
130 }
131  
132 0 ▶sub darwin {
Subroutine darwin: not called CC=4 SCAR=30.0
Pod documented
133 0 my $pro = shift;
134 0 ▶ my $fre = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
135 0 ▶ return $pro->{_darwin_version} if $pro->{_darwin_version} && !$fre;
Branch: 1 branch
BranchTrueFalseTotal
if $pro->{"_darwin_version"} and not $fre 0 0 0
Condition0%0/3
$pro->{"_darwin_version"} and not $fre
!ll&&!rl&&r
000
MC/DC0%0/2
$pro->{"_darwin_version"} and not $fre
$pro->{"_darwin_version"} not $fre
Truth table0%0/3
A and not(B)
A= $pro->{"_darwin_version"}B= not $fre
ABresult
0X0
100
111
136 0 $pro->gettype( $conf{sfw}, $fre );
137 0 ( $pro->{_darwin_version} ) = $pro->{ $conf{sfw} }->{ $conf{sfx} }->{ $conf{drw} } =~ m/\s(\d+(\.\d+)*)\D/;
138 0 return $pro->{_darwin_version};
139 }
140  
141 0 ▶sub state_hashref {
Subroutine state_hashref: not called CC=1 SCAR=6.9
Pod documented
142 0 my $pro = shift;
143 0 my %x = %{$pro};
144 0 return \%x;
145 }
146  
147 0 ▶sub xml {
Subroutine xml: not called CC=8 SCAR=42.8
Pod documented
148 0 my $pro = shift;
149 0 my $key = shift;
150 0 ▶ my $fh = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
151 0 ▶ my $raw = exists $types{$key} ? `$conf{'bin'} $conf{'xml'} $key` : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $types{$key} ? : 0 0 0
152 0 ▶ print $fh $raw if ref $fh eq 'GLOB' && defined $raw;
Branch: 1 branch
BranchTrueFalseTotal
if ref $fh eq "GLOB" and defined $raw 0 0 0
Condition0%0/3
ref $fh eq "GLOB" and defined $raw
!ll&&!rl&&r
000
MC/DC0%0/2
ref $fh eq "GLOB" and defined $raw
ref $fh eq "GLOB" defined $raw
Truth table0%0/3
A and B
A= ref $fh eq "GLOB"B= defined $raw
ABresult
0X0
100
111
153 0 ▶ if ( $fh && !ref $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh and not ref $fh) 0 0 0
Condition0%0/3
$fh and not ref $fh
!ll&&!rl&&r
000
MC/DC0%0/2
$fh and not ref $fh
$fh not ref $fh
Truth table0%0/3
A and not(B)
A= $fhB= not ref $fh
ABresult
0X0
100
111
154 0 ▶ open XML, ">$fh" or return;
Branch: 1 branch
BranchTrueFalseTotal
open XML, ">$fh" or return 0 0 0
155 0 print XML $raw;
156 0 close XML;
157 }
158 0 return $raw;
159 }
160  
161 1 1;
162  
163 __END__
↑ Summary