blib/lib/App/NDTools/NDProc.pm

filter: Statementstmt 89.5% 197 / 220 Branchbran 88.1% 67 / 76 Conditioncond 91.6% 11 / 12 MC/DCmcdc 87.5% 7 / 8 Subroutinesub 62.7% 27 / 43 Podpod 0.0% 0 / 16 totaltotal 82.4% 309 / 375 CC 50 SCAR 16.3
CC
50
Cov
89%
process_rules
12.2
exec
9.5
resolve_rules
8.0
CRAP 5.1
« blib/lib/App/NDTools/NDProc/Module/Merge.pm ↑ Summary blib/lib/App/NDTools/NDTool.pm »
1 package App::NDTools::NDProc;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use parent 'App::NDTools::NDTool';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Getopt::Long qw(GetOptionsFromArray :config bundling);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Log::Log4Cli;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Module::Find qw(findsubmod);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use App::NDTools::Slurp qw(s_decode s_dump s_encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Storable qw(dclone freeze thaw);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Struct::Diff 0.94 qw(diff split_diff);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Struct::Path 0.80 qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Struct::Path::PerlStyle 0.80 qw(str2path);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 our $VERSION = '0.26';
17  
18 sub arg_opts {
19 73 ▶ my $self = shift;
Subroutine arg_opts: called CC=1 SCAR=0.0
Pod undocumented
20  
21 73 Getopt::Long::Configure('pass_through');
22  
23 my %arg_opts = (
24 $self->SUPER::arg_opts(),
25 'builtin-format=s' => \$self->{OPTS}->{'builtin-format'},
26 'builtin-rules=s' => \$self->{OPTS}->{'builtin-rules'},
27 'disable-module=s@' => \$self->{OPTS}->{'disable-module'},
28 'dump-blame=s' => \$self->{OPTS}->{'dump-blame'},
29 'dump-rules=s' => \$self->{OPTS}->{'dump-rules'},
30 'embed-blame=s' => \$self->{OPTS}->{'embed-blame'},
31 'embed-rules=s' => \$self->{OPTS}->{'embed-rules'},
32 'list-modules|l' => \$self->{OPTS}->{'list-modules'},
33 'module|m=s' => \$self->{OPTS}->{module},
34 'rules=s@' => \$self->{OPTS}->{rules},
35 73 );
36 73 delete $arg_opts{'help|h'}; # skip in first args parsing -- will be accessable for modules
37 73 delete $arg_opts{'version|V'}; # --"--
38  
39 73 return %arg_opts;
40 }
41  
42 sub configure {
43 73 ▶ my $self = shift;
Subroutine configure: called CC=4 SCAR=13.9
Pod undocumented
44  
45 73 $self->index_modules();
46  
47 73 $self->{rules} = [];
48 8 map { push @{$self->{rules}}, @{$self->load_struct($_)} }
49 8 @{$self->{OPTS}->{rules}}
50 73 ▶ if ($self->{OPTS}->{rules});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"OPTS"}{"rules"} 8 65 73
51  
52 73 ▶ if ($self->{OPTS}->{module} or @{$self->{rules}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"OPTS"}{"module"} or @{$self->{"rules"};}) 67 6 73
Condition100%3/3
$self->{"OPTS"}{"module"} or @{$self->{"rules"};}
l!l&&r!l&&!r
6076
MC/DC100%2/2
$self->{"OPTS"}{"module"} or @{$self->{"rules"};}
$self->{"OPTS"}{"module"} @{$self->{"rules"};}
Truth table100%3/3
A or B
A= $self->{"OPTS"}{"module"}B= @{$self->{"rules"};}
ABresult
1X1
011
000
53 67 ▶ log_info { "Explicit rules used: builtin will be ignored" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
54 67 $self->{OPTS}->{'builtin-rules'} = undef;
55 }
56  
57 $self->{OPTS}->{'disable-module'} =
58 73 { map { $_ => 1 } @{$self->{OPTS}->{'disable-module'}} };
59 }
60  
61 sub defaults {
62 return {
63 73 ▶ 'blame' => 1, # may be redefined per-rule
Subroutine defaults: called CC=1 SCAR=0.0
Pod undocumented
64 'builtin-format' => "", # raw
65 'modpath' => [ 'App::NDTools::NDProc::Module' ],
66 };
67 }
68  
69 sub dump_arg {
70 57 ▶ my ($self, $uri, $arg) = @_;
Subroutine dump_arg: called CC=1 SCAR=0.2
Pod undocumented
71  
72 57 ▶ log_debug { "Dumping result to $uri" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
73 57 s_dump($uri, $self->{OPTS}->{ofmt}, $self->{OPTS}->{pretty}, $arg);
74 }
75  
76 sub dump_blame {
77 56 ▶ my ($self, $blame) = @_;
Subroutine dump_blame: called CC=2 SCAR=7.0
Pod undocumented
78  
79 56 ▶ return unless (defined $self->{OPTS}->{'dump-blame'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"OPTS"}{"dump-blame"} 52 4 56
80  
81 4 ▶ log_debug { "Dumping blame to $self->{OPTS}->{'dump-blame'}" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
82 s_dump($self->{OPTS}->{'dump-blame'}, $self->{OPTS}->{ofmt},
83 4 $self->{OPTS}->{pretty}, $blame);
84 }
85  
86 sub dump_rules {
87 4 ▶ my $self = shift;
Subroutine dump_rules: called CC=3 SCAR=11.0
Pod undocumented
88  
89 4 for my $rule (@{$self->{rules}}) {
90 # remove undefs - defaults will be used anyway
91 9 ▶ map { defined $rule->{$_} || delete $rule->{$_} } keys %{$rule};
Branch: 1 branch
BranchTrueFalseTotal
defined $rule->{$_} or delete $rule->{$_} 12 24 36
92 }
93 4 ▶ log_debug { "Dumping rules to $self->{OPTS}->{'dump-rules'}" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
94 s_dump($self->{OPTS}->{'dump-rules'}, $self->{OPTS}->{ofmt},
95 4 $self->{OPTS}->{pretty}, $self->{rules});
96 }
97  
98 sub embed {
99 4 ▶ my ($self, $data, $path, $thing) = @_;
Subroutine embed: called CC=4 SCAR=14.0
Pod undocumented
100  
101 4 my $spath = eval { str2path($path) };
102 4 ▶ die_fatal "Unable to parse '$path' ($@)", 4 if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
103 4 my $ref = eval { (path($data, $spath, expand => 1))[0]};
104 4 ▶ die_fatal "Unable to lookup '$path' ($@)", 4 if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
105  
106 4 ${$ref} = $self->{OPTS}->{'builtin-format'} ?
107 4 ▶ s_encode($thing, $self->{OPTS}->{'builtin-format'}) :
Branch: 1 branch
BranchTrueFalseTotal
$self->{'OPTS'}{'builtin-format'} ? : 1 3 4
108 $thing;
109 }
110  
111 sub exec {
112 72 ▶ my $self = shift;
Subroutine exec: called CC=9 SCAR=22.5
Pod undocumented
113  
114 72 ▶ if ($self->{OPTS}->{'list-modules'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"OPTS"}{"list-modules"}) 1 71 72
115 1 map { printf "%-10s %-8s %s\n", @{$_} } $self->list_modules;
116 1 die_info undef, 0;
117 }
118  
119 71 ▶ if (defined $self->{OPTS}->{module}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"OPTS"}{"module"}) 60 11 71
120 die_fatal "Unknown module specified '$self->{OPTS}->{module}'", 1
121 60 ▶ unless (exists $self->{MODS}->{$self->{OPTS}->{module}});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"MODS"}{$self->{"OPTS"}{"module"}} 1 59 60
122 59 $self->init_module($self->{OPTS}->{module});
123  
124 59 my $mod = $self->{MODS}->{$self->{OPTS}->{module}}->new();
125 59 for my $rule ($mod->parse_args($self->{ARGV})->get_opts()) {
126 $rule->{modname} = $self->{OPTS}->{module},
127 59 push @{$self->{rules}}, $rule;
128 }
129 }
130  
131 # parse the rest of args (unrecognized by module (if was specified by args))
132 # to be sure there is no unsupported opts remain
133 my @rest_opts = (
134 0 ▶ 'help|h' => sub { $self->usage; exit 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 0 ▶ 'version|V' => sub { print $self->VERSION . "\n"; exit 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
136 69 );
137  
138 69 Getopt::Long::Configure('nopass_through');
139 69 ▶ unless (GetOptionsFromArray($self->{ARGV}, @rest_opts)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&GetOptionsFromArray($self->{"ARGV"}, @rest_opts)) 0 69 69
140 0 $self->usage;
141 0 die_fatal "Unsupported opts passed", 1;
142 }
143  
144 69 ▶ if ($self->{OPTS}->{'dump-rules'} and not @{$self->{ARGV}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'OPTS'}{'dump-rules'} and not @{$self->{'ARGV'};}) { } 3 66 69
Condition100%3/3
$self->{"OPTS"}{"dump-rules"} and not @{$self->{"ARGV"};}
!ll&&!rl&&r
6513
MC/DC100%2/2
$self->{"OPTS"}{"dump-rules"} and not @{$self->{"ARGV"};}
$self->{"OPTS"}{"dump-rules"} not @{$self->{"ARGV"};}
Truth table100%3/3
A and not(B)
A= $self->{"OPTS"}{"dump-rules"}B= not @{$self->{"ARGV"};}
ABresult
0X0
100
111
145 3 $self->dump_rules();
146 } else {
147 66 ▶ $self->check_args(@{$self->{ARGV}}) or die_fatal undef, 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->check_args(@{$$self{"ARGV"};}) or &die_fatal(undef, 1) 0 65 65
148 65 $self->process_args(@{$self->{ARGV}});
149 }
150  
151 58 die_info "All done", 0;
152 }
153  
154 sub index_modules {
155 73 ▶ my $self = shift;
Subroutine index_modules: called CC=4 SCAR=14.0
Pod undocumented
156  
157 73 my $required = { map { $_->{modname} => 1 } @{$self->{rules}} };
158 73 ▶ $required->{$self->{OPTS}->{module}} = 1 if ($self->{OPTS}->{module});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"OPTS"}{"module"} 60 13 73
159  
160 73 for my $path (@{$self->{OPTS}->{modpath}}) {
161 73 ▶ log_trace { "Indexing modules in $path" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
162 73 for my $m (findsubmod $path) {
163 292 $self->{MODS}->{(split('::', $m))[-1]} = $m;
164 }
165 }
166  
167 73 return $self;
168 }
169  
170 sub init_module {
171 145 ▶ my ($self, $mod) = @_;
Subroutine init_module: called CC=3 SCAR=11.2
Pod undocumented
172  
173 145 ▶ return if ($self->{_initialized_mods}->{$mod});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_initialized_mods"}{$mod} 70 75 145
174  
175 75 ▶ log_trace { "Inititializing module $mod ($self->{MODS}->{$mod})" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
176 75 eval "require $self->{MODS}->{$mod}";
177 75 ▶ die_fatal "Failed to initialize module '$mod' ($@)", 1 if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 75 75
178 75 $self->{_initialized_mods}->{$mod} = 1;
179 }
180  
181 sub list_modules {
182 1 ▶ my $self = shift;
Subroutine list_modules: called CC=1 SCAR=0.0
Pod undocumented
183  
184 return map {
185 4 $self->init_module($_);
186 4 [ $_, $self->{MODS}->{$_}->VERSION, $self->{MODS}->{$_}->MODINFO ]
187 1 } sort keys %{$self->{MODS}};
188 }
189  
190 sub load_arg {
191 89 ▶ shift->load_struct(@_);
Subroutine load_arg: called CC=1 SCAR=0.0
Pod undocumented
192 }
193  
194 6 *load_source = \&load_arg;
195  
196 sub load_builtin_rules {
197 3 ▶ my ($self, $data, $path) = @_;
Subroutine load_builtin_rules: called CC=4 SCAR=14.1
Pod undocumented
198  
199 3 ▶ log_debug { "Loading builtin rules from '$path'" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
200 3 my $spath = eval { str2path($path) };
201 3 ▶ die_fatal "Unable to parse path ($@)", 4 if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
202 3 my $rules = eval { (path($data, $spath, deref => 1, strict => 1))[0] };
203 3 ▶ die_fatal "Unable to lookup path ($@)", 4 if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
204  
205 return $self->{OPTS}->{'builtin-format'} ?
206 3 ▶ s_decode($rules, $self->{OPTS}->{'builtin-format'}) :
Branch: 1 branch
BranchTrueFalseTotal
$self->{'OPTS'}{'builtin-format'} ? : 1 2 3
207 $rules;
208 }
209  
210 sub process_args {
211 65 ▶ my $self = shift;
Subroutine process_args: called CC=7 SCAR=19.6
Pod undocumented
212  
213 65 for my $arg (@_) {
214 67 ▶ log_info { "Processing $arg" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
215 67 my $data = $self->load_arg($arg, $self->{OPTS}->{ifmt});
216  
217 67 ▶ if ($self->{OPTS}->{'builtin-rules'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"OPTS"}{"builtin-rules"}) 3 64 67
218 3 $self->{rules} = $self->load_builtin_rules($data, $self->{OPTS}->{'builtin-rules'});
219 # restore original rules - may be changed while processing structure
220 $self->{OPTS}->{'embed-rules'} = $self->{OPTS}->{'builtin-rules'}
221 3 ▶ if (not defined $self->{OPTS}->{'embed-rules'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"OPTS"}{"embed-rules"} 3 0 3
222 }
223  
224 67 ▶ if ($self->{OPTS}->{'dump-rules'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"OPTS"}{"dump-rules"}) 1 66 67
225 1 $self->dump_rules();
226 1 next;
227 }
228  
229 66 my @blame = $self->resolve_rules($arg)->process_rules(\$data);
230  
231 57 ▶ if ($self->{OPTS}->{'embed-blame'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"OPTS"}{"embed-blame"}) 1 56 57
232 1 ▶ log_debug { "Embedding blame to '$self->{OPTS}->{'embed-blame'}'" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
233 1 $self->embed($data, $self->{OPTS}->{'embed-blame'}, \@blame);
234 }
235  
236 57 ▶ if ($self->{OPTS}->{'embed-rules'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"OPTS"}{"embed-rules"}) 3 54 57
237 3 ▶ log_debug { "Embedding rules to '$self->{OPTS}->{'embed-rules'}'" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
238 3 $self->embed($data, $self->{OPTS}->{'embed-rules'}, $self->{rules});
239 }
240  
241 57 $self->dump_arg($arg, $data);
242 56 $self->dump_blame(\@blame);
243 }
244 }
245  
246 sub process_rules {
247 66 ▶ my ($self, $data) = @_;
Subroutine process_rules: called CC=12 SCAR=25.0
Pod undocumented
248  
249 66 my $rcnt = 0; # rules counter
250 66 my @blame;
251  
252 66 for my $rule (@{$self->{resolved_rules}}) {
253 85 ▶ if (exists $self->{OPTS}->{'disable-module'}->{$rule->{modname}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"OPTS"}{"disable-module"}{$rule->{"modname"}}) 6 79 85
254 6 ▶ log_debug { "Skip rule #$rcnt (module $rule->{modname} is disabled by args)" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
255 6 next;
256 }
257 79 ▶ if ($rule->{disabled}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"disabled"}) 0 79 79
258 0 ▶ log_debug { "Rule #$rcnt ($rule->{modname}) is disabled, skip it" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
259 0 next;
260 }
261 die_fatal "Unknown module specified ($rule->{modname}; rule #$rcnt)", 1
262 79 ▶ unless (exists $self->{MODS}->{$rule->{modname}});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"MODS"}{$rule->{"modname"}} 1 78 79
263  
264 78 ▶ log_debug { "Processing rule #$rcnt ($rule->{modname})" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
265 78 $self->init_module($rule->{modname});
266  
267 78 ▶ my $result = ref ${$data} ? dclone(${$data}) : ${$data};
Branch: 1 branch
BranchTrueFalseTotal
ref ${$data;} ? : 77 1 78
268 78 ▶ my $source = exists $rule->{source} ? thaw($self->{sources}->{$rule->{source}}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $rule->{'source'} ? : 23 55 78
269 78 $self->{MODS}->{$rule->{modname}}->new->process($data, $rule, $source);
270  
271 70 my $changes = { rule_id => 0 + $rcnt };
272 70 ▶ if (defined $rule->{blame} ? $rule->{blame} : $self->{OPTS}->{blame}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $rule->{"blame"} ? $rule->{"blame"} : $self->{"OPTS"}{"blame"}) 68 2 70
defined $rule->{'blame'} ? : 2 68 70
273 68 my $diff = split_diff(diff($result, ${$data}, noO => 1, noU => 1));
274 68 ▶ $changes->{R} = delete $diff->{a} if (exists $diff->{a}); # more obvious
Branch: 1 branch
BranchTrueFalseTotal
if exists $diff->{"a"} 62 6 68
275 68 ▶ $changes->{A} = delete $diff->{b} if (exists $diff->{b}); # --"--
Branch: 1 branch
BranchTrueFalseTotal
if exists $diff->{"b"} 65 3 68
276 }
277 70 ▶ map { $changes->{$_} = $rule->{$_} if (defined $rule->{$_}) }
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule->{$_} 23 187 210
278 qw(blame comment source); # preserve useful info
279 70 push @blame, dclone($changes);
280 } continue {
281 76 $rcnt++;
282 }
283  
284 57 return @blame;
285 }
286  
287 sub resolve_rules {
288 66 ▶ my ($self, $opt_src) = @_;
Subroutine resolve_rules: called CC=8 SCAR=20.8
Pod undocumented
289  
290 66 ▶ log_debug { "Resolving rules" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
291 66 $self->{resolved_rules} = dclone($self->{rules});
292  
293 66 for my $rule (@{$self->{resolved_rules}}) {
294 # single path may be specified as string, convert it to list
295 $rule->{path}->[0] = delete $rule->{path}
296 85 ▶ if (exists $rule->{path} and not ref $rule->{path});
Branch: 1 branch
BranchTrueFalseTotal
if exists $rule->{"path"} and not ref $rule->{"path"} 10 75 85
Condition100%3/3
exists $rule->{"path"} and not ref $rule->{"path"}
!ll&&!rl&&r
17410
MC/DC100%2/2
exists $rule->{"path"} and not ref $rule->{"path"}
exists $rule->{"path"} not ref $rule->{"path"}
Truth table100%3/3
A and not(B)
A= exists $rule->{"path"}B= not ref $rule->{"path"}
ABresult
0X0
100
111
297  
298 85 ▶ next unless (exists $rule->{source});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rule->{"source"} 62 23 85
299 23 ▶ unless (defined $rule->{source} and $rule->{source} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rule->{"source"} and $rule->{"source"} ne "") 1 22 23
Condition66%2/3
defined $rule->{"source"} and $rule->{"source"} ne ""
!ll&&!rl&&r
1022
MC/DC50%1/2
defined $rule->{"source"} and $rule->{"source"} ne ""
defined $rule->{"source"} $rule->{"source"} ne ""
Truth table66%2/3
A and B
A= defined $rule->{"source"}B= $rule->{"source"} ne ""
ABresult
0X0
100
111
300 # use processing doc as source
301 1 $rule->{source} = $opt_src;
302 }
303 23 ▶ next if (exists $self->{sources}->{$rule->{source}});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"sources"}{$rule->{"source"}} 1 22 23
304 $self->{sources}->{$rule->{source}} =
305 22 freeze($self->load_source($rule->{source}, $self->{OPTS}->{ifmt}));
306 }
307  
308 66 return $self;
309 }
310  
311 6 1; # End of App::NDTools::NDProc
« blib/lib/App/NDTools/NDProc/Module/Merge.pm ↑ Summary blib/lib/App/NDTools/NDTool.pm »