Subversion Repositories DevTools

Rev

Rev 7299 | Details | Compare with Previous | Last modification | View Log | RSS feed

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