File Coverage

blib/lib/Decision/Depends/Sig.pm
Criterion Covered Total %
statement 43 47 91.4
branch 12 18 66.6
condition 4 6 66.6
subroutine 9 11 81.8
pod 0 6 0.0
total 68 88 77.2


line stmt bran cond sub pod time code
1             # --8<--8<--8<--8<--
2             #
3             # Copyright (C) 2008 Smithsonian Astrophysical Observatory
4             #
5             # This file is part of Decision::Depends
6             #
7             # Decision-Depends is free software: you can redistribute it and/or modify
8             # it under the terms of the GNU General Public License as published by
9             # the Free Software Foundation, either version 3 of the License, or (at
10             # your option) any later version.
11             #
12             # This program is distributed in the hope that it will be useful,
13             # but WITHOUT ANY WARRANTY; without even the implied warranty of
14             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15             # GNU General Public License for more details.
16             #
17             # You should have received a copy of the GNU General Public License
18             # along with this program. If not, see .
19             #
20             # -->8-->8-->8-->8--
21              
22             package Decision::Depends::Sig;
23              
24             require 5.005_62;
25 11     11   62 use strict;
  11         23  
  11         425  
26 11     11   59 use warnings;
  11         23  
  11         346  
27              
28 11     11   60 use Carp;
  11         33  
  11         713  
29 11     11   65 use Digest::MD5;
  11         22  
  11         466  
30 11     11   63 use IO::File;
  11         19  
  11         9933  
31              
32             ## no critic ( ProhibitAccessOfPrivateData )
33              
34             our $VERSION = '0.20';
35              
36             our %attr = ( depend => 1,
37             depends => 1,
38             force => 1,
39             sig => 1 );
40              
41             sub new
42             {
43 8     8 0 25 my $class = shift;
44 8   33     416 $class = ref($class) || $class;
45              
46 8         19 my ( $state, $spec ) = @_;
47              
48 8         54 my $self = { %$spec, state => $state };
49              
50             # only accept string values
51 8 50       39 croak( __PACKAGE__,
52             "->new: bad type for Signature dependency `$self->{val}': must be scalar" )
53             unless '' eq ref $self->{val};
54              
55             # ensure that no bogus attributes are set
56 8         12 my @notok = grep { ! exists $attr{$_} } keys %{$self->{attr}};
  9         46  
  8         179  
57 8 50       33 croak( __PACKAGE__,
58             "->new: bad attributes for Signature dependency `$self->{val}': ",
59             join( ', ', @notok ) ) if @notok;
60              
61              
62 8         80 bless $self, $class;
63             }
64              
65             sub depends
66             {
67 7     7 0 18 my ( $self, $target, $time ) = @_;
68              
69 7         21 my $state = $self->{state};
70              
71 7 100       468 croak( __PACKAGE__,
72             "->depends: non-existant signature file `$self->{val}'" )
73             unless -f $self->{val};
74              
75 6         16 my @deps = ();
76              
77 6         46 my $prev_val = $state->getSig( $target, $self->{val} );
78              
79 6 100       33 if ( defined $prev_val )
80             {
81 4   100     23 my $is_not_equal =
82             ( exists $self->{attr}{force} ?
83             $self->{attr}{force} : $state->Force ) ||
84             cmpSig( $prev_val, mkSig( $self->{val} ) );
85              
86 4 100       29 if ( $is_not_equal )
87             {
88 3 50       22 print STDOUT " signature file `", $self->{val}, "' has changed\n"
89             if $state->Verbose;
90 3         11 push @deps, $self->{val};
91             }
92             else
93             {
94 1 50       6 print STDOUT " signature file `", $self->{val}, "' is unchanged\n"
95             if $state->Verbose;
96             }
97              
98             }
99             else
100             {
101 2 50       25 print STDOUT " No signature on file for `", $self->{val}, "'\n"
102             if $state->Verbose;
103 2         6 push @deps, $self->{val};
104             }
105              
106 6         47 sig => \@deps;
107              
108             }
109              
110             sub cmpSig
111             {
112 2     2 0 20 $_[0] ne $_[1];
113             }
114              
115             sub mkSig
116             {
117 7     7 0 61729 my ( $file ) = @_;
118              
119 7 50       622 my $fh = IO::File->new( $file, 'r' )
120             or croak( __PACKAGE__, "->mkSig: non-existant signature file `$file'" );
121              
122 7         2197 Digest::MD5->new->addfile($fh)->hexdigest;
123             }
124              
125             sub update
126             {
127 0     0 0   my ( $self, $target ) = @_;
128              
129 0           $self->{state}->setSig( $target, $self->{val}, mkSig( $self->{val} ) );
130             }
131              
132             sub pprint
133             {
134 0     0 0   my $self = shift;
135              
136 0           $self->{val};
137             }
138              
139             1;