Subversion Repositories DevTools

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
392 dpurdie 1
#! perl
2
########################################################################
3
# Copyright ( C ) 2006 ERG Limited, All rights reserved
4
#
5
# Module name   : jats.sh
6
# Module type   : Makefile system
7
# Compiler(s)   : n/a
8
# Environment(s): jats
9
#
10
# Description   : Locate naughty ClearCase views that have not been torn down
11
#                 Remove the views if they are older than specified days.
12
#
13
# Usage:
14
#
15
# Version   Who      Date        Description
16
#
17
#......................................................................#
18
 
19
require 5.006_001;
20
use strict;
21
use warnings;
22
#use Data::Dumper;
23
use Cwd;
24
use JatsError;
25
use Time::Local;
26
use Pod::Usage;                             # required for help support
27
use Getopt::Long;
28
 
29
my $VERSION = "1.0.0";                      # Update this
30
my $opt_verbose = $ENV{'GBE_VERBOSE'};      # Allow global verbose
31
my $opt_help = 0;
32
my $opt_delete;
33
my $opt_age = 1;
34
my $opt_user;
35
my $opt_buildadm = 0;
36
 
37
my @vlist;
38
my $now = time;
39
 
40
#-------------------------------------------------------------------------------
41
# Function        : Mainline Entry Point
42
#
43
# Description     :
44
#
45
# Inputs          :
46
#
47
my $result = GetOptions (
48
            "help|h:+"          => \$opt_help,
49
            "manual:3"          => \$opt_help,
50
            "verbose|v:+"       => \$opt_verbose,
51
            "delete"            => \$opt_delete,
52
            "age=i"             => \$opt_age,
53
            "user=s"            => \$opt_user,
54
            "buildviews"        => \$opt_buildadm,
55
 
56
            );
57
 
58
            #
59
            #   UPDATE THE DOCUMENTATION AT THE END OF THIS FILE !!!
60
            #
61
 
62
#
63
#   Process help and manual options
64
#
65
pod2usage(-verbose => 0, -message => "Version: $VERSION") if ($opt_help == 1 || ! $result);
66
pod2usage(-verbose => 1) if ($opt_help == 2 );
67
pod2usage(-verbose => 2) if ($opt_help > 2);
68
pod2usage(-verbose => 0, -message => "Version: $VERSION") if ( $#ARGV >= 0 );
69
 
70
#
71
#   Configure the error reporting process now that we have the user options
72
#
73
ErrorConfig( 'name'    =>'CC_TEARDOWN',
74
             'verbose' => $opt_verbose,
75
            );
76
 
77
Error ("-user and -buildviews are mutually exclusive")
78
    if ( $opt_user && $opt_buildadm );
79
 
80
#
81
#   Determine a search filter
82
#
83
if ( $opt_user )
84
{
85
    $opt_user = '^' . $opt_user;
86
}
87
else
88
{
89
    $opt_user = $ENV{USER} || '.*';
90
    $opt_user = '^' . $opt_user;
91
}
92
 
93
if ( $opt_buildadm )
94
{
95
    $opt_user = '_[0-9]{10,}_';
96
}
97
 
98
#
99
#   Scan for views
100
#
101
my $cmd = "cleartool lsview";
102
Verbose2($cmd);
103
Verbose2("User: $opt_user");
104
open(SHOWCMD, "$cmd 2>&1 |") || Error( "can't run command: $!");
105
while (<SHOWCMD>)
106
{
107
    #
108
    #   Filter output from the user
109
    #
110
    chomp;
111
    s~^\*~~;
112
    s~^\s+~~;
113
#    Verbose2 ("Data: $_");
114
#    next unless ( m~[0-9]{10}~ );
115
     next unless ( m~$opt_user~ );
116
#    next unless ( m~jzhou1_~ );
117
#    next unless ( m~_build_~ );
118
#    next unless ( m~buildadm_AUPERA~ );
119
 
120
    my @data = split (' ' );
121
    Verbose ("View: $data[0]");
122
    push @vlist, $data[0];
123
}
124
close(SHOWCMD);
125
 
126
#
127
#   Process views and determine information
128
#
129
foreach my $view ( @vlist )
130
{
131
 
132
    Verbose ("Process View: $view");
133
    my $uuid;
134
    my $cmd = "cleartool lsview -age $view";
135
    Verbose2($cmd);
136
    open(SHOWCMD, "$cmd 2>&1 |") || Error( "can't run command: $!");
137
    my @text = <SHOWCMD>;
138
    close(SHOWCMD);
139
 
140
    chomp $text[1];
141
 
142
    $text[1] =~ m~accessed ([^ ]+) by (.*)~;
143
    my $when = $1;
144
    my $who = $2;
145
    my $age;
146
 
147
    if ( $when )
148
    {
149
        $age =  age($when);
150
    }
151
    else
152
    {
153
        $age = 999999;
154
        $who = 'unknown';
155
        $when = 'unknown';
156
    }
157
    printf "%-20s(%3d) %50s, %s\n", $when, $age, $view, $who;
158
 
159
    if ( $opt_delete )
160
    {
161
        if ( $age < $opt_age )
162
        {
163
            print "Skip view. Too new\n";
164
        }
165
        else
166
        {
167
           ClearTool ("rmview -force -tag $view");
168
 
169
            #
170
            #   If the view tag still exists then delete the view the hard way
171
            #   Use 'lsview' to locate the views uuid
172
            #
173
            Verbose("Look for View Tag");
174
            my $cmd = "cleartool lsview -long $view";
175
            open(CMD, "$cmd 2>&1 |") || Error( "can't run command: $!");
176
            while (<CMD>)
177
            {
178
                #
179
                #   Filter output from the user
180
                #
181
                chomp;
182
                Verbose("lsview: $_");
183
                $uuid = $1 if ( m~^View uuid:\s+(.*)~ );
184
            }
185
            close(CMD);
186
 
187
            if ( $uuid )
188
            {
189
                Warning ("Deleting view - the hard way");
190
                ClearTool( "--Quiet", "rmview -force -all -uuid $uuid" );
191
                ClearTool( "--Quiet", "unregister -view -uuid $uuid" );
192
                ClearTool( "--Quiet", "rmtag -view -all $view" );
193
            }
194
        }
195
    }
196
}
197
 
198
#-------------------------------------------------------------------------------
199
# Function        : ClearTool
200
#
201
# Description     : Issue a cleartool command
202
#                   Filter out many of the stupid messages
203
#
204
# Inputs          : Options and Command line
205
#                   Options:
206
#                       --Quiet     - Supress all command output
207
#
208
# Returns         : Error code
209
#
210
sub ClearTool
211
{
212
    my $quiet;
213
 
214
    #
215
    #   Scan for initial options
216
    #       --Quiet
217
    #
218
    if ( $_[0] eq '--Quiet' )
219
    {
220
        $quiet = 1;
221
        shift;
222
    }
223
 
224
    my $cmd = "cleartool @_";
225
 
226
    Verbose ("ClearTool: $cmd");
227
    open(CMD, "$cmd 2>&1 |") || Error "can't run command: $!";
228
    while (<CMD>)
229
    {
230
        #
231
        #   Filter output from the user
232
        #
233
        next if ( $quiet );
234
        unless ( $opt_verbose )
235
        {
236
            next if ( m~Making dir~ );
237
            next if ( m~End dir~ );
238
            next if ( m~Processing dir~ );
239
            next if ( m~Error~ );
240
        }
241
        print $_;
242
    }
243
    close(CMD);
244
 
245
    Verbose2 "ClearTool Exit Status: $?";
246
    return $? / 256;
247
}
248
 
249
 
250
#-------------------------------------------------------------------------------
251
# Function        : age
252
#
253
# Description     : Convert a clearcase date into an age. Days ago
254
#
255
# Inputs          : $when           - CC date
256
#
257
# Returns         : Days ago
258
#
259
 
260
sub age
261
{
262
    my ($when) = @_;
263
 
264
    $when =~ m~(\d+)-(\d+)-(\d+)~;
265
    my $mday = $3;
266
    my $mon = $2 - 1;
267
    my $year = $1;
268
 
269
    my $age = timelocal(0,0,0,$mday,$mon,$year);
270
    my $delta = int( ($now - $age) / ( 60 * 60 * 24 ));
271
 
272
#    my $rev = localtime( $age );
273
 
274
    return $delta;
275
}
276
 
277
#-------------------------------------------------------------------------------
278
#   Documentation
279
#
280
 
281
=pod
282
 
283
=head1 NAME
284
 
285
jats cc_tear_down - Tear down old clearcase views
286
 
287
=head1 SYNOPSIS
288
 
289
  jats etool cc_tear_down [options]
290
 
291
 Options:
292
    -help[=n]           - brief help message
293
    -help -help         - Detailed help message
294
    -man[=n]            - Full documentation
295
    -verbose[=n]        - Verbose operation
296
    -delete             - Delete the views. Default is to list.
297
    -age=nn             - Only older than nn days (default1 1)
298
    -user=text          - Only a users views. default current user
299
    -buildviews         - Only views created by the build daemon
300
 
301
=head1 OPTIONS
302
 
303
=over 8
304
 
305
=item B<-help[=n]>
306
 
307
Print a brief help message and exits.
308
 
309
The verbosity of the help text can be controlled by setting the help level to a
310
number in the range of 1 to 3, or by invoking the option multiple times.
311
 
312
=item B<-man[=n]>
313
 
314
Without a numeric argument this is the same as -help=3. Full help will be
315
displayed.
316
 
317
With a numeric argument, this option is the same as -help=n.
318
 
319
=item B<-verbose[=n]>
320
 
321
This option will increase the level of verbosity of the utility.
322
 
323
If an argument is provided, then it will be used to set the level, otherwise the
324
existing level will be incremented. This option may be specified multiple times.
325
 
326
=item B<-delete>
327
 
328
This optio will cause the listed views to be deleted.
329
 
330
By default the views are not deleted.
331
 
332
=item B<-age=nn>
333
 
334
Only consider views that are older than 'nn' days.
335
 
336
=item B<-user=text>
337
 
338
Only consider views for a specified user. The default value is to use the
339
name of the current user. It does assume that the views are in the form
340
created by JATS.
341
 
342
The text is an anchored regular expression. It can be used in many ways to limit
343
the views listed.
344
 
345
JATS will create views in a well defined form that contains:
346
 
347
=over 8
348
 
349
=item * The user name
350
 
351
=item * The name of the computer on which the view is created
352
 
353
=item * The package name and version on which the view is based
354
 
355
=item * The type of view.
356
 
357
If a sandbox view, then then name of then sandbox is also used.
358
 
359
=back
360
 
361
ie: SomeUser_auperawsXXX_sandbox.SandBoxName_PackageName_PackageVersion
362
 
363
Examples
364
 
365
=over 8
366
 
367
=item * -user=SomeUser_
368
 
369
List views that were created by 'SomeUser'.
370
 
371
=item * -user=SomeUser_auperawsXXX
372
 
373
List views that were created by 'SomeUser' on the machine 'auperawsXXX'.
374
 
375
=back
376
 
377
=item B<-buildviews>
378
 
379
This option will limit the utility to views created by various build tools.
380
These have a special 10 digit number sequence in the name.
381
 
382
=back
383
 
384
=head1 DESCRIPTION
385
 
386
This utility is used to locate and tear down old Clear Case views. You may need
387
to be the VOB owner to do this.
388
 
389
=cut
390