Subversion Repositories DevTools

Rev

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

Rev Author Line No. Line
392 dpurdie 1
#! perl
2
########################################################################
6177 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   : Process a hijacked file(s)
11
#                 Check it in on a branch
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 Getopt::Long;
24
use Pod::Usage;                         # required for help support
25
 
26
#
27
#   Config items
28
#
29
my $branch  = "ddp_test_branch";        # Branch to checkout the file in
30
                                        # Use one name for lots of work
31
 
32
my $opt_verbose = 0;                    # Verbosity
33
my $opt_help = 0;
34
my $opt_manual;
35
my $pname = "HIJACK_CI";                # Utility name for display messages
36
my $VERSION = "1.0.0";
37
 
38
#
39
#   Globals
40
#
41
my @error_list;
42
my $last_result;
43
 
44
#-------------------------------------------------------------------------------
45
# Function        : Main Entry
46
#
47
# Description     : This small program will process hijacked files in a
48
#                   static view. It will create a branch for the file and then
49
#                   check the file in on the branch
50
#
51
#
52
# Inputs          : None
53
#
54
# Returns         : Even less.
55
#
56
 
57
my $result = GetOptions (
58
                "help+"     => \$opt_help,          # flag, multiple use allowed
59
                "manual"    => \$opt_manual,        # flag
60
                "verbose+"  => \$opt_verbose,       # flag
61
                );
62
 
63
#
64
#   Process help and manual options
65
#
66
pod2usage(-verbose => 0, -message => "Version: $VERSION")  if ($opt_help == 1  || ! $result);
67
pod2usage(-verbose => 1)  if ($opt_help == 2 );
68
pod2usage(-verbose => 2)  if ($opt_manual || ($opt_help > 2));
69
 
70
error ("No files to process")
71
    unless ( $#ARGV >= 0  );
72
 
73
#
74
#   Process files one by one
75
#
76
foreach  ( @ARGV )
77
{
78
    process_file( $_ );
79
}
80
exit;
81
 
82
 
83
#-------------------------------------------------------------------------------
84
# Function        : process_file
85
#
86
# Description     : Process one file
87
#
88
# Inputs          : A single file
89
#
90
# Returns         :
91
#
92
sub process_file
93
{
94
    my ($file) = @_;
95
    #
96
    #   Sanity test
97
    #
98
    unless ( -f $file )
99
    {
100
        Warning ("File not found: $file");
101
        return 1;
102
    }
103
 
104
    #
105
    #   Ensure that the file is hijacked
106
    #
107
    ClearCmd( "ls  $file" );
108
    error ("Program Terminated") if ( @error_list );
109
 
110
    unless ( $last_result =~ m~\[.*hijacked.*\]~ )
111
    {
112
        Warning ("File is not hijacked: $file");
113
        return 1;
114
    }
115
 
116
    #
117
    #   remove any .keep file
118
    #   Its easier than trying to detect which file ClearCase will create
119
    #
120
    if ( -f "$file.keep"  )
121
    {
122
        unlink ( "$file.keep" ) || error ("Cannot delete $file.keep");
123
    }
124
 
125
    #
126
    #   Status the version of the file in the view
127
    #   Use to determine vital file info
128
    #
129
    ClearCmd( "ls  -short $file" );
130
    error ("Program Terminated") if ( @error_list );
131
 
132
    $last_result =~ tr~\\/~/~s;
133
    my $full_path = $last_result;
134
 
135
    my $branch_path = $last_result;
136
       $branch_path =~ s~[/][^/]*$~~;
137
 
138
    my $root_branch = $branch_path;
139
       $root_branch =~ s~.*@/~~;
140
 
141
    my $is_on_branch = 0;
142
       $is_on_branch = 1 if ( $root_branch =~ m~/$branch~  ) ;
143
 
144
    my $target_branch = $is_on_branch ? $root_branch : "$root_branch/$branch";
145
 
146
    vprint ("Full Path        : $full_path");
147
    vprint ("BranchPath       : $branch_path");
148
    vprint ("RootPath         : $root_branch");
149
    vprint ("TgtBranch        : $target_branch");
150
    vprint ("Is Branched      : $is_on_branch");
151
 
152
    #
153
    #   Branch the file if it has not been branched
154
    #
155
    unless ( $is_on_branch )
156
    {
157
 
158
        #
159
        #   Ensure that the required branch exists in this VOB
160
        #
161
        ClearCmd ("lstype -short brtype:$branch" );
162
        if ( $last_result ne $branch )
163
        {
164
            vprint ("Create the branch in this VOB" );
165
            ClearCmd ("mkbrtype -c \"A development branch\" $branch" );
166
            error ("Program Terminated") if ( @error_list );
167
        }
168
 
169
        vprint ("Test. Has the file been branched" );
170
        if ( ClearCmd( "describe -short ${branch_path}/${branch}/0" ) )
171
        {
172
            vprint ("Create the initial branch point" );
173
            ClearCmd( "mkbranch -nco -nc -nwarn $branch $full_path" );
174
        }
175
    }
176
 
177
    #
178
    #   Checkout the file on the branch
179
    #   Don't checkout any data - its not needed
180
    #   Since its a hijacked file the file will be renamed to a .keep file
181
    #
182
    vprint ("Checkout the file on a branch" );
183
    ClearCmd ("co -nc -nq -ndata -nwarn -branch \"$target_branch\" $full_path");
184
    error ("Program Terminated") if ( @error_list );
185
 
186
    #
187
    #   Checkin the hijacked file
188
    #   Checkin the .keep file directly
189
    #
190
    ClearCmd ("ci -c \"Hijacked file check in\" -identical -from \"$file.keep\" \"$file\"");
191
    error ("Program Terminated") if ( @error_list );
192
}
193
 
194
#-------------------------------------------------------------------------------
195
# Function        : ClearCmd
196
#
197
# Description     : Similar to the system command
198
#                   Does allow standard output and standard error to be captured
199
#                   to a log file
200
#
201
#                   Used since I was having problems with calling other programs
202
#                   and control-C. It could hang the terminal session.
203
#
204
# Inputs          :
205
#
206
# Returns         :
207
#
208
sub ClearCmd
209
{
210
    my( $cmd ) = @_;
211
    vprint2( "cleartool $cmd" );
212
 
213
        @error_list = ();
214
        open(CMD, "cleartool $cmd  2>&1 |")    || error( "can't run command: $!" );
215
        while (<CMD>)
216
        {
217
            chomp;
218
            $last_result = $_;
219
            $last_result =~ tr~\\/~/~s;
220
            vprint ( "cleartool resp:" . $_);
221
            push @error_list, $_ if ( m~Error:~ );
222
        }
223
        close(CMD);
224
 
225
    vprint2( "Exit Status: $?" );
226
    return $? / 256;
227
}
228
 
229
#-------------------------------------------------------------------------------
230
# FUNCTION        : error
231
#
232
# DESCRIPTION     : Display an error message and exit
233
#
234
# INPUTS          : Arguments to be displayed
235
#
236
# RETURNS         : This function does not return
237
#
238
sub error
239
{
240
    print STDERR "[$pname] ERROR: @_\n" ;
241
    foreach ( @error_list )
242
    {
243
        print "$_\n";
244
    }
245
    exit 1;
246
}
247
 
248
sub Warning
249
{
250
    print STDERR "[$pname] WARNING: @_\n" ;
251
}
252
 
253
 
254
#-------------------------------------------------------------------------------
255
# Function        : vprint
256
#
257
# Description     : Print if verbose mode is enabled
258
#
259
# Inputs          :
260
#
261
# Returns         :
262
#
263
 
264
sub vprint
265
{
266
    print STDERR "[$pname] @_\n"
267
        if ( $opt_verbose );
268
}
269
sub vprint2
270
{
271
    print STDERR "[$pname] @_\n"
272
        if ( $opt_verbose > 1 );
273
}
274
 
275
 
276
#-------------------------------------------------------------------------------
277
#   Documentation
278
#
279
 
280
=pod
281
 
282
=head1 NAME
283
 
284
xxxxx - Save a hijacked file on a private branch
285
 
286
=head1 SYNOPSIS
287
 
288
  xxxxxx [options] files...
289
 
290
 Options:
291
    -help               - brief help message
292
    -help -help         - Detailed help message
293
    -man                - Full documentation
294
    -verbose            - Verbose operation
295
 
296
=head1 OPTIONS
297
 
298
=over 8
299
 
300
=item B<-help>
301
 
302
Print a brief help message and exits.
303
 
304
=item B<-help -help>
305
 
306
Print a detailed help message with an explanation for each option.
307
 
308
=item B<-man>
309
 
310
Prints the manual page and exits.
311
 
312
=item B<-verbose>
313
 
314
Increases program output. This option may be specified mutiple times
315
 
316
=back
317
 
318
=head1 DESCRIPTION
319
 
320
This small program will process hijacked files in a static view. It will create
321
a branch for the file and then check the file in on the branch
322
 
323
=cut
324