lib/CPANPLUS/Internals/Source/Memory.pm

filter: Statementstmt 97.5% 160 / 164 Branchbran 59.6% 31 / 52 Conditioncond 87.5% 21 / 24 MC/DCmcdc 69.2% 9 / 13 Subroutinesub 100% 28 / 28 Podpod n/a totaltotal 88.6% 249 / 281 CC 37 SCAR 17.3
CC
37
Cov
88%
__memory_save_source
14.3
__memory_retrieve_source
8.3
_init_trees
7.0
CRAP 5.6
« blib/lib/CPANPLUS.pm ↑ Summary blib/lib/CPANPLUS/Internals/Source/SQLite/Tie.pm »
1 package CPANPLUS::Internals::Source::Memory;
2  
3 14 ▶use base 'CPANPLUS::Internals::Source';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 14 ▶use CPANPLUS::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use CPANPLUS::Module;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use CPANPLUS::Module::Fake;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use CPANPLUS::Module::Author;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use CPANPLUS::Internals::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 14 ▶use File::Fetch;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 14 ▶use Archive::Extract;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 14 ▶use IPC::Cmd qw[can_run];
Subroutine BEGIN: called CC=1 SCAR=0.0
17 14 ▶use File::Temp qw[tempdir];
Subroutine BEGIN: called CC=1 SCAR=0.0
18 14 ▶use File::Basename qw[dirname];
Subroutine BEGIN: called CC=1 SCAR=0.0
19 14 ▶use Params::Check qw[allow check];
Subroutine BEGIN: called CC=1 SCAR=0.0
20 14 ▶use Module::Load::Conditional qw[can_load];
Subroutine BEGIN: called CC=1 SCAR=0.0
21 14 ▶use Locale::Maketext::Simple Class => 'CPANPLUS', Style => 'gettext';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 14 ▶use vars qw[$VERSION];
Subroutine BEGIN: called CC=1 SCAR=0.0
24 14 $VERSION = "0.9914";
25  
26 14 $Params::Check::VERBOSE = 1;
27  
28 =head1 NAME
29  
30 CPANPLUS::Internals::Source::Memory - In memory implementation
31  
32 =cut
33  
34 ### flag to show if init_trees got its' data from storable. This allows
35 ### us to not write an existing stored file back to disk
36 14 { my $from_storable;
37  
38 sub _init_trees {
39 32 ▶ my $self = shift;
Subroutine _init_trees: called CC=7 SCAR=19.5
40 32 my $conf = $self->configure_object;
41 32 my %hash = @_;
42  
43 32 my($path,$uptodate,$verbose,$use_stored);
44 32 my $tmpl = {
45 path => { default => $conf->get_conf('base'), store => \$path },
46 verbose => { default => $conf->get_conf('verbose'), store => \$verbose },
47 uptodate => { required => 1, store => \$uptodate },
48 use_stored => { default => 1, store => \$use_stored },
49 };
50  
51 32 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 0 32 32
52  
53 ### retrieve the stored source files ###
54 32 ▶ my $stored = $self->__memory_retrieve_source(
Condition100%5/5
$self->__memory_retrieve_source('path', $path, 'uptodate', $uptodate && $use_stored, 'verbose', $verbose) || {}
l!l
131
$uptodate && $use_stored
!ll&&!rl&&r
14171
MC/DC100%1/1
$self->__memory_retrieve_source('path', $path, 'uptodate', $uptodate && $use_stored, 'verbose', $verbose) || {}
$self->__memory_retrieve_source('path', $path, 'uptodate', $uptodate && $use_stored, 'verbose', $verbose)
MC/DC100%2/2
$uptodate && $use_stored
$uptodate $use_stored
Truth table100%2/2
A
A= $self->__memory_retrieve_source('path', $path, 'uptodate', $uptodate && $use_stored, 'verbose', $verbose)
Aresult
11
00
Truth table100%3/3
A && B
A= $uptodateB= $use_stored
ABresult
0X0
100
111
55 path => $path,
56 uptodate => $uptodate && $use_stored,
57 verbose => $verbose,
58 ) || {};
59  
60 ### we got this from storable if $stored has keys..
61 32 ▶ $from_storable = keys %$stored ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
keys %$stored ? : 1 31 32
62  
63 ### set up the trees
64 32 ▶ $self->_atree( $stored->{_atree} || {} );
Condition100%2/2
$stored->{'_atree'} || {}
l!l
131
MC/DC100%1/1
$stored->{'_atree'} || {}
$stored->{'_atree'}
Truth table100%2/2
A
A= $stored->{'_atree'}
Aresult
11
00
65 32 ▶ $self->_mtree( $stored->{_mtree} || {} );
Condition100%2/2
$stored->{'_mtree'} || {}
l!l
131
MC/DC100%1/1
$stored->{'_mtree'} || {}
$stored->{'_mtree'}
Truth table100%2/2
A
A= $stored->{'_mtree'}
Aresult
11
00
66  
67 32 return 1;
68 }
69  
70 32 ▶ sub _standard_trees_completed { return $from_storable }
Subroutine _standard_trees_completed: called CC=1 SCAR=0.0
71 32 ▶ sub _custom_trees_completed { return $from_storable }
Subroutine _custom_trees_completed: called CC=1 SCAR=0.0
72  
73 sub _finalize_trees {
74 33 ▶ my $self = shift;
Subroutine _finalize_trees: called CC=4 SCAR=13.9
75 33 my $conf = $self->configure_object;
76 33 my %hash = @_;
77  
78 33 my($path,$uptodate,$verbose);
79 33 my $tmpl = {
80 path => { default => $conf->get_conf('base'), store => \$path },
81 verbose => { default => $conf->get_conf('verbose'), store => \$verbose },
82 uptodate => { required => 1, store => \$uptodate },
83 };
84  
85 33 { local $Params::Check::ALLOW_UNKNOWN = 1;
86 33 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 0 33 33
87 }
88  
89 ### write the stored files to disk, so we can keep using them
90 ### from now on, till they become invalid
91 ### write them if the original sources weren't uptodate, or
92 ### we didn't just load storable files
93 33 ▶ $self->__memory_save_source() if !$uptodate or not $from_storable;
Branch: 1 branch
BranchTrueFalseTotal
unless $uptodate and $from_storable 32 1 33
Condition100%3/3
$uptodate and $from_storable
!ll&&!rl&&r
15171
MC/DC100%2/2
$uptodate and $from_storable
$uptodate $from_storable
Truth table100%3/3
A and B
A= $uptodateB= $from_storable
ABresult
0X0
100
111
94  
95 33 return 1;
96 }
97  
98 ### saves current memory state
99 sub _save_state {
100 1 ▶ my $self = shift;
Subroutine _save_state: called CC=1 SCAR=0.0
101 1 return $self->_finalize_trees( @_, uptodate => 0 );
102 }
103 }
104  
105 14 sub _add_author_object {
106 124 ▶ my $self = shift;
Subroutine _add_author_object: called CC=3 SCAR=11.1
107 124 my %hash = @_;
108  
109 124 my $class;
110 my $tmpl = {
111 class => { default => 'CPANPLUS::Module::Author', store => \$class },
112 124 map { $_ => { required => 1 } }
113 qw[ author cpanid email ]
114 };
115  
116 124 my $href = do {
117 124 local $Params::Check::NO_DUPLICATES = 1;
118 124 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 0 124 124
119 };
120  
121 124 my $obj = $class->new( %$href, _id => $self->_id );
122  
123 124 ▶ $self->author_tree->{ $href->{'cpanid'} } = $obj or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->author_tree->{$href->{"cpanid"}} = $obj or return 0 124 124
124  
125 124 return $obj;
126 }
127  
128 {
129 my $tmpl = {
130 class => { default => 'CPANPLUS::Module' },
131 14 map { $_ => { required => 1 } } qw[
132 module version path comment author package description dslip mtime
133 ],
134 };
135  
136 sub _add_module_object {
137 310 ▶ my $self = shift;
Subroutine _add_module_object: called CC=3 SCAR=11.1
138 310 my %hash = @_;
139  
140 310 my $href = do {
141 310 local $Params::Check::SANITY_CHECK_TEMPLATE = 0;
142 310 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 0 310 310
143 };
144 310 my $class = delete $href->{class};
145  
146 310 my $obj = $class->new( %$href, _id => $self->_id );
147  
148 ### Every module get's stored as a module object ###
149 310 ▶ $self->module_tree->{ $href->{module} } = $obj or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->module_tree->{$href->{"module"}} = $obj or return 0 310 310
150  
151 310 return $obj;
152 }
153 }
154  
155 14 { my %map = (
156 _source_search_module_tree => [ module_tree => 'CPANPLUS::Module' ],
157 _source_search_author_tree => [ author_tree => 'CPANPLUS::Module::Author' ],
158 );
159  
160 14 ▶ while( my($sub, $aref) = each %map ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %map) 28 14 42
161 14 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
162  
163 28 my($meth, $class) = @$aref;
164  
165 *$sub = sub {
166 84 ▶ my $self = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
167 84 my $conf = $self->configure_object;
168 84 my %hash = @_;
169  
170 84 my($authors,$list,$verbose,$type);
171 84 my $tmpl = {
172 data => { default => [],
173 strict_type=> 1, store => \$authors },
174 allow => { required => 1, default => [ ], strict_type => 1,
175 store => \$list },
176 verbose => { default => $conf->get_conf('verbose'),
177 store => \$verbose },
178 type => { required => 1, allow => [$class->accessors()],
179 store => \$type },
180 };
181  
182 84 ▶ my $args = check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $args = check($tmpl, \%hash) or return 0 84 84
183  
184 84 my @rv;
185 84 for my $obj ( values %{ $self->$meth } ) {
186 #push @rv, $auth if check(
187 # { $type => { allow => $list } },
188 # { $type => $auth->$type }
189 # );
190 822 ▶ push @rv, $obj if allow( $obj->$type() => $list );
Branch: 1 branch
BranchTrueFalseTotal
if allow($obj->$type, $list) 400 422 822
191 }
192  
193 84 return @rv;
194 }
195 28 }
196 }
197  
198 =pod
199  
200 =head2 $cb->__memory_retrieve_source(name => $name, [path => $path, uptodate => BOOL, verbose => BOOL])
201  
202 This method retrieves a I<storable>d tree identified by C<$name>.
203  
204 It takes the following arguments:
205  
206 =over 4
207  
208 =item name
209  
210 The internal name for the source file to retrieve.
211  
212 =item uptodate
213  
214 A flag indicating whether the file-cache is up-to-date or not.
215  
216 =item path
217  
218 The absolute path to the directory holding the source files.
219  
220 =item verbose
221  
222 A boolean flag indicating whether or not to be verbose.
223  
224 =back
225  
226 Will get information from the config file by default.
227  
228 Returns a tree on success, false on failure.
229  
230 =cut
231  
232 sub __memory_retrieve_source {
233 32 ▶ my $self = shift;
Subroutine __memory_retrieve_source: called CC=8 SCAR=21.2
234 32 my %hash = @_;
235 32 my $conf = $self->configure_object;
236  
237 32 my $tmpl = {
238 path => { default => $conf->get_conf('base') },
239 verbose => { default => $conf->get_conf('verbose') },
240 uptodate => { default => 0 },
241 };
242  
243 32 ▶ my $args = check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $args = check($tmpl, \%hash) or return 0 32 32
244  
245 ### check if we can retrieve a frozen data structure with storable ###
246 32 ▶ my $storable = can_load( modules => {'Storable' => '0.0'} )
Branch: 1 branch
BranchTrueFalseTotal
if $conf->get_conf("storable") 32 0 32
247 if $conf->get_conf('storable');
248  
249 32 ▶ return unless $storable;
Branch: 1 branch
BranchTrueFalseTotal
unless $storable 0 32 32
250  
251 ### $stored is the name of the frozen data structure ###
252 32 my $stored = $self->__memory_storable_file( $args->{path} );
253  
254 32 ▶ if ($storable && -e $stored && -s _ && $args->{'uptodate'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($storable and -e $stored and -s _ and $args->{'uptodate'}) { } 1 31 32
Condition77%7/9
$storable and -e $stored and -s _ and $args->{"uptodate"}
!ll&&!rl&&r
12191
$storable and -e $stored and -s _
!ll&&!rl&&r
12020
$storable and -e $stored
!ll&&!rl&&r
01220
MC/DC50%2/4
$storable and -e $stored and -s _ and $args->{"uptodate"}
$storable -e $stored -s _ $args->{"uptodate"}
Truth table60%3/5
A and B and C and D
A= $storableB= -e $storedC= -s _D= $args->{"uptodate"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
255 1 msg( loc("Retrieving %1", $stored), $args->{'verbose'} );
256  
257 1 my $href = Storable::retrieve($stored);
258 1 return $href;
259 } else {
260 31 return;
261 }
262 }
263  
264 =pod
265  
266 =head2 $cb->__memory_save_source([verbose => BOOL, path => $path])
267  
268 This method saves all the parsed trees in I<storable>d format if
269 C<Storable> is available.
270  
271 It takes the following arguments:
272  
273 =over 4
274  
275 =item path
276  
277 The absolute path to the directory holding the source files.
278  
279 =item verbose
280  
281 A boolean flag indicating whether or not to be verbose.
282  
283 =back
284  
285 Will get information from the config file by default.
286  
287 Returns true on success, false on failure.
288  
289 =cut
290  
291 sub __memory_save_source {
292 32 ▶ my $self = shift;
Subroutine __memory_save_source: called CC=11 SCAR=26.6
293 32 my %hash = @_;
294 32 my $conf = $self->configure_object;
295  
296  
297 32 my $tmpl = {
298 path => { default => $conf->get_conf('base'), allow => DIR_EXISTS },
299 verbose => { default => $conf->get_conf('verbose') },
300 force => { default => 1 },
301 };
302  
303 32 ▶ my $args = check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $args = check($tmpl, \%hash) or return 0 32 32
304  
305 32 my $aref = [qw[_mtree _atree]];
306  
307 ### check if we can retrieve a frozen data structure with storable ###
308 32 my $storable;
309 32 ▶ $storable = can_load( modules => {'Storable' => '0.0'} )
Branch: 1 branch
BranchTrueFalseTotal
if $conf->get_conf("storable") 32 0 32
310 if $conf->get_conf('storable');
311 32 ▶ return unless $storable;
Branch: 1 branch
BranchTrueFalseTotal
unless $storable 0 32 32
312  
313 32 my $to_write = {};
314 32 foreach my $key ( @$aref ) {
315 64 ▶ next unless ref( $self->$key );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->$key 0 64 64
316 64 $to_write->{$key} = $self->$key;
317 }
318  
319 32 ▶ return unless keys %$to_write;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$to_write 0 32 32
320  
321 ### $stored is the name of the frozen data structure ###
322 32 my $stored = $self->__memory_storable_file( $args->{path} );
323  
324 32 ▶ if (-e $stored && not -w $stored) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $stored and not -w $stored) 0 32 32
Condition66%2/3
-e $stored and not -w $stored
!ll&&!rl&&r
12200
MC/DC0%0/2
-e $stored and not -w $stored
-e $stored not -w $stored
Truth table66%2/3
A and not(B)
A= -e $storedB= not -w $stored
ABresult
0X0
100
111
325 0 msg( loc("%1 not writable; skipped.", $stored), $args->{'verbose'} );
326 0 return;
327 }
328  
329 msg( loc("Writing compiled source information to disk. This might take a little while."),
330 32 $args->{'verbose'} );
331  
332 32 my $flag;
333 32 ▶ unless( Storable::nstore( $to_write, $stored ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Storable::nstore($to_write, $stored)) 0 32 32
334 0 error( loc("could not store %1!", $stored) );
335 0 $flag++;
336 }
337  
338 32 ▶ return $flag ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$flag ? : 0 32 32
339 }
340  
341 sub __memory_storable_file {
342 64 ▶ my $self = shift;
Subroutine __memory_storable_file: called CC=4 SCAR=14.3
343 64 my $conf = $self->configure_object;
344 64 ▶ my $path = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $path = shift() or return 0 64 64
345  
346 ### check if we can retrieve a frozen data structure with storable ###
347 64 ▶ my $storable = $conf->get_conf('storable')
Branch: 1 branch
BranchTrueFalseTotal
$conf->get_conf('storable') ? : 64 0 64
348 ? can_load( modules => {'Storable' => '0.0'} )
349 : 0;
350  
351 64 ▶ return unless $storable;
Branch: 1 branch
BranchTrueFalseTotal
unless $storable 0 64 64
352  
353 ### $stored is the name of the frozen data structure ###
354 ### changed to use File::Spec->catfile -jmb
355 64 my $stored = File::Spec->rel2abs(
356 File::Spec->catfile(
357 $path, #base dir
358 $conf->_get_source('stored') #file
359 . '.s' .
360 $Storable::VERSION #the version of storable
361 . '.c' .
362 $self->VERSION #the version of CPANPLUS
363 . STORABLE_EXT #append a suffix
364 )
365 );
366  
367 64 return $stored;
368 }
369  
370  
371  
372  
373 # Local variables:
374 # c-indentation-style: bsd
375 # c-basic-offset: 4
376 # indent-tabs-mode: nil
377 # End:
378 # vim: expandtab shiftwidth=4:
379  
380 14 1;
« blib/lib/CPANPLUS.pm ↑ Summary blib/lib/CPANPLUS/Internals/Source/SQLite/Tie.pm »