ExtHandler.pm untested

filter: Statementstmt 0.0% 0 / 348 Branchbran 0.0% 0 / 168 Conditioncond 0.0% 0 / 63 MC/DCmcdc 0.0% 0 / 36 Subroutinesub 0.0% 0 / 36 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 674 CC 85 SCAR 59.7
CC
85
Cov
0%
process_options
1260.0
run_tasks
110.0
register_option
72.0
CRAP 390.7
1 package VUser::ExtHandler;
2 use warnings;
3 use strict;
4  
5 # Copyright 2004 Randy Smith
6 # $Id: ExtHandler.pm,v 1.51 2007-09-24 20:16:06 perlstalker Exp $
7  
8 our $REVISION = (split (' ', '$Revision: 1.51 $'))[1];
9 our $VERSION = "0.5.0";
10  
11 use lib qw(..);
12 use Getopt::Long;
13 use VUser::ExtLib;
14 use VUser::Meta;
15 use VUser::Log qw(:levels);
16  
17 use Regexp::Common qw /number/;
18 #use Regexp::Common qw /number RE_ALL/;
19  
20 sub DEFAULT_PRIORITY { 10; }
21  
22 my $log;
23  
24 sub new
25 {
26  
27 my $self = shift;
28 my $class = ref($self) || $self;
29 my $cfg = shift;
30 $log = shift;
31  
32 if (not defined $log
33 and defined $main::log
34 and UNIVERSAL::isa($main::log, 'VUser::Log')
35 ) {
36 $log = $main::log;
37 } elsif (defined $log
38 and UNIVERSAL::isa($log, 'VUser::Log')
39 ) {
40 # noop
41 } else {
42 $log = VUser::Log->new($cfg, 'vuser/eh');
43 }
44  
45 # {keyword}{action}{tasks}[order][tasks (sub refs)]
46 # {keyword}{action}{options}{option} = type
47 # {keyword}{_meta}{option} = VUser::Meta
48 my $me = {'keywords' => {},
49 'required' => {},
50 'descrs' => {},
51 };
52  
53 bless $me, $class;
54  
55 #$me->load_extensions(%$cfg);
56  
57 return $me;
58 }
59  
60 sub register_keyword
61 {
62 my $self = shift;
63 my $keyword = shift;
64 my $descr = shift;
65  
66 unless (exists $self->{keywords}{$keyword}) {
67 $self->{keywords}{$keyword} = {};
68  
69 $log->log(LOG_DEBUG, "Reg keyword : $keyword");
70  
71 $self->{descrs}{$keyword} = {_descr => $descr};
72 }
73 }
74  
75 sub register_action
76 {
77 my $self = shift;
78 my $keyword = shift;
79 my $action = shift;
80 my $descr = shift;
81  
82 if ($action =~ /^-/) {
83 die "Unable to register action. Action may not start with a '-'.\n";
84 }
85  
86 unless (exists $self->{keywords}{$keyword}) {
87 die "Unable to register action on unknown keyword '$keyword'.\n";
88 }
89  
90 unless (exists $self->{keywords}{$keyword}{$action}) {
91 $log->log(LOG_DEBUG, "Reg action for $keyword: $action");
92 $self->{keywords}{$keyword}{$action} = {tasks => [], options => {}};
93 $self->{descrs}{$keyword}{$action} = {_descr => $descr};
94 }
95 }
96  
97 #$eh->register_option('key', 'action',
98 # $option, $type, $required, $descr, $widget
99 # - OR -
100 # $meta, $required
101 # );
102 sub register_option
103 {
104 my $self = shift;
105 my $keyword = shift;
106 my $action = shift;
107 my $option = shift;
108  
109 my $meta;
110 my $required = 0;
111  
112 if (eval { $option->isa('VUser::Meta'); }) {
113 $meta = $option;
114 $required = shift;
115 } else {
116 if ($main::DEBUG) {
117 use Data::Dumper;
118 $log->log(LOG_DEBUG, 'Option: '.Dumper($option));
119 }
120 die "Option on $keyword|$action was not a VUser::Meta\n";
121 }
122  
123 $log->log(LOG_DEBUG, "Reg Opt: $keyword|$action %s %s %s",
124 $meta->name, $meta->type, $required?'Req':'');
125  
126 unless (exists $self->{keywords}{$keyword}) {
127 die "Unable to register option on unknown keyword '$keyword'.\n";
128 }
129  
130 unless (exists $self->{keywords}{$keyword}{$action}) {
131 die "Unable to register option on unknown action '$action'.\n";
132 }
133  
134 # if (exists $self->{keywords}{$keyword}{$action}{options}{$option}) {
135 if (exists $self->{keywords}{$keyword}{$action}{options}{$meta->name}) {
136 # Let's silently ignore duplicate option definitions the way we
137 # do for keywords and actions. This will allow an extension to
138 # register an option to guarantee that it's there rather than having
139 # to rely on another extension to register the option.
140 #die "Unable to register option for $keyword|$action. '$option' already exists.\n";
141 } else {
142 $self->{keywords}{$keyword}{$action}{options}{$meta->name} = $meta;
143 if ($required) {
144 $self->{required}{$keyword}{$action}{$meta->name} = 1;
145 } else {
146 $self->{required}{$keyword}{$action}{$meta->name} = 0;
147 }
148 $self->{descrs}{$keyword}{$action}{$meta->name} = {_descr => $meta->description};
149 }
150  
151 # Q: Should I auto register meta data?
152 # A: No. Let the extensions register the meta data they feel
153 # is important. This will yeild a nicer way of pulling in
154 # "standard" meta data for extensions that build on defaults.
155 #$self->register_meta($keyword, $meta);
156 }
157  
158 sub register_meta
159 {
160 my $self = shift;
161 my $keyword = shift;
162
163 my $meta;
164
165 if (ref $_[0] and $_[0]->isa('VUser::Meta')) {
166 $meta = $_[0];
167 } else {
168 $meta = new VUser::Meta(@_);
169 }
170  
171 unless (exists $self->{keywords}{$keyword}) {
172 die "Unable to register option on unknown keyword: '$keyword'.\n";
173 }
174  
175 if (defined $self->{$keyword}{'_meta'}{$meta->name}) {
176 # Silently ignore duplicates.
177 } else {
178 $self->{$keyword}{'_meta'}{$meta->name} = $meta;
179 }
180 }
181  
182 sub is_required
183 {
184 my $self = shift;
185 my $keyword = shift;
186 my $action = shift;
187 my $option = shift;
188  
189 if ($self->{required}{$keyword}{$action}{$option}) {
190 return 1;
191 } else {
192 return 0;
193 }
194 }
195  
196 sub check_required
197 {
198 my $self = shift;
199 my $keyword = shift;
200 my $action = shift;
201 my $opts = shift;
202  
203 foreach my $option (grep { $self->is_required($keyword, $action, $_); }
204 keys %{$self->{required}{$keyword}{$action}}) {
205 if (not exists($opts->{$option})) {
206 return $option;
207 }
208 }
209 return '';
210 }
211  
212 sub register_task
213 {
214 my $self = shift;
215 my $keyword = shift;
216 my $action = shift;
217 my $handler = shift; # sub ref. Takes 4 params: The tied config
218 # the options ref, the action, and the
219 # ::ExtHandler
220 my $priority = shift;
221  
222 unless (exists $self->{keywords}{$keyword}) {
223 die "Unable to register task on unknown keyword '$keyword'.\n";
224 }
225  
226 unless (exists $self->{keywords}{$keyword}{$action}) {
227 die "Unable to register task on unknown action '$action'.\n";
228 }
229  
230 # Default priority is 10.
231 $priority = DEFAULT_PRIORITY unless defined $priority;
232 if ($priority =~ /^[+-]\s*\d+/) {
233 $priority =~ s/\s//g; # remove any excess whitespace
234 $priority = DEFAULT_PRIORITY() + $priority;
235 $priority = 0 if $priority < 0;
236 }
237  
238 if (defined $self->{keywords}{$keyword}{$action}{tasks}[$priority]) {
239 push @{$self->{keywords}{$keyword}{$action}{tasks}[$priority]}, $handler;
240 } else {
241 $self->{keywords}{$keyword}{$action}{tasks}[$priority] = [$handler];
242 }
243 }
244  
245 sub get_keywords
246 {
247 my $self = shift;
248  
249 return sort keys %{ $self->{keywords}};
250 }
251  
252 sub is_keyword
253 {
254 my $self = shift;
255 my $keyword = shift;
256  
257 if (defined $self->{keywords}{$keyword}) {
258 return 1;
259 } else {
260 return 0;
261 }
262 }
263  
264 sub get_actions
265 {
266 my $self = shift;
267 my $keyword = shift;
268  
269 return sort keys %{ $self->{keywords}{$keyword} };
270 }
271  
272 sub get_options
273 {
274 my $self = shift;
275 my $keyword = shift;
276 my $action = shift;
277
278 my $real_act = $action;
279 if (not defined $self->{keywords}{$keyword}{$action}
280 and defined $self->{keywords}{$keyword}{'*'}) {
281 $real_act = '*';
282 }
283  
284 return sort keys %{ $self->{keywords}{$keyword}{$real_act}{options}};
285 }
286  
287 # Return unsorted list of VUser::Meta objects;
288 sub get_meta
289 {
290 my $self = shift;
291 my $keyword = shift;
292 my $option = shift;
293  
294 my @meta = ();
295  
296 return undef if not defined $self->{$keyword};
297  
298 if (defined $option) {
299 push @meta, $self->{$keyword}{'_meta'}{$option};
300 } else {
301 foreach my $opt (keys %{$self->{$keyword}{'_meta'}}) {
302 push @meta, $self->{$keyword}{'_meta'}{$opt};
303 }
304 }
305  
306 return @meta;
307 }
308  
309 sub get_description
310 {
311 my $self = shift;
312 my $keyword = shift;
313 my $action = shift;
314 my $option = shift;
315  
316 if ($keyword and $action and $option) {
317 return $self->{descrs}{$keyword}{$action}{$option}{_descr};
318 } elsif ($keyword and $action) {
319 return $self->{descrs}{$keyword}{$action}{_descr};
320 } elsif ($keyword) {
321 return $self->{descrs}{$keyword}{_descr};
322 }
323 }
324  
325 sub load_extensions
326 {
327 my $self = shift;
328 my $cfg = shift;
329  
330 $self->{'_loaded'} = {};
331 $self->{'_loadorder'} = [];
332  
333 my $exts = $cfg->{ vuser }{ extensions };
334 $exts = '' unless $exts;
335 VUser::ExtLib::strip_ws($exts);
336 $log->log(LOG_DEBUG, "Cfg extensions: $exts");
337  
338 my @exts = split / /, $exts;
339 eval { $self->load_extensions_list($cfg, @exts) };
340 }
341  
342 sub load_extensions_list {
343 my $self = shift;
344 my $cfg = shift;
345 my @exts = @_;
346  
347 $self->{'_loaded'} = {};
348 $self->{'_loadorder'} = [];
349  
350 $self->load_extension('CORE');
351 $log->log(LOG_DEBUG, "Cfg extensions: ".join(',', @exts));
352 foreach my $extension (@exts)
353 {
354 eval { $self->load_extension( $extension, $cfg ); };
355 $log->log(LOG_DEBUG, "Unable to load %s: %s", $extension, $@) if $@;
356 }
357 }
358  
359 sub load_extension
360 {
361 my $self = shift;
362 my $ext = shift;
363 my $cfg = shift;
364  
365 my $pm = 'VUser::'.$ext; # Module name
366  
367 # Don't load an extensions we've already seen.
368 if ($self->{'_loaded'}{$ext}) {
369 $log->log(LOG_INFO, "$ext is already loaded. Skipping");
370 return;
371 }
372  
373 # Import the extention module
374 eval( "require $pm" );
375 die $@ if $@;
376 no strict "refs";
377  
378 # Check for module dependencies
379 $log->log(LOG_DEBUG, "Checking dependencies for %s", $ext);
380 if ($pm->can('depends')) {
381 my @depends = ();
382 @depends = $pm->depends($cfg);
383  
384 foreach my $depend (@depends) {
385 next if not $depend; # Should not happen but let's be careful
386 $log->log(LOG_INFO, "$ext depends on $depend");
387 eval { $self->load_extension($depend, $cfg); };
388 die "Unable to load dependency $depend: $@\n" if $@;
389 }
390 }
391
392 $log->log(LOG_INFO, "Loading extension: $ext");
393 $self->{'_loaded'}{$ext} = 1;
394 push (@{$self->{'_loadorder'}}, $ext);
395 &{$pm.'::init'}($self, %{ $cfg });
396 }
397  
398 sub unload_extensions
399 {
400 my $self = shift;
401 my %cfg = @_;
402  
403 # foreach my $ext (keys %{ $self->{'_loaded'} }) {
404 foreach my $ext (reverse (@{$self->{'_loadorder'}})) {
405 eval { $self->unload_extension($ext, %cfg); };
406 warn "Unable to unload $ext: $@\n" if $@;
407 }
408 }
409  
410 sub unload_extension
411 {
412 my $self = shift;
413 my $ext = shift;
414 my %cfg = @_;
415  
416 my $pm = 'VUser::'.$ext;
417  
418 no strict ('refs');
419 $log->log(LOG_INFO, "Unloading extension: $ext");
420 if (UNIVERSAL::can($pm, 'unload')) {
421 &{$pm.'::unload'}($self, %cfg);
422 }
423 }
424  
425 sub run_tasks
426 {
427 my $self = shift;
428 my $keyword = shift;
429 my $action = shift;
430 my $cfg = shift;
431  
432 my %opts = @_;
433  
434 my $wild_action = 0;
435 if (exists $self->{keywords}{$keyword}{$action}) {
436 $wild_action = 0;
437 } elsif (exists $self->{keywords}{$keyword}{'*'}) {
438 $wild_action = 1;
439 } else {
440 die "Unknown action '$action'\n";
441 }
442  
443 $log->log(LOG_DEBUG,"Keyword: '$keyword' Action: '$action' ARGV: @ARGV");
444  
445 eval { %opts = $self->process_options($keyword, $action, $cfg, %opts); };
446 die $@ if $@;
447  
448 my @tasks = ();
449 if ($wild_action) {
450 @tasks = @{$self->{keywords}{$keyword}{'*'}{tasks}};
451 } else {
452 @tasks = @{$self->{keywords}{$keyword}{$action}{tasks}};
453 }
454  
455 my @results = ();
456 foreach my $priority (@tasks) {
457 foreach my $task (@$priority) {
458 # Return values?
459 my $rs = &$task($cfg, \%opts, $action, $self);
460 if (not defined $rs) {
461 } elsif (UNIVERSAL::isa($rs, "VUser::ResultSet")) {
462 push @results, $rs;
463 } elsif (UNIVERSAL::isa($rs, "ARRAY")) {
464 # Someone sent us an array ref. Go through the list
465 # and push any ::ResultSets on to the result list.
466 foreach my $r (@$rs) {
467 if (UNIVERSAL::isa($r, "VUser::ResultSet")) {
468 push @results, $r;
469 } elsif (UNIVERSAL::isa($r, 'ARRAY')) {
470 push @results, $r;
471 }
472 }
473 }
474 }
475 }
476  
477 return \@results;
478 }
479  
480 sub cleanup
481 {
482 my $self = shift;
483 my %cfg = @_;
484  
485 eval { $self->unload_extensions(%cfg); };
486 warn $@ if $@;
487 }
488  
489 sub process_options {
490 my $self = shift;
491 my $keyword = shift;
492 my $action = shift;
493 my $cfg = shift;
494 my %opts = @_;
495  
496 if ($main::DEBUG >= 1) {
497 use Data::Dumper;
498 $log->log(LOG_DEBUG, 'Options: '. Dumper(\%opts));
499 }
500  
501 unless (exists $self->{keywords}{$keyword}) {
502 die "Unknown module '$keyword'\n";
503 }
504  
505 my $wild_action = 0;
506 if (exists $self->{keywords}{$keyword}{$action}) {
507 $wild_action = 0;
508 } elsif (exists $self->{keywords}{$keyword}{'*'}) {
509 $wild_action = 1;
510 } else {
511 die "Unknown action '$action'\n";
512 }
513  
514 # If we're processessing a wild action, we need to check the
515 # '*' action instead of the passed in action.
516 my $real_action = $wild_action? '*': $action;
517  
518 # If opts is not empty, we'll just use the option's we're given
519 # otherwise, we'll get the options using GetOptions()
520  
521 if (%opts) {
522 # We need to do some error checking here on the option type.
523 # Getopt::Long takes care of it in the other case, but we need to
524 # do that ourselves here.
525 foreach my $opt (keys %{$self->{keywords}{$keyword}{$real_action}{options}}) {
526 my $type = $self->{keywords}{$keyword}{$real_action}{options}{$opt};
527  
528 # Giant switch-type block to validate Getopt::Long types with the
529 # passed in values.
530 if ($type eq '!') {
531 if ($opts{$opt}) {
532 $opts{$opt} = 1;
533 } else {
534 $opts{$opt} = 0;
535 }
536  
537 if ($opts{"no$opt"} or $opts{"no-$opt"}) {
538 $opts{$opt} = 0;
539 }
540 } elsif ($type eq '+') {
541 # All we can do here is make sure the option is an int.
542 unless ($opts{$opt} =~ $RE{num}{int}) {
543 die "$opt is not an integer.\n";
544 }
545 } elsif ($type =~ /^([=:])([siof])([@%])?$/) {
546 if ($1 eq '='
547 and exists $opts{$opt}
548 and not defined $opts{$opt}) {
549 die "Missing required value: $opt\n";
550 }
551  
552 my $d_type = $2;
553 my $dest_type = $3;
554  
555 $log->log(LOG_DEBUG, "Key: %s; Act: %s, Opt: %s; Type: %s d_type: %s",
556 $keyword, $real_action, $opt, $type, $d_type);
557 $log->log(LOG_DEBUG, "Req: %s Def: %s",
558 $self->is_required($keyword, $real_action, $opt)? 'Yes':'No',
559 defined $opts{$opt}?"Yes ($opts{$opt})":'No'
560 );
561  
562 if ($d_type eq 's') {
563 # There's nothing to verify here
564 } elsif ($d_type eq 'i'
565 #and defined $opts{$opt}
566 # This line is causing the warnings.
567 #and not $opts{$opt} =~ /^$RE{num}{int}$/
568 ) {
569 # Ok, this is really stupid. I had to move this
570 # check into a seperate if because it was causing
571 # a weird warning about 'Use of uninitialized value
572 # in string eq at vuser-ng/lib/VUser/ExtHandler.pm
573 # line 339.'
574 if (defined $opts{$opt}
575 and not $opts{$opt} =~ /^$RE{num}{int}$/) {
576 die "$opt is not an integer.\n";
577 }
578 } elsif ($d_type eq 'o'
579 and defined $opts{$opt}
580 and not ($opts{$opt} =~ /^$RE{num}{int}$/
581 or $opts{$opt} =~ /^$RE{num}{oct}$/
582 or $opts{$opt} =~ /^$RE{num}{hex}$/
583 )
584 ) {
585 die "$opt is not an extended integer.";
586 } elsif ($2 eq 'f'
587 and defined $opts{$opt}
588 and not $opts{$opt} =~ /^$RE{num}{real}$/) {
589 die "$opt is not a real number.";
590 }
591 } elsif ($type =~ /^:(-?\d+)([@%])?$/) {
592 my $num = $1;
593 if (defined $opts{$opt}) {
594 die "$opt is not an integer." unless $opts{$opt} =~ /$RE{num}{int}/;
595 } else {
596 $opts{$opt} = $num;
597 }
598 } elsif ($type =~ /^:+([@%])?$/) {
599 if (defined $opts{$opt}) {
600 die "$opt is not an integer." unless $opts{$opt} =~ /$RE{num}{int}/;
601 } else {
602 $opts{$opt}++;
603 }
604 }
605 }
606 } else {
607 # Prepare options for GetOptions();
608 my @opt_defs = ();
609
610 foreach my $opt (keys %{$self->{keywords}{$keyword}{$real_action}{options}}) {
611 my $gopt_type = '';
612 #my $type = $self->{keywords}{$keyword}{$real_action}{options}{$opt}a;
613 #$type = '' unless defined $type;
614  
615 my $type = $self->{keywords}{$keyword}{$real_action}{options}{$opt}->type;
616 if ($type eq 'string') {
617 $gopt_type = '=s';
618 } elsif ($type eq 'integer' or $type eq 'int') {
619 $gopt_type = '=i';
620 } elsif ($type eq 'counter') {
621 $gopt_type = '+';
622 } elsif ($type eq 'boolean' or $type eq 'bool') {
623 $gopt_type = '!';
624 } elsif ($type eq 'float') {
625 $gopt_type = '=f';
626 }
627  
628 my $def = $opt.$gopt_type;
629 push @opt_defs, $def;
630 }
631
632 $log->log(LOG_DEBUG, "Opt defs: @opt_defs");
633 if (@opt_defs) {
634 GetOptions(\%opts, @opt_defs);
635 }
636 }
637  
638 # Check for required options
639 my $opt = $self->check_required ($keyword, $real_action, \%opts);
640 if ($opt) {
641 die "Missing required option '$opt'.\n";
642 }
643  
644 if ($main::DEBUG >= 1) {
645 use Data::Dumper;
646 $log->log(LOG_DEBUG, 'Real Options: '. Dumper(\%opts));
647 }
648  
649 return %opts;
650 }
651  
652 1;
653  
654 __END__