Subversion Repositories DevTools

Rev

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

Rev Author Line No. Line
2401 dpurdie 1
########################################################################
6177 dpurdie 2
# COPYRIGHT - VIX IP PTY LTD ("VIX"). ALL RIGHTS RESERVED.
2401 dpurdie 3
#
4
# Module name   : cc2svn_labeldirs.pl
5
# Module type   : Makefile system
6
# Compiler(s)   : Perl
7
# Environment(s): jats
8
#
9
# Description   : Used to process package-versions that are incorrectly
4030 dpurdie 10
#                 labled - the package has not been labeled up to the root
2401 dpurdie 11
#                 of the VOB
12
#
13
#                 The utility works by scanning the VOB for labeled files
14
#                 It then determines the full path and will detect paths
15
#                 that have not been labled.
16
#
17
#                 It will then label them
18
#
19
#                 The search process is not fast.
20
#
21
# Usage:        jats cc2svn_labeldirs 'CC::/VobName/Path::Label'
22
#
23
#......................................................................#
24
 
25
require 5.008_002;
26
use strict;
27
use warnings;
28
 
29
use Pod::Usage;
30
use Getopt::Long;
31
 
32
use JatsError;
33
use JatsSystem;
34
 
35
#
36
#  Globals that can be set immediately
37
#
38
my $VERSION = "1.1.0";
39
my $ats = "@@";
40
my $UNIX = $ENV{'GBE_UNIX'};
41
my $UNIX_VOB_PREFIX = '/vobs/';
42
my $VOB_SEP = $UNIX ? '/' : '\\';
43
 
44
#
45
#   Options
46
#
47
my $opt_debug   = $ENV{'GBE_DEBUG'};        # Allow global debug
48
my $opt_verbose = $ENV{'GBE_VERBOSE'};      # Allow global verbose
49
my $opt_help = 0;
50
my $opt_drive = $UNIX ? '/view' : 'o:';
51
my $opt_viewname = 'administration_view';
52
my $opt_vob;
53
my $opt_label;
54
my $opt_test = 0;
55
 
56
my $vob_name;
57
my $vob_desc;
58
my $view_path;
59
my @paths;
60
my %usedDirs;
2403 dpurdie 61
my %notLabled;
2401 dpurdie 62
my $label_error = 0;
63
 
64
my @error_list;
65
my @last_results;
66
my $last_result;
67
my $label_exists = 0;
68
my $label_is_locked = 0;
69
my $label_is_unlocked;
70
my $label_count = 0;
71
 
72
#-------------------------------------------------------------------------------
73
# Function        : main Entry Point
74
#
75
# Description     : 
76
#
77
# Inputs          : 
78
#
79
# Returns         : 
80
#
81
my $result = GetOptions (
82
                "help:+"        => \$opt_help,
83
                "manual:3"      => \$opt_help,
84
                "verbose:+"     => \$opt_verbose,
2764 dpurdie 85
                "label:s"       => \$opt_label,
2401 dpurdie 86
                'vob:s'         => \$opt_vob,
87
                'test!'         => \$opt_test,
88
                );
89
 
90
                #
91
                #   UPDATE THE DOCUMENTATION AT THE END OF THIS FILE !!!
92
                #
93
 
94
#
95
#   Process help and manual options
96
#
97
pod2usage(-verbose => 0, -message => "Version: $VERSION") if ($opt_help == 1 || ! $result);
98
pod2usage(-verbose => 1) if ( $opt_help == 2 );
99
pod2usage(-verbose => 2) if ( $opt_help > 2 );
100
 
101
#
102
#   Configure the error reporting process now that we have the user options
103
#
104
ErrorConfig( 'name'    =>'LABELDIRS',
105
             'verbose' => $opt_verbose,
106
             'on_exit' => \&display_error_list
107
            );
108
 
109
#
110
#   Sanity test
111
#
2764 dpurdie 112
Error ("No labels provided") if ( $#ARGV < 0 && ! $opt_label);
113
Error ("Too many labels provided") if ( $#ARGV >= 0 && $opt_label);
2401 dpurdie 114
Error ("Too many labels provided") if ( $#ARGV > 0);
2764 dpurdie 115
$opt_label = $ARGV[0] unless $opt_label;
2401 dpurdie 116
 
117
#
118
#   Convert label with embedded VCS information into a 'normal' form.
119
#   Form:
120
#       CC::label
121
#       CC::path::label
122
#       CC::::label
123
#
124
$opt_label =~ tr~\\/~/~s;
125
if ( $opt_label =~ m~^(.+?)::(.*?)(::(.+))?$~ )
126
{
127
    Error ("Label contains invalid Version Control Identifier($1): $_")
128
        if ( $1 ne 'CC' );
129
 
130
    my $ll = $2;
131
    my $path;
132
    if ( $3 )
133
    {
134
        $ll = $4;
135
        my @pelements = split( m'/+', $2);
136
        $path = $pelements[1] || '';
137
        if ( $path  )
138
        {
139
 
140
            Error ("Multiple conflicting Embedded Vobs",
141
                   "Vob: $opt_vob",
142
                   "VCS Spec: $opt_label" ) if ( $opt_vob && $path ne $opt_vob );
143
            $opt_vob = $path;
144
        }
145
    }
146
    $opt_label = $ll;
147
    Verbose ("Clean URL: $opt_vob, $opt_label");
148
}
149
else
150
{
2764 dpurdie 151
    Error ("No VOB specified") unless $opt_vob;
2401 dpurdie 152
    $opt_vob =~ s~^/~~;
153
    $opt_vob =~ s~/.*?$~~;
154
}
155
Error ("No VOB specified") unless ( $opt_vob );
156
 
157
#
158
#   Ensure that the 'cleartool' program can be located
159
#
160
Verbose ("Locate clearcase utility in users path");
161
Error ("Cannot locate the 'cleartool' utility in the users PATH")
162
    unless ( LocateProgInPath('cleartool', '--All') );
163
 
164
#
165
#   Ensure that the 'administration_view' is available
166
#   Then start the view, before checking its availability
167
#
168
if( ClearCmd('lsview', $opt_viewname) )
169
{
170
    Error ("Required view not found: $opt_viewname",
171
           "This is a dynamic view that should exist as it is used by the build system");
172
}
173
 
174
if( ClearCmd( 'startview', $opt_viewname) )
175
{
176
    Error ("Cannot start the required view: $opt_viewname");
177
}
178
 
179
$view_path = "$opt_drive/$opt_viewname";
180
$view_path .= $UNIX_VOB_PREFIX if ( $UNIX );
181
Error ("Cannot locate the required dynamic view: $view_path",
182
       "The view exits and has been started. It cannot be found")
183
    if ( ! -d $view_path  );
184
 
185
$vob_name = $UNIX_VOB_PREFIX . $opt_vob if ( $UNIX && $opt_vob !~ m~^${UNIX_VOB_PREFIX}~ );
186
$vob_name =~ s~/~$VOB_SEP~g;
187
 
188
$vob_desc = '@' . $vob_name;
189
$vob_desc =~ s~//~/~g;
190
 
191
Message ("VobName: $vob_name, $vob_desc");
192
 
193
#
194
#   Mount the target VOB, but only if its not already mounted
195
#
196
ClearCmd( 'lsvob', $vob_name);
197
unless ( $last_result =~ m~^\*\s+$vob_name~)
198
{
199
    Message ("Mounting VOB: $vob_name");
200
    if( ClearCmd( 'mount', $vob_name) )
201
    {
202
        Error ("Could not mount the VOB: $vob_name");
203
    }
204
}
205
 
206
#
207
#   Change to the directory that contains the admin view
208
#   This will ensure that the 2nd line of the dump comamnd contains
209
#   the vob extended pathname within that view. This will be used
210
#   to simplify the pairing of files
211
#
212
Verbose2 ("getIds: chdir: $view_path");
213
chdir ($view_path) || Error ("Did not chdir to $view_path" );
214
 
215
 
216
#
217
#   Ensure that the label exists inthe VOB
218
#
219
LocateLabel($opt_label);
220
 
221
#
222
#   Search the VOB for all Objects with the label
223
#   Then examine each object and determine the set of unique paths
224
#
225
@paths = findLabledOjects($opt_label);
226
processOneItem($_) foreach ( @paths );
227
 
228
#
2403 dpurdie 229
#   Now have a list of paths
2401 dpurdie 230
#
231
foreach my $path ( reverse sort { length($a) <=> length($b) } keys %usedDirs )
232
{
2403 dpurdie 233
my $ppath = $path; $ppath =~ s~.*/vobs/~~;
234
    Verbose("Examine: $ppath");
235
    unless ( tailIsLabeled ($path) )
236
    {
237
        $notLabled{$path} = 1;
238
        Verbose("NotLabled: $ppath");
239
#print "0: $path\n";
240
    }
241
}
242
 
243
#
244
#   Walk the list of paths and label the required directories
245
#   Walk from bottom to top (longest first)
246
#
247
foreach my $path ( reverse sort { length($a) <=> length($b) } keys %notLabled )
248
{
2401 dpurdie 249
    Verbose("Path: $path");
2403 dpurdie 250
    my $ppath = $path;
251
    $ppath =~ s~.*/vobs/~~;
252
 
253
    my $testdir = $path;
254
    #
255
    #   One final test
256
    #
257
    unless ( tailIsLabeled ($path) )
2401 dpurdie 258
    {
2403 dpurdie 259
        print "$testdir - Must label\n";
260
        smartUnLock();
261
        ClearCmd ("mklabel", '-c', 'JATS cc2svn_labeldirs', $opt_label, $path ) unless $opt_test;
262
        if ( display_error_list() )
2401 dpurdie 263
        {
2403 dpurdie 264
            $label_error++;
2401 dpurdie 265
        }
266
        else
267
        {
2403 dpurdie 268
            $label_count++ unless ($opt_test);
2401 dpurdie 269
        }
270
    }
2403 dpurdie 271
    else
272
    {
273
        print "$ppath - Already labled\n";
274
    }
2401 dpurdie 275
}
276
 
277
#
278
#   All done
279
#
280
smartLock();
2403 dpurdie 281
Message ("Items Found: ". scalar @paths);
2401 dpurdie 282
Message ("Labels applied: $label_count");
283
Message ("Label  errors : $label_error") if $label_error;
284
Error ("Not all required paths labled") if ( $label_error );
285
exit (0);
286
 
287
#-------------------------------------------------------------------------------
2403 dpurdie 288
# Function        : tailIsLabeled
289
#
290
# Description     : Determine if the tail of the current path is labled
291
#
292
# Inputs          : $test   - Path to test
293
#
294
# Returns         : 1       - Is labled
295
#                   0       - Is not Labled
296
#
297
sub tailIsLabeled
298
{
299
    my ($test) = @_;
300
 
301
#    print "$path\n";
302
    my $found = 0;
303
    my $more = 100;
304
 
305
    #
306
    #   First remove the last element - it will be a label (number)
307
    #
308
    $test =~ s~/[^/]*?$~~;
309
    while ( $more-- )
310
    {
311
        if ( -e $test . '/' . $opt_label )
312
        {
313
            return 1;
314
        }
315
        $test =~ s~(.*)/(.*?)$~~;
316
        $more = 1 if ( $2 eq 'main' );
317
        $test = $1;
318
    }
319
    return 0;
320
}
321
 
322
#-------------------------------------------------------------------------------
2401 dpurdie 323
# Function        : processOneItem
324
#
325
# Description     : Examine a CC extended path. Break it into bits and
326
#                   determine with parts of the path have not been labled
327
#
328
# Inputs          : $path               - Path to process
329
#
330
# Returns         : Fill in %usedDirs
331
#
332
sub processOneItem
333
{
334
    my ($path) = @_;
2403 dpurdie 335
 
336
    #
337
    #   Break of the last bit OBJECT/main/....../nn
338
    #
339
    while ( $path =~ m~(/.*)/([^/]+/main/)(.*?)$~ )
2401 dpurdie 340
    {
341
        $path = $1;
2403 dpurdie 342
        my $tagPath = $path;
343
        $tagPath =~ s~/\d+$~~;
344
        $usedDirs{$path} = 1 if $tagPath =~ m~\@\@~;
2401 dpurdie 345
    }
346
}
347
 
348
#-------------------------------------------------------------------------------
349
# Function        : findLabledOjects
350
#
351
# Description     : Find all objects on the VOB with the specified label
352
#
353
# Inputs          : $opt_label      - Label to find
354
#
355
# Returns         : Results into @paths
356
#
357
sub findLabledOjects
358
{
359
    my ($opt_label) = @_;
360
    my @results;
361
    Message ("Locate objects in VOB: $opt_vob" );
362
 
363
    my $cmd = QuoteCommand("cleartool", "find", "$opt_vob", '-all', "-version", "lbtype($opt_label)", "-print");
364
    Verbose2($cmd);
365
 
366
    open(SHOWCMD, "$cmd 2>&1 |") || Error( "can't run command: $!");
367
    while (<SHOWCMD>)
368
    {
369
        #
370
        #   Filter output from the user
371
        #
372
        chomp;
373
        Verbose3($_);
4030 dpurdie 374
        next if ( m~/lost\+found~ );
2401 dpurdie 375
        push @results, $_;
376
    }
377
    close(SHOWCMD);
378
    return @results;
379
}
380
 
381
#-------------------------------------------------------------------------------
382
# Function        : LocateLabel
383
#
384
# Description     : Check that the label exists
385
#                   Determine if the label is locked
386
#
387
# Inputs          : $opt_label  - Label to locate
388
#
389
# Returns         : Nothing
390
#
391
sub LocateLabel
392
{
393
    Verbose ("Check label");
394
    ClearCmd ("describe", "-fmt", "%[locked]p", "lbtype:$opt_label$vob_desc" );
395
    $label_exists = 1 unless( $opt_test
396
                          || grep ( /Label type not found/, @error_list )
397
                          || grep ( /Unable to determine VOB/, @error_list )
398
                            );
399
    if ( $label_exists && $last_result )
400
    {
401
        $label_is_locked = ($last_result =~ m~unlocked~)? 0:1;
402
    }
403
    Verbose ("Check label: Exist:$label_exists, Locked:$label_is_locked");
404
}
405
 
406
#-------------------------------------------------------------------------------
407
# Function        : smartLock
408
#
409
# Description     : Lock, unlock label
410
#
411
# Inputs          : mode            : 1 - lock if it was unclocked
412
#                                     0 - unlock unless already done
413
#
414
# Returns         : 
415
#
416
sub smartUnLock
417
{
418
    if ( $label_is_locked && ! $label_is_unlocked)
419
    {
420
        ClearCmd ("unlock", "lbtype:$opt_label$vob_desc" ) unless $opt_test;
421
        Error ("Program Terminated") if ( @error_list );
422
        $label_is_unlocked = 1;
423
    }
424
}
425
 
426
sub smartLock
427
{
428
    if ( $label_is_unlocked)
429
    {
430
        ClearCmd ("lock", "lbtype:$opt_label$vob_desc" ) unless $opt_test;
431
        Error ("Program Terminated") if ( @error_list );
432
        $label_is_unlocked = 0;
433
    }
434
}
435
 
436
#-------------------------------------------------------------------------------
437
# Function        : ClearCmd
438
#
439
# Description     : Similar to the system command
440
#                   Does allow standard output and standard error to be captured
441
#                   to a log file
442
#
443
#                   Used since I was having problems with calling other programs
444
#                   and control-C. It could hang the terminal session.
445
#
446
# Inputs          :
447
#
448
# Returns         :
449
#
450
sub ClearCmd
451
{
452
    my $cmd = QuoteCommand (@_);
453
    Verbose2 "cleartool $cmd";
454
 
455
        @error_list = ();
456
        @last_results = ();
457
        $last_result = undef;
458
        my $cmd_handle;
459
        open($cmd_handle, "sudo -u buildadm cleartool $cmd  2>&1 |")    || Error "can't run command: $!";
460
        while (<$cmd_handle>)
461
        {
462
            chomp;
463
            $last_result = $_;
464
            $last_result =~ tr~\\/~/~s;
465
            push @last_results, $last_result;
466
            Verbose ( "cleartool resp:" . $_);
467
            push @error_list, $_ if ( m~Error:~ );
468
        }
469
        close($cmd_handle);
470
 
471
    Verbose2 "Exit Status: $?";
472
    return $? / 256;
473
}
474
 
475
#-------------------------------------------------------------------------------
476
# Function        : display_error_list
477
#
478
# Description     : Display the error list
479
#                   This function is registered as an Error callback function
480
#                   it will be called on error exit
481
#
482
#                   Will clear error list when called, so that it can be used
483
#                   in non-exit situations.
484
#
485
# Inputs          :
486
#
487
# Returns         : true            - Errors in list
488
#                   false           - No error in list
489
#
490
sub display_error_list
491
{
492
    return 0 unless ( @error_list );
493
    print "$_\n" foreach ( @error_list );
494
    @error_list = ();
495
 
496
    smartLock();
497
    return 1;
498
}
499
 
2764 dpurdie 500
#-------------------------------------------------------------------------------
501
#   Documentation
502
#
2401 dpurdie 503
 
2764 dpurdie 504
=pod
505
 
506
=for htmltoc    SYSUTIL::cc2svn::
507
 
508
=head1 NAME
509
 
510
cc2svn_labeldirs - Locate directories that have not been labeled in ClearCase
511
 
512
=head1 SYNOPSIS
513
 
514
  jats cc2svn_labeldirs [options] [CC::/vob::label]
515
 
516
 Options:
517
    -help              - brief help message
518
    -help -help        - Detailed help message
519
    -man               - Full documentation
520
    -[no]test          - Only display missing labels. Default:test
521
    -label=label       - Specify the label
522
    -vob=vob           - Specify VOB
523
 
524
=head1 OPTIONS
525
 
526
=over 8
527
 
528
=back