Subversion Repositories DevTools

Rev

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

Rev Author Line No. Line
227 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   : Generate and test build manifest
11
#                 This tool is intended to be used by the automated build system
12
#
13
# Usage:
14
#
15
#
16
#......................................................................#
17
 
261 dpurdie 18
require 5.008_002;
227 dpurdie 19
 
20
use strict;
21
use warnings;
22
use JatsError;
23
use FileUtils;
24
 
25
use Pod::Usage;                             # required for help support
26
use Getopt::Long;
27
use Cwd;
28
use ExtUtils::Manifest qw(manifind);        # Needs perl later than 5.6.1
29
 
30
my $VERSION = "1.0.0";                      # Update this
31
 
32
#
33
#   Options
34
#
35
my $opt_debug   = $ENV{'GBE_DEBUG'};        # Allow global debug
36
my $opt_verbose = $ENV{'GBE_VERBOSE'};      # Allow global verbose
37
my $opt_help = 0;
38
my $opt_manual = 0;
39
my $opt_buildsystem;
40
my $opt_manifest_file;
41
my $opt_rootdir;
42
my $opt_check;
43
my $opt_generate;
44
my @opt_exclude;
45
my @opt_rewrite;
46
my %opt_rewrite;
47
 
48
my @num_excluded;
49
my @num_rewrite;
50
 
51
#-------------------------------------------------------------------------------
52
# Function        : Mainline Entry Point
53
#
54
# Description     :
55
#
56
# Inputs          :
57
#
58
my $result = GetOptions (
59
                "help+"         => \$opt_help,              # flag, multiple use allowed
60
                "manual|man"    => \$opt_manual,            # flag
61
                "verbose+"      => \$opt_verbose,           # flag, multiple use allowed
62
                "buildsystem"   => \$opt_buildsystem,       # Flag,
63
                "rootdir=s"     => \$opt_rootdir,           # String
64
                "manifest=s"    => \$opt_manifest_file,     # String
65
                "check"         => \$opt_check,             # Flag
66
                "generate"      => \$opt_generate,          # Flag
67
                "exclude=s"     => \@opt_exclude,           # Multiple strings
68
                "rewrite=s"     => \@opt_rewrite,           # Multiple strings
69
                );
70
 
71
                #
72
                #   UPDATE THE DOCUMENTATION AT THE END OF THIS FILE !!!
73
                #
74
 
75
#
76
#   Process help and manual options
77
#
78
pod2usage(-verbose => 0, -message => "Version: $VERSION") if ($opt_help == 1 || ! $result);
79
pod2usage(-verbose => 1) if ($opt_help == 2 );
80
pod2usage(-verbose => 2) if ($opt_manual || ($opt_help > 2));
81
pod2usage(-verbose => 0, -message => "Version: $VERSION") if ( $#ARGV >= 0 );
82
 
83
#
84
#   Configure the error reporting process now that we have the user options
85
#
86
ErrorConfig( 'name'    =>'MANIFEST',
87
             'verbose' => $opt_verbose,
88
            );
89
 
90
 
91
#
92
#   Validate user options
93
#
94
Error ("No Manifest file provided") if ( ! $opt_manifest_file);
95
#Error ("No Rootdir provided") if ( !$opt_rootdir);
96
 
97
#
98
#   Ensure the md5sum utility can be found
99
#
100
Verbose ("Check existance of utility: md5sum");
101
$result = qx( md5sum --version 2>&1 );
102
Error( "Cannot locate utility md5sum" )if ( $? );
103
 
104
#
105
#   Init the file utilities
106
#
107
InitFileUtils();
108
 
109
#-------------------------------------------------------------------------------
110
#   Generate the manifest file
111
#   This is done using the MD5SUM utility
112
#
113
#   Note: The MD5SUM program has a significant startup time.
114
#         The script will process up to 10 files at a time to improve speed
115
#
116
if ( ! $opt_check || $opt_generate )
117
{
118
    #
119
    #   Process the rewrite array and create a hash of entries
120
    #   Any file that is rewritten will be excluded
121
    #
122
    foreach my $entry ( @opt_rewrite )
123
    {
124
        my( $old, $new, @rest ) = split('=', $entry );
125
        Error ("Bad rewite entry: $entry") if ( @rest || !$new || !$old );
126
        $opt_rewrite{$old} = $new;
127
        $opt_rewrite{$new} = "";
128
    }
129
 
130
    #
131
    #   Add in the excluded entries
132
    #
133
    foreach my $entry ( @opt_exclude )
134
    {
135
        $opt_rewrite{$entry} = "";
136
    }
137
#    DebugDumpData("RewriteData", \%opt_rewrite);
138
 
139
 
140
    #
141
    #   Delete the existing file, if it exists
142
    #   Done in the users current directory
143
    #
144
    Verbose ("Delete any existing manifest file");
145
    if ( -f $opt_manifest_file )
146
    {
147
        unlink ($opt_manifest_file) || Error( "Cannot delete existing manifest file: $opt_manifest_file");
148
    }
149
 
150
    #
151
    #   Open a handle to the manifest file
152
    #
153
    open (MANIFEST, ">", $opt_manifest_file) || Error("Cannot create manifest file: $opt_manifest_file");
154
 
155
    #
156
    #   Change to the specified subdirectory
157
    #
158
    if ( $opt_rootdir )
159
    {
160
        Verbose ("chdir to specified root directory");
161
        chdir( $opt_rootdir ) || Error("Specified Root Diretory not found: $opt_rootdir");
162
    }
163
 
164
    #
165
    #   Generate a list of files to be included in the manifest
166
    #   Directories are not included
167
    #
168
    Verbose ("Generate file list");
169
    my $found = manifind();
170
    #DebugDumpData( "Data", $found );
171
    my @files = sort keys %$found;
172
    $found = "";
173
    Verbose("Number of files in manifest: " . (1 + $#files));
174
    Error ("No files in manifest") unless ( $#files > 0 );
175
 
176
    #
177
    #   Generate Md5sum info for each file
178
    #
179
    Verbose ("Generate manifest file");
180
    my @list;
181
    foreach my $file ( @files )
182
    {
183
        #
184
        #   Detect files that require special handling
185
        #   Some files are rewritten ( auto.pl -> build.pl )
186
        #   Some file are excluded ( build.pl )
187
        #
188
        my $rewrite;
189
        my $fileonly = $file;
190
        $fileonly =~ s~.*/~~;
191
        if ( exists $opt_rewrite{$fileonly} )
192
        {
193
            $rewrite = $opt_rewrite{$fileonly};
194
            unless ( $rewrite )
195
            {
196
                Verbose("Excluding: $file");
197
                push @num_excluded, $file;
198
                next;
199
            }
200
        }
201
 
202
        #
203
        #   Speed up MD5 processing by passing multiple files to the command
204
        #   When we hit a "rewritten" file, then we will first process and files
205
        #   in the speed up list, then the one special file
206
        #
207
        if ( ! $rewrite )
208
        {
209
            push @list, '"' . $file . '"';
210
        }
211
 
212
        if ( $#list >= 10 || $rewrite )
213
        {
214
            print "." unless ($opt_verbose > 1);
215
            process_list( @list );
216
            @list = "";
217
        }
218
 
219
        #
220
        #   Rewrite
221
        #   process a single entry. There will not be many of these
222
        #
223
        if ( $rewrite )
224
        {
225
            #
226
            #   Generate the MD5SUM info
227
            #
228
            print "." unless ($opt_verbose > 1);
229
            my $data = qx(md5sum "$file");
230
 
231
            #
232
            #   Rewrite any required file name
233
            #
234
            Verbose ("Rewrite: $fileonly to $rewrite");
235
            $data =~ s~$fileonly$~$rewrite~;
236
            push @num_rewrite, $file;
237
 
238
            #   Append to the output file
239
            #
240
            print MANIFEST "$data";
241
            chomp($data);
242
            Verbose2( "REWRITE: $data" );
243
        }
244
    }
245
    process_list( @list );
246
    close MANIFEST;
247
 
248
    #
249
    #   Informational warnings
250
    #
251
    Message("Number of files excluded : " . (1 + $#num_excluded), @num_excluded) if ( $#num_excluded >= 0);
252
    Message("Number of files rewritten: " . (1 + $#num_rewrite), @num_rewrite) if ( $#num_rewrite >= 0 );
253
 
254
}
255
 
256
sub process_list
257
{
258
    my @list = @_;
259
 
260
    #
261
    #   Anything to do
262
    #
263
    return if ( $#list < 0 );
264
 
265
#    print ".";
266
    open(CMD, "md5sum @list |") || Error "Can't run command: $!";
267
    while (<CMD>)
268
    {
269
        print MANIFEST $_;
270
        chomp;
271
        Verbose2("CMD: $_");
272
    }
273
    close CMD;
274
}
275
 
276
#-------------------------------------------------------------------------------
277
#   Validate the manifest
278
#
279
if ( $opt_check )
280
{
281
    my @failed;
282
    my $match_count = 0;
283
    #
284
    #   Determine the absolute path to the manifest file
285
    #   Required as the file may be a relative path
286
    #
287
    my $abs_manifest = AbsPath( $opt_manifest_file );
288
    Verbose("AbsPath manifest path: $abs_manifest");
289
 
290
    #
291
    #   Ensure the file exists
292
    #
293
    Error("Cannot locate manifest file: $opt_manifest_file") unless -f $opt_manifest_file;
294
    Error("Cannot locate abs manifest file: $opt_manifest_file") unless -f $abs_manifest;
295
 
296
    #
297
    #   Change to the specified subdirectory
298
    #
299
    if ( $opt_rootdir )
300
    {
301
        Verbose ("chdir to specified root directory");
302
        chdir( $opt_rootdir ) || Error("Specified Root Diretory not found: $opt_rootdir");
303
    }
304
 
305
    #
306
    #   Test Md5sum info for each file
307
    #   Process each line to allow success to be suppressed
308
    #
309
    Verbose ("Validate manifest file");
310
    open(CMD, "md5sum --check $abs_manifest  2>&1 |") || Error "Can't run command: $!";
311
    while (<CMD>)
312
    {
313
        chomp;
314
        Verbose2("CMD: $_");
315
        next if ( $_ =~ /^md5sum/ );
316
        if ( $_ =~ /: OK$/ )
317
        {
318
            $match_count++;
319
            next;
320
        };
321
        push @failed, $_;
322
    }
323
    close CMD;
324
 
325
    #
326
    #   Report failed files
327
    #
328
    Message ("Files validated: $match_count");
329
    if ( @failed )
330
    {
331
        Error("The following lines failed the Md5SUM:", @failed);
332
    }
333
}
334
 
335
exit 0;
336
 
337
 
338
#-------------------------------------------------------------------------------
339
#   Documentation
340
#
341
 
342
=pod
343
 
361 dpurdie 344
=for htmltoc    SYSUTIL::
345
 
227 dpurdie 346
=head1 NAME
347
 
348
jats_manifest - Generate and Test a manifest
349
 
350
=head1 SYNOPSIS
351
 
352
jats etool jats_manifest [options]
353
 
354
 Options:
355
    -help              - brief help message
356
    -help -help        - Detailed help message
357
    -man               - Full documentation
358
    -generate          - Generate the manifest(default)
359
    -check             - Check the provided manifest
360
    -manifest=file     - Path of the manifest file to use
361
    -rootdir=path      - Root of the file tree to process (default=.)
362
    -exclude=file      - File to excluded during generation
363
    -rewrite=old=new   - Filename to rewrite during generation
364
 
365
=head1 OPTIONS
366
 
367
=over 8
368
 
369
=item B<-help>
370
 
371
Print a brief help message and exits.
372
 
373
=item B<-help -help>
374
 
375
Print a detailed help message with an explanation for each option.
376
 
377
=item B<-man>
378
 
379
Prints the manual page and exits.
380
 
381
=item B<-generate>
382
 
383
Generate the  manifest file. This is the default operation.
384
 
385
This option may be used in conjunction with B<-check> to generate and check the
386
manifest in the one operation.
387
 
388
=item B<-check>
389
 
390
Check the provided manifest against the provide directory tree.
391
 
392
=item B<-manifest>
393
 
394
This option specifies the path to the manifest file to generate or check. If the
395
file is within the rootdir, then it may cause an error on checking.
396
 
397
This option is mandatory.
398
 
399
=item B<-rootdir=path>
400
 
401
This option specifies the root of the directory tree to process. If it is not
402
provided then then current directory will be used.
403
 
404
=item B<-exclude=file>
405
 
406
This option specifies the name of a file to be excluded from the generated
407
manifest. It has no effect during checking. The named file does not include
408
any path information. All files matching the name will be excluded.
409
 
410
This option may be used multiple times to exclude multiple files.
411
 
412
=item B<-rewrite=old=new>
413
 
414
This option allows a file to be renamed during the manifest generation
415
process. This may be used within the build environment to allow the auto.pl file
416
to be known as build.pl within the manifest.
417
 
418
This option may be used multiple times to rename multiple files.
419
 
420
=back
421
 
422
=head1 DESCRIPTION
423
 
424
This program will generate and check a "manifest" file.
425
 
426
The file consists of a line of information for each file within the
427
specified directory tress. The file is created and processed by the MD5SUM
428
utility.
429
 
430
=head1 EXAMPLE
431
 
432
=head2 Generation
433
 
434
 jats etool jats_manifest -manifest=MANIFEST
435
                          -rootdir=c:/clearcase/view/DPG_SWBase
436
                          -rewrite=auto.pl=build.pl
437
 
438
This command will generate a file called MANIFEST in the current directory based
439
on all files in the directory tree rooted at c:/clearcase/view/DPG_SWBase. The
440
file 'auto.pl' will be stored in the manifest as build.pl.
441
 
442
=head2 Checking
443
 
444
 jats etool jats_manifest -manifest=MANIFEST
445
                          -rootdir=c:/my_static_view/DPG_SWBase
446
                          -check
447
 
448
This command will check the directory tree rooted at
449
c:/my_static_view/DPG_SWBase against the MANIFEST file found in the local directory.
450
 
451
=cut
452