Subversion Repositories camp_sysinfo_client_3

Rev

Rev 114 | Rev 124 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 114 Rev 115
Line 30... Line 30...
30
our @EXPORT = qw( $clientName $serialNumber $hostname @moduleDirs @scriptDirs 
30
our @EXPORT = qw( $clientName $serialNumber $hostname @moduleDirs @scriptDirs 
31
                  $transports $sysinfo3 %sendTypes $binDir $moduleDir
31
                  $transports $sysinfo3 %sendTypes $binDir $moduleDir
32
                  $scriptDir $confDir $binName $confName
32
                  $scriptDir $confDir $binName $confName
33
                  &showConf &transportsToConfig &getAnswer &yesno  
33
                  &showConf &transportsToConfig &getAnswer &yesno  
34
                  &writeConfig &processParameters $TESTING &checkDirectoryExists
34
                  &writeConfig &processParameters $TESTING &checkDirectoryExists
35
                  &runCommand &setDryRun &enableModules &makeConfig &findConf &deepmerge
35
                  &runCommand &setDryRun &enableModules &makeConfig &findConf
36
                );
36
                );
37
 
37
 
38
our $TESTING = 0;
38
our $TESTING = 0;
39
our $dryRun;
39
our $dryRun;
40
our $clientName = '';
40
our $clientName = '';
Line 101... Line 101...
101
                                      ]
101
                                      ]
102
                                   }
102
                                   }
103
                );
103
                );
104
 
104
 
105
 
105
 
106
# recursively merge multiple hashes/arrays to an arbitrary depth
-
 
107
# stolen from
-
 
108
# https://stackoverflow.com/questions/2767477/how-can-i-merge-several-hashes-into-one-hash-in-perl
-
 
109
sub deepmerge {
-
 
110
    my (@structs) = @_;
-
 
111
    my $new;
-
 
112
 
-
 
113
    # filter out non-existant structs
-
 
114
    @structs = grep {defined($_)} @structs;
-
 
115
 
-
 
116
    my $ref = ref($structs[0]);
-
 
117
    if (not all(map {ref($_) eq $ref} @structs)) { 
-
 
118
        warn("deepmerge: all structs are not $ref\n");
-
 
119
    } 
-
 
120
 
-
 
121
    my @tomerge = grep {ref($_) eq $ref} @structs;
-
 
122
    return qr/$tomerge[0]/ if scalar(@tomerge) == 1 and $ref eq 'Regexp';
-
 
123
    return $tomerge[0] if scalar(@tomerge) == 1;
-
 
124
 
-
 
125
    if ($ref eq '') { 
-
 
126
        $new = pop(@tomerge); # prefer farthest right
-
 
127
    } elsif ($ref eq 'Regexp') { 
-
 
128
        $new = qr/$tomerge[$#tomerge]/;
-
 
129
    } elsif ($ref eq 'ARRAY') { 
-
 
130
        $new = [];
-
 
131
        for my $i (0 .. max(map {scalar(@$_) - 1} @tomerge)) { 
-
 
132
            $new->[$i] = deepmerge(map {$_->[$i]} @tomerge);
-
 
133
        }
-
 
134
    } elsif ($ref eq 'HASH') { 
-
 
135
        $new = {};
-
 
136
        for my $key (uniq(map {keys %$_} @tomerge)) { 
-
 
137
            $new->{$key} = deepmerge(map {$_->{$key}} @tomerge);
-
 
138
        }
-
 
139
    } else {
-
 
140
        # ignore all other structures...
-
 
141
        $new = '';
-
 
142
    }
-
 
143
 
-
 
144
    return $new;
-
 
145
}
-
 
146
 
-
 
147
sub enableModules {
106
sub enableModules {
148
   my $moduleDir = shift;
107
   my $moduleDir = shift;
149
   my %modules;
108
   my %modules;
150
   if ( opendir( my $dh, "$moduleDir" ) ) {
109
   if ( opendir( my $dh, "$moduleDir" ) ) {
151
      %modules = map{ $_ => { 'enabled' => -x "$moduleDir/$_" } } 
110
      %modules = map{ $_ => { 'enabled' => -x "$moduleDir/$_" } }