Outthentic/Story.pm

filter: Statementstmt 9.4% 28 / 296 Branchbran 0.0% 0 / 134 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 14.8% 8 / 54 Podpod 0.0% 0 / 33 totaltotal 6.4% 36 / 562 CC 88 SCAR 52.3
CC
88
Cov
6%
_resolve_os
702.0
do_ruby_hook
210.0
do_python_hook
182.0
CRAP 186.1
« Outthentic.pm ↑ Summary run-test.pl »
1 package Outthentic::Story;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Outthentic::DSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Outthentic::Story::Stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::ShareDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use File::Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our @EXPORT = qw{
14  
15 new_story end_of_story set_story story_cache_dir
16  
17 get_prop set_prop
18  
19 debug_mod1 debug_mod2 debug_mod12
20  
21 set_stdout get_stdout stdout_file
22  
23 dsl captures capture stream match_lines
24  
25 run_story apply_story_vars story_var story_vars_pretty
26  
27 do_perl_hook
28  
29 do_ruby_hook
30  
31 do_python_hook
32  
33 do_bash_hook
34  
35 ignore_story_err
36  
37 quit
38  
39 outthentic_die
40  
41 project_root_dir
42  
43 test_root_dir
44  
45 cache_root_dir
46  
47 host
48  
49 dump_os
50  
51 };
52  
53 1 our @stories = ();
54 1 our $OS;
55  
56 sub new_story {
57
58  
59 0 ▶ my $self = {
Subroutine new_story: not called CC=1 SCAR=6.9
Pod undocumented
60 ID => scalar(@stories),
61 props => {
62 ignore_story_err => 0 ,
63 dsl => Outthentic::DSL->new() ,
64 story_vars => {} },
65 };
66  
67 0 push @stories, $self;
68  
69 0 1;
70  
71 }
72  
73 sub end_of_story {
74  
75 0 ▶ if (debug_mod12()){
Subroutine end_of_story: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (debug_mod12()) 0 0 0
76 0 main::note("end of story: ".(get_prop('story')));
77 }
78  
79 0 delete $stories[-1];
80  
81 }
82  
83 sub set_story {
84  
85 0 ▶ my $dist_lib_dir = File::ShareDir::dist_dir('Outthentic');
Subroutine set_story: not called CC=2 SCAR=17.9
Pod undocumented
86  
87 0 my $ruby_run_cmd;
88  
89 0 ▶ if (-f project_root_dir()."/Gemfile" ){
Branch: 1 branch
BranchTrueFalseTotal
if (-f project_root_dir() . '/Gemfile') { } 0 0 0
90 0 $ruby_run_cmd = "cd ".project_root_dir()." && bundle exec ruby -I $dist_lib_dir -r outthentic -I ".story_cache_dir()
91 } else {
92 0 $ruby_run_cmd = "ruby -I $dist_lib_dir -r outthentic -I ".story_cache_dir();
93 }
94  
95 0 my $python_run_cmd = "PYTHONPATH=\$PYTHONPATH:".(story_cache_dir()).":$dist_lib_dir python";
96  
97 0 get_prop('dsl')->{languages}->{ruby} = $ruby_run_cmd;
98  
99 0 get_prop('dsl')->{languages}->{python} = $python_run_cmd;
100  
101 0 get_prop('dsl')->{cache_dir} = story_cache_dir();
102  
103 0 my $bash_run_opts = "source "._bash_glue_file()." && source $dist_lib_dir/outthentic.bash";
104  
105 0 get_prop('dsl')->{languages}->{ruby} = $ruby_run_cmd;
106  
107 0 get_prop('dsl')->{languages}->{bash} = $bash_run_opts;
108  
109 0 _make_cache_dir();
110  
111 0 _mk_perl_glue_file();
112  
113 0 _mk_ruby_glue_file();
114  
115 0 _mk_python_glue_file();
116  
117 0 _mk_bash_glue_file();
118  
119 }
120  
121 sub _story {
122 0 ▶ @stories[-1];
Subroutine _story: not called CC=1 SCAR=6.9
123 }
124  
125 sub _story_id {
126 0 ▶ _story()->{ID};
Subroutine _story_id: not called CC=1 SCAR=6.9
127 }
128  
129 sub get_prop {
130  
131 0 ▶ my $name = shift;
Subroutine get_prop: not called CC=1 SCAR=6.9
Pod undocumented
132  
133 0 _story()->{props}->{$name};
134
135 }
136  
137 sub set_prop {
138  
139 0 ▶ my $name = shift;
Subroutine set_prop: not called CC=1 SCAR=6.9
Pod undocumented
140 0 my $value = shift;
141 0 _story()->{props}->{$name} = $value;
142
143 }
144  
145  
146 sub project_root_dir {
147 0 ▶ get_prop('project_root_dir');
Subroutine project_root_dir: not called CC=1 SCAR=6.9
Pod undocumented
148 }
149  
150  
151 sub test_root_dir { # this one is deprected and exists for back compatibilty, use cache_root_dir instead
152 0 ▶ get_prop('cache_root_dir');
Subroutine test_root_dir: not called CC=1 SCAR=6.9
Pod undocumented
153 }
154  
155 sub cache_root_dir {
156 0 ▶ get_prop('cache_root_dir');
Subroutine cache_root_dir: not called CC=1 SCAR=6.9
Pod undocumented
157 }
158  
159 sub host {
160 0 ▶ get_prop('host');
Subroutine host: not called CC=1 SCAR=6.9
Pod undocumented
161 }
162  
163 sub ignore_story_err {
164  
165 0 ▶ my $val = shift;
Subroutine ignore_story_err: not called CC=2 SCAR=17.9
Pod undocumented
166 0 my $rv;
167  
168 0 ▶ if (defined $val){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 0 0 0
169 0 set_prop('ignore_story_err',$val);
170 } else {
171 0 $rv = get_prop('ignore_story_err');
172 }
173 0 $rv;
174 }
175  
176  
177 sub debug_mod1 {
178  
179 0 ▶ get_prop('debug') == 1
Subroutine debug_mod1: not called CC=1 SCAR=6.9
Pod undocumented
180 }
181  
182 sub debug_mod2 {
183  
184 0 ▶ get_prop('debug') == 2
Subroutine debug_mod2: not called CC=1 SCAR=6.9
Pod undocumented
185 }
186  
187 sub debug_mod12 {
188  
189 0 ▶ debug_mod1() or debug_mod2()
Subroutine debug_mod12: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
debug_mod1() or debug_mod2() 0 0 0
190 }
191  
192  
193 sub set_stdout {
194  
195 0 ▶ my $line = shift;
Subroutine set_stdout: not called CC=2 SCAR=17.9
Pod undocumented
196 0 ▶ open FSTDOUT, ">>", stdout_file() or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open FSTDOUT, ">>", stdout_file() or die $! 0 0 0
197 0 print FSTDOUT $line, "\n";
198 0 close FSTDOUT;
199  
200 }
201  
202 sub get_stdout {
203  
204 0 ▶ return unless -f stdout_file();
Subroutine get_stdout: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless -f stdout_file() 0 0 0
205  
206 0 my $data;
207  
208 0 ▶ open FSTDOUT, stdout_file() or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open FSTDOUT, stdout_file() or die $! 0 0 0
209 0 my $data = join "", <FSTDOUT>;
210 0 close FSTDOUT;
211 0 $data;
212 }
213  
214 sub stdout_file {
215  
216 0 ▶ story_cache_dir()."/std.out"
Subroutine stdout_file: not called CC=1 SCAR=6.9
Pod undocumented
217  
218 }
219  
220 sub _make_cache_dir {
221  
222 0 ▶ my $cache_dir = cache_root_dir()."/story-"._story_id();
Subroutine _make_cache_dir: not called CC=4 SCAR=30.0
223  
224 0 ▶ if (debug_mod12()){
Branch: 1 branch
BranchTrueFalseTotal
if (debug_mod12()) 0 0 0
225 0 main::note("make cache dir: $cache_dir");
226 }
227 0 ▶ File::Path::Tiny::mk($cache_dir) or die "can't create $cache_dir, error: $!";
Branch: 1 branch
BranchTrueFalseTotal
File::Path::Tiny::mk($cache_dir) or die "can't create $cache_dir, error: $!" 0 0 0
228 0 ▶ File::Path::Tiny::empty_dir($cache_dir) or die "can't empty $cache_dir, error: $!";
Branch: 1 branch
BranchTrueFalseTotal
File::Path::Tiny::empty_dir($cache_dir) or die "can't empty $cache_dir, error: $!" 0 0 0
229 }
230  
231 sub story_cache_dir {
232 0 ▶ cache_root_dir()."/story-"._story_id();
Subroutine story_cache_dir: not called CC=1 SCAR=6.9
Pod undocumented
233 }
234  
235 sub _perl_glue_file {
236 0 ▶ story_cache_dir()."/glue.pm";
Subroutine _perl_glue_file: not called CC=1 SCAR=6.9
237 }
238  
239 sub _ruby_glue_file {
240 0 ▶ story_cache_dir()."/glue.rb";
Subroutine _ruby_glue_file: not called CC=1 SCAR=6.9
241 }
242  
243 sub _python_glue_file {
244 0 ▶ story_cache_dir()."/glue.py";
Subroutine _python_glue_file: not called CC=1 SCAR=6.9
245 }
246  
247 sub _bash_glue_file {
248 0 ▶ story_cache_dir()."/glue.bash";
Subroutine _bash_glue_file: not called CC=1 SCAR=6.9
249 }
250  
251 sub dsl {
252 0 ▶ get_prop('dsl')
Subroutine dsl: not called CC=1 SCAR=6.9
Pod undocumented
253 }
254  
255 sub stream {
256 0 ▶ dsl()->stream
Subroutine stream: not called CC=1 SCAR=6.9
Pod undocumented
257 }
258  
259 sub captures {
260  
261 dsl()->{captures}
262 0 ▶}
Subroutine captures: not called CC=1 SCAR=6.9
Pod undocumented
263  
264 sub capture {
265 0 ▶ dsl()->{captures}->[0]
Subroutine capture: not called CC=1 SCAR=6.9
Pod undocumented
266 }
267  
268 sub match_lines {
269  
270 dsl()->{match_lines}
271 0 ▶}
Subroutine match_lines: not called CC=1 SCAR=6.9
Pod undocumented
272  
273 sub run_story {
274  
275 0 ▶ my $path = shift;
Subroutine run_story: not called CC=7 SCAR=40.3
Pod undocumented
276  
277 0 ▶ my $story_vars = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
278  
279 0 Outthentic::Story::Stat->new_story({
280 vars => $story_vars,
281 path => $path
282 });
283  
284 0 my $cache_root_dir = get_prop('cache_root_dir');
285  
286 0 my $project_root_dir = get_prop('project_root_dir');
287  
288 0 my $story_module = "$cache_root_dir/modules/$path/story.outth";
289  
290 0 ▶ die "story module file $story_module does not exist" unless -e $story_module;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $story_module 0 0 0
291  
292 0 ▶ if (debug_mod12()){
Branch: 1 branch
BranchTrueFalseTotal
if (debug_mod12()) 0 0 0
293 0 main::note("run downstream story: $path");
294 0 for my $k (keys %{$story_vars}){
295 0 my $v = $story_vars->{$k};
296 0 main::note("downstream story var: $k => $v");
297 }
298 }
299  
300 {
301 0 package main;
302 0 ▶ unless (do $story_module) {
Branch: 1 branch
BranchTrueFalseTotal
unless (do $story_module) 0 0 0
303 0 ▶ die "couldn't parse story module file $story_module: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
304 }
305 }
306  
307 # return statistic for downstream story just executed
308 0 return Outthentic::Story::Stat->current;
309 }
310  
311 sub do_perl_hook {
312  
313 0 ▶ my $hook_file = shift;
Subroutine do_perl_hook: not called CC=3 SCAR=24.8
Pod undocumented
314  
315 {
316 0 package main;
317 0 ▶ unless (do $hook_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (do $hook_file) 0 0 0
318 0 ▶ die "couldn't parse perl hook file $hook_file: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
319 }
320 }
321  
322 0 return 1;
323 }
324  
325 sub quit {
326 0 ▶ my $msg = shift;
Subroutine quit: not called CC=1 SCAR=6.9
Pod undocumented
327 0 chomp($msg);
328 0 main::print_story_header();
329 0 main::note("? forcefully exit: $msg");
330 0 exit(0);
331 }
332  
333 sub outthentic_die {
334 0 ▶ my $msg = shift;
Subroutine outthentic_die: not called CC=1 SCAR=6.9
Pod undocumented
335 0 chomp($msg);
336 0 main::print_story_header();
337 0 main::note("!! forcefully die: $msg");
338 0 $main::STATUS = 0;
339 0 exit(1);
340 }
341  
342 sub _mk_perl_glue_file {
343  
344 0 ▶ open PERL_GLUE, ">", _perl_glue_file() or confess "can't create perl glue file ".(_perl_glue_file())." : $!";
Subroutine _mk_perl_glue_file: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
open PERL_GLUE, ">", _perl_glue_file() or confess(q[can't create perl glue file ] . _perl_glue_file() . " : $!") 0 0 0
345  
346 0 my $cache_root_dir = cache_root_dir();
347 0 my $story_dir = get_prop('story_dir');
348 0 my $project_root_dir = project_root_dir();
349 0 my $debug_mod12 = debug_mod12();
350 0 my $cache_dir = story_cache_dir;
351  
352 0 my $os = _resolve_os();
353  
354 0 print PERL_GLUE <<"CODE";
355  
356 package glue;
357 1;
358  
359 package main;
360 use strict;
361
362 sub debug_mod12 {
363 $debug_mod12
364 }
365  
366 sub cach_root_dir {
367 '$cache_root_dir'
368 }
369  
370 sub test_root_dir {
371 '$cache_root_dir'
372 }
373  
374 sub project_root_dir {
375 '$project_root_dir'
376 }
377  
378 sub cache_dir {
379 '$cache_dir'
380 }
381  
382 sub story_dir {
383 '$story_dir'
384 }
385  
386 sub os { '$os' }
387  
388  
389 1;
390  
391 CODE
392  
393 0 close PERL_GLUE;
394  
395 }
396  
397 sub _mk_ruby_glue_file {
398  
399 0 ▶ open RUBY_GLUE, ">", _ruby_glue_file() or die $!;
Subroutine _mk_ruby_glue_file: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
open RUBY_GLUE, ">", _ruby_glue_file() or die $! 0 0 0
400  
401 0 my $stdout_file = stdout_file();
402 0 my $cache_root_dir = cache_root_dir();
403 0 my $story_dir = get_prop('story_dir');
404 0 my $project_root_dir = project_root_dir();
405 0 my $debug_mod12 = debug_mod12();
406  
407 0 my $cache_dir = story_cache_dir;
408  
409 0 print RUBY_GLUE <<"CODE";
410  
411 def debug_mod12
412 '$debug_mod12'
413 end
414  
415 def cache_root_dir
416 '$cache_root_dir'
417 end
418  
419 def test_root_dir
420 '$cache_root_dir'
421 end
422  
423 def project_root_dir
424 '$project_root_dir'
425 end
426  
427 def cache_dir
428 '$cache_dir'
429 end
430  
431 def story_dir
432 '$story_dir'
433 end
434  
435 def stdout_file
436 '$stdout_file'
437 end
438  
439 CODE
440  
441 0 close RUBY_GLUE;
442  
443 }
444  
445 sub _mk_python_glue_file {
446  
447 0 ▶ open PYTHON_GLUE, ">", _python_glue_file() or die $!;
Subroutine _mk_python_glue_file: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
open PYTHON_GLUE, ">", _python_glue_file() or die $! 0 0 0
448  
449 0 my $stdout_file = stdout_file();
450 0 my $cache_root_dir = cache_root_dir();
451 0 my $story_dir = get_prop('story_dir');
452 0 my $project_root_dir = project_root_dir();
453 0 my $debug_mod12 = debug_mod12();
454  
455 0 my $cache_dir = story_cache_dir;
456  
457 0 print PYTHON_GLUE <<"CODE";
458  
459 def debug_mod12():
460 return $debug_mod12
461  
462 def cache_root_dir():
463 return '$cache_root_dir'
464  
465 def test_root_dir():
466 return '$cache_root_dir'
467  
468 def project_root_dir():
469 return '$project_root_dir'
470  
471 def cache_dir():
472 return '$cache_dir'
473  
474 def story_dir():
475 return '$story_dir'
476  
477 def stdout_file():
478 return '$stdout_file'
479  
480 CODE
481  
482 0 close PYTHON_GLUE;
483  
484 }
485  
486 sub _mk_bash_glue_file {
487  
488  
489 0 ▶ my $story_dir = get_prop('story_dir');
Subroutine _mk_bash_glue_file: not called CC=2 SCAR=17.9
490  
491 0 ▶ open BASH_GLUE, ">", _bash_glue_file() or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open BASH_GLUE, ">", _bash_glue_file() or die $! 0 0 0
492  
493 0 my $stdout_file = stdout_file();
494 0 my $cache_root_dir = cache_root_dir();
495 0 my $project_root_dir = project_root_dir();
496 0 my $debug_mod12 = debug_mod12();
497  
498 0 my $cache_dir = story_cache_dir;
499  
500 0 my $os = _resolve_os();
501  
502 0 print BASH_GLUE <<"CODE";
503  
504 debug_mod=debug_mod12
505  
506 test_root_dir=$cache_root_dir
507  
508 cache_root_dir=$cache_root_dir
509  
510 project_root_dir=$project_root_dir
511  
512 cache_dir=$cache_dir
513  
514 story_dir=$story_dir
515  
516 stdout_file=$stdout_file
517  
518 os=$os
519  
520 CODE
521  
522 0 close BASH_GLUE;
523  
524 }
525  
526 sub do_ruby_hook {
527  
528 0 ▶ my $file = shift;
Subroutine do_ruby_hook: not called CC=14 SCAR=53.5
Pod undocumented
529  
530 0 my $ruby_lib_dir = File::ShareDir::dist_dir('Outthentic');
531  
532 0 my $cmd;
533  
534 0 ▶ if (-f project_root_dir()."/Gemfile" ){
Branch: 1 branch
BranchTrueFalseTotal
if (-f project_root_dir() . '/Gemfile') { } 0 0 0
535 0 $cmd = "cd ".project_root_dir()." && bundle exec ruby -I $ruby_lib_dir -r outthentic -I ".story_cache_dir()." $file"
536 } else {
537 0 $cmd = "ruby -I $ruby_lib_dir -r outthentic -I ".story_cache_dir()." $file"
538 }
539  
540 0 ▶ if (debug_mod12()){
Branch: 1 branch
BranchTrueFalseTotal
if (debug_mod12()) 0 0 0
541 0 main::note("do_ruby_hook: $cmd");
542 }
543  
544  
545 0 my $rand = int(rand(1000));
546  
547 0 my $st = system("$cmd 2>".story_cache_dir()."/$rand.err 1>".story_cache_dir()."/$rand.out");
548  
549 0 ▶ if($st != 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($st != 0) 0 0 0
550 0 die "do_ruby_hook failed. \n see ".story_cache_dir()."/$rand.err for details";
551 }
552  
553 0 my $out_file = story_cache_dir()."/$rand.out";
554  
555 0 ▶ open RUBY_HOOK_OUT, $out_file or die "can't open RUBY_HOOK_OUT file $out_file to read!";
Branch: 1 branch
BranchTrueFalseTotal
open RUBY_HOOK_OUT, $out_file or die "can't open RUBY_HOOK_OUT file $out_file to read!" 0 0 0
556  
557 0 my @out = <RUBY_HOOK_OUT>;
558  
559 0 close RUBY_HOOK_OUT;
560  
561 0 my $story_vars_json;
562  
563 0 for my $l (@out) {
564  
565 0 ▶ next if $l=~/#/;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /#/ 0 0 0
566  
567 0 ▶ quit($1) if $l=~/quit:(.*)/;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /quit:(.*)/ 0 0 0
568 0 ▶ outthentic_die($1) if $l=~/outthentic_die:(.*)/;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /outthentic_die:(.*)/ 0 0 0
569  
570 0 ▶ ignore_story_err($1) if $l=~/ignore_story_err:\s+(\d)/;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /ignore_story_err:\s+(\d)/ 0 0 0
571
572 0 ▶ if ($l=~s/story_var_json_begin.*// .. $l=~s/story_var_json_end.*//){
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ s/story_var_json_begin.*// .. $l =~ s/story_var_json_end.*//) 0 0 0
573 0 $story_vars_json.=$l;
574 0 next;
575 }
576  
577  
578 0 ▶ if ($l=~/story:\s+(\S+)/){
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /story:\s+(\S+)/) 0 0 0
579  
580 0 my $path = $1;
581  
582 0 ▶ if (debug_mod12()){
Branch: 1 branch
BranchTrueFalseTotal
if (debug_mod12()) 0 0 0
583 0 main::note("run downstream story from ruby hook");
584 }
585  
586 0 ▶ run_story($path, decode_json($story_vars_json||{}));
Condition0%0/2
$story_vars_json || {}
l!l
00
MC/DC0%0/1
$story_vars_json || {}
$story_vars_json
Truth table0%0/2
A
A= $story_vars_json
Aresult
11
00
587 0 $story_vars_json = undef;
588  
589 }
590 }
591  
592 0 return 1;
593 }
594  
595 sub do_python_hook {
596  
597 0 ▶ my $file = shift;
Subroutine do_python_hook: not called CC=13 SCAR=52.0
Pod undocumented
598  
599 0 my $python_lib_dir = File::ShareDir::dist_dir('Outthentic');
600  
601 0 my $cmd = "PYTHONPATH=\$PYTHONPATH:".(story_cache_dir()).":$python_lib_dir python $file";
602
603 0 ▶ if (debug_mod12()){
Branch: 1 branch
BranchTrueFalseTotal
if (debug_mod12()) 0 0 0
604 0 main::note("do_python_hook: $cmd");
605 }
606  
607  
608 0 my $rand = int(rand(1000));
609  
610 0 my $st = system("$cmd 2>".story_cache_dir()."/$rand.err 1>".story_cache_dir()."/$rand.out");
611  
612 0 ▶ if($st != 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($st != 0) 0 0 0
613 0 die "do_python_hook failed. \n see ".story_cache_dir()."/$rand.err for details";
614 }
615  
616 0 my $out_file = story_cache_dir()."/$rand.out";
617  
618 0 ▶ open PYTHON_HOOK_OUT, $out_file or die "can't open PYTHON_HOOK_OUT file $out_file to read!";
Branch: 1 branch
BranchTrueFalseTotal
open PYTHON_HOOK_OUT, $out_file or die "can't open PYTHON_HOOK_OUT file $out_file to read!" 0 0 0
619  
620 0 my @out = <PYTHON_HOOK_OUT>;
621  
622 0 close PYTHON_HOOK_OUT;
623  
624 0 my $story_vars_json;
625  
626 0 for my $l (@out) {
627  
628 0 ▶ next if $l=~/#/;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /#/ 0 0 0
629  
630 0 ▶ quit($1) if $l=~/quit:(.*)/;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /quit:(.*)/ 0 0 0
631 0 ▶ outthentic_die($1) if $l=~/outthentic_die:(.*)/;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /outthentic_die:(.*)/ 0 0 0
632  
633 0 ▶ ignore_story_err($1) if $l=~/ignore_story_err:\s+(\d)/;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /ignore_story_err:\s+(\d)/ 0 0 0
634
635 0 ▶ if ($l=~s/story_var_json_begin.*// .. $l=~s/story_var_json_end.*//){
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ s/story_var_json_begin.*// .. $l =~ s/story_var_json_end.*//) 0 0 0
636 0 $story_vars_json.=$l;
637 0 next;
638 }
639  
640  
641 0 ▶ if ($l=~/story:\s+(\S+)/){
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /story:\s+(\S+)/) 0 0 0
642  
643 0 my $path = $1;
644  
645 0 ▶ if (debug_mod12()){
Branch: 1 branch
BranchTrueFalseTotal
if (debug_mod12()) 0 0 0
646 0 main::note("run downstream story from python hook");
647 }
648  
649 0 ▶ run_story($path, decode_json($story_vars_json||{}));
Condition0%0/2
$story_vars_json || {}
l!l
00
MC/DC0%0/1
$story_vars_json || {}
$story_vars_json
Truth table0%0/2
A
A= $story_vars_json
Aresult
11
00
650  
651 0 $story_vars_json = undef;
652  
653 }
654 }
655  
656 0 return 1;
657 }
658  
659 sub do_bash_hook {
660  
661 0 ▶ my $file = shift;
Subroutine do_bash_hook: not called CC=12 SCAR=50.5
Pod undocumented
662  
663 0 my $bash_lib_dir = File::ShareDir::dist_dir('Outthentic');
664  
665 0 my $cmd = "source "._bash_glue_file()." && source $bash_lib_dir/outthentic.bash";
666  
667 0 $cmd.=" && source $file";
668  
669 0 $cmd="bash -c '$cmd'";
670  
671 0 ▶ if (debug_mod12()){
Branch: 1 branch
BranchTrueFalseTotal
if (debug_mod12()) 0 0 0
672 0 main::note("do_bash_hook: $cmd");
673 }
674  
675  
676 0 my $rand = int(rand(1000));
677  
678 0 my $st = system("$cmd 2>".story_cache_dir()."/$rand.err 1>".story_cache_dir()."/$rand.out");
679  
680 0 ▶ if($st != 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($st != 0) 0 0 0
681 0 die "do_bash_hook failed. \n see ".story_cache_dir()."/$rand.err for details";
682 }
683  
684 0 my $out_file = story_cache_dir()."/$rand.out";
685  
686 0 ▶ open HOOK_OUT, $out_file or die "can't open HOOK_OUT file $out_file to read!";
Branch: 1 branch
BranchTrueFalseTotal
open HOOK_OUT, $out_file or die "can't open HOOK_OUT file $out_file to read!" 0 0 0
687  
688 0 my @out = <HOOK_OUT>;
689  
690 0 close HOOK_OUT;
691  
692 0 my %story_vars_bash = ();
693  
694 0 for my $l (@out) {
695  
696 0 ▶ next if $l=~/#/;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /#/ 0 0 0
697
698 0 ▶ quit($1) if $l=~/quit:(.*)/;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /quit:(.*)/ 0 0 0
699 0 ▶ outthentic_die($1) if $l=~/outthentic_die:(.*)/;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /outthentic_die:(.*)/ 0 0 0
700  
701 0 ▶ ignore_story_err($1) if $l=~/ignore_story_err:\s+(\d)/;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /ignore_story_err:\s+(\d)/ 0 0 0
702
703 0 ▶ if ($l=~/story_var_bash:\s+(\S+)\s+(.*)/){
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /story_var_bash:\s+(\S+)\s+(.*)/) 0 0 0
704 0 $story_vars_bash{$1}=$2;
705 #warn %story_vars_bash;
706 0 next;
707 }
708  
709 0 ▶ if ($l=~/story:\s+(\S+)/){
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /story:\s+(\S+)/) 0 0 0
710 0 my $path = $1;
711 0 ▶ if (debug_mod12()){
Branch: 1 branch
BranchTrueFalseTotal
if (debug_mod12()) 0 0 0
712 0 main::note("run downstream story from bash hook");
713 }
714 0 run_story($path, {%story_vars_bash});
715 0 %story_vars_bash = ();
716 }
717 }
718  
719 0 return 1;
720  
721 }
722  
723  
724 sub apply_story_vars {
725  
726 0 ▶ my $story_vars = Outthentic::Story::Stat->current->{vars};
Subroutine apply_story_vars: not called CC=4 SCAR=30.0
Pod undocumented
727  
728 0 set_prop( story_vars => $story_vars );
729  
730 0 ▶ open STORY_VARS, ">", (story_cache_dir())."/variables.json"
Branch: 1 branch
BranchTrueFalseTotal
open STORY_VARS, ">", story_cache_dir() . '/variables.json' or die q[can't open ] . story_cache_dir() . "/variables.json write: $!" 0 0 0
731 or die "can't open ".(story_cache_dir())."/variables.json write: $!";
732  
733 0 print STORY_VARS encode_json($story_vars);
734  
735 0 close STORY_VARS;
736  
737 0 ▶ open STORY_VARS, ">", (story_cache_dir())."/variables.bash"
Branch: 1 branch
BranchTrueFalseTotal
open STORY_VARS, ">", story_cache_dir() . '/variables.bash' or die q[can't open ] . story_cache_dir() . "/variables.bash write: $!" 0 0 0
738 or die "can't open ".(story_cache_dir())."/variables.bash write: $!";
739  
740 0 for my $name (keys %{$story_vars} ){
741 0 print STORY_VARS "$name=".$story_vars->{$name}."\n";
742 }
743  
744 0 close STORY_VARS;
745  
746 }
747  
748 sub story_var {
749  
750 0 ▶ my $name = shift;
Subroutine story_var: not called CC=1 SCAR=6.9
Pod undocumented
751  
752 0 get_prop( 'story_vars' )->{$name};
753  
754 }
755  
756 sub story_vars_pretty {
757  
758 0 ▶ join " ", map { "$_:".(story_var($_)) } sort keys %{get_prop( 'story_vars' ) };
Subroutine story_vars_pretty: not called CC=1 SCAR=6.9
Pod undocumented
759  
760 }
761  
762 sub dump_os {
763  
764 0 ▶return $^O if $^O =~ 'MSWin';
Subroutine dump_os: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $^O =~ /MSWin/ 0 0 0
765  
766 0 my $cmd = <<'HERE';
767 #! /usr/bin/env sh
768  
769 # Find out the target OS
770 if [ -s /etc/os-release ]; then
771 # freedesktop.org and systemd
772 . /etc/os-release
773 OS=$NAME
774 VER=$VERSION_ID
775 elif lsb_release -h >/dev/null 2>&1; then
776 # linuxbase.org
777 OS=$(lsb_release -si)
778 VER=$(lsb_release -sr)
779 elif [ -s /etc/lsb-release ]; then
780 # For some versions of Debian/Ubuntu without lsb_release command
781 . /etc/lsb-release
782 OS=$DISTRIB_ID
783 VER=$DISTRIB_RELEASE
784 elif [ -s /etc/debian_version ]; then
785 # Older Debian/Ubuntu/etc.
786 OS=Debian
787 VER=$(cat /etc/debian_version)
788 elif [ -s /etc/SuSe-release ]; then
789 # Older SuSE/etc.
790 printf "TODO\n"
791 elif [ -s /etc/redhat-release ]; then
792 # Older Red Hat, CentOS, etc.
793 OS=$(cat /etc/redhat-release| head -n 1)
794 else
795 RELEASE_INFO=$(cat /etc/*-release 2>/dev/null | head -n 1)
796  
797 if [ ! -z "$RELEASE_INFO" ]; then
798 OS=$(printf -- "$RELEASE_INFO" | awk '{ print $1 }')
799 VER=$(printf -- "$RELEASE_INFO" | awk '{ print $NF }')
800 else
801 # Fall back to uname, e.g. "Linux <version>", also works for BSD, etc.
802 OS=$(uname -s)
803 VER=$(uname -r)
804 fi
805 fi
806  
807 echo "$OS$VER"
808  
809 HERE
810  
811 0 `$cmd`
812  
813 }
814  
815 sub _resolve_os {
816  
817
818 0 ▶ if (!$OS){
Subroutine _resolve_os: not called CC=26 SCAR=65.5
Branch: 1 branch
BranchTrueFalseTotal
unless ($Outthentic::Story::OS) 0 0 0
819  
820 0 DONE: while (1) {
821 0 my $data = dump_os();
822 0 ▶ $data=~/alpine/i and $OS = 'alpine' and last DONE;
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /alpine/i and $Outthentic::Story::OS = "alpine" and last DONE 0 0 0
Condition0%0/2
$data =~ /alpine/i and $Outthentic::Story::OS = "alpine"
!ll
00
MC/DC0%0/1
$data =~ /alpine/i and $Outthentic::Story::OS = "alpine"
$data =~ /alpine/i
Truth table0%0/2
A
A= $data =~ /alpine/i
Aresult
00
11
823 0 ▶ $data=~/minoca/i and $OS = "minoca" and last DONE;
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /minoca/i and $Outthentic::Story::OS = "minoca" and last DONE 0 0 0
Condition0%0/2
$data =~ /minoca/i and $Outthentic::Story::OS = "minoca"
!ll
00
MC/DC0%0/1
$data =~ /minoca/i and $Outthentic::Story::OS = "minoca"
$data =~ /minoca/i
Truth table0%0/2
A
A= $data =~ /minoca/i
Aresult
00
11
824 0 ▶ $data=~/centos linux(\d+)/i and $OS = "centos$1" and last DONE;
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /centos linux(\d+)/i and $Outthentic::Story::OS = "centos$1" and last DONE 0 0 0
Condition0%0/2
$data =~ /centos linux(\d+)/i and $Outthentic::Story::OS = "centos$1"
!ll
00
MC/DC0%0/1
$data =~ /centos linux(\d+)/i and $Outthentic::Story::OS = "centos$1"
$data =~ /centos linux(\d+)/i
Truth table0%0/2
A
A= $data =~ /centos linux(\d+)/i
Aresult
00
11
825 0 ▶ $data=~/Red Hat.*release\s+(\d)/i and $OS = "centos$1" and last DONE;
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /Red Hat.*release\s+(\d)/i and $Outthentic::Story::OS = "centos$1" and last DONE 0 0 0
Condition0%0/2
$data =~ /Red Hat.*release\s+(\d)/i and $Outthentic::Story::OS = "centos$1"
!ll
00
MC/DC0%0/1
$data =~ /Red Hat.*release\s+(\d)/i and $Outthentic::Story::OS = "centos$1"
$data =~ /Red Hat.*release\s+(\d)/i
Truth table0%0/2
A
A= $data =~ /Red Hat.*release\s+(\d)/i
Aresult
00
11
826 0 ▶ $data=~/arch/i and $OS = 'archlinux' and last DONE;
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /arch/i and $Outthentic::Story::OS = "archlinux" and last DONE 0 0 0
Condition0%0/2
$data =~ /arch/i and $Outthentic::Story::OS = "archlinux"
!ll
00
MC/DC0%0/1
$data =~ /arch/i and $Outthentic::Story::OS = "archlinux"
$data =~ /arch/i
Truth table0%0/2
A
A= $data =~ /arch/i
Aresult
00
11
827 0 ▶ $data=~/funtoo/i and $OS = 'funtoo' and last DONE;
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /funtoo/i and $Outthentic::Story::OS = "funtoo" and last DONE 0 0 0
Condition0%0/2
$data =~ /funtoo/i and $Outthentic::Story::OS = "funtoo"
!ll
00
MC/DC0%0/1
$data =~ /funtoo/i and $Outthentic::Story::OS = "funtoo"
$data =~ /funtoo/i
Truth table0%0/2
A
A= $data =~ /funtoo/i
Aresult
00
11
828 0 ▶ $data=~/fedora/i and $OS = 'fedora' and last DONE;
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /fedora/i and $Outthentic::Story::OS = "fedora" and last DONE 0 0 0
Condition0%0/2
$data =~ /fedora/i and $Outthentic::Story::OS = "fedora"
!ll
00
MC/DC0%0/1
$data =~ /fedora/i and $Outthentic::Story::OS = "fedora"
$data =~ /fedora/i
Truth table0%0/2
A
A= $data =~ /fedora/i
Aresult
00
11
829 0 ▶ $data=~/amazon/i and $OS = 'amazon' and last DONE;
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /amazon/i and $Outthentic::Story::OS = "amazon" and last DONE 0 0 0
Condition0%0/2
$data =~ /amazon/i and $Outthentic::Story::OS = "amazon"
!ll
00
MC/DC0%0/1
$data =~ /amazon/i and $Outthentic::Story::OS = "amazon"
$data =~ /amazon/i
Truth table0%0/2
A
A= $data =~ /amazon/i
Aresult
00
11
830 0 ▶ $data=~/ubuntu/i and $OS = 'ubuntu' and last DONE;
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /ubuntu/i and $Outthentic::Story::OS = "ubuntu" and last DONE 0 0 0
Condition0%0/2
$data =~ /ubuntu/i and $Outthentic::Story::OS = "ubuntu"
!ll
00
MC/DC0%0/1
$data =~ /ubuntu/i and $Outthentic::Story::OS = "ubuntu"
$data =~ /ubuntu/i
Truth table0%0/2
A
A= $data =~ /ubuntu/i
Aresult
00
11
831 0 ▶ $data=~/debian/i and $OS = 'debian' and last DONE;
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /debian/i and $Outthentic::Story::OS = "debian" and last DONE 0 0 0
Condition0%0/2
$data =~ /debian/i and $Outthentic::Story::OS = "debian"
!ll
00
MC/DC0%0/1
$data =~ /debian/i and $Outthentic::Story::OS = "debian"
$data =~ /debian/i
Truth table0%0/2
A
A= $data =~ /debian/i
Aresult
00
11
832 0 ▶ $data=~/darwin/i and $OS = 'darwin' and last DONE;
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /darwin/i and $Outthentic::Story::OS = "darwin" and last DONE 0 0 0
Condition0%0/2
$data =~ /darwin/i and $Outthentic::Story::OS = "darwin"
!ll
00
MC/DC0%0/1
$data =~ /darwin/i and $Outthentic::Story::OS = "darwin"
$data =~ /darwin/i
Truth table0%0/2
A
A= $data =~ /darwin/i
Aresult
00
11
833 0 ▶ $data=~/MSWin/i and $OS = 'windows' and last DONE;
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /MSWin/i and $Outthentic::Story::OS = "windows" and last DONE 0 0 0
Condition0%0/2
$data =~ /MSWin/i and $Outthentic::Story::OS = "windows"
!ll
00
MC/DC0%0/1
$data =~ /MSWin/i and $Outthentic::Story::OS = "windows"
$data =~ /MSWin/i
Truth table0%0/2
A
A= $data =~ /MSWin/i
Aresult
00
11
834 0 warn "unknown os: $data";
835 0 last DONE;
836 }
837 }
838 0 return $OS;
839 }
840  
841 package main;
842  
843 0 ▶sub os { Outthentic::Story::_resolve_os }
Subroutine os: not called CC=1 SCAR=6.9
844  
845  
846  
847 1 1;
848  
849 __END__
« Outthentic.pm ↑ Summary run-test.pl »