blib/lib/Module/ScanDeps/Cache.pm

filter: Statementstmt 100% 63 / 63 Branchbran 67.8% 19 / 28 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 4 totaltotal 80.5% 95 / 118 CC 16 SCAR 12.8
CC
16
Cov
86%
store_cache
7.5
prereq_missing
3.1
_file_2_md5
3.1
CRAP 3.6
« blib/lib/Module/ScanDeps.pm ↑ Summary blib/script/scandeps.pl »
1 package Module::ScanDeps::Cache;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 my $has_DMD5;
5 1 eval { require Digest::MD5 };
6 1 ▶$has_DMD5 = 1 unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 1 0 1
7 1 my $has_Storable;
8 1 eval { require Storable };
9 1 ▶$has_Storable = 1 unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 1 0 1
10
11
12 1 my $cache;
13 my $cache_file;
14 1 my $cache_dirty;
15
16 9 ▶sub prereq_missing{
Subroutine prereq_missing: called CC=3 SCAR=11.4
Pod undocumented
17 9 my @missing;
18 9 ▶ push @missing, 'Digest::MD5' unless $has_DMD5;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_DMD5 0 9 9
19 9 ▶ push @missing, 'Storable' unless $has_Storable;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_Storable 0 9 9
20 9 return @missing;
21 }
22
23 8 ▶sub init_from_file{
Subroutine init_from_file: called CC=3 SCAR=11.0
Pod undocumented
24 8 my $c_file = shift;
25 8 ▶ return 0 if prereq_missing();
Branch: 1 branch
BranchTrueFalseTotal
if prereq_missing() 0 8 8
26 8 eval{$cache = Storable::retrieve($c_file)};
27 #warn $@ if ($@);
28 8 ▶ unless ($cache){
Branch: 1 branch
BranchTrueFalseTotal
unless ($cache) 1 7 8
29 1 warn "Couldn't retrieve data from file $c_file. Building new cache.\n";
30 1 $cache = {};
31 }
32 8 $cache_file = $c_file;
33 8 return 1;
34 }
35
36 8 ▶sub store_cache{
Subroutine store_cache: called CC=5 SCAR=20.1
Pod undocumented
37 8 ▶ my $c_file = shift || $cache_file;
Condition33%1/3
shift() || $cache_file
l!l&&r!l&&!r
080
MC/DC0%0/2
shift() || $cache_file
shift() $cache_file
Truth table33%1/3
A || B
A= shift()B= $cache_file
ABresult
1X1
011
000
38 # no need to store to the file we retrieved from
39 # unless we have seen changes written to the cache
40 8 ▶ return unless ($cache_dirty
Branch: 1 branch
BranchTrueFalseTotal
unless $cache_dirty or $c_file ne $cache_file 0 8 8
Condition33%1/3
$cache_dirty or $c_file ne $cache_file
l!l&&r!l&&!r
800
MC/DC0%0/2
$cache_dirty or $c_file ne $cache_file
$cache_dirty $c_file ne $cache_file
Truth table33%1/3
A or B
A= $cache_dirtyB= $c_file ne $cache_file
ABresult
1X1
011
000
41 || $c_file ne $cache_file);
42 8 ▶ Storable::nstore($cache, $c_file)
Branch: 1 branch
BranchTrueFalseTotal
Storable::nstore($cache, $c_file) or warn "Could not store cache to file $c_file!" 8 0 8
43 or warn "Could not store cache to file $c_file!";
44 }
45
46 9 ▶sub get_cache_cb{
Subroutine get_cache_cb: called CC=1 SCAR=0.0
Pod undocumented
47 75 ▶ return sub{
Subroutine __ANON__: called CC=3 SCAR=11.0
48 75 my %args = @_;
49 75 ▶ if ( $args{action} eq 'read' ){
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'action'} eq 'read') { } 54 21 75
elsif ($args{'action'} eq 'write') { } 20 1 21
50 54 return _read_cache( %args );
51 }
52 elsif ( $args{action} eq 'write' ){
53 20 return _write_cache( %args );
54 }
55 1 die "action in cache_cb must be read or write!";
56 9 };
57 }
58
59 ### check for existence of the entry
60 ### check for identity of the file
61 ### pass cached value in $mod_aref
62 ### return true in case of a hit
63
64 54 ▶sub _read_cache{
Subroutine _read_cache: called CC=3 SCAR=11.0
65 54 my %args = @_;
66 54 my ($key, $file, $mod_aref) = @args{qw/key file modules/};
67 54 ▶ return 0 unless (exists $cache->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $cache->{$key} 20 34 54
68 34 my $entry = $cache->{$key};
69 34 my $checksum = _file_2_md5($file);
70 34 ▶ if ($entry->{checksum} eq $checksum){
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{"checksum"} eq $checksum) 33 1 34
71 33 @$mod_aref = @{$entry->{modules}};
72 33 return 1;
73 }
74 1 return 0;
75 }
76
77 20 ▶sub _write_cache{
Subroutine _write_cache: called CC=2 SCAR=7.0
78 20 my %args = @_;
79 20 my ($key, $file, $mod_aref) = @args{qw/key file modules/};
80 20 ▶ my $entry = $cache->{$key} ||= {};
Condition50%1/2
$cache->{$key} ||= {}
l!l
020
MC/DC0%0/1
$cache->{$key} ||= {}
$cache->{$key}
Truth table50%1/2
A
A= $cache->{$key}
Aresult
11
00
81 20 my $checksum = _file_2_md5($file);
82 20 $entry->{checksum} = $checksum;
83 20 $entry->{modules} = [@$mod_aref];
84 20 $cache_dirty = 1;
85 20 return 1;
86 }
87
88 54 ▶sub _file_2_md5{
Subroutine _file_2_md5: called CC=3 SCAR=11.2
89 54 my $file = shift;
90 54 ▶ open my $fh, '<', $file or die "can't open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "can't open ${file}: $!" 54 0 54
91 54 my $md5 = Digest::MD5->new;
92 54 $md5->addfile($fh);
93 54 ▶ close $fh or die "can't close $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "can't close ${file}: $!" 54 0 54
94 54 return $md5->hexdigest;
95 }
96 1 1;
97
« blib/lib/Module/ScanDeps.pm ↑ Summary blib/script/scandeps.pl »