ProcAlike/PerlInfo.pm

filter: Statementstmt 14.0% 18 / 128 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 35.7% 5 / 14 Podpod 33.3% 1 / 3 totaltotal 10.9% 24 / 220 CC 32 SCAR 44.3
CC
32
Cov
9%
manage_modules
210.0
manage_env
72.0
_get_module_metric
20.0
CRAP 84.2
« ProcAlike/POEInfo.pm ↑ Summary ProcAlike/ModuleInfo.pm »
1 # Declare our package
2 package POE::Devel::ProcAlike::PerlInfo;
3 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # Initialize our version
6 1 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 $VERSION = '0.02';
8  
9 # Set our superclass
10 1 ▶use base 'Filesys::Virtual::Async::inMemory';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # portable tools
13 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
16 # make sure we set a readonly filesystem!
17 0 return __PACKAGE__->SUPER::new(
18 'readonly' => 1,
19 );
20 }
21  
22 #/perl
23 # # place for generic perl data
24 #
25 # binary # $^X
26 # version # $^V
27 # pid # $$
28 # script # $0
29 # osname # $^O
30 # starttime # $^T
31 #
32 # inc # dumps the @inc array
33 #
34 # /env
35 # # dumps the %ENV hash
36 #
37 # PWD # data is $ENV{PWD}
38 # ...
39 #
40 # /modules
41 # # lists all loaded modules
42 #
43 # /Foo-Bar
44 # # module name will be converted to above format
45 #
46 # version # $module->VERSION ||= 'UNDEF'
47 # path # module's path in %INC
48 1 my %fs = (
49 'binary' => $^X . "\n",
50 'version' => "$^V\n",
51 'pid' => $$ . "\n",
52 'script' => $0 . "\n",
53 'osname' => $^O . "\n",
54 'starttime' => $^T . "\n",
55 'inc' => join( "\n", @INC ) . "\n",
56  
57 'env' => \&manage_env,
58  
59 'modules' => \&manage_modules,
60 );
61  
62 # helper to get loaded modules
63 0 ▶sub _get_loadedmodules {
Subroutine _get_loadedmodules: not called CC=2 SCAR=17.9
64 0 my @list = grep { $_ !~ /(?:al|ix)$/ } keys %INC; # remove those annoying non-module files
65 0 for ( @list ) {
66 0 s/\.pm$//; # remove trailing .pm
67 0 $_ = join( '-', File::Spec->splitdir( $_ ) ); # convert "/" into "-" ( portably )
68 }
69  
70 0 return \@list;
71 }
72  
73 0 ▶sub _get_module_metrics {
Subroutine _get_module_metrics: not called CC=1 SCAR=6.9
74 0 return [ qw( version path ) ];
75 }
76  
77 0 ▶sub _get_module_metric {
Subroutine _get_module_metric: not called CC=4 SCAR=30.0
78 0 my $incpath = shift;
79 0 my $module = shift;
80 0 my $metric = shift;
81  
82 # what metric?
83 0 ▶ if ( $metric eq 'version' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($metric eq 'version') { } 0 0 0
elsif ($metric eq 'path') { } 0 0 0
84 0 my $size = join( '::', split( '-', $module ) );
85  
86 ## no critic
87 0 $size = eval "$size->VERSION";
88 ## use critic
89  
90 0 ▶ if ( defined $size ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size) { } 0 0 0
91 0 return $size . "\n";
92 } else {
93 0 return 'UNDEF' . "\n";
94 }
95 } elsif ( $metric eq 'path' ) {
96 0 return $INC{ $incpath } . "\n";
97 } else {
98 0 die "unknown module metric: $metric\n";
99 }
100 }
101  
102 0 ▶sub manage_modules {
Subroutine manage_modules: not called CC=14 SCAR=53.5
Pod undocumented
103 0 my( $type, @path ) = @_;
104  
105 # what's the operation?
106 0 ▶ if ( $type eq 'readdir' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'readdir') { } 0 0 0
elsif ($type eq 'stat') { } 0 0 0
elsif ($type eq 'open') { } 0 0 0
107 # trying to read the root or the module itself?
108 0 ▶ if ( defined $path[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path[0]) { } 0 0 0
109 # shortcut, because we always know what's in the module dir
110 0 return _get_module_metrics();
111 } else {
112 # list all loaded modules
113 0 return _get_loadedmodules();
114 }
115 } elsif ( $type eq 'stat' ) {
116 # set some default data
117 0 my ($atime, $ctime, $mtime, $size, $modes);
118 0 $atime = $ctime = $mtime = time();
119 0 my ($dev, $ino, $rdev, $blocks, $gid, $uid, $nlink, $blksize) = ( 0, 0, 0, 1, (split( /\s+/, $) ))[0], $>, 1, 1024 );
120  
121 # trying to stat the dir or stuff inside it?
122 0 ▶ if ( defined $path[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path[0]) { } 0 0 0
123 # convert it back to %INC type
124 0 my $incpath = join( '/', split( '-', $path[0] ) ) . '.pm';
125  
126 # does it exist?
127 0 ▶ if ( ! exists $INC{ $incpath } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $INC{$incpath}) 0 0 0
128 0 return;
129 }
130  
131 # trying to stat the module or data inside it?
132 0 ▶ if ( defined $path[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path[1]) { } 0 0 0
133 # valid filename?
134 0 ▶ if ( ! grep { $_ eq $path[1] } @{ _get_module_metrics() } or defined $path[2] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not grep({$_ eq $path[1];} @{_get_module_metrics();}) or defined $path[2]) 0 0 0
Condition0%0/3
not grep({$_ eq $path[1];} @{_get_module_metrics();}) or defined $path[2]
l!l&&r!l&&!r
000
MC/DC0%0/2
not grep({$_ eq $path[1];} @{_get_module_metrics();}) or defined $path[2]
not grep({$_ eq $path[1];} @{_get_module_metrics();}) defined $path[2]
Truth table0%0/3
not(A) or B
A= not grep({$_ eq $path[1];} @{_get_module_metrics();})B= defined $path[2]
ABresult
1X1
011
000
135 0 return;
136 }
137  
138 0 $modes = oct( '100644' );
139  
140 # get the data
141 0 $size = length( _get_module_metric( $incpath, $path[0], $path[1] ) );
142 } else {
143 # a directory, munge the data
144 0 $size = 0;
145 0 $modes = oct( '040755' );
146 0 $nlink = 2;
147 }
148 } else {
149 # a directory, munge the data
150 0 $size = 0;
151 0 $modes = oct( '040755' );
152 0 $nlink = 2 + scalar @{ _get_loadedmodules() };
153 }
154  
155 # finally, return the darn data!
156 0 return( [ $dev, $ino, $modes, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks ] );
157 } elsif ( $type eq 'open' ) {
158 # convert it back to %INC type
159 0 my $incpath = join( '/', split( '-', $path[0] ) ) . '.pm';
160  
161 # does it exist?
162 0 ▶ if ( ! exists $INC{ $incpath } or ! defined $path[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $INC{$incpath} and defined $path[1]) 0 0 0
Condition0%0/3
exists $INC{$incpath} and defined $path[1]
!ll&&!rl&&r
000
MC/DC0%0/2
exists $INC{$incpath} and defined $path[1]
exists $INC{$incpath} defined $path[1]
Truth table0%0/3
A and B
A= exists $INC{$incpath}B= defined $path[1]
ABresult
0X0
100
111
163 0 return;
164 }
165  
166 # valid filename?
167 0 ▶ if ( ! grep { $_ eq $path[1] } @{ _get_module_metrics() } or defined $path[2] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not grep({$_ eq $path[1];} @{_get_module_metrics();}) or defined $path[2]) 0 0 0
Condition0%0/3
not grep({$_ eq $path[1];} @{_get_module_metrics();}) or defined $path[2]
l!l&&r!l&&!r
000
MC/DC0%0/2
not grep({$_ eq $path[1];} @{_get_module_metrics();}) or defined $path[2]
not grep({$_ eq $path[1];} @{_get_module_metrics();}) defined $path[2]
Truth table0%0/3
not(A) or B
A= not grep({$_ eq $path[1];} @{_get_module_metrics();})B= defined $path[2]
ABresult
1X1
011
000
168 0 return;
169 }
170  
171 # get the metric!
172 0 my $data = _get_module_metric( $incpath, $path[0], $path[1] );
173 0 return \$data;
174 }
175 }
176  
177 0 ▶sub manage_env {
Subroutine manage_env: not called CC=8 SCAR=42.8
Pod undocumented
178 0 my( $type, @path ) = @_;
179  
180 # what's the operation?
181 0 ▶ if ( $type eq 'readdir' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'readdir') { } 0 0 0
elsif ($type eq 'stat') { } 0 0 0
elsif ($type eq 'open') { } 0 0 0
182 # we don't have any subdirs so simply return the entire hash!
183 0 return [ keys %ENV ];
184 } elsif ( $type eq 'stat' ) {
185 # set some default data
186 0 my ($atime, $ctime, $mtime, $size, $modes);
187 0 $atime = $ctime = $mtime = time();
188 0 my ($dev, $ino, $rdev, $blocks, $gid, $uid, $nlink, $blksize) = ( 0, 0, 0, 1, (split( /\s+/, $) ))[0], $>, 1, 1024 );
189  
190 # trying to stat the dir or stuff inside it?
191 0 ▶ if ( defined $path[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path[0]) { } 0 0 0
192 # does it exist?
193 0 ▶ if ( ! exists $ENV{ $path[0] } or defined $path[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $ENV{$path[0]} or defined $path[1]) 0 0 0
Condition0%0/3
not exists $ENV{$path[0]} or defined $path[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
not exists $ENV{$path[0]} or defined $path[1]
not exists $ENV{$path[0]} defined $path[1]
Truth table0%0/3
not(A) or B
A= not exists $ENV{$path[0]}B= defined $path[1]
ABresult
1X1
011
000
194 0 return;
195 }
196  
197 # a file, munge the data
198 0 $size = length( $ENV{ $path[0] } . "\n" );
199 0 $modes = oct( '100644' );
200 } else {
201 # a directory, munge the data
202 0 $size = 0;
203 0 $modes = oct( '040755' );
204 0 $nlink = 2;
205 }
206  
207 # finally, return the darn data!
208 0 return( [ $dev, $ino, $modes, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks ] );
209 } elsif ( $type eq 'open' ) {
210 # return a scalar ref
211 0 ▶ if ( exists $ENV{ $path[0] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{$path[0]}) { } 0 0 0
212 0 my $data = $ENV{ $path[0] } . "\n";
213 0 return \$data;
214 } else {
215 0 return;
216 }
217 }
218 }
219  
220 # we cheat here and not implement a lot of stuff because we know the FUSE api never calls the "extra" APIs
221 # that ::Async provides. Furthermore, this is a read-only filesystem so we can skip even more APIs :)
222  
223 # _rmtree
224  
225 # _scandir
226  
227 # _move
228  
229 # _copy
230  
231 # _load
232  
233 0 ▶sub _readdir {
Subroutine _readdir: not called CC=2 SCAR=17.9
234 0 my( $self, $path ) = @_;
235  
236 0 ▶ if ( $path eq File::Spec->rootdir() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq 'File::Spec'->rootdir) { } 0 0 0
237 0 return [ keys %fs ];
238 } else {
239 # sanitize the path
240 0 my @dirs = File::Spec->splitdir( $path );
241 0 shift( @dirs ); # get rid of the root entry which is always '' for me
242 0 return $fs{ $dirs[0] }->( 'readdir', @dirs[ 1 .. $#dirs ] );
243 }
244 }
245  
246 # _rmdir
247  
248 # _mkdir
249  
250 # _rename
251  
252 # _mknod
253  
254 # _unlink
255  
256 # _chmod
257  
258 # _truncate
259  
260 # _chown
261  
262 # _utime
263  
264 0 ▶sub _stat {
Subroutine _stat: not called CC=4 SCAR=30.0
265 0 my( $self, $path ) = @_;
266  
267 # stating the root?
268 0 ▶ if ( $path eq File::Spec->rootdir() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "File::Spec"->rootdir) 0 0 0
269 0 my ($atime, $ctime, $mtime, $size, $modes);
270 0 $atime = $ctime = $mtime = time();
271 0 my ($dev, $ino, $rdev, $blocks, $gid, $uid, $nlink, $blksize) = ( 0, 0, 0, 1, (split( /\s+/, $) ))[0], $>, 1, 1024 );
272 0 $size = 0;
273 0 $modes = oct( '040755' );
274  
275 # count subdirs
276 0 $nlink = 2 + grep { ref $fs{ $_ } } keys %fs;
277  
278 0 return( [ $dev, $ino, $modes, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks ] );
279 }
280  
281 # sanitize the path
282 0 my @dirs = File::Spec->splitdir( $path );
283 0 shift( @dirs ); # get rid of the root entry which is always '' for me
284  
285 0 ▶ if ( exists $fs{ $dirs[0] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fs{$dirs[0]}) { } 0 0 0
286 # directory or file?
287 0 ▶ if ( ref $fs{ $dirs[0] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fs{$dirs[0]}) { } 0 0 0
288 # trying to stat the dir or the subpath?
289 0 return $fs{ $dirs[0] }->( 'stat', @dirs[ 1 .. $#dirs ] );
290 } else {
291 # arg, stat is a finicky beast!
292 0 my $size = length( $fs{ $dirs[0] } );
293 0 my $modes = oct( '100644' );
294  
295 0 my ($dev, $ino, $rdev, $blocks, $gid, $uid, $nlink, $blksize) = ( 0, 0, 0, 1, (split( /\s+/, $) ))[0], $>, 1, 1024 );
296 0 my ($atime, $ctime, $mtime);
297 0 $atime = $ctime = $mtime = time();
298  
299 0 return( [ $dev, $ino, $modes, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks ] );
300 }
301 } else {
302 0 return;
303 }
304 }
305  
306 # _write
307  
308 0 ▶sub _open {
Subroutine _open: not called CC=4 SCAR=30.0
309 0 my( $self, $path ) = @_;
310  
311 # sanitize the path
312 0 my @dirs = File::Spec->splitdir( $path );
313 0 shift( @dirs ); # get rid of the root entry which is always '' for me
314 0 ▶ if ( defined $dirs[0] and exists $fs{ $dirs[0] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dirs[0] and exists $fs{$dirs[0]}) { } 0 0 0
Condition0%0/3
defined $dirs[0] and exists $fs{$dirs[0]}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $dirs[0] and exists $fs{$dirs[0]}
defined $dirs[0] exists $fs{$dirs[0]}
Truth table0%0/3
A and B
A= defined $dirs[0]B= exists $fs{$dirs[0]}
ABresult
0X0
100
111
315 # directory or file?
316 0 ▶ if ( ref $fs{ $dirs[0] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fs{$dirs[0]}) { } 0 0 0
317 0 return $fs{ $dirs[0] }->( 'open', @dirs[ 1 .. $#dirs ] );
318 } else {
319 # return a scalar ref
320 0 return \$fs{ $dirs[0] };
321 }
322 } else {
323 0 return;
324 }
325 }
326  
327 1 1;
328 __END__
« ProcAlike/POEInfo.pm ↑ Summary ProcAlike/ModuleInfo.pm »