File Coverage

blib/lib/Perl/Configure.pm
Criterion Covered Total %
statement 32 57 56.1
branch 3 14 21.4
condition n/a
subroutine 9 10 90.0
pod 0 3 0.0
total 44 84 52.3


line stmt bran cond sub pod time code
1             package Perl::Configure;
2 1     1   2078 use strict;
  1         2  
  1         47  
3 1     1   6 use warnings;
  1         2  
  1         35  
4              
5 1     1   1296 use Expect;
  1         51751  
  1         82  
6 1     1   15 use Perl::Configure::Questions;
  1         3  
  1         20  
7 1     1   1440 use Data::Dumper;
  1         9081  
  1         86  
8 1     1   8 use YAML qw(Dump LoadFile);
  1         3  
  1         51  
9 1     1   1345 use Log::Log4perl qw(:easy);
  1         65036  
  1         9  
10              
11             our $VERSION = '0.08';
12              
13             ###########################################
14             sub new {
15             ###########################################
16 2     2 0 579 my($class, @options) = @_;
17              
18 2         17 my $self = {
19             exp => Expect->new(),
20             yml_file => undef,
21             timeout => 600,
22             questions => Perl::Configure::Questions->new(),
23             @options
24             };
25              
26 2         17 $self->{bk} = $self->{questions}->by_key(),
27             $self->{bp} = $self->{questions}->by_pattern(),
28              
29             $self->{exp}->raw_pty(1);
30              
31 2         73 bless $self, $class;
32              
33 2 50       12 if(defined $self->{yml_file}) {
34 0         0 my $data = LoadFile $self->{yml_file};
35 0         0 $self->define(%$data);
36             }
37              
38 2         7 return $self;
39             }
40              
41             ###########################################
42             sub define {
43             ###########################################
44 2     2 0 215 my($self, %args) = @_;
45              
46 2         17 for my $key (keys %args) {
47 3 100       10 if(! exists $self->{bk}->{$key}) {
48 1         7 LOGDIE "Unknown token: '$key'. Must be one of ",
49             join(", ", sort $self->{questions}->tokens()), ".";
50             }
51 2         9 $self->{define}->{$key} = $args{$key};
52             }
53             }
54              
55             ###########################################
56             sub run {
57             ###########################################
58 0     0 0   my($self) = @_;
59              
60 0           unlink "Policy.sh";
61              
62 0           my @patterns = $self->{questions}->patterns();
63              
64 0 0         $self->{exp}->spawn("./Configure")
65             or LOGDIE "Cannot spawn: $!\n";
66              
67 0           { my ($matched_pattern_position, $error, $match,
  0            
68             $before_match, $after_match) =
69             $self->{exp}->expect(
70             $self->{timeout},
71 0           map { -re => $_ } @patterns);
72              
73 0 0         if(defined $match) {
74              
75 0           DEBUG "Match: [$match]";
76              
77             my $token = $self->{bp}->{
78 0           $patterns[$matched_pattern_position-1]}->[0];
79              
80 0 0         if(! defined $token) {
81 0           LOGDIE "Internal error: match($match) but no token: ",
82             "pos=$matched_pattern_position ",
83             "error=$error ",
84             "before=$before_match ",
85             "after=$after_match ",
86             ;
87             }
88              
89 0           my $response = "";
90 0           DEBUG "Entry for token '$token': ", Dumper($self->{bk}->{$token});
91              
92             # Grab default answer if existent
93 0           my $override = $self->{bk}->{$token}->[2];
94 0 0         $response = $override if defined $override;
95              
96 0 0         if(exists $self->{define}->{$token}) {
97 0           $response = $self->{define}->{$token};
98 0           INFO "Overriding with [$response]";
99             } else {
100 0           INFO "Filling in [DEFAULT=$response]";
101             }
102              
103 0           DEBUG "Response: [$response]";
104 0           $self->{exp}->send("$response\n");
105              
106 0           redo;
107             }
108             }
109             }
110              
111             1;
112              
113             __END__