Subversion Repositories DevTools

Rev

Rev 2026 | Rev 4382 | Go to most recent revision | Details | Compare with Previous | 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;
4362 dpurdie 134
    my $view_tag_uuid;
392 dpurdie 135
    my $cmd = "cleartool lsview -age $view";
136
    Verbose2($cmd);
137
    open(SHOWCMD, "$cmd 2>&1 |") || Error( "can't run command: $!");
138
    my @text = <SHOWCMD>;
139
    close(SHOWCMD);
140
 
141
    chomp $text[1];
142
 
4362 dpurdie 143
    my ($when, $who, $age);
144
    if ($text[1] =~ m~accessed ([^ ]+) by (.*)~)
145
    {
146
        $when = $1;
147
        $who = $2;
148
    }
392 dpurdie 149
 
150
    if ( $when )
151
    {
152
        $age =  age($when);
153
    }
154
    else
155
    {
156
        $age = 999999;
157
        $who = 'unknown';
158
        $when = 'unknown';
159
    }
160
    printf "%-20s(%3d) %50s, %s\n", $when, $age, $view, $who;
161
 
162
    if ( $opt_delete )
163
    {
164
        if ( $age < $opt_age )
165
        {
166
            print "Skip view. Too new\n";
167
        }
168
        else
169
        {
170
           ClearTool ("rmview -force -tag $view");
171
 
172
            #
173
            #   If the view tag still exists then delete the view the hard way
174
            #   Use 'lsview' to locate the views uuid
175
            #
176
            Verbose("Look for View Tag");
177
            my $cmd = "cleartool lsview -long $view";
178
            open(CMD, "$cmd 2>&1 |") || Error( "can't run command: $!");
179
            while (<CMD>)
180
            {
181
                #
182
                #   Filter output from the user
183
                #
184
                chomp;
185
                Verbose("lsview: $_");
186
                $uuid = $1 if ( m~^View uuid:\s+(.*)~ );
4362 dpurdie 187
                $view_tag_uuid = $1 if ( m~View tag uuid(.*)~ );
392 dpurdie 188
            }
189
            close(CMD);
190
 
191
            if ( $uuid )
192
            {
193
                Warning ("Deleting view - the hard way");
194
                ClearTool( "--Quiet", "rmview -force -all -uuid $uuid" );
195
                ClearTool( "--Quiet", "unregister -view -uuid $uuid" );
196
                ClearTool( "--Quiet", "rmtag -view -all $view" );
197
            }
4362 dpurdie 198
            elsif ($view_tag_uuid)
199
            {
200
                ClearTool( "--Quiet", "rmtag -view -all $view" );
201
            }
392 dpurdie 202
        }
203
    }
204
}
205
 
206
#-------------------------------------------------------------------------------
207
# Function        : ClearTool
208
#
209
# Description     : Issue a cleartool command
210
#                   Filter out many of the stupid messages
211
#
212
# Inputs          : Options and Command line
213
#                   Options:
214
#                       --Quiet     - Supress all command output
215
#
216
# Returns         : Error code
217
#
218
sub ClearTool
219
{
220
    my $quiet;
221
 
222
    #
223
    #   Scan for initial options
224
    #       --Quiet
225
    #
226
    if ( $_[0] eq '--Quiet' )
227
    {
228
        $quiet = 1;
229
        shift;
230
    }
231
 
232
    my $cmd = "cleartool @_";
233
 
234
    Verbose ("ClearTool: $cmd");
235
    open(CMD, "$cmd 2>&1 |") || Error "can't run command: $!";
236
    while (<CMD>)
237
    {
238
        #
239
        #   Filter output from the user
240
        #
241
        next if ( $quiet );
242
        unless ( $opt_verbose )
243
        {
244
            next if ( m~Making dir~ );
245
            next if ( m~End dir~ );
246
            next if ( m~Processing dir~ );
247
            next if ( m~Error~ );
248
        }
249
        print $_;
250
    }
251
    close(CMD);
252
 
253
    Verbose2 "ClearTool Exit Status: $?";
254
    return $? / 256;
255
}
256
 
257
 
258
#-------------------------------------------------------------------------------
259
# Function        : age
260
#
261
# Description     : Convert a clearcase date into an age. Days ago
262
#
263
# Inputs          : $when           - CC date
264
#
265
# Returns         : Days ago
266
#
267
 
268
sub age
269
{
270
    my ($when) = @_;
271
 
272
    $when =~ m~(\d+)-(\d+)-(\d+)~;
273
    my $mday = $3;
274
    my $mon = $2 - 1;
275
    my $year = $1;
276
 
277
    my $age = timelocal(0,0,0,$mday,$mon,$year);
278
    my $delta = int( ($now - $age) / ( 60 * 60 * 24 ));
279
 
280
#    my $rev = localtime( $age );
281
 
282
    return $delta;
283
}
284
 
285
#-------------------------------------------------------------------------------
286
#   Documentation
287
#
288
 
289
=pod
290
 
291
=head1 NAME
292
 
293
jats cc_tear_down - Tear down old clearcase views
294
 
295
=head1 SYNOPSIS
296
 
297
  jats etool cc_tear_down [options]
298
 
299
 Options:
300
    -help[=n]           - brief help message
301
    -help -help         - Detailed help message
302
    -man[=n]            - Full documentation
303
    -verbose[=n]        - Verbose operation
304
    -delete             - Delete the views. Default is to list.
305
    -age=nn             - Only older than nn days (default1 1)
306
    -user=text          - Only a users views. default current user
307
    -buildviews         - Only views created by the build daemon
308
 
309
=head1 OPTIONS
310
 
311
=over 8
312
 
313
=item B<-help[=n]>
314
 
315
Print a brief help message and exits.
316
 
317
The verbosity of the help text can be controlled by setting the help level to a
318
number in the range of 1 to 3, or by invoking the option multiple times.
319
 
320
=item B<-man[=n]>
321
 
322
Without a numeric argument this is the same as -help=3. Full help will be
323
displayed.
324
 
325
With a numeric argument, this option is the same as -help=n.
326
 
327
=item B<-verbose[=n]>
328
 
329
This option will increase the level of verbosity of the utility.
330
 
331
If an argument is provided, then it will be used to set the level, otherwise the
332
existing level will be incremented. This option may be specified multiple times.
333
 
334
=item B<-delete>
335
 
336
This optio will cause the listed views to be deleted.
337
 
338
By default the views are not deleted.
339
 
340
=item B<-age=nn>
341
 
342
Only consider views that are older than 'nn' days.
343
 
344
=item B<-user=text>
345
 
346
Only consider views for a specified user. The default value is to use the
347
name of the current user. It does assume that the views are in the form
348
created by JATS.
349
 
350
The text is an anchored regular expression. It can be used in many ways to limit
351
the views listed.
352
 
353
JATS will create views in a well defined form that contains:
354
 
355
=over 8
356
 
357
=item * The user name
358
 
359
=item * The name of the computer on which the view is created
360
 
361
=item * The package name and version on which the view is based
362
 
363
=item * The type of view.
364
 
365
If a sandbox view, then then name of then sandbox is also used.
366
 
367
=back
368
 
369
ie: SomeUser_auperawsXXX_sandbox.SandBoxName_PackageName_PackageVersion
370
 
371
Examples
372
 
373
=over 8
374
 
375
=item * -user=SomeUser_
376
 
377
List views that were created by 'SomeUser'.
378
 
379
=item * -user=SomeUser_auperawsXXX
380
 
381
List views that were created by 'SomeUser' on the machine 'auperawsXXX'.
382
 
383
=back
384
 
385
=item B<-buildviews>
386
 
387
This option will limit the utility to views created by various build tools.
388
These have a special 10 digit number sequence in the name.
389
 
390
=back
391
 
392
=head1 DESCRIPTION
393
 
394
This utility is used to locate and tear down old Clear Case views. You may need
395
to be the VOB owner to do this.
396
 
397
=cut
398