Subversion Repositories DevTools

Rev

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

Rev Author Line No. Line
2401 dpurdie 1
########################################################################
2
# Copyright (C) 1998-2012 Vix Technology, All rights reserved
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
10
#                 labled - the package has not been labedl up to the root
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;
61
my $label_error = 0;
62
 
63
my @error_list;
64
my @last_results;
65
my $last_result;
66
my $label_exists = 0;
67
my $label_is_locked = 0;
68
my $label_is_unlocked;
69
my $label_count = 0;
70
 
71
#-------------------------------------------------------------------------------
72
# Function        : main Entry Point
73
#
74
# Description     : 
75
#
76
# Inputs          : 
77
#
78
# Returns         : 
79
#
80
my $result = GetOptions (
81
                "help:+"        => \$opt_help,
82
                "manual:3"      => \$opt_help,
83
                "verbose:+"     => \$opt_verbose,
84
                "label"         => \$opt_label,
85
                'vob:s'         => \$opt_vob,
86
                'test!'         => \$opt_test,
87
                );
88
 
89
                #
90
                #   UPDATE THE DOCUMENTATION AT THE END OF THIS FILE !!!
91
                #
92
 
93
#
94
#   Process help and manual options
95
#
96
pod2usage(-verbose => 0, -message => "Version: $VERSION") if ($opt_help == 1 || ! $result);
97
pod2usage(-verbose => 1) if ( $opt_help == 2 );
98
pod2usage(-verbose => 2) if ( $opt_help > 2 );
99
pod2usage(-verbose => 0, -message => "Version: $VERSION") if ( $#ARGV < 0 );
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
#
112
Error ("No labels provided") if ( $#ARGV < 0);
113
Error ("Too many labels provided") if ( $#ARGV > 0);
114
$opt_label = $ARGV[0];
115
 
116
#
117
#   Convert label with embedded VCS information into a 'normal' form.
118
#   Form:
119
#       CC::label
120
#       CC::path::label
121
#       CC::::label
122
#
123
$opt_label =~ tr~\\/~/~s;
124
if ( $opt_label =~ m~^(.+?)::(.*?)(::(.+))?$~ )
125
{
126
    Error ("Label contains invalid Version Control Identifier($1): $_")
127
        if ( $1 ne 'CC' );
128
 
129
    my $ll = $2;
130
    my $path;
131
    if ( $3 )
132
    {
133
        $ll = $4;
134
        my @pelements = split( m'/+', $2);
135
        $path = $pelements[1] || '';
136
        if ( $path  )
137
        {
138
 
139
            Error ("Multiple conflicting Embedded Vobs",
140
                   "Vob: $opt_vob",
141
                   "VCS Spec: $opt_label" ) if ( $opt_vob && $path ne $opt_vob );
142
            $opt_vob = $path;
143
        }
144
    }
145
    $opt_label = $ll;
146
    Verbose ("Clean URL: $opt_vob, $opt_label");
147
}
148
else
149
{
150
    $opt_vob =~ s~^/~~;
151
    $opt_vob =~ s~/.*?$~~;
152
}
153
Error ("No VOB specified") unless ( $opt_vob );
154
 
155
#
156
#   Ensure that the 'cleartool' program can be located
157
#
158
Verbose ("Locate clearcase utility in users path");
159
Error ("Cannot locate the 'cleartool' utility in the users PATH")
160
    unless ( LocateProgInPath('cleartool', '--All') );
161
 
162
#
163
#   Ensure that the 'administration_view' is available
164
#   Then start the view, before checking its availability
165
#
166
if( ClearCmd('lsview', $opt_viewname) )
167
{
168
    Error ("Required view not found: $opt_viewname",
169
           "This is a dynamic view that should exist as it is used by the build system");
170
}
171
 
172
if( ClearCmd( 'startview', $opt_viewname) )
173
{
174
    Error ("Cannot start the required view: $opt_viewname");
175
}
176
 
177
$view_path = "$opt_drive/$opt_viewname";
178
$view_path .= $UNIX_VOB_PREFIX if ( $UNIX );
179
Error ("Cannot locate the required dynamic view: $view_path",
180
       "The view exits and has been started. It cannot be found")
181
    if ( ! -d $view_path  );
182
 
183
$vob_name = $UNIX_VOB_PREFIX . $opt_vob if ( $UNIX && $opt_vob !~ m~^${UNIX_VOB_PREFIX}~ );
184
$vob_name =~ s~/~$VOB_SEP~g;
185
 
186
$vob_desc = '@' . $vob_name;
187
$vob_desc =~ s~//~/~g;
188
 
189
Message ("VobName: $vob_name, $vob_desc");
190
 
191
#
192
#   Mount the target VOB, but only if its not already mounted
193
#
194
ClearCmd( 'lsvob', $vob_name);
195
unless ( $last_result =~ m~^\*\s+$vob_name~)
196
{
197
    Message ("Mounting VOB: $vob_name");
198
    if( ClearCmd( 'mount', $vob_name) )
199
    {
200
        Error ("Could not mount the VOB: $vob_name");
201
    }
202
}
203
 
204
#
205
#   Change to the directory that contains the admin view
206
#   This will ensure that the 2nd line of the dump comamnd contains
207
#   the vob extended pathname within that view. This will be used
208
#   to simplify the pairing of files
209
#
210
Verbose2 ("getIds: chdir: $view_path");
211
chdir ($view_path) || Error ("Did not chdir to $view_path" );
212
 
213
 
214
#
215
#   Ensure that the label exists inthe VOB
216
#
217
LocateLabel($opt_label);
218
 
219
#
220
#   Search the VOB for all Objects with the label
221
#   Then examine each object and determine the set of unique paths
222
#
223
@paths = findLabledOjects($opt_label);
224
processOneItem($_) foreach ( @paths );
225
 
226
#
227
#   Walk the list of paths and label the required directories
228
#   Walk from bottom to top ( longest first )
229
#
230
foreach my $path ( reverse sort { length($a) <=> length($b) } keys %usedDirs )
231
{
232
    Verbose("Path: $path");
233
    unless ( $usedDirs{$path} )
234
    {
235
        my $ppath = $path;
236
        $ppath =~ s~.*/vobs/~~;
237
 
238
        my $testdir = $path;
239
        unless ( $testdir =~ m~\@\@~ ) {
240
            $testdir .= '/.@@';
241
        } else {
242
            $testdir =~ s~/[^/]*$~~;
243
        }
244
 
245
        #
246
        #   One final test
247
        #
248
        if ( -e $testdir . '/' . $opt_label )
249
        {
250
            print "$ppath - Already labled\n";
251
        }
252
        else
253
        {
254
            print "$ppath - Must label\n";
255
            smartUnLock();
256
            ClearCmd ("mklabel", '-c', 'JATS cc2svn_labeldirs', $opt_label, $path ) unless $opt_test;
257
            if ( display_error_list() )
258
            {
259
                $label_error++;
260
            }
261
            else
262
            {
263
                $label_count++;
264
            }
265
        }
266
    }
267
}
268
 
269
#
270
#   All done
271
#
272
smartLock();
273
Message ("Labels applied: $label_count");
274
Message ("Label  errors : $label_error") if $label_error;
275
Error ("Not all required paths labled") if ( $label_error );
276
exit (0);
277
 
278
#-------------------------------------------------------------------------------
279
# Function        : processOneItem
280
#
281
# Description     : Examine a CC extended path. Break it into bits and
282
#                   determine with parts of the path have not been labled
283
#
284
# Inputs          : $path               - Path to process
285
#
286
# Returns         : Fill in %usedDirs
287
#
288
sub processOneItem
289
{
290
    my ($path) = @_;
291
    my $last_type = 0;
292
    my $type = 0;
293
    while ( $path =~ m~\@\@~ )
294
    {
295
        my $tail;
296
        $last_type = $type;
297
        $type = 0;
298
        #
299
        #   Remove bits until I remove a /main
300
        #
301
    #print "--- $path\n";
302
        $path =~ m~(.*)/([^/]*$)~;
303
        $path = $1;
304
        $tail = $2;
305
        if ( $tail eq 'main' )
306
        {
307
            $type = 1;
308
        }
309
        if ( $last_type == 1 )
310
        {
311
            my $type = 0;
312
            my $testdir = $path;
313
            unless ( $testdir =~ m~\@\@~ ) {
314
                $testdir .= '/.@@';
315
            } else {
316
                $testdir =~ s~/[^/]*$~~;
317
            }
318
 
319
            if ( ! exists $usedDirs{$path} )
320
            {
321
                $usedDirs{$path} = ( -e $testdir . '/' . $opt_label ) || 0;
322
            }
323
        }
324
    }
325
}
326
 
327
#-------------------------------------------------------------------------------
328
# Function        : findLabledOjects
329
#
330
# Description     : Find all objects on the VOB with the specified label
331
#
332
# Inputs          : $opt_label      - Label to find
333
#
334
# Returns         : Results into @paths
335
#
336
sub findLabledOjects
337
{
338
    my ($opt_label) = @_;
339
    my @results;
340
    Message ("Locate objects in VOB: $opt_vob" );
341
 
342
    my $cmd = QuoteCommand("cleartool", "find", "$opt_vob", '-all', "-version", "lbtype($opt_label)", "-print");
343
    Verbose2($cmd);
344
 
345
    open(SHOWCMD, "$cmd 2>&1 |") || Error( "can't run command: $!");
346
    while (<SHOWCMD>)
347
    {
348
        #
349
        #   Filter output from the user
350
        #
351
        chomp;
352
        Verbose3($_);
353
        push @results, $_;
354
    }
355
    close(SHOWCMD);
356
    return @results;
357
}
358
 
359
#-------------------------------------------------------------------------------
360
# Function        : LocateLabel
361
#
362
# Description     : Check that the label exists
363
#                   Determine if the label is locked
364
#
365
# Inputs          : $opt_label  - Label to locate
366
#
367
# Returns         : Nothing
368
#
369
sub LocateLabel
370
{
371
    Verbose ("Check label");
372
    ClearCmd ("describe", "-fmt", "%[locked]p", "lbtype:$opt_label$vob_desc" );
373
    $label_exists = 1 unless( $opt_test
374
                          || grep ( /Label type not found/, @error_list )
375
                          || grep ( /Unable to determine VOB/, @error_list )
376
                            );
377
    if ( $label_exists && $last_result )
378
    {
379
        $label_is_locked = ($last_result =~ m~unlocked~)? 0:1;
380
    }
381
    Verbose ("Check label: Exist:$label_exists, Locked:$label_is_locked");
382
}
383
 
384
#-------------------------------------------------------------------------------
385
# Function        : smartLock
386
#
387
# Description     : Lock, unlock label
388
#
389
# Inputs          : mode            : 1 - lock if it was unclocked
390
#                                     0 - unlock unless already done
391
#
392
# Returns         : 
393
#
394
sub smartUnLock
395
{
396
    if ( $label_is_locked && ! $label_is_unlocked)
397
    {
398
        ClearCmd ("unlock", "lbtype:$opt_label$vob_desc" ) unless $opt_test;
399
        Error ("Program Terminated") if ( @error_list );
400
        $label_is_unlocked = 1;
401
    }
402
}
403
 
404
sub smartLock
405
{
406
    if ( $label_is_unlocked)
407
    {
408
        ClearCmd ("lock", "lbtype:$opt_label$vob_desc" ) unless $opt_test;
409
        Error ("Program Terminated") if ( @error_list );
410
        $label_is_unlocked = 0;
411
    }
412
}
413
 
414
#-------------------------------------------------------------------------------
415
# Function        : ClearCmd
416
#
417
# Description     : Similar to the system command
418
#                   Does allow standard output and standard error to be captured
419
#                   to a log file
420
#
421
#                   Used since I was having problems with calling other programs
422
#                   and control-C. It could hang the terminal session.
423
#
424
# Inputs          :
425
#
426
# Returns         :
427
#
428
sub ClearCmd
429
{
430
    my $cmd = QuoteCommand (@_);
431
    Verbose2 "cleartool $cmd";
432
 
433
        @error_list = ();
434
        @last_results = ();
435
        $last_result = undef;
436
        my $cmd_handle;
437
        open($cmd_handle, "sudo -u buildadm cleartool $cmd  2>&1 |")    || Error "can't run command: $!";
438
        while (<$cmd_handle>)
439
        {
440
            chomp;
441
            $last_result = $_;
442
            $last_result =~ tr~\\/~/~s;
443
            push @last_results, $last_result;
444
            Verbose ( "cleartool resp:" . $_);
445
            push @error_list, $_ if ( m~Error:~ );
446
        }
447
        close($cmd_handle);
448
 
449
    Verbose2 "Exit Status: $?";
450
    return $? / 256;
451
}
452
 
453
#-------------------------------------------------------------------------------
454
# Function        : display_error_list
455
#
456
# Description     : Display the error list
457
#                   This function is registered as an Error callback function
458
#                   it will be called on error exit
459
#
460
#                   Will clear error list when called, so that it can be used
461
#                   in non-exit situations.
462
#
463
# Inputs          :
464
#
465
# Returns         : true            - Errors in list
466
#                   false           - No error in list
467
#
468
sub display_error_list
469
{
470
    return 0 unless ( @error_list );
471
    print "$_\n" foreach ( @error_list );
472
    @error_list = ();
473
 
474
    smartLock();
475
    return 1;
476
}
477
 
478