| 1 | package Shell::EnvImporter; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 1 | our @ISA = qw(); | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 1 | our $VERSION = '1.06'; | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 1 | ▶ | use Shell::EnvImporter::Shell; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | ▶ | use Class::MethodMaker 2.0 [ | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 1 | new => [qw(-init new)], | |||||||||||||||||||
| 15 | scalar => [qw( | ||||||||||||||||||||
| 16 | debuglevel | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | command | ||||||||||||||||||||
| 19 | file | ||||||||||||||||||||
| 20 | shell | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | auto_run | ||||||||||||||||||||
| 23 | auto_import | ||||||||||||||||||||
| 24 | import_modified | ||||||||||||||||||||
| 25 | import_added | ||||||||||||||||||||
| 26 | import_removed | ||||||||||||||||||||
| 27 | import_filter | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | shellobj | ||||||||||||||||||||
| 30 | result | ||||||||||||||||||||
| 31 | )], | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 1 | ]; | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 1 | ▶ | use constant DEFAULT_SHELL => 'sh'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 1 | ▶ | use constant DEFAULTS => ( | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 41 | auto_run => 1, | ||||||||||||||||||||
| 42 | auto_import => 1, | ||||||||||||||||||||
| 43 | import_modified => 1, | ||||||||||||||||||||
| 44 | import_added => 1, | ||||||||||||||||||||
| 45 | import_removed => 0, | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | shell => DEFAULT_SHELL, | ||||||||||||||||||||
| 48 | 1 | ); | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | ########## | ||||||||||||||||||||
| 52 | 15 | ▶ | sub init { | ||||||||||||||||||
|
Subroutine
init: called CC=9 SCAR=22.4
Pod
undocumented
| |||||||||||||||||||||
| 53 | ########## | ||||||||||||||||||||
| 54 | 15 | my $self = shift; | |||||||||||||||||||
| 55 | 15 | my %args = @_; | |||||||||||||||||||
| 56 | 15 | my %defaults = (DEFAULTS); | |||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | # Set supplied fields with defaults | ||||||||||||||||||||
| 59 | 15 | my @fields = (keys %args, keys %defaults); | |||||||||||||||||||
| 60 | 15 | my %fields; @fields{@fields} = (1) x @fields; | |||||||||||||||||||
| 61 | 15 | @fields = keys %fields; | |||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 15 | foreach my $field (@fields) { | |||||||||||||||||||
| 64 | 105 | ▶ | if ($self->can($field)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | 105 | my $curval = $self->$field(); | |||||||||||||||||||
| 66 | 105 | ▶ | my $arg = exists($args{$field}) ? $args{$field} : $defaults{$field}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | 105 | ▶ | if (ref($curval) =~ /ARRAY/) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 68 | 0 | $self->$field(@$arg); | |||||||||||||||||||
| 69 | } elsif (ref($curval) =~ /HASH/) { | ||||||||||||||||||||
| 70 | 0 | $self->$field(%$arg); | |||||||||||||||||||
| 71 | } else { | ||||||||||||||||||||
| 72 | 105 | $self->$field($arg); | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | # Create a shell object | ||||||||||||||||||||
| 78 | 15 | $self->dprint(1, "Creating shell object\n"); | |||||||||||||||||||
| 79 | 15 | ▶ | my $shellobj = $self->_get_shell() or return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | # If file is supplied, command is to source the file. | ||||||||||||||||||||
| 82 | 15 | ▶ | if (defined($self->file)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | 3 | $self->dprint(1, "Setting command to source '", $self->file, "'\n"); | |||||||||||||||||||
| 84 | 3 | $self->command($shellobj->sourcecommand($self->file)); | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | # Run the command if requested. | ||||||||||||||||||||
| 88 | 15 | ▶ | $self->run() if ($self->auto_run); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | ######### | ||||||||||||||||||||
| 95 | 15 | ▶ | sub run { | ||||||||||||||||||
|
Subroutine
run: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||
| 96 | ######### | ||||||||||||||||||||
| 97 | 15 | my $self = shift; | |||||||||||||||||||
| 98 | 15 | ▶ | my $command = shift || $self->command; | ||||||||||||||||||
|
Condition66%2/3
shift() || $self->command
MC/DC0%0/2
shift() || $self->command
shift()
$self->command
Truth table66%2/3
A || B
A= shift()B= $self->command
| |||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 15 | $self->dprint(1, "Executing command\n"); | |||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 15 | ▶ | unless (defined($command)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | 0 | $@ = "Can't run without a command"; | |||||||||||||||||||
| 104 | 0 | return undef; | |||||||||||||||||||
| 105 | }; | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 15 | my $rv = $self->shellobj->run( | |||||||||||||||||||
| 108 | command => $command, | ||||||||||||||||||||
| 109 | ); | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 14 | $self->result($rv); | |||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 14 | ▶ | if ($self->auto_import) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | 10 | ▶ | if ($self->import_filter) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | 1 | $self->env_import_filtered(); | |||||||||||||||||||
| 116 | } else { | ||||||||||||||||||||
| 117 | 9 | $self->env_import; | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 14 | return $rv; | |||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | ################ | ||||||||||||||||||||
| 128 | 12 | ▶ | sub env_import { | ||||||||||||||||||
|
Subroutine
env_import: called CC=7 SCAR=19.9
Pod
documented
| |||||||||||||||||||||
| 129 | ################ | ||||||||||||||||||||
| 130 | 12 | my $self = shift; | |||||||||||||||||||
| 131 | 12 | my @vars; | |||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 12 | $self->dprint(1, "Performing policy import\n"); | |||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 12 | ▶ | unless (defined($self->result)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 136 | 0 | $@ = "Can't import before a successful run"; | |||||||||||||||||||
| 137 | 0 | return undef; | |||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 12 | ▶ | if (ref($_[0])) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 141 | 0 | @vars = @{$_[0]}; | |||||||||||||||||||
| 142 | } elsif (@_) { | ||||||||||||||||||||
| 143 | 2 | @vars = @_; | |||||||||||||||||||
| 144 | } else { | ||||||||||||||||||||
| 145 | 10 | @vars = $self->result->changed_keys; | |||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 12 | my %import; @import{@vars} = (1) x @vars; | |||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 12 | foreach my $var ($self->result->changed_keys) { | |||||||||||||||||||
| 151 | 35 | ▶ | next unless ($import{$var}); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 152 | 34 | my $type = $self->result->changed_index($var)->type; | |||||||||||||||||||
| 153 | 34 | my $newval = $self->result->changed_index($var)->value; | |||||||||||||||||||
| 154 | 34 | my $fn = "import_${type}"; | |||||||||||||||||||
| 155 | 34 | ▶ | next unless ($self->$fn()); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 21 | $self->dprint(2, "Importing $type var $var=$newval\n"); | |||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | 21 | $self->_import_var($var, $newval, $type); | |||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | } | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | 12 | return $self->result; | |||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | } | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | ######################### | ||||||||||||||||||||
| 169 | 2 | ▶ | sub env_import_filtered { | ||||||||||||||||||
|
Subroutine
env_import_filtered: called CC=6 SCAR=19.2
Pod
documented
| |||||||||||||||||||||
| 170 | ######################### | ||||||||||||||||||||
| 171 | 2 | my $self = shift; | |||||||||||||||||||
| 172 | 2 | ▶ | my $filter = shift || $self->import_filter; | ||||||||||||||||||
|
Condition66%2/3
shift() || $self->import_filter
MC/DC0%0/2
shift() || $self->import_filter
shift()
$self->import_filter
Truth table66%2/3
A || B
A= shift()B= $self->import_filter
| |||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 2 | $self->dprint(1, "Performing filtered import\n"); | |||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | 2 | ▶ | unless (ref($filter)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 177 | 0 | $@ = "Can't do filtered import without a filter"; | |||||||||||||||||||
| 178 | 0 | return undef; | |||||||||||||||||||
| 179 | } | ||||||||||||||||||||
| 180 | 2 | ▶ | unless (defined($self->result)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 181 | 0 | $@ = "Can't import before a successful run"; | |||||||||||||||||||
| 182 | 0 | return undef; | |||||||||||||||||||
| 183 | } | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | 2 | foreach my $var ($self->result->changed_keys) { | |||||||||||||||||||
| 186 | 6 | my $type = $self->result->changed_index($var)->type; | |||||||||||||||||||
| 187 | 6 | my $newval = $self->result->changed_index($var)->value; | |||||||||||||||||||
| 188 | 6 | ▶ | next unless ($filter->($var, $newval, $type)); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | 3 | $self->dprint(2, "Importing $type var $var=$newval\n"); | |||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | 3 | $self->_import_var($var, $newval, $type); | |||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | } | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | 2 | return $self->result; | |||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | } | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | ################# | ||||||||||||||||||||
| 204 | 0 | ▶ | sub restore_env { | ||||||||||||||||||
|
Subroutine
restore_env: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 205 | ################# | ||||||||||||||||||||
| 206 | 0 | my $self = shift; | |||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | 0 | $self->dprint(1, "Restoring environment\n"); | |||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | 0 | ▶ | unless (defined($self->result)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 211 | 0 | $@ = "Can't restore before a successful run"; | |||||||||||||||||||
| 212 | 0 | return undef; | |||||||||||||||||||
| 213 | } | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | # Delete all environment variables | ||||||||||||||||||||
| 216 | 0 | map(delete($ENV{$_}), keys %ENV); | |||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | # Restore them from the result backup | ||||||||||||||||||||
| 219 | 0 | @ENV{$self->result->start_env_keys} = $self->result->start_env_values; | |||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | 0 | return 1; | |||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | } | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | ############ | ||||||||||||||||||||
| 228 | 86 | ▶ | sub dprint { | ||||||||||||||||||
|
Subroutine
dprint: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 229 | ############ | ||||||||||||||||||||
| 230 | 86 | my $self = shift; | |||||||||||||||||||
| 231 | 86 | my $level = shift; | |||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | 86 | my($package, $filename, $line) = caller; | |||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | 86 | ▶ | print STDERR "-" x $level, " $package:$line : ", @_ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 236 | if ($self->debuglevel >= $level); | ||||||||||||||||||||
| 237 | |||||||||||||||||||||
| 238 | } | ||||||||||||||||||||
| 239 | |||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | |||||||||||||||||||||
| 242 | |||||||||||||||||||||
| 243 | ############################################################################## | ||||||||||||||||||||
| 244 | ########################### Private subroutines ############################# | ||||||||||||||||||||
| 245 | ############################################################################## | ||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | |||||||||||||||||||||
| 249 | ################ | ||||||||||||||||||||
| 250 | 15 | ▶ | sub _get_shell { | ||||||||||||||||||
|
Subroutine
_get_shell: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 251 | ################ | ||||||||||||||||||||
| 252 | 15 | my $self = shift; | |||||||||||||||||||
| 253 | 15 | my $shellname = $self->shell; | |||||||||||||||||||
| 254 | |||||||||||||||||||||
| 255 | 15 | my $shellclass = join("::", ref($self), 'Shell', $shellname); | |||||||||||||||||||
| 256 | |||||||||||||||||||||
| 257 | 15 | $self->dprint(2, "Shell class: $shellclass\n"); | |||||||||||||||||||
| 258 | |||||||||||||||||||||
| 259 | 1 | ▶ | eval "use $shellclass;"; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 260 | 15 | ▶ | return undef if ($@); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 261 | |||||||||||||||||||||
| 262 | 15 | my $shellobj; | |||||||||||||||||||
| 263 | 15 | ▶ | unless ($shellobj = $shellclass->new(debuglevel => $self->debuglevel)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 264 | 0 | $@ = "Couldn't create shell object"; | |||||||||||||||||||
| 265 | 0 | return undef; | |||||||||||||||||||
| 266 | } | ||||||||||||||||||||
| 267 | |||||||||||||||||||||
| 268 | 15 | $self->shellobj($shellobj); | |||||||||||||||||||
| 269 | |||||||||||||||||||||
| 270 | 15 | return $shellobj; | |||||||||||||||||||
| 271 | |||||||||||||||||||||
| 272 | } | ||||||||||||||||||||
| 273 | |||||||||||||||||||||
| 274 | |||||||||||||||||||||
| 275 | |||||||||||||||||||||
| 276 | |||||||||||||||||||||
| 277 | ################# | ||||||||||||||||||||
| 278 | 24 | ▶ | sub _import_var { | ||||||||||||||||||
|
Subroutine
_import_var: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 279 | ################# | ||||||||||||||||||||
| 280 | 24 | my $self = shift; | |||||||||||||||||||
| 281 | 24 | my $var = shift; | |||||||||||||||||||
| 282 | 24 | my $newval = shift; | |||||||||||||||||||
| 283 | 24 | my $type = shift; | |||||||||||||||||||
| 284 | |||||||||||||||||||||
| 285 | 24 | ▶ | if ($type eq 'removed') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 286 | 4 | delete($ENV{$var}); | |||||||||||||||||||
| 287 | } else { | ||||||||||||||||||||
| 288 | 20 | $ENV{$var} = $newval; | |||||||||||||||||||
| 289 | } | ||||||||||||||||||||
| 290 | 24 | $self->result->imported_push($var); | |||||||||||||||||||
| 291 | } | ||||||||||||||||||||
| 292 | |||||||||||||||||||||
| 293 | |||||||||||||||||||||
| 294 | |||||||||||||||||||||
| 295 | |||||||||||||||||||||
| 296 | |||||||||||||||||||||
| 297 | |||||||||||||||||||||
| 298 | 1 | 1; | |||||||||||||||||||
| 299 | |||||||||||||||||||||
| 300 | __END__ | ||||||||||||||||||||