File Coverage

blib/lib/Decision/Depends/Time.pm
Criterion Covered Total %
statement 38 41 92.6
branch 9 16 56.2
condition 4 9 44.4
subroutine 6 7 85.7
pod 0 4 0.0
total 57 77 74.0


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::Time;
23              
24             require 5.005_62;
25 11     11   59 use strict;
  11         21  
  11         422  
26 11     11   57 use warnings;
  11         23  
  11         273  
27              
28 11     11   257 use Carp;
  11         24  
  11         9355  
29              
30             ## no critic ( ProhibitAccessOfPrivateData )
31              
32             our $VERSION = '0.20';
33              
34             our %attr = ( depend => 1,
35             depends => 1,
36             force => 1,
37             time => 1,
38             orig => 1 );
39              
40             sub new
41             {
42 19     19 0 35 my $class = shift;
43 19   33     152 $class = ref($class) || $class;
44              
45 19         42 my ( $state, $spec ) = @_;
46              
47 19         197 my $self = { %$spec, state => $state };
48              
49              
50             # only accept string values
51 19 50       111 croak( __PACKAGE__,
52             "->new: bad type for Time dependency `$self->{val}': must be scalar" )
53             unless '' eq ref $self->{val};
54              
55             # ensure that no bogus attributes are set
56 19         46 my @notok = grep { ! exists $attr{$_} } keys %{$self->{attr}};
  20         87  
  19         134  
57 19 50       179 croak( __PACKAGE__,
58             "->new: bad attributes for Time dependency `$self->{val}': ",
59             join( ', ', @notok ) ) if @notok;
60              
61             # ensure that the dependency exists
62             # croak( __PACKAGE__, "->new: non-existant dependency: $self->{val}" )
63             # unless -f $self->{val};
64              
65 19         228 bless $self, $class;
66             }
67              
68             sub depends
69             {
70 9     9 0 34 my ( $self, $target, $time ) = @_;
71              
72 9         90 my $state = $self->{state};
73              
74 9         33 my $depfile = $self->{val};
75 9 50       107 my $depfiles =
76             exists $self->{attr}{orig} ?
77             [ $depfile ] : $state->getSLinks( $depfile );
78              
79 9         33 my $links = $depfile ne $depfiles->[0];
80              
81 9         36 my @deps = ();
82              
83             # loop through dependencies, check if any is younger than the target
84 9         55 for my $dep ( @$depfiles )
85             {
86 9         30 my $deptag = $dep;
87 9 100       43 $deptag .= " (slinked to `$depfile')" if $links;
88              
89 9         15 my @sb;
90 9   33     41 my $dtime =
91             $state->getTime( $dep ) ||
92             ((@sb = stat( $dep )) ? $sb[9] : undef);
93              
94 9 100       1676 croak( __PACKAGE__, "->cmp: non-existant dependency: $dep" )
95             unless defined $dtime;
96              
97 7         52 $state->setTime( $dep, $dtime );
98              
99 7   66     57 my $is_not_equal =
100             ( exists $self->{attr}{force} ?
101             $self->{attr}{force} : $state->Force )
102             || $dtime > $time;
103              
104             # if time of dependency is greater than of target, it's younger
105 7 50       50 if ( $is_not_equal )
106             {
107 7 50       33 print STDOUT " file `$deptag' is younger\n" if $state->Verbose;
108 7         43 push @deps, $dep;
109             }
110             else
111             {
112 0 0       0 print STDOUT " file `$deptag' is older\n" if $state->Verbose;
113             }
114             }
115              
116 7         74 time => \@deps;
117             }
118              
119             sub update
120 3     3 0 23 {
121             # do nothing; keep DepXXX class API clean
122             }
123              
124             sub pprint
125             {
126 0     0 0   my $self = shift;
127              
128 0           $self->{val};
129             }
130              
131             1;