Stepford/Graph.pm

filter: Statementstmt 96.0% 144 / 150 Branchbran 77.2% 17 / 22 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 3 / 3 Subroutinesub 96.0% 24 / 25 Podpod 0.0% 0 / 8 totaltotal 90.6% 194 / 214 CC 17 SCAR 10.6
CC
17
Cov
94%
step_is_up_to_date
6.0
_constructor_args_for_class
6.0
run_step
3.0
CRAP 2.9
↑ Summary Stepford/Runner.pm »
1 package Stepford::Graph;
2  
3 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 39 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 39 our $VERSION = '0.006001';
8  
9 39 ▶use List::AllUtils qw( all none );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 39 ▶use Stepford::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 39 ▶use Stepford::Types qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
12 ArrayRef
13 Bool
14 HashRef
15 Logger
16 Maybe
17 Num
18 Step
19 39 );
20 39 ▶use Try::Tiny qw( catch try );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 39 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 39 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 39 has config => (
26 is => 'ro',
27 isa => HashRef,
28 required => 1,
29 );
30  
31 39 has logger => (
32 is => 'ro',
33 isa => Logger,
34 required => 1,
35 );
36  
37 39 has step_class => (
38 is => 'ro',
39 isa => Step,
40 required => 1,
41 );
42  
43 39 has _step_object => (
44 is => 'ro',
45 isa => Step,
46 lazy => 1,
47 builder => '_build_step_object',
48 );
49  
50 has last_run_time => (
51 is => 'ro',
52 isa => Maybe [Num],
53 writer => 'set_last_run_time',
54 clearer => '_clear_last_run_time',
55 lazy => 1,
56 644 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
57 ## no critic (RequireInitializationForLocalVars)
58 644 local $_;
59 644 shift->_step_object->last_run_time;
60 },
61 39 );
62  
63 has step_productions_as_hashref => (
64 is => 'ro',
65 isa => HashRef,
66 writer => 'set_step_productions_as_hashref',
67 clearer => '_clear_step_productions_as_hashref',
68 lazy => 1,
69 382 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
70 ## no critic (RequireInitializationForLocalVars)
71 382 local $_;
72 382 shift->_step_object->productions_as_hashref;
73 },
74 39 );
75  
76 39 has _children_graphs => (
77 traits => ['Array'],
78 init_arg => 'children_graphs',
79 is => 'ro',
80 isa => ArrayRef ['Stepford::Graph'],
81 required => 1,
82 );
83  
84 39 has is_being_processed => (
85 is => 'ro',
86 isa => Bool,
87 default => 0,
88 writer => 'set_is_being_processed',
89 );
90  
91 39 has has_been_processed => (
92 is => 'ro',
93 isa => Bool,
94 default => 0,
95 writer => 'set_has_been_processed',
96 );
97  
98 619 ▶sub _build_step_object {
Subroutine _build_step_object: called CC=1 SCAR=0.0
99 619 my $self = shift;
100 619 my $args = $self->_constructor_args_for_class;
101  
102 619 $self->logger->debug( $self->step_class . '->new' );
103 619 return $self->step_class->new($args);
104 }
105  
106 619 ▶sub _constructor_args_for_class {
Subroutine _constructor_args_for_class: called CC=6 SCAR=18.0
107 619 my $self = shift;
108  
109 619 my $class = $self->step_class;
110 619 my $config = $self->config;
111  
112 619 my %args;
113 619 for my $init_arg (
114 2154 grep { defined }
115 2154 map { $_->init_arg } $class->meta->get_all_attributes
116 ) {
117  
118 $args{$init_arg} = $config->{$init_arg}
119 2154 ▶ if exists $config->{$init_arg};
Branch: 1 branch
BranchTrueFalseTotal
if exists $config->{$init_arg} 453 1701 2154
120 }
121  
122 619 my %productions = $self->_children_productions;
123  
124 619 for my $dep ( map { $_->name } $class->dependencies ) {
125 469 ▶ next if exists $args{$dep};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$dep} 0 469 469
126  
127 # XXX - I'm not sure this error is reachable. We already check that a
128 # class's declared dependencies can be satisfied while building the
129 # graph. That said, it doesn't hurt to leave this check in here, and it
130 # might help illuminate bugs in the Runner itself.
131 Stepford::Error->throw(
132 "Cannot construct a $class object. We are missing a required production: $dep"
133 469 ▶ ) unless exists $productions{$dep};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $productions{$dep} 0 469 469
134  
135 469 $args{$dep} = $productions{$dep};
136 }
137  
138 619 $args{logger} = $self->logger;
139  
140 619 return \%args;
141 }
142  
143 # Note: this is intentionally depth-first traversal as this is required for
144 # the correct sort order when running steps.
145 1257 ▶sub traverse {
Subroutine traverse: called CC=2 SCAR=6.9
Pod undocumented
146 1257 my $self = shift;
147 1257 my $cb = shift;
148  
149 1257 for my $graph ( @{ $self->_children_graphs } ) {
150 997 $graph->traverse($cb);
151 }
152 1215 $cb->($self);
153 1188 return;
154 }
155  
156 # This checks if the step/graph is in a state where we can run it, e.g.,
157 # it isn't being processed currently, the children have been processed, it
158 # hasn't already been processed. It does not do any checks on the internal
159 # state of the step (e.g., last run times). Rather, it is intended for
160 # completely internal flow control.
161 #
162 # This is called repeatedly in a multi-process build to figure out whether we
163 # are ready to consider running the step.
164 1215 ▶sub can_run_step {
Subroutine can_run_step: called CC=3 SCAR=11.0
Pod undocumented
165 1215 my $self = shift;
166  
167 # These checks are not logged as they are part of Stepford's internal
168 # flow control and might be run many times for a single step.
169 return
170 1215 ▶ !$self->is_being_processed
Condition100%6/6
!$self->is_being_processed && $self->children_have_been_processed && !$self->has_been_processed
!ll&&!rl&&r
307289619
not $self->is_being_processed and $self->children_have_been_processed
!ll&&!rl&&r
27280908
MC/DC100%3/3
!$self->is_being_processed && $self->children_have_been_processed && !$self->has_been_processed
not $self->is_being_processed $self->children_have_been_processed !$self->has_been_processed
Truth table100%4/4
not(A) and B && not(C)
A= not $self->is_being_processedB= $self->children_have_been_processedC= !$self->has_been_processed
ABCresult
0XX0
10X0
1100
1111
171 && $self->children_have_been_processed
172 && !$self->has_been_processed;
173 }
174  
175 # This checks whether we should run the step. It is meant to be run after we
176 # determine that the step is in a state where it _can_ run. This primarily
177 # looks at the internal state of the step, e.g., last run times.
178 #
179 # This is generally only called once, immediately before we run the step or
180 # decide never to run it.
181 #
182 # can and should are separated as they serve two different purposes in the
183 # Runner's flow control.
184 619 ▶sub step_is_up_to_date {
Subroutine step_is_up_to_date: called CC=6 SCAR=18.0
Pod undocumented
185 619 my $self = shift;
186 619 my $force_step_execution = shift;
187  
188 619 my $step_class = $self->step_class;
189  
190 619 ▶ if ($force_step_execution) {
Branch: 1 branch
BranchTrueFalseTotal
if ($force_step_execution) 4 615 619
191 4 $self->logger->info("Force execution is enabled for $step_class.");
192 4 return 0;
193 }
194  
195 615 ▶ unless ( defined $self->last_run_time ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->last_run_time) 341 274 615
196 341 $self->logger->debug("No last run time for $step_class.");
197 341 return 0;
198 }
199  
200 274 ▶ unless ( @{ $self->_children_graphs } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->_children_graphs;}) 135 139 274
201 135 $self->logger->debug("No previous steps for $step_class.");
202 135 return 1;
203 }
204  
205 139 ▶ if ( my @missing
Branch: 1 branch
BranchTrueFalseTotal
if (my(@missing) = grep({not defined $_->last_run_time;} @{$self->_children_graphs;})) 0 139 139
206 205 = grep { !defined $_->last_run_time } @{ $self->_children_graphs } ) {
207 $self->logger->debug(
208 "A previous step for $step_class does not have a last run time: "
209 0 . join ', ', map { $_->step_class } @missing );
210 0 return 0;
211 }
212  
213 139 my $step_last_run_time = $self->last_run_time;
214 205 my @newer_children = grep { $_->last_run_time > $step_last_run_time }
215 139 @{ $self->_children_graphs };
216 139 ▶ unless (@newer_children) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@newer_children) 102 37 139
217 102 $self->logger->info("$step_class is up to date.");
218 102 return 1;
219 }
220  
221 $self->logger->info(
222 "Last run time for $step_class is "
223 . $self->last_run_time
224 . '. The following children have newer last run times: '
225 . join ', ',
226 37 map { $_->step_class . ' (' . $_->last_run_time . ')' }
227 @newer_children,
228 );
229  
230 37 return 0;
231 }
232  
233 212 ▶sub run_step {
Subroutine run_step: called CC=3 SCAR=11.0
Pod undocumented
234 212 my $self = shift;
235  
236 212 ▶ die 'Tried running '
Branch: 1 branch
BranchTrueFalseTotal
unless $self->children_have_been_processed 0 212 212
237 . $self->step_class
238 . ' when not all children have been processed.'
239 unless $self->children_have_been_processed;
240  
241 212 ▶ die 'Tried running '
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_being_processed 0 212 212
242 . $self->step_class
243 . ' when it is currently being run'
244 if $self->is_being_processed;
245  
246 212 $self->set_is_being_processed(1);
247  
248 212 $self->logger->info( 'Running ' . $self->step_class );
249  
250 {
251 # We use many list functions that will modify the underlying array
252 # they are called on if $_ is modified. We localize it for safety.
253 ## no critic (RequireInitializationForLocalVars)
254 212 local $_;
255 212 $self->_step_object->run;
256 }
257 210 $self->set_is_being_processed(0);
258 210 $self->set_has_been_processed(1);
259 210 $self->_clear_last_run_time;
260 210 $self->_clear_step_productions_as_hashref;
261  
262 210 return;
263 }
264  
265 0 ▶sub productions {
Subroutine productions: not called CC=1 SCAR=6.9
Pod undocumented
266 0 my $self = shift;
267  
268 return (
269 $self->_children_productions,
270 0 %{ $self->step_productions_as_hashref },
271 );
272 }
273  
274 619 ▶sub _children_productions {
Subroutine _children_productions: called CC=1 SCAR=0.0
275 619 my $self = shift;
276  
277 return
278 476 map { %{ $_->step_productions_as_hashref } }
279 619 @{ $self->_children_graphs };
280 }
281  
282 1400 ▶sub children_have_been_processed {
Subroutine children_have_been_processed: called CC=1 SCAR=0.0
Pod undocumented
283 1400 my $self = shift;
284  
285 1400 ▶ all { $_->has_been_processed } @{ $self->_children_graphs };
Subroutine __ANON__: called CC=1 SCAR=0.0
286 }
287  
288 201 ▶sub is_serializable {
Subroutine is_serializable: called CC=1 SCAR=0.0
Pod undocumented
289 201 my $self = shift;
290  
291 # A step can be serialized as long as it and all of its children do not
292 # implement Stepford::Role::Step::Unserializable
293 352 ▶ none { $_->step_class->does('Stepford::Role::Step::Unserializable') }
Subroutine __ANON__: called CC=1 SCAR=0.0
294 201 ( $self, @{ $self->_children_graphs } );
295 }
296  
297 152 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod undocumented
298 152 my $self = shift;
299  
300 return join(
301 "\n",
302 (
303 152 map { sprintf( '[ %s ] --> [ %s ]', @$_ ) }
304 _parent_child_pairs($self)
305 ),
306 q{},
307 );
308 }
309  
310 152 ▶sub _parent_child_pairs {
Subroutine _parent_child_pairs: called CC=2 SCAR=6.9
311 152 my $parent = shift;
312  
313 152 my @pairs;
314 152 my @parents = ($parent);
315 152 my %seen;
316 152 ▶ while (@parents) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parents) 738 152 890
317 738 my $current_parent = shift @parents;
318 738 my @children = @{ $current_parent->_children_graphs };
319  
320 738 my @children_step_classes = map { $_->step_class } @children;
321 738 my $current_parent_step_class = $current_parent->step_class;
322  
323 push @pairs,
324 738 map { [ $current_parent_step_class, $_ ] } @children_step_classes;
325  
326 push @parents,
327 738 grep { !exists $seen{ $_->step_class } } @children;
328  
329 738 @seen{@children_step_classes} = ();
330 }
331  
332 152 return @pairs;
333 }
334  
335 39 __PACKAGE__->meta->make_immutable;
336  
337 39 1;
338  
339 # ABSTRACT: Contains the step dependency graph
340  
341 __END__
↑ Summary Stepford/Runner.pm »