File Coverage

blib/lib/TSM.pm
Criterion Covered Total %
statement 23 102 22.5
branch 8 44 18.1
condition 5 18 27.7
subroutine 4 10 40.0
pod 4 7 57.1
total 44 181 24.3


line stmt bran cond sub pod time code
1             package TSM;
2              
3 1     1   650 use strict;
  1         1  
  1         42  
4 1     1   5 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
  1         2  
  1         76  
5 1     1   5 use Carp;
  1         11  
  1         2034  
6              
7             require Exporter;
8             require AutoLoader;
9              
10             @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             @EXPORT = qw(
15            
16             );
17             @EXPORT_OK = qw(
18              
19             );
20             $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             sub new {
29 1     1 1 73 my $self = shift;
30 1   33     8 my $class = ref $self || $self;
31 1         3 my $instance = {};
32 1         2 my $rcfile = undef;
33             #
34             # Set $rcfile to either .tsmrc or ~/.tsmrc depending on which comes first.
35             #
36 1 50 33     23 $rcfile = "$ENV{HOME}/.tsmrc" if (exists $ENV{HOME} && -e "$ENV{HOME}/.tsmrc");
37 1 50       9 $rcfile = ".tsmrc" if ( -e ".tsmrc");
38             #
39             # Populate the parameter hash with default values and overwrite them
40             # with the parameter of the caller, if any.
41             #
42 1         6 my %parameter = (id => undef, pa => undef, file => $rcfile, @_);
43             #
44             # Read the ID and password from the parameters, if provided.
45             #
46 1 50       7 $instance->{id} = $parameter{id} if ($parameter{id});
47 1 50       3 $instance->{pa} = $parameter{pa} if ($parameter{pa});
48             #
49             # Read the ID and password from $rcfile, if ID has not been
50             # provided yet.
51             #
52 1 50 33     9 if (! defined($instance->{id}) and $parameter{file}) {
53 0 0       0 open (TSMRC, "$parameter{file}") or croak "Error opening $parameter{file} to read ID and password: $!";
54 0         0 while () {
55 0 0       0 chomp($instance->{id} = $1) if (m/^ID\s+(.*)$/);
56 0 0       0 chomp($instance->{pa} = $1) if (m/^PA\s+(.*)$/);
57             };
58 0 0       0 close (TSMRC) or croak "Error closing $parameter{file}: $!";
59             };
60             #
61             # Read ID and/or password from STDIN if not provided yet.
62             #
63 1 50 33     5 unless ($instance->{id} and $instance->{pa}) {
64 1 50       10 if ( -t STDIN ) {
65 0         0 my $stty = `stty`;
66 0 0       0 unless($instance->{id}){
67 0         0 print "Enter your user id: ";
68 0         0 chomp($instance->{id}=);
69             };
70 0 0       0 unless($instance->{pa}){
71             # Turn off echoing
72 0         0 `stty -echo`;
73 0         0 print "Enter the password for $instance->{id}: ";
74 0         0 chomp($instance->{pa}=);
75             # Turn on echoing
76 0         0 `stty echo`;
77 0         0 print "\n";
78             };
79             };
80             };
81             #
82             # Test if the ID and password could has been read from somewhere.
83             #
84 1 50 33     5 unless ($instance->{id} and $instance->{pa}){
85 1         350 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             sub dsmadmc (@) {
95 0     0 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 0         open(DSMADMC, "dsmadmc -ID=$instance->{id} -PA=$instance->{pa} $options \"$command\"
104             or croak "Cannot open TSM session: $!\n";
105             #
106             # Remove status messages and concatenate the output to a string
107             #
108 0           while()
109             {
110 0 0         last if (m/^(ANS800[12]I).*(\d+)\.$/);
111 0 0         if (m/^(ANS8000I)/)
112             {
113 0           $start = 1;
114 0           next;
115             };
116 0 0 0       if ($start and !/^\s*$/)
117             {
118 0           chomp;
119 0           push(@output,$_);
120             };
121             };
122 0 0         close(DSMADMC) or carp "Cannot close TSM session: $!";
123 0           return @output;
124             };
125              
126             sub select_single ($) {
127 0     0 1   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             sub select($){
152 0     0 1   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             {
182 0     0 1   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             {
213 0     0 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 0         push (@columns, $1) if $element =~ (/^(\w+)\t\d{1,2}$/);
226             };
227             #
228             # Return the columns array
229             #
230 0           return @columns;
231             };
232              
233             sub get_columnlabels($)
234             {
235 0     0 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 0         if($columns[$i] =~ /.*\s+as\s+\"*(\w+)\"*/) { $columns[$i] = $1; };
  0            
248 0 0         if($columns[$i] eq '*'){
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;
260             __END__