blib/lib/App/Pipeline/Simple.pm

filter: Statementstmt 80.5% 232 / 288 Branchbran 57.4% 77 / 134 Conditioncond 40.3% 21 / 52 MC/DCmcdc 9.3% 3 / 32 Subroutinesub 96.8% 31 / 32 Podpod 100% 22 / 22 totaltotal 68.9% 386 / 560 CC 99 SCAR 34.1
CC
99
Cov
70%
run
107.5
stringify
38.9
render
21.2
CRAP 30.4
↑ Summary /usr/local/bin/perldoc »
1 #-----------------------------------------------------------------
2 # App::Pipeline::Simple
3 #
4 ## no critic
5 package App::Pipeline::Simple;
6 # ABSTRACT: Simple workflow manager
7 8 our $VERSION = '0.9.1'; # VERSION
8  
9 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 ## use critic
13  
14 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use YAML::Syck;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use Log::Log4perl qw(get_logger :levels :no_extra_logdie_message);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 #use Data::Printer;
20  
21 #-----------------------------------------------------------------
22 # Global variables
23 #-----------------------------------------------------------------
24  
25 8 my $logger_level = {
26 '-1' => $WARN,
27 '0' => $INFO,
28 '1' => $DEBUG,
29 };
30  
31 #-----------------------------------------------------------------
32 # new
33 #-----------------------------------------------------------------
34 5 ▶sub new {
Subroutine new: called CC=13 SCAR=26.5
Pod documented
35 5 my ($class, @args) = @_;
36  
37 # create an object
38 5 ▶ my $self = bless {}, ref ($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
39  
40 # set all @args into this object with 'set' values
41 5 ▶ my (%args) = (@args == 1 ? (value => $args[0]) : @args);
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 ? : 0 5 5
42  
43 # do dir() first so that we know where to write the log
44 5 ▶ $self->dir($args{'dir'}) if defined $args{'dir'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"dir"} 3 2 5
45  
46 # start logging
47 5 $self->_configure_logging;
48  
49 5 foreach my $key (keys %args) {
50 16 ▶ next if $key eq 'config'; # this needs to be evaluated last
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "config" 3 13 16
51 13 ▶ next if $key eq 'dir'; # done this
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "dir" 3 10 13
52 ## no critic
53 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
54 ## use critic
55 10 $self->$key($args{$key});
56 }
57 # delayed to first find out the verbosity level
58 5 $self->logger->info("Logging into file: [ ". $self->dir. '/pipeline.log'. " ]");
59  
60 # this argument needs to be done last
61 5 ▶ $self->config($args{'config'}) if defined $args{'config'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"config"} 3 2 5
62  
63 # look into dir() if config not given
64 $self->config($self->dir. '/config.yml')
65 5 ▶ if not $self->{config} and defined $self->dir and -e $self->dir. '/config.yml';
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"config"} and defined $self->dir and -e $self->dir . '/config.yml' 0 5 5
Condition66%4/6
not $self->{"config"} and defined $self->dir and -e $self->dir . '/config.yml'
!ll&&!rl&&r
320
not $self->{"config"} and defined $self->dir
!ll&&!rl&&r
302
MC/DC0%0/3
not $self->{"config"} and defined $self->dir and -e $self->dir . '/config.yml'
not $self->{"config"} defined $self->dir -e $self->dir . '/config.yml'
Truth table50%2/4
not(A) and B and C
A= not $self->{"config"}B= defined $self->dirC= -e $self->dir . '/config.yml'
ABCresult
0XX0
10X0
1100
1111
66  
67 # die if no config found
68 $self->logger->fatal("pipeline config file not provided or not found in pwd")
69 5 ▶ if not $self->{config} and not $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"config"} or $self->debug 0 5 5
Condition66%2/3
$self->{"config"} or $self->debug
l!l&&r!l&&!r
320
MC/DC0%0/2
$self->{"config"} or $self->debug
$self->{"config"} $self->debug
Truth table66%2/3
A or B
A= $self->{"config"}B= $self->debug
ABresult
1X1
011
000
70  
71 # done
72 5 return $self;
73 }
74  
75  
76 #-----------------------------------------------------------------
77 # Configure the logger
78 #-----------------------------------------------------------------
79  
80 5 ▶sub _configure_logging {
Subroutine _configure_logging: called CC=2 SCAR=6.9
81 5 my $self = shift;
82  
83 5 my $logger_config = q(
84 log4perl.category.Pipeline = INFO, Screen
85 log4perl.appender.Screen = Log::Log4perl::Appender::Screen
86 log4perl.appender.Screen.stderr = 1
87 log4perl.appender.Screen.layout = Log::Log4perl::Layout::SimpleLayout
88 );
89  
90 5 Log::Log4perl->init_once( \$logger_config );
91 5 my $logger = Log::Log4perl->get_logger("Pipeline");
92  
93 5 ▶ if ($self->dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dir) 3 2 5
94 3 my $to_file = Log::Log4perl::Appender->new
95 ("Log::Log4perl::Appender::File",
96 name => 'Log',
97 filename => $self->dir. '/pipeline.log',
98 mode => 'append');
99 3 my $pattern = '%d [%r] %p %L | %m%n';
100 3 my $layout = Log::Log4perl::Layout::PatternLayout->new ($pattern);
101 3 $to_file->layout ($layout);
102  
103 3 $logger->add_appender($to_file);
104 }
105  
106 5 $logger->level( $INFO );
107 5 $self->logger($logger);
108 }
109  
110  
111  
112 #-----------------------------------------------------------------
113 #
114 #-----------------------------------------------------------------
115  
116 6 ▶sub verbose {
Subroutine verbose: called CC=2 SCAR=6.9
Pod documented
117 6 my ($self, $value) = @_;
118 6 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 5 1 6
119 5 $self->{_verbose} = $value;
120  
121 # verbose = -1 0 1
122 # log level = WARN INFO DEBUG
123  
124 5 $self->logger->level( $logger_level->{$value} );
125 }
126 6 return $self->{_verbose};
127 }
128  
129 53 ▶sub id {
Subroutine id: called CC=2 SCAR=6.9
Pod documented
130 53 my ($self, $value) = @_;
131 53 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 15 38 53
132 15 $self->{_id} = $value;
133 }
134 53 return $self->{_id};
135 }
136  
137 4 ▶sub description {
Subroutine description: called CC=2 SCAR=7.0
Pod documented
138 4 my ($self, $value) = @_;
139 4 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 4 0 4
140 4 $self->{_description} = $value;
141 }
142 4 return $self->{_description};
143 }
144  
145 5 ▶sub name {
Subroutine name: called CC=2 SCAR=6.9
Pod documented
146 5 my ($self, $value) = @_;
147 5 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 4 1 5
148 4 $self->{_name} = $value;
149 }
150 5 return $self->{_name};
151 }
152  
153 2 ▶sub path {
Subroutine path: called CC=2 SCAR=6.9
Pod documented
154 2 my ($self, $value) = @_;
155 2 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 1 1 2
156 1 $self->{_path} = $value;
157 }
158 2 return $self->{_path};
159 }
160  
161 1 ▶sub next_id {
Subroutine next_id: called CC=2 SCAR=7.0
Pod documented
162 1 my ($self, $value) = @_;
163 1 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 1 0 1
164 1 $self->{_next_id} = $value;
165 }
166 1 return $self->{_next_id};
167 }
168  
169  
170 4 ▶sub input {
Subroutine input: called CC=2 SCAR=6.9
Pod documented
171 4 my ($self, $value) = @_;
172 4 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 1 3 4
173 1 $self->{_input} = $value;
174 }
175 4 return $self->{_input};
176 }
177  
178  
179 6 ▶sub itype {
Subroutine itype: called CC=2 SCAR=6.9
Pod documented
180 6 my ($self, $value) = @_;
181 6 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 1 5 6
182 1 $self->{_itype} = $value;
183 }
184 6 return $self->{_itype};
185 }
186  
187 2 ▶sub start {
Subroutine start: called CC=2 SCAR=7.6
Pod documented
188 2 my ($self, $value) = @_;
189 2 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 2 2
190 0 $self->{_start} = $value;
191 }
192 2 return $self->{_start};
193 }
194  
195  
196 0 ▶sub stop {
Subroutine stop: not called CC=2 SCAR=17.9
Pod documented
197 0 my ($self, $value) = @_;
198 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
199 0 $self->{_stop} = $value;
200 }
201 0 return $self->{_stop};
202 }
203  
204  
205 4 ▶sub debug {
Subroutine debug: called CC=2 SCAR=6.9
Pod documented
206 4 my ($self, $value) = @_;
207 4 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 2 2 4
208 2 $self->{_debug} = $value;
209 }
210 4 return $self->{_debug};
211 }
212  
213 59 ▶sub logger {
Subroutine logger: called CC=2 SCAR=6.9
Pod documented
214 59 my ($self, $value) = @_;
215 59 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 5 54 59
216 5 $self->{_logger} = $value;
217 }
218 59 return $self->{_logger};
219 }
220  
221 4 ▶sub config {
Subroutine config: called CC=18 SCAR=29.2
Pod documented
222 4 my ($self, $config) = @_;
223  
224 4 ▶ if ($config) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config) 3 1 4
225 3 $self->logger->info("Using config file: [ ". $config. " ]");
226 3 my $pwd = `pwd`; chomp $pwd;
227 3 $self->logger->debug("pwd: $pwd");
228 3 $self->logger->debug("config: $config");
229 3 ▶ die unless -e $config;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $config 0 3 3
230 # copy the pipeline config
231  
232 3 ▶ if ($self->dir and not -e $self->dir."/config.yml") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dir and not -e $self->dir . '/config.yml') 3 0 3
Condition33%1/3
$self->dir and not -e $self->dir . '/config.yml'
!ll&&!rl&&r
003
MC/DC0%0/2
$self->dir and not -e $self->dir . '/config.yml'
$self->dir not -e $self->dir . '/config.yml'
Truth table33%1/3
A and not(B)
A= $self->dirB= not -e $self->dir . '/config.yml'
ABresult
0X0
100
111
233 3 copy $config, $self->dir."/config.yml";
234 3 $self->logger->info("Config file [ $config ] copied to: [ ".
235 $self->dir."/config.yml ]");
236 }
237  
238 3 $self->{config} = LoadFile($self->dir."/config.yml");
239  
240 # set pipeline start parameters
241 3 $self->id('s0');
242 3 ▶ $self->name($self->{config}->{name} || '');
Condition50%1/2
$self->{'config'}{'name'} || ''
l!l
30
MC/DC0%0/1
$self->{'config'}{'name'} || ''
$self->{'config'}{'name'}
Truth table50%1/2
A
A= $self->{'config'}{'name'}
Aresult
11
00
243 3 ▶ $self->description($self->{config}->{description} || '');
Condition50%1/2
$self->{'config'}{'description'} || ''
l!l
30
MC/DC0%0/1
$self->{'config'}{'description'} || ''
$self->{'config'}{'description'}
Truth table50%1/2
A
A= $self->{'config'}{'description'}
Aresult
11
00
244  
245 # go through all steps once
246 3 my $nexts; # hashref for finding start point(s)
247 3 for my $id (sort keys %{$self->{config}->{steps}}) {
248 11 my $step = $self->{config}->{steps}->{$id};
249  
250 # bless all steps into Pipeline objects
251 11 bless $step, ref($self);
252  
253 #print "ERROR: $id already exists\n" if defined $self->step($id);
254 # create the list of all steps to be used by each_step()
255 11 $step->id($id);
256 11 push @{$self->{steps}}, $step;
257  
258 # a step without a parent is a starting point, store those with children
259 11 foreach my $next (@{$step->{next}}) {
260 8 $nexts->{$next}++;
261 }
262 }
263  
264 # store starting points, not listed as children
265 3 foreach my $step ($self->each_step) {
266 3 push @{$self->{next}}, $step->id
267 11 ▶ unless $nexts->{$step->id}
Branch: 1 branch
BranchTrueFalseTotal
unless $$nexts{$step->id} 3 8 11
268 }
269  
270 #run needs to fail if starting input values are not set!
271 # my $s = p $self->config;# here x
272 # $self->logger->info("Self-config after: [". $s. "]" );# here x
273  
274 # insert the startup value into the appropriate starting step
275 # unless we are reading old config
276 3 ▶ if ($self->itype and $self->input) { # only if new starting input value has been given
Branch: 1 branch
BranchTrueFalseTotal
if ($self->itype and $self->input) 1 2 3
Condition66%2/3
$self->itype and $self->input
!ll&&!rl&&r
201
MC/DC50%1/2
$self->itype and $self->input
$self->itype $self->input
Truth table66%2/3
A and B
A= $self->itypeB= $self->input
ABresult
0X0
100
111
277 1 $self->logger->info("Input value: [". $self->input. "]" );
278 1 $self->logger->info("Input type: [". $self->itype. "]" );
279 1 my $real_start_id;
280 1 for my $step ( $self->each_step) {
281  
282 # if input type is right, insert the value
283 # note only one of the each type can be used
284 1 foreach my $key ( keys %{$step->{args}} ) {
285 2 my $arg = $step->{args}->{$key};
286  
287 2 ▶ next unless $key eq 'in';
Branch: 1 branch
BranchTrueFalseTotal
unless $key eq "in" 1 1 2
288 1 ▶ next unless defined $arg->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg->{"type"} 0 1 1
289 1 ▶ next unless $arg->{type} eq $self->itype;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg->{"type"} eq $self->itype 0 1 1
290  
291 1 $arg->{value} = $self->input;
292 1 $real_start_id = $step->id;
293 }
294 }
295 1 $self->{next} = undef;
296 1 push @{$self->{next}}, $real_start_id;
297 1 $self->logger->info("Starting point: [". $real_start_id. "]" );
298  
299 # the stored config file needs to be overwritten with these modifications
300 1 open my $OUT, ">", $self->dir."/config.yml";
301 1 print $OUT Dump ($self->config);
302 }
303 }
304 4 return $self->{config};
305 }
306  
307 #-----------------------------------------------------------------
308 #
309 #-----------------------------------------------------------------
310 46 ▶sub dir {
Subroutine dir: called CC=7 SCAR=20.0
Pod documented
311 46 my ($self, $dir) = @_;
312 46 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) 4 42 46
313 4 ▶ mkdir $dir unless -e $dir and -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $dir and -d $dir 2 2 4
Condition66%2/3
-e $dir and -d $dir
!ll&&!rl&&r
202
MC/DC50%1/2
-e $dir and -d $dir
-e $dir -d $dir
Truth table66%2/3
A and B
A= -e $dirB= -d $dir
ABresult
0X0
100
111
314 4 ▶ croak "Can not create project directory $dir"
Branch: 1 branch
BranchTrueFalseTotal
unless -e $dir and -d $dir 0 4 4
Condition33%1/3
-e $dir and -d $dir
!ll&&!rl&&r
004
MC/DC0%0/2
-e $dir and -d $dir
-e $dir -d $dir
Truth table33%1/3
A and B
A= -e $dirB= -d $dir
ABresult
0X0
100
111
315 unless -e $dir and -d $dir;
316 4 $self->{_dir} = $dir;
317 }
318 46 ▶ $self->{_dir} || '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_dir"} or "" 38 8 46
319 }
320  
321 #-----------------------------------------------------------------
322 #
323 #-----------------------------------------------------------------
324 16 ▶sub step {
Subroutine step: called CC=1 SCAR=0.0
Pod documented
325 16 my ($self) = shift;
326 16 my $id = shift;
327 16 return $self->{config}->{steps}->{$id};
328 }
329  
330 30 ▶sub each_next {
Subroutine each_next: called CC=1 SCAR=0.0
Pod documented
331 #map { $_->{id} } grep { $_->{id} } @{shift->{next}};
332 30 @{shift->{next}};
333 }
334  
335 5 ▶sub each_step {
Subroutine each_step: called CC=1 SCAR=0.0
Pod documented
336 5 @{shift->{steps}};
337 }
338  
339  
340  
341 2 ▶sub run {
Subroutine run: called CC=23 SCAR=46.8
Pod documented
342 2 my ($self) = shift;
343 2 ▶ unless ($self->dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->dir) 0 2 2
344 0 $self->logger->fatal("Need an output directory to run()");
345 0 croak "Need an output directory to run()";
346 }
347  
348 ###
349 # check for input file and warn if not found
350  
351 2 chdir $self->{_dir};
352  
353 #
354 # Determine where in the pipeline to start
355 #
356  
357 2 my @steps; # array of next execution points
358  
359 # User has given a starting point id
360 2 ▶ if ($self->start) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->start) { } 0 2 2
elsif (-e $self->dir . '/pipeline.log') { } 2 0 2
361 0 $self->logger->info("Start point: user input [". $self->start. "]" );
362 0 push @steps, $self->start;
363 0 $self->logger->info("Starting at [". $self->start. "]" );
364 }
365 # determine if and where the execution of the pipeline was interrupted
366 elsif (-e $self->dir. "/pipeline.log") {
367 2 $self->logger->info("Start point: consult the log [".
368 $self->dir. "/pipeline.log ]");
369 2 ▶ open my $LOG, '<', $self->dir. "/pipeline.log"
Branch: 1 branch
BranchTrueFalseTotal
&App::Pipeline::Simple::open(my $LOG, "<", $self->dir . '/pipeline.log') or $self->logger->fatal(q[Can't open ] . $self->dir . "/pipeline.log for reading: $!") 2 0 2
370 or $self->logger->fatal("Can't open ". $self->dir.
371 "/pipeline.log for reading: $!");
372 2 my $in_execution;
373  
374 # look into only the latest run
375 my @log;
376 2 ▶ while (<$LOG>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $LOG)) 0 2 2
377 0 push @log, $_;
378 0 ▶ @log = () if /Run started/;
Branch: 1 branch
BranchTrueFalseTotal
if /Run started/ 0 0 0
379 }
380  
381 2 my $done;
382 2 for (@log) {
383 0 ▶ next unless /\[(\d+)\]/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\[(\d+)\]/ 0 0 0
384 0 undef $in_execution; # start of a new run
385 0 ▶ next unless /\| (Running|Finished) +\[(\w+)\]/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\| (Running|Finished) +\[(\w+)\]/ 0 0 0
386 0 ▶ $in_execution->{$2}++ if $1 eq 'Running';
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "Running" 0 0 0
387 0 ▶ delete $in_execution->{$2} if $1 eq 'Finished';
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "Finished" 0 0 0
388 0 ▶ $done = 1 if /DONE/;
Branch: 1 branch
BranchTrueFalseTotal
if /DONE/ 0 0 0
389 }
390  
391 2 @steps = sort keys %$in_execution;
392 2 ▶ if (scalar @steps == 0 and $done) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @steps == 0 and $done) { } 0 2 2
elsif (@steps) { } 0 2 2
Condition33%1/3
scalar @steps == 0 and $done
!ll&&!rl&&r
020
MC/DC0%0/2
scalar @steps == 0 and $done
scalar @steps == 0 $done
Truth table33%1/3
A and B
A= scalar @steps == 0B= $done
ABresult
0X0
100
111
393 0 $self->logger->warn("Pipeline is already finished. ".
394 "Drop -config and define the start step to rerun" );
395 0 exit 0;
396 }
397 elsif (@steps) {
398 0 $self->logger->info("Continuing at ". $steps[0] );
399 } else {
400 # start from beginning
401 2 @steps = $self->each_next;
402 2 $self->logger->info("Starting at [". $steps[0] . "]");
403 }
404 }
405 else {
406 # start from beginning
407 0 $self->logger->info("Start point: start from beginning" );
408 0 @steps = $self->each_next;
409 0 $self->logger->info("Starting at [". $steps[0] . "]");
410  
411 }
412  
413 #
414 # Execute one step at a time
415 #
416  
417 2 $self->logger->info("Run started");
418  
419 2 ▶ while (my $step_id = shift @steps) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $step_id = shift @steps) 6 2 8
420 6 $self->logger->debug("steps: [". join (", ", @steps). "]");
421 6 my $step = $self->step($step_id);
422 6 ▶ croak "ERROR: Step [$step_id] does not exist" unless $step;
Branch: 1 branch
BranchTrueFalseTotal
unless $step 0 6 6
423 # check that we got an object
424  
425 # check that the input file exists
426 6 foreach my $arg (@{$step->{arg}}) {
427 0 ▶ next unless $arg->{key} eq 'in';
Branch: 1 branch
BranchTrueFalseTotal
unless $arg->{"key"} eq "in" 0 0 0
428 0 ▶ next unless $arg->{type} =~ /file|dir/ ;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg->{"type"} =~ /file|dir/ 0 0 0
429 }
430  
431 6 my $command = $step->render;
432 6 $self->logger->info("Running [". $step->id . "] $command" );
433 6 `$command`;
434 6 $self->logger->info("Finished [". $step->id . "]" );
435  
436 # Add next step(s) to the execution queue unless
437 # the user has asked to stop here
438 6 ▶ if ( defined $self->{_stop} and $step->id eq $self->{_stop} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_stop'} and $step->id eq $self->{'_stop'}) { } 0 6 6
Condition33%1/3
defined $self->{"_stop"} and $step->id eq $self->{"_stop"}
!ll&&!rl&&r
600
MC/DC0%0/2
defined $self->{"_stop"} and $step->id eq $self->{"_stop"}
defined $self->{"_stop"} $step->id eq $self->{"_stop"}
Truth table33%1/3
A and B
A= defined $self->{"_stop"}B= $step->id eq $self->{"_stop"}
ABresult
0X0
100
111
439 0 $self->logger->info("Stopping at [". $step->id . "]" );
440 } else {
441 6 push @steps, $step->each_next;
442 }
443  
444 }
445 2 $self->logger->info("DONE" );
446 2 return 1;
447 }
448  
449  
450 #-----------------------------------------------------------------
451 # Render a step into a command line string
452 #-----------------------------------------------------------------
453  
454 16 ▶sub render {
Subroutine render: called CC=15 SCAR=30.5
Pod documented
455 16 my ($step, $display) = @_;
456  
457 16 my $str;
458 # path to program
459 16 ▶ if (defined $step->{path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $step->{"path"}) 0 16 16
460 0 $str .= $step->{path};
461 0 ▶ $str .= '/' unless substr($str, -1, 1) eq '/' ;
Branch: 1 branch
BranchTrueFalseTotal
unless substr($str, -1, 1) eq "/" 0 0 0
462 }
463 # program name
464 16 ▶ $str .= $step->{name} || '';
Condition50%1/2
$step->{'name'} || ''
l!l
160
MC/DC0%0/1
$step->{'name'} || ''
$step->{'name'}
Truth table50%1/2
A
A= $step->{'name'}
Aresult
11
00
465  
466 # arguments
467 16 my $endstr = '';
468  
469 16 foreach my $key (keys %{$step->{args}}) {
470 44 my $arg = $step->{args}->{$key};
471  
472 44 ▶ if (ref $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg) { } 32 12 44
473 32 ▶ if (defined $arg->{type} and $arg->{type} eq 'unnamed') {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $arg->{'type'} and $arg->{'type'} eq 'unnamed') { } 4 28 32
elsif (defined $arg->{'type'} and $arg->{'type'} eq 'redir') { } 28 0 28
elsif (defined $arg->{'value'}) { } 0 0 0
Condition66%2/3
defined $arg->{"type"} and $arg->{"type"} eq "unnamed"
!ll&&!rl&&r
0284
MC/DC50%1/2
defined $arg->{"type"} and $arg->{"type"} eq "unnamed"
defined $arg->{"type"} $arg->{"type"} eq "unnamed"
Truth table66%2/3
A and B
A= defined $arg->{"type"}B= $arg->{"type"} eq "unnamed"
ABresult
0X0
100
111
474 4 $str .= ' '. $arg->{value};
475 }
476 ▶ elsif (defined $arg->{type} and $arg->{type} eq 'redir') {
Condition33%1/3
defined $arg->{"type"} and $arg->{"type"} eq "redir"
!ll&&!rl&&r
0028
MC/DC0%0/2
defined $arg->{"type"} and $arg->{"type"} eq "redir"
defined $arg->{"type"} $arg->{"type"} eq "redir"
Truth table33%1/3
A and B
A= defined $arg->{"type"}B= $arg->{"type"} eq "redir"
ABresult
0X0
100
111
477 28 ▶ if ($key eq 'in') {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'in') { } 12 16 28
elsif ($key eq 'out') { } 16 0 16
478 12 $endstr .= " < ". $arg->{value};
479 }
480 elsif ($key eq 'out') {
481 16 $endstr .= " > ". $arg->{value};
482 } else {
483 0 croak "Unknown key ". $key;
484 }
485 }
486 elsif (defined $arg->{value}) {
487 0 $str .= " -". $key. " ". $arg->{value};
488 } else {
489 0 $str .= " -". $key;
490 }
491 } else {
492 12 ▶ if ($arg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) { } 3 9 12
493 3 $str .= " -". $key. " ". $arg;
494 } else {
495 9 $str .= " -". $key;
496 }
497 }
498 }
499 16 $str .= $endstr;
500  
501 16 ▶ $str =~ s/(['"])/\\$1/g if $display;
Branch: 1 branch
BranchTrueFalseTotal
if $display 10 6 16
502  
503 16 return $str;
504 }
505  
506 2 ▶sub stringify {
Subroutine stringify: called CC=12 SCAR=36.6
Pod documented
507 2 my ($self) = @_;
508  
509 2 $self->logger->info("Stringify starting" );
510  
511 2 my @res;
512 # add checks for duplicated ids
513  
514 # add check for a next pointer that leads nowhere
515  
516 2 my @steps = $self->each_next;
517  
518 2 my $outputs; # hashref for storing input and output filenames
519 2 ▶ while (my $step_id = shift @steps) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $step_id = shift @steps) 10 2 12
520 10 my $step = $self->step($step_id);
521  
522 10 push @res, $step->id, "\n";
523 10 push @res, "\t", $step->render('4display'), " # ";
524 10 map { push @res, "->", $_, " " } $step->each_next;
525  
526 10 push @steps, $step->each_next;
527  
528 10 foreach my $arg (@{$step->{arg}}) {
529 0 ▶ if ($arg->{key} eq 'out') {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg->{'key'} eq 'out') { } 0 0 0
elsif ($arg->{'key'} eq 'in' and $arg->{'type'} ne 'redir') { } 0 0 0
530 0 for ($step->each_next) {
531 push @res, "\n\t", "WARNING: Output file [".
532 $arg->{value}."] is read by [",
533 $outputs->{$arg->{value}}, "] and [$_]"
534 0 ▶ if $outputs->{$arg->{value}};
Branch: 1 branch
BranchTrueFalseTotal
if $$outputs{$arg->{"value"}} 0 0 0
535  
536 0 $outputs->{$arg->{value}} = $_;
537 }
538 }
539 ▶ elsif ($arg->{key} eq 'in' and $arg->{type} ne 'redir') {
Condition0%0/3
$arg->{"key"} eq "in" and $arg->{"type"} ne "redir"
!ll&&!rl&&r
000
MC/DC0%0/2
$arg->{"key"} eq "in" and $arg->{"type"} ne "redir"
$arg->{"key"} eq "in" $arg->{"type"} ne "redir"
Truth table0%0/3
A and B
A= $arg->{"key"} eq "in"B= $arg->{"type"} ne "redir"
ABresult
0X0
100
111
540 0 ▶ my $prev_step_id = $outputs->{$arg->{value}} || '';
Condition0%0/2
$$outputs{$arg->{'value'}} || ''
l!l
00
MC/DC0%0/1
$$outputs{$arg->{'value'}} || ''
$$outputs{$arg->{'value'}}
Truth table0%0/2
A
A= $$outputs{$arg->{'value'}}
Aresult
11
00
541 push @res, "\n\t". "ERROR: Output from the previous step is not [".
542 0 ▶ ($arg->{value} || ''). "]"
Branch: 1 branch
BranchTrueFalseTotal
if $prev_step_id ne $step->id and $prev_step_id eq $self->id 0 0 0
Condition0%0/5
$prev_step_id ne $step->id and $prev_step_id eq $self->id
!ll&&!rl&&r
000
$arg->{'value'} || ''
l!l
00
MC/DC0%0/2
$prev_step_id ne $step->id and $prev_step_id eq $self->id
$prev_step_id ne $step->id $prev_step_id eq $self->id
MC/DC0%0/1
$arg->{'value'} || ''
$arg->{'value'}
Truth table0%0/3
A and B
A= $prev_step_id ne $step->idB= $prev_step_id eq $self->id
ABresult
0X0
100
111
Truth table0%0/2
A
A= $arg->{'value'}
Aresult
11
00
543 if $prev_step_id ne $step->id and $prev_step_id eq $self->id;
544 }
545 # test for steps not referenced by other steps (missing next tag)
546 }
547 10 push @res, "\n";
548 }
549 2 return join '', @res;
550 }
551  
552  
553 1 ▶sub graphviz {
Subroutine graphviz: called CC=4 SCAR=26.2
Pod documented
554 1 my $self = shift;
555 1 my $function = shift;
556  
557 1 $self->logger->info("Graphing started. Redirect to a dot file" );
558  
559 1 require GraphViz;
560 0 my $g= GraphViz->new;
561  
562 0 my $end;
563 0 $g->add_node($self->id,
564 label => $self->id.
565 $self->render('4display'), rank => 'top');
566 0 map { $g->add_edge('s0' => $_) } $self->each_next;
567 0 ▶ if ($self->description) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->description) 0 0 0
568 0 $g->add_node('desc', label => $self->description,
569 shape => 'box', rank => 'top');
570 0 $g->add_edge('s0' => 'desc');
571 }
572  
573 0 foreach my $step ($self->each_step) {
574 0 $g->add_node($step->id, label => $step->id );
575 0 ▶ if ($step->each_next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step->each_next) { } 0 0 0
576 0 map { $g->add_edge($step->id => $_, label => " ". $step->render('display') ) }
577 $step->each_next;
578 } else {
579 0 $end++;
580 0 $g->add_node($end, label => ' ');
581 0 $g->add_edge($step->id => $end, label => " ". $step->render('display') );
582 }
583  
584 }
585 0 return $g->as_dot;
586  
587 0 $self->logger->info("Graphing done. Process the dot ".
588 "file (e.g. dot -Tpng p.dot|display " );
589  
590 }
591  
592 8 1;
593  
594 __END__
↑ Summary /usr/local/bin/perldoc »