blib/lib/TSM.pm

filter: Statementstmt 27.5% 30 / 109 Branchbran 16.6% 8 / 48 Conditioncond 27.7% 5 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 40.0% 4 / 10 Podpod 57.1% 4 / 7 totaltotal 25.0% 51 / 204 CC 38 SCAR 43.7
CC
38
Cov
25%
new
139.2
dsmadmc
72.0
get_columnlabels
20.0
CRAP 78.8
↑ Summary
1 package TSM;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 require Exporter;
8 1 require AutoLoader;
9  
10 1 @ISA = qw(Exporter AutoLoader);
11 # Items to export into callers namespace by default. Note: do not export
12 # names by default without a very good reason. Use EXPORT_OK instead.
13 # Do not simply export all your public functions/methods/constants.
14 1 @EXPORT = qw(
15
16 );
17 1 @EXPORT_OK = qw(
18  
19 );
20 1 $VERSION = '0.60';
21  
22  
23 # Preloaded methods go here.
24 #
25 # Constructor of the TSM instance. This object will be used to authenticate
26 # at the TSM server
27 #
28 1 ▶sub new {
Subroutine new: called CC=21 SCAR=49.4
Pod documented
29 1 my $self = shift;
30 1 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
31 1 my $instance = {};
32 1 my $rcfile = undef;
33 #
34 # Set $rcfile to either .tsmrc or ~/.tsmrc depending on which comes first.
35 #
36 1 ▶ $rcfile = "$ENV{HOME}/.tsmrc" if (exists $ENV{HOME} && -e "$ENV{HOME}/.tsmrc");
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENV{"HOME"} and -e qq[$ENV{"HOME"}/.tsmrc] 0 1 1
Condition33%1/3
exists $ENV{"HOME"} and -e qq[$ENV{"HOME"}/.tsmrc]
!ll&&!rl&&r
010
MC/DC0%0/2
exists $ENV{"HOME"} and -e qq[$ENV{"HOME"}/.tsmrc]
exists $ENV{"HOME"} -e qq[$ENV{"HOME"}/.tsmrc]
Truth table33%1/3
A and B
A= exists $ENV{"HOME"}B= -e qq[$ENV{"HOME"}/.tsmrc]
ABresult
0X0
100
111
37 1 ▶ $rcfile = ".tsmrc" if ( -e ".tsmrc");
Branch: 1 branch
BranchTrueFalseTotal
if -e ".tsmrc" 0 1 1
38 #
39 # Populate the parameter hash with default values and overwrite them
40 # with the parameter of the caller, if any.
41 #
42 1 my %parameter = (id => undef, pa => undef, file => $rcfile, @_);
43 #
44 # Read the ID and password from the parameters, if provided.
45 #
46 1 ▶ $instance->{id} = $parameter{id} if ($parameter{id});
Branch: 1 branch
BranchTrueFalseTotal
if $parameter{"id"} 0 1 1
47 1 ▶ $instance->{pa} = $parameter{pa} if ($parameter{pa});
Branch: 1 branch
BranchTrueFalseTotal
if $parameter{"pa"} 0 1 1
48 #
49 # Read the ID and password from $rcfile, if ID has not been
50 # provided yet.
51 #
52 1 ▶ if (! defined($instance->{id}) and $parameter{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $instance->{"id"} and $parameter{"file"}) 0 1 1
Condition33%1/3
not defined $instance->{"id"} and $parameter{"file"}
!ll&&!rl&&r
010
MC/DC0%0/2
not defined $instance->{"id"} and $parameter{"file"}
not defined $instance->{"id"} $parameter{"file"}
Truth table33%1/3
not(A) and B
A= not defined $instance->{"id"}B= $parameter{"file"}
ABresult
0X0
100
111
53 0 ▶ open (TSMRC, "$parameter{file}") or croak "Error opening $parameter{file} to read ID and password: $!";
Branch: 1 branch
BranchTrueFalseTotal
open TSMRC, qq[$parameter{"file"}] or croak(qq[Error opening $parameter{"file"} to read ID and password: $!]) 0 0 0
54 0 ▶ while (<TSMRC>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline TSMRC)) 0 0 0
55 0 ▶ chomp($instance->{id} = $1) if (m/^ID\s+(.*)$/);
Branch: 1 branch
BranchTrueFalseTotal
if /^ID\s+(.*)$/ 0 0 0
56 0 ▶ chomp($instance->{pa} = $1) if (m/^PA\s+(.*)$/);
Branch: 1 branch
BranchTrueFalseTotal
if /^PA\s+(.*)$/ 0 0 0
57 };
58 0 ▶ close (TSMRC) or croak "Error closing $parameter{file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
close TSMRC or croak(qq[Error closing $parameter{"file"}: $!]) 0 0 0
59 };
60 #
61 # Read ID and/or password from STDIN if not provided yet.
62 #
63 1 ▶ unless ($instance->{id} and $instance->{pa}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($instance->{"id"} and $instance->{"pa"}) 1 0 1
Condition33%1/3
$instance->{"id"} and $instance->{"pa"}
!ll&&!rl&&r
100
MC/DC0%0/2
$instance->{"id"} and $instance->{"pa"}
$instance->{"id"} $instance->{"pa"}
Truth table33%1/3
A and B
A= $instance->{"id"}B= $instance->{"pa"}
ABresult
0X0
100
111
64 1 ▶ if ( -t STDIN ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-t STDIN) 0 1 1
65 0 my $stty = `stty`;
66 0 ▶ unless($instance->{id}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($instance->{"id"}) 0 0 0
67 0 print "Enter your user id: ";
68 0 chomp($instance->{id}=<STDIN>);
69 };
70 0 ▶ unless($instance->{pa}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($instance->{"pa"}) 0 0 0
71 # Turn off echoing
72 0 `stty -echo`;
73 0 print "Enter the password for $instance->{id}: ";
74 0 chomp($instance->{pa}=<STDIN>);
75 # Turn on echoing
76 0 `stty echo`;
77 0 print "\n";
78 };
79 };
80 };
81 #
82 # Test if the ID and password could has been read from somewhere.
83 #
84 1 ▶ unless ($instance->{id} and $instance->{pa}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($instance->{"id"} and $instance->{"pa"}) 1 0 1
Condition33%1/3
$instance->{"id"} and $instance->{"pa"}
!ll&&!rl&&r
100
MC/DC0%0/2
$instance->{"id"} and $instance->{"pa"}
$instance->{"id"} $instance->{"pa"}
Truth table33%1/3
A and B
A= $instance->{"id"}B= $instance->{"pa"}
ABresult
0X0
100
111
85 1 croak "You have not provided a user ID and/or a password. Exiting";
86 };
87 #
88 # Bless the $instance into the $class package and return it to the caller.
89 #
90 0 bless($instance, $class);
91 0 return $instance;
92 }
93  
94 0 ▶sub dsmadmc (@) {
Subroutine dsmadmc: not called CC=8 SCAR=42.8
Pod undocumented
95 0 my $instance = shift;
96 0 my $options = shift;
97 0 my $command = join " ",@_;
98 0 my $start = 0;
99 0 my @output =();
100 #
101 # Open a session to the TSM server
102 #
103 0 ▶ open(DSMADMC, "dsmadmc -ID=$instance->{id} -PA=$instance->{pa} $options \"$command\" </dev/null |" )
Branch: 1 branch
BranchTrueFalseTotal
open DSMADMC, qq[dsmadmc -ID=$instance->{"id"} -PA=$instance->{"pa"} $options "$command" </dev/null |] or croak("Cannot open TSM session: $!\n") 0 0 0
104 or croak "Cannot open TSM session: $!\n";
105 #
106 # Remove status messages and concatenate the output to a string
107 #
108 0 ▶ while(<DSMADMC>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DSMADMC)) 0 0 0
109 {
110 0 ▶ last if (m/^(ANS800[12]I).*(\d+)\.$/);
Branch: 1 branch
BranchTrueFalseTotal
if /^(ANS800[12]I).*(\d+)\.$/ 0 0 0
111 0 ▶ if (m/^(ANS8000I)/)
Branch: 1 branch
BranchTrueFalseTotal
if (/^(ANS8000I)/) 0 0 0
112 {
113 0 $start = 1;
114 0 next;
115 };
116 0 ▶ if ($start and !/^\s*$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($start and not /^\s*$/) 0 0 0
Condition0%0/3
$start and not /^\s*$/
!ll&&!rl&&r
000
MC/DC0%0/2
$start and not /^\s*$/
$start not /^\s*$/
Truth table0%0/3
A and not(B)
A= $startB= not /^\s*$/
ABresult
0X0
100
111
117 {
118 0 chomp;
119 0 push(@output,$_);
120 };
121 };
122 0 ▶ close(DSMADMC) or carp "Cannot close TSM session: $!";
Branch: 1 branch
BranchTrueFalseTotal
close DSMADMC or carp("Cannot close TSM session: $!") 0 0 0
123 0 return @output;
124 };
125  
126 0 ▶sub select_single ($) {
Subroutine select_single: not called CC=2 SCAR=17.9
Pod documented
127 0 my $instance = shift;
128 0 my $command = shift;
129 #
130 # Extraxt the column labels from the command
131 #
132 0 my @columns = $instance->get_columnlabels($command);
133 #
134 # Extract the values from the select command
135 #
136 0 my @record = split(/\t/,($instance->dsmadmc("-TAB", "select $command"))[0]);
137 #
138 # Populate the hash with label/value as pairs
139 #
140 0 my $output =();
141 0 for my $i (0 .. $#columns)
142 {
143 0 $output->{"$columns[$i]"} = "$record[$i]";
144 };
145 #
146 # Return a pointer to this hash
147 #
148 0 return $output;
149 };
150  
151 0 ▶sub select($){
Subroutine select: not called CC=3 SCAR=24.8
Pod documented
152 0 my $instance = shift;
153 0 my $command = shift;
154 #
155 # Extraxt the column labels from the command
156 #
157 0 my @columns = $instance->get_columnlabels($command);
158 #
159 # Get the result from the TSM server
160 #
161 0 my @select = $instance->dsmadmc("-TAB", "select $command");
162 #
163 # Populate the array of hashes
164 #
165 0 my $output = ();
166 0 for my $i (0..$#select)
167 {
168 0 my @record = split(/\t/,@select[$i]);
169 0 for my $j (0 .. $#columns)
170 {
171 0 $output->[$i]{$columns[$j]} = "$record[$j]";
172 };
173 };
174 #
175 # Return a pointer to this array
176 #
177 0 return $output;
178 };
179  
180 sub select_hash (@)
181 0 ▶{
Subroutine select_hash: not called CC=3 SCAR=24.8
Pod documented
182 0 my $instance = shift;
183 0 my $hashref = shift;
184 0 my $command = shift;
185 #
186 # Extraxt the column labels from the command
187 #
188 0 my @columns = $instance->get_columnlabels($command);
189 #
190 # Get the result from the TSM server
191 #
192 0 my @select = $instance->dsmadmc("-TAB", "select $command");
193 #
194 # Populate the hash of hashes
195 #
196 0 for my $i (0..$#select)
197 {
198 0 my @record = split(/\t/,@select[$i]);
199 0 for my $j (1 .. $#columns)
200 {
201 0 $hashref->{$record[0]}{$columns[$j]} = "$record[$j]";
202 };
203 };
204 #
205 # Return the number of addedd/changed entries
206 #
207 0 return scalar @select;
208 };
209  
210  
211 sub get_columns($)
212 0 ▶{
Subroutine get_columns: not called CC=3 SCAR=24.8
Pod undocumented
213 0 my $instance = shift;
214 0 my $table_name = uc(shift);
215 #
216 # Get the result from the TSM server
217 #
218 0 my @select = $instance->dsmadmc("-TAB", "select colname, colno from columns where tabname='$table_name' order by colno");
219 #
220 # Populate the columns array
221 #
222 0 my @columns =();
223 0 foreach my $element (@select)
224 {
225 0 ▶ push (@columns, $1) if $element =~ (/^(\w+)\t\d{1,2}$/);
Branch: 1 branch
BranchTrueFalseTotal
if $element =~ /^(\w+)\t\d{1,2}$/ 0 0 0
226 };
227 #
228 # Return the columns array
229 #
230 0 return @columns;
231 };
232  
233 sub get_columnlabels($)
234 0 ▶{
Subroutine get_columnlabels: not called CC=4 SCAR=30.0
Pod undocumented
235 0 my $instance = shift;
236 0 my $command = shift;
237 #
238 # Extract the table and column info
239 #
240 0 $command =~ /(.*)\s+from\s+(\w+)\s*.*/i;
241 0 my $table_name = $2;
242 0 my @columns = split (/\s*,\s*/,$1);
243 #
244 # Populate the columns array
245 #
246 0 for my $i (0 .. $#columns) {
247 0 ▶ if($columns[$i] =~ /.*\s+as\s+\"*(\w+)\"*/) { $columns[$i] = $1; };
Branch: 1 branch
BranchTrueFalseTotal
if ($columns[$i] =~ /.*\s+as\s+\"*(\w+)\"*/) 0 0 0
248 0 ▶ if($columns[$i] eq '*'){
Branch: 1 branch
BranchTrueFalseTotal
if ($columns[$i] eq "*") 0 0 0
249 0 @columns = $instance->get_columns($table_name);
250 };
251 };
252 #
253 # Return the columns array
254 #
255 0 return @columns;
256 };
257 # Autoload methods go after =cut, and are processed by the autosplit program.
258  
259 1 1;
260 __END__
↑ Summary