| 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
| |||||||||||||||||||||||||||||
| 7 | 1 | my $has_Storable; | |||||||||||||||||||||||||||
| 8 | 1 | eval { require Storable }; | |||||||||||||||||||||||||||
| 9 | 1 | ▶ | $has_Storable = 1 unless $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 19 | 9 | ▶ | push @missing, 'Storable' unless $has_Storable; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 26 | 8 | eval{$cache = Storable::retrieve($c_file)}; | |||||||||||||||||||||||||||
| 27 | #warn $@ if ($@); | ||||||||||||||||||||||||||||
| 28 | 8 | ▶ | unless ($cache){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
shift() || $cache_file
shift()
$cache_file
Truth table33%1/3
A || B
A= shift()B= $cache_file
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$cache_dirty or $c_file ne $cache_file
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
| |||||||||||||||||||||||||||||
| 41 | || $c_file ne $cache_file); | ||||||||||||||||||||||||||||
| 42 | 8 | ▶ | Storable::nstore($cache, $c_file) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 68 | 34 | my $entry = $cache->{$key}; | |||||||||||||||||||||||||||
| 69 | 34 | my $checksum = _file_2_md5($file); | |||||||||||||||||||||||||||
| 70 | 34 | ▶ | if ($entry->{checksum} eq $checksum){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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} ||= {}
MC/DC0%0/1
$cache->{$key} ||= {}
$cache->{$key}
Truth table50%1/2
A
A= $cache->{$key}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 94 | 54 | return $md5->hexdigest; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 1 | 1; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||