Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
227 dpurdie 1
###############################################################################
2
# CLASS Clearcase::Cmd
3
# Base Class for executing clearcase commands and allowing results to be proceesed.
4
# 
5
# The class should be instantiated, the Clearcase Command and Arguements set and
6
# then the CCexecute should be called to execute the command.  By default this 
7
# class will then simply just print the result to the console if no derived class
8
# overrides the CCProcess method.
9
# Dereived Classes should override the CCProcess member, which is called by CCexecute
10
# after the Clearcase command has been executed, to process the output of the 
11
# command as required.
12
#
13
# By convention all members/methods that begin with an '_' are private.
14
# 
15
###############################################################################
16
 
17
package Clearcase::Cmd;
18
 
19
use strict;
20
use Cwd;
21
use DeployUtils::Logger;
22
use IO::File;
23
use File::Temp qw /mktemp/;
24
use File::Spec qw /tmpdir/;
25
 
26
#==============================================================================
27
#   Constructor
28
#==============================================================================
29
sub new
30
{
31
    my $obclass = shift;
32
    my $class = ref($obclass) || $obclass;
33
 
34
    LogDebug("ClearCase::Cmd::new Instantiating new object of class $class");
35
    bless my $self = {
36
        _CWD             => Cwd::cwd,
37
        _CLEARTOOL       => ( $^O eq "solaris" ) ? "/usr/atria/bin/cleartool" : "cleartool",
38
        _CMD             => "",
39
        _ARGS            => "",
40
        _TMPNAME         => "",
41
        _CMDOUTPUT       => new IO::File,
42
        _PROCESSOUTPUT   => 1,
43
        _ABORTONERROR    => 1
44
    } => ( $class );
45
    return($self);
46
}   # new
47
 
48
 
49
#==============================================================================
50
#   DESTROY ( Desctructor )
51
#==============================================================================
52
sub DESTROY
53
{
54
    my $self = shift;
55
 
56
    $self->reset();
57
}   # DESTROY
58
 
59
 
60
#==============================================================================
61
#   reset
62
# Closes the filehandle and removes the temp file if any
63
#==============================================================================
64
sub reset
65
{
66
    my $self = shift;
67
 
68
    $self->{_CMDOUTPUT}->close if ( defined($self->{_CMDOUTPUT}) && defined($self->{_CMDOUTPUT}->fileno()) );
69
    unlink($self->{_TMPNAME}) if ( $self->{_TMPNAME} ne "" && -f $self->{_TMPNAME} );
70
}   # reset
71
 
72
 
73
###############################################################################
74
# Set/Get member methods.
75
# The following functions allow callers to set/get internal members.
76
###############################################################################
77
 
78
#==============================================================================
79
#   CCcwd accessor method returns CWD
80
#==============================================================================
81
sub CCcwd
82
{
83
    my $self = shift;
84
    return $self->{_CWD};
85
}   # CCcwd
86
 
87
 
88
#==============================================================================
89
#   CCcmd accessor sets & returns the Clearcase command
90
#==============================================================================
91
sub CCcmd
92
{
93
    my $self = shift;
94
    if ( @_ )
95
    {
96
        # if we are setting the command then we need to make sure the object is reset in case its a new cmd
97
        $self->reset();
98
        $self->{_CMD} = shift;
99
        $self->{_ARGS} = "";
100
        LogDebug("ClearCase::Cmd::CCcmd Set CC Command to $self->{_CMD}");
101
    }
102
    return $self->{_CMD};
103
}   # cc_cmd
104
 
105
 
106
#==============================================================================
107
#   CCargs accessor sets & returns the Clearcase command arguements
108
#==============================================================================
109
sub CCargs
110
{
111
    my $self = shift;
112
    if ( @_ )
113
    {
114
        $self->{_ARGS} = shift;
115
        LogDebug("ClearCase::Cmd::CCargs Set CC Args to $self->{_ARGS}");
116
    }
117
    return $self->{_ARGS};
118
}   # cc_args
119
 
120
 
121
#==============================================================================
122
#   CCaddArgs accessor appends arsg to existing args list
123
#==============================================================================
124
sub CCaddArgs
125
{
126
    my $self = shift;
127
    foreach my $i ( @_ )
128
    {
129
        $self->{_ARGS} .= " " . $i;
130
    }
131
    LogDebug("ClearCase::Cmd::CCaddArgs Set CC Args to $self->{_ARGS}");
132
    return $self->{_ARGS};
133
}   # cc_args
134
 
135
 
136
#==============================================================================
137
#   processOutput
138
# Sets the processOutput flag to 1 or 0.  If 1 clearcase commands are redirected
139
# to a file for processing by CCProcess, otherwise 0 does not redirection or processing.
140
#==============================================================================
141
sub processOutput
142
{
143
    my $self = shift;
144
    if ( @_ )
145
    {
146
        my $val = shift;
147
        $self->{_PROCESSOUTPUT} = ( $val == 0 ) ? 0 : 1;
148
        LogDebug("ClearCase::Cmd::processOutput Set to $self->{_PROCESSOUTPUT}");
149
    }
150
    return $self->{_PROCESSOUTPUT};
151
 
152
}   # processOutput
153
 
154
 
155
#==============================================================================
156
#   abortOnError
157
# Sets the abortOnError flag to 1 or 0.  If 0 then CCexecute ignores errors and 
158
# returns the error code, if 1 then errors are reported & script terminates
159
#==============================================================================
160
sub abortOnError
161
{
162
    my $self = shift;
163
    if ( @_ )
164
    {
165
        my $val = shift;
166
        $self->{_ABORTONERROR} = ( $val == 0 ) ? 0 : 1;
167
        LogDebug("ClearCase::Cmd::abortOnError Set to $self->{_ABORTONERROR}");
168
    }
169
    return $self->{_ABORTONERROR};
170
 
171
}   # abortOnError
172
 
173
 
174
#==============================================================================
175
#   CCexecute executes cleartool cmd with args and stores resulting output in
176
#   IO::File handle in self
177
#==============================================================================
178
sub CCexecute
179
{
180
    my $self = shift;
181
    my $retval;
182
 
183
    if ( $self->{_CMD} eq "" )
184
    {
185
        LogError("ClearCase::Cmd::CCexecute No Command to execute");
186
    }
187
 
188
    if ( $self->{_PROCESSOUTPUT} )
189
    {
190
        $self->{_TMPNAME} = File::Spec->tmpdir() . "/" . mktemp("CCCmdXXXXX");
191
        LogDebug("ClearCase::Cmd::CCexecute Executing cmd \"$self->{_CLEARTOOL} $self->{_CMD} $self->{_ARGS} > $self->{_TMPNAME}\".");
192
        $retval = system("$self->{_CLEARTOOL} $self->{_CMD} $self->{_ARGS} > $self->{_TMPNAME}") >> 8;
193
    }
194
    else
195
    {
196
        LogDebug("ClearCase::Cmd::CCexecute Executing cmd \"$self->{_CLEARTOOL} $self->{_CMD} $self->{_ARGS}\".");
197
        $retval = system("$self->{_CLEARTOOL} $self->{_CMD} $self->{_ARGS}") >> 8;
198
    }
199
 
200
    if ( $retval != 0 )
201
    {
202
        unlink($self->{_TMPNAME}) if ( $self->{_TMPNAME} ne "" && -f $self->{_TMPNAME} );
203
        LogError("ClearCase::Cmd::CCexecute Cmd Execution Failed with error $retval") if ( $self->{_ABORTONERROR} );
204
        LogError("-x", "ClearCase::Cmd::CCexecute Cmd Execution Failed with error $retval");
205
    }
206
 
207
    if ( $self->{_PROCESSOUTPUT} )
208
    {
209
        $self->{_CMDOUTPUT}->open("<$self->{_TMPNAME}") || LogError("ClearCase::Cmd::CCexecute Cannot open clearcase command results file");
210
 
211
        # Calls processData member to process result.
212
        # default version in this class simply prints the result to std out.
213
 
214
        $self->CCprocess();
215
    }
216
 
217
    return $retval;
218
}   # cc_exec
219
 
220
 
221
#==============================================================================
222
#   CCprocess processes the output from the execute command.
223
#   This is the default version that just simply dumps the output from the 
224
#   clearcase command executed to console
225
#   Any Derived classes can override this and either do nothing or process output 
226
#   as required.
227
#   It is a function called by the CCexecute method and should not be
228
#   called externally
229
#==============================================================================
230
sub CCprocess
231
{
232
    my $self = shift;
233
    my $line;
234
    while ( $line = $self->{_CMDOUTPUT}->getline() )
235
    {
236
        print $line;
237
    }
238
}   # CCprocess
239
 
240
 
241
#==============================================================================
242
#   getline retunrs the next line from the IO File class using its getline method
243
#==============================================================================
244
sub getline
245
{
246
    my $self = shift;
247
 
248
    return $self->{_CMDOUTPUT}->getline();
249
}   # getline
250
 
251
 
252
#==============================================================================
253
#   dumpSelf, debugging member to dump selfs hash
254
#==============================================================================
255
sub dumpSelf
256
{
257
    use Data::Dumper;
258
 
259
    my $self = shift;
260
 
261
    print Data::Dumper->Dump([$self], [ref($self)]);
262
}   # dumpSelf
263
1;