blib/lib/Env/Modulecmd.pm

filter: Statementstmt 79.2% 61 / 77 Branchbran 28.5% 8 / 28 Conditioncond 34.7% 8 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 57.3% 86 / 150 CC 26 SCAR 31.1
CC
26
Cov
60%
_modulecmd
35.3
import
26.1
BEGIN
7.0
CRAP 22.5
↑ Summary
1 # $Id: Modulecmd.pm,v 5.3 2014/08/18 16:56:11 ronisaac Exp $
2  
3 # Copyright (c) 2001-2014, Morgan Stanley.
4 # Distributed under the terms of the GNU General Public License.
5 # Please see the copyright notice at the end of this file for more information.
6  
7 package Env::Modulecmd;
8  
9 1 ▶BEGIN {
Subroutine BEGIN: called CC=5 SCAR=19.4
10 # defaults: if Env::Modulecmd is built using perl5.005 or later, the
11 # magic strings below are replaced with values supplied to 'make' at
12 # build time
13  
14 1 my $modulecmd = '';
15 1 my $modulepath = '';
16  
17 1 ▶ $ENV{PERL_MODULECMD} ||= $modulecmd unless ($modulecmd =~ /^\@\@/);
Branch: 1 branch
BranchTrueFalseTotal
unless $modulecmd =~ /^\@\@/ 1 0 1
Condition33%1/3
$ENV{"PERL_MODULECMD"} ||= $modulecmd
l!l&&r!l&&!r
001
MC/DC0%0/2
$ENV{"PERL_MODULECMD"} ||= $modulecmd
$ENV{"PERL_MODULECMD"} $modulecmd
Truth table33%1/3
A ||= B
A= $ENV{"PERL_MODULECMD"}B= $modulecmd
ABresult
1X1
011
000
18 1 ▶ $ENV{MODULEPATH} ||= $modulepath unless ($modulepath =~ /^\@\@/);
Branch: 1 branch
BranchTrueFalseTotal
unless $modulepath =~ /^\@\@/ 1 0 1
Condition33%1/3
$ENV{"MODULEPATH"} ||= $modulepath
l!l&&r!l&&!r
001
MC/DC0%0/2
$ENV{"MODULEPATH"} ||= $modulepath
$ENV{"MODULEPATH"} $modulepath
Truth table33%1/3
A ||= B
A= $ENV{"MODULEPATH"}B= $modulepath
ABresult
1X1
011
000
19 }
20  
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use vars qw($VERSION $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 $VERSION = 1.3;
28  
29 1 ▶my $modulecmd = $ENV{'PERL_MODULECMD'} || 'modulecmd';
Condition50%1/2
$ENV{'PERL_MODULECMD'} || 'modulecmd'
l!l
01
MC/DC0%0/1
$ENV{'PERL_MODULECMD'} || 'modulecmd'
$ENV{'PERL_MODULECMD'}
Truth table50%1/2
A
A= $ENV{'PERL_MODULECMD'}
Aresult
11
00
30  
31 1 ▶sub import {
Subroutine import: called CC=6 SCAR=32.6
32 1 my @args = @_;
33 1 shift @args;
34  
35 # import just dispatches commands to _modulecmd
36  
37 1 foreach my $arg (@args) {
38 0 ▶ if (ref ($arg) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq 'HASH') { } 0 0 0
39 0 my %hash = %{$arg};
40 0 foreach my $key (keys %hash) {
41 0 my $val = $hash{$key};
42 0 ▶ if (ref ($val) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 0 0 0
43 0 _modulecmd ($key, $_) for @{$val};
44 } else {
45 0 _modulecmd ($key, $val);
46 }
47 }
48 } else {
49 0 _modulecmd ('load', $arg);
50 }
51 }
52 }
53  
54 1 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=6.9
55 1 my @modules = @_;
56  
57 # AUTOLOAD, like import, calls _modulecmd with the requested function
58  
59 1 my $fun = $AUTOLOAD;
60 1 $fun =~ s/^.*:://;
61  
62 1 _modulecmd ($fun, $_) for @modules;
63 }
64  
65 1 ▶sub _indent {
Subroutine _indent: called CC=1 SCAR=0.0
66 1 my ($str) = @_;
67  
68 1 $str =~ s/\n$//;
69 1 $str =~ s/\n/\n -> /g;
70 1 $str = " -> $str\n";
71  
72 1 return ($str);
73 }
74  
75 1 ▶sub _modulecmd {
Subroutine _modulecmd: called CC=16 SCAR=35.6
76 1 my ($fun, $module) = @_;
77  
78 # here's where the actual work gets done. we call modulecmd and
79 # capture its standard output and standard error. we used to use
80 # IPC::Open3, but switched to temp files to resolve a potential hang
81 # on MS Windows 7 and up.
82  
83 1 my @cmd = ($modulecmd, "perl", $fun, $module);
84  
85 # 1. save stdout and stderr and redirect them to (unlinked) temp
86 # files
87  
88 1 my $SAVE_OUT = IO::File->new (">&" . STDOUT->fileno);
89 1 my $SAVE_ERR = IO::File->new (">&" . STDERR->fileno);
90  
91 1 my $OUT = IO::File->new_tmpfile;
92 1 my $ERR = IO::File->new_tmpfile;
93  
94 1 STDOUT->fdopen ($OUT, "w");
95 1 STDERR->fdopen ($ERR, "w");
96  
97 1 STDOUT->autoflush (1);
98 1 STDERR->autoflush (1);
99  
100 # 2. call modulecmd
101  
102 1 my $retcode = system (@cmd);
103 1 my $syserr = $!;
104  
105 # 3. read the output from the temp files and restore stdout and
106 # stderr
107  
108 1 STDOUT->fdopen ($SAVE_OUT, "w");
109 1 STDERR->fdopen ($SAVE_ERR, "w");
110  
111 1 $OUT->seek (0, 0);
112 1 $ERR->seek (0, 0);
113  
114 1 my $out;
115 my $err;
116 1 my $buf;
117  
118 1 ▶ $out .= $buf while read $OUT, $buf, 1024;
Branch: 1 branch
BranchTrueFalseTotal
read $OUT, $buf, 1024 and do { $out .= $buf } 0 1 1
119 1 ▶ $err .= $buf while read $ERR, $buf, 1024;
Branch: 1 branch
BranchTrueFalseTotal
read $ERR, $buf, 1024 and do { $err .= $buf } 0 1 1
120  
121 1 undef $OUT;
122 1 undef $ERR;
123  
124 # ok, how did we do?
125  
126 1 ▶ if ($err || $retcode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err or $retcode) 1 0 1
Condition33%1/3
$err or $retcode
l!l&&r!l&&!r
010
MC/DC0%0/2
$err or $retcode
$err $retcode
Truth table33%1/3
A or B
A= $errB= $retcode
ABresult
1X1
011
000
127 1 my $croak = 0;
128  
129 # attempt to guess whether the stderr output is a real error
130 # generated by modulecmd, or just an informational message output
131 # by the module itself. error messages from modulecmd (like
132 # "Couldn't find modulefile ... in MODULEPATH") fall into two
133 # categories: they either (a) start with "ERROR:", or (b) start
134 # and end with a row of dashes, and contain the message shown
135 # below. (note that "occurred" is misspelled as "occured" in the
136 # modulecmd source.)
137  
138 1 ▶ my $error_from_modulecmd =
Condition33%3/9
$err =~ /^ERROR:/ || $err =~ /^-----/ && $err =~ /-----\s*$/ && $err =~ /An error occur*ed while processing your module command/
l!l&&r!l&&!r
001
$err =~ /^-----/ and $err =~ /-----\s*$/ and $err =~ /An error occur*ed while processing your module command/
!ll&&!rl&&r
100
$err =~ /^-----/ and $err =~ /-----\s*$/
!ll&&!rl&&r
100
MC/DC0%0/4
$err =~ /^ERROR:/ || $err =~ /^-----/ && $err =~ /-----\s*$/ && $err =~ /An error occur*ed while processing your module command/
$err =~ /^ERROR:/ $err =~ /^-----/ $err =~ /-----\s*$/ $err =~ /An error occur*ed while processing your module command/
Truth table20%1/5
A || B and C and D
A= $err =~ /^ERROR:/B= $err =~ /^-----/C= $err =~ /-----\s*$/D= $err =~ /An error occur*ed while processing your module command/
ABCDresult
1XXX1
01111
00XX0
010X0
01100
139 (($err =~ /^ERROR:/) or
140 ($err =~ /^-----/ and $err =~ /-----\s*$/ and
141 $err =~ /An error occur*ed while processing your module command/));
142  
143 1 ▶ $croak = 1
Branch: 1 branch
BranchTrueFalseTotal
if $error_from_modulecmd 0 1 1
144 if $error_from_modulecmd;
145  
146 # now check for failure from the system() call. start by checking
147 # for a non-zero return code, which works on some versions of
148 # MS Windows and most other systems.
149 #
150 # on MS Windows, shell commands are always wrapped with cmd.exe,
151 # and some versions of cmd.exe will always exit with a zero return
152 # code. Windows 7 seems to pass through the return code of the
153 # last command, but Windows XP and earlier can be problematic.
154 #
155 # modulecmd itself will hardly ever exit with a non-zero return
156 # code. however, there are two cases where it will: (a) invalid
157 # syntax, like "modulecmd no-such-shell list"; and (b) "modulecmd
158 # perl load /no/such/directory". in these cases, we hopefully
159 # already determined (using the pattern above) that this is an
160 # error message from modulecmd. if not, we assume it's a message
161 # about a failure to call modulecmd in the first place.
162  
163 1 ▶ if ($retcode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retcode) 1 0 1
164 1 $croak = 1;
165  
166 1 ▶ unless ($error_from_modulecmd) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($error_from_modulecmd) 1 0 1
167 1 ▶ croak
Condition33%1/3
$err || $syserr
l!l&&r!l&&!r
010
MC/DC0%0/2
$err || $syserr
$err $syserr
Truth table33%1/3
A || B
A= $errB= $syserr
ABresult
1X1
011
000
168 ("Unable to execute '@cmd':\n" .
169 _indent ($err || $syserr) .
170 "Error loading module $module");
171 }
172 }
173  
174 # now, if $croak is set, it's a fatal error, so croak on it.
175 # otherwise, issue a warning, but only if -w is in effect.
176  
177 0 ▶ if ($croak) {
Branch: 1 branch
BranchTrueFalseTotal
if ($croak) { } 0 0 0
178 0 croak
179 ("Errors from '@cmd':\n" .
180 _indent ($err) .
181 "Error loading module $module");
182 } else {
183 0 ▶ carp
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
184 ("Messages from '@cmd':\n" .
185 _indent ($err) .
186 "Possible error loading module $module")
187 if $^W;
188 }
189 }
190  
191 # if we got here, then the command didn't fail. if it did generate
192 # output, then we have something to eval.
193  
194 0 ▶ if ($out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out) 0 0 0
195  
196 # what if we try to eval something that's not valid perl? in this
197 # case, eval will die, with a message indicating what went wrong.
198 # we want to catch this and nicely print out the error.
199  
200 0 eval $out;
201  
202 0 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
203 ("'@cmd' generated output:\n" .
204 _indent ($out) .
205 "Error evaluating:\n" .
206 _indent ($@) .
207 "Error loading module $module")
208 if $@;
209 }
210 }
211  
212 1 1;
213  
214 __END__
↑ Summary