Subversion Repositories DevTools

Rev

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

Rev Author Line No. Line
235 dpurdie 1
########################################################################
2
# Copyright (C) 2007 ERG Limited, All rights reserved
3
#
4
# Module name   : jats.sh
5
# Module type   : Makefile system
6
# Compiler(s)   : n/a
7
# Environment(s): jats
8
#
9
# Description   : JATS Make Time Support
10
#                 This package contains a collection of very useful functions
11
#                 that are invoked by the JATS generated makefiles to perform
12
#                 complicated operations at Make Time
13
#
14
#                 The functions are designed to be invoked as:
15
#                   $(GBE_PERL) -Mjats_runtime -e <function> -- <args>+
16
#
17
#                 The functions in this packages are designed to take parameters
18
#                 from @ARVG as this makes the interface easier to read.
19
#
20
#                 This package is used to speedup and simplify the JATS builds
21
#                 Speedup (under windows)
22
#                       Its quicker to start up one perl instance than
23
#                       to invoke a shell script that performs multiple commands
24
#                       Windows is very slow in forking another task.
25
#
26
#                 Simplify
27
#                       Removes some of the complications incurred due to different
28
#                       behaviour of utilities on different platforms. In particular
29
#                       the 'rm' command
30
#
31
#                       Perl is a better cross platform language than shell script
32
#                       as we have greater control over the release of perl.
33
#
34
#......................................................................#
35
 
255 dpurdie 36
require 5.006_001;
235 dpurdie 37
use strict;
38
use warnings;
39
 
40
package jats_runtime;
41
 
42
our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS, $VERSION);
43
use Exporter;
44
use JatsError qw(:name=jats_runtime);
45
 
46
$VERSION = 1.00;
47
@ISA = qw(Exporter);
48
 
49
# Symbols to autoexport (:DEFAULT tag)
50
@EXPORT = qw( rmlitter
321 dpurdie 51
              rm_opr
235 dpurdie 52
              rm_rf
53
              rm_f
54
              mkpath
55
              printenv
56
              printargs
321 dpurdie 57
              echo
235 dpurdie 58
            );
59
 
60
use File::Path qw(rmtree);
321 dpurdie 61
our %opts;
235 dpurdie 62
 
63
#BEGIN
64
#{
65
#    print "-------jats_runtime initiated\n";
66
#}
67
 
68
#-------------------------------------------------------------------------------
321 dpurdie 69
# Function        : process_options
70
#
71
# Description     : Extract options from the front of the command stream
72
#                   Stops at the first argument that doesn't start with a
73
#                   '--'
74
#
75
#                   Options of the form --Opt=Val are split out
76
#                   Options of the form --Opt will set (or increment a value)
77
#
78
# Inputs          : None: Uses global ARGV
79
#
80
# Returns         : None: Resets global argv
81
#                         Populates the %opts hash
82
#
83
sub process_options
84
{
85
    while ( my $entry = shift @ARGV )
86
    {
87
        last if ( $entry eq '--' );
88
        if ( $entry =~  m/^--(.*)/  )
89
        {
90
            if ( $1 =~ m/(.*)=(.*)/ )
91
            {
92
                $opts{$1} = $2;
93
            }
94
            else
95
            {
96
                $opts{$1}++;
97
            }
98
        }
99
        else
100
        {
101
            unshift @ARGV, $entry;
102
            last;
103
        }
104
    }
105
    #
106
    #   Process some known options
107
    #
108
    $opts{'Progress'} = $opts{'Verbose'}    if ( $opts{'Verbose'} );
109
    ErrorConfig( 'name', $opts{Name})       if ( $opts{'Name'} );
110
    ErrorConfig( 'verbose', $opts{Verbose}) if ( $opts{'Verbose'} );
111
    DebugDumpData("RunTime Opts", \%opts )  if ( $opts{'ShowOpts'} );;
112
    Message ("RunTime args: @ARGV")         if ( $opts{'ShowArgs'} );
113
    printenv()                              if ( $opts{'ShowEnv'} );
114
    Message ($opts{'Message'})              if ( $opts{'Message'} );
115
}
116
 
117
#-------------------------------------------------------------------------------
235 dpurdie 118
# Function        : rmlitter
119
#
120
# Description     : Remove litter from a build directory
121
#
122
# Inputs          : ARGV    A list of files (with wildcards) to delete in the
123
#                           current, and named, directories.
124
#
125
#                           Options:    -f File list follows (default)
126
#                                       -d Dir  list follows
127
#
128
#                           Example:    *.err -d OBJ BIN
129
#                                       Will delete *.err OBJ/*.err BIN/*.err
130
#
131
# Returns         : 0
132
#
133
sub rmlitter
134
{
321 dpurdie 135
    process_options();
136
 
235 dpurdie 137
    my @flist;
138
    my @dlist = '.';
139
 
140
    #
141
    #   Parse arguments
142
    #   Collect filenames and dirnames. Switch between the two collection lists
143
    #
144
    #
145
    my $listp = \@flist;
146
    foreach my $ii ( @ARGV )
147
    {
148
        if ( $ii eq '-f' ) {
149
            $listp = \@flist;
150
 
151
        } elsif ( $ii eq '-d' ) {
152
            $listp = \@dlist;
153
 
154
        } else {
155
            push @$listp, $ii;
156
        }
157
    }
158
 
159
    #
160
    #   Process all directories looking for matching files
161
    #   Delete files
162
    #
163
    foreach my $dir ( @dlist )
164
    {
165
        foreach my $file ( @flist )
166
        {
167
            my $path = "$dir/$file";
168
            $path =~ s~ ~\\ ~g;
169
            my @del = glob ( $path );
170
            if ( @del )
171
            {
321 dpurdie 172
                Message ("rmlitter. @del") if ($opts{'Progress'} );
235 dpurdie 173
                chmod '777', @del;
174
                unlink @del;
175
            }
176
        }
177
    }
178
}
179
 
180
#-------------------------------------------------------------------------------
181
# Function        : expand_wildcards
182
#
183
# Description     : Expand argument wildcards
184
#                   Replace @ARGV with an expanded list of files to process
185
#                   This is a helper function
186
#
187
#
188
# Inputs          : @ARGV
189
#
190
# Returns         : @ARGV
191
#
192
sub expand_wildcards
193
{
194
    #
195
    #   Replace spaces with escaped spaces to assist the 'glob'
196
    #
197
    sub escape_space
198
    {
199
        my ($item) = @_;
200
        $item =~ s~ ~\\ ~g;
201
        return $item;
202
    }
203
    @ARGV = map(/[*?]/o ? glob (escape_space($_)) : $_ , @ARGV);
204
}
205
 
206
#-------------------------------------------------------------------------------
207
# Function        : rm_rf
208
#
209
# Description     : Remove all files and directories specified
210
#
211
# Inputs          : @ARGV       - A list of files and directories
212
#
213
# Returns         : Nothing
214
#
215
sub rm_rf
216
{
321 dpurdie 217
    process_options();
235 dpurdie 218
    expand_wildcards();
219
    my @dirs =  grep -e $_,@ARGV;
220
    if ( @dirs )
221
    {
222
        rmtree(\@dirs,0,0);
223
    }
224
}
225
 
226
#-------------------------------------------------------------------------------
227
# Function        : rm_f
228
#
229
# Description     : Remove all named files
230
#                   Will not remove directores - even if named
231
#
321 dpurdie 232
#                   Unix Note:
233
#                   Need to handle broken soft links
234
#
235
#
235 dpurdie 236
# Inputs          : @ARGV       - A list of files to delete
237
#
238
# Returns         :
239
#
240
sub rm_f {
321 dpurdie 241
    process_options();
235 dpurdie 242
    expand_wildcards();
243
 
244
    foreach my $file (@ARGV) {
321 dpurdie 245
        Message ("Delete: $file") if ($opts{'Progress'} );
246
        next if -d $file;
247
        next unless ( -e $file || -l $file );
235 dpurdie 248
        next if _unlink($file);
321 dpurdie 249
        Warning "Cannot delete $file: $!";
250
    }
251
}
235 dpurdie 252
 
321 dpurdie 253
#-------------------------------------------------------------------------------
254
# Function        : rm_opr
255
#
256
# Description     : Combo deletion operation
257
#                   Parameter driven to delete many things in one command
258
#
259
# Inputs          : Options and paths
260
#                   Options. Set mode for following paths
261
#                       -f   remove named file
262
#                       -d   remove named directory if empty
263
#                       -rf  remove directory or file
264
#                       -fd  remove file and directory if empty
265
#
266
# Returns         : 
267
#
268
sub rm_opr
269
{
270
    my $mode = '-f';
271
    process_options();
272
    foreach my $file (@ARGV) {
273
        if ( $file eq '-f' ) {
274
            $mode = $file;
275
        } elsif ( $file eq '-d' ) {
276
            $mode =$file;
277
        } elsif ( $file eq '-rf' ) {
278
            $mode =$file;
279
        } elsif ( $file eq '-fd' ) {
280
            $mode =$file;
281
        } elsif ( $file =~ m/^-/ ) {
282
            Error ("rm_opr - unknown option: $file");
283
        } else {
284
            #
285
            #   Not an option must be a file/dir to delete
286
            #
287
            if ( $mode eq '-f' ) {
288
                Message ("Delete File: $file") if ($opts{'Progress'} );
289
                _unlink($file);
235 dpurdie 290
 
321 dpurdie 291
            } elsif ( $mode eq '-d' ) {
292
                Message ("Delete Empty Dir: $file") if ($opts{'Progress'} );
293
                rmdir $file;
294
 
295
            } elsif ( $mode eq '-rf' ) {
296
                Message ("Delete Dir: $file") if ($opts{'Progress'} );
297
                rmtree($file,0,0);
298
 
299
            } elsif ( $mode eq '-fd' ) {
300
                Message ("Delete File: $file") if ($opts{'Progress'} );
301
                _unlink($file);
302
                my $dir = $file;
303
                $dir =~ tr~\\/~/~s;
304
                Message ("Remove Empty Dir: $dir") if ($opts{'Progress'} );
305
                if ( $dir =~ s~/[^/]+$~~ )
306
                {
307
                    rmdir $dir;
308
                }
309
            }
310
        }
235 dpurdie 311
    }
312
}
313
 
321 dpurdie 314
 
235 dpurdie 315
#-------------------------------------------------------------------------------
316
# Function        : mkpath
317
#
318
# Description     : Create a directory tree
319
#                   This will create all the parent directories in the path
320
#
321
# Inputs          : @ARGV   - An array of paths to create
322
#
323
# Returns         :
324
#
325
sub mkpath
326
{
321 dpurdie 327
    process_options();
235 dpurdie 328
    expand_wildcards();
329
    File::Path::mkpath([@ARGV],0,0777);
330
}
331
 
332
#-------------------------------------------------------------------------------
333
# Function        : _unlink
334
#
335
# Description     : Helper function
336
#                   Unlink a list of files
337
#
321 dpurdie 338
# Inputs          : A file to delete
235 dpurdie 339
#
321 dpurdie 340
# Returns         : False: File still exists
235 dpurdie 341
#
342
sub _unlink {
321 dpurdie 343
    my ($file) = @_;
344
    if ( ! unlink $file  )
235 dpurdie 345
    {
321 dpurdie 346
        chmod(0777, $file);
347
        return unlink $file;
235 dpurdie 348
    }
321 dpurdie 349
    return 1;
235 dpurdie 350
}
351
 
352
#-------------------------------------------------------------------------------
353
# Function        : printenv
354
#
355
# Description     : 
356
#
357
# Inputs          : 
358
#
359
# Returns         : 
360
#
361
sub printenv
362
{
363
    foreach my $entry ( sort keys %ENV )
364
    {
321 dpurdie 365
        print "    $entry=$ENV{$entry}\n";
235 dpurdie 366
    }
367
}
368
 
369
#-------------------------------------------------------------------------------
370
# Function        : printargs
371
#
372
# Description     : Print my argumements
373
#
374
# Inputs          : User arguments
375
#
376
# Returns         : Nothing
377
#
378
sub printargs
379
{
321 dpurdie 380
    Message "Arguments", @ARGV;
381
}
382
 
383
#-------------------------------------------------------------------------------
384
# Function        : echo
385
#
386
# Description     : echo my argumements
387
#
388
# Inputs          : User arguments
389
#
390
# Returns         : Nothing
391
#
392
sub echo
393
{
394
    process_options();
395
    Message @ARGV;
396
}
397
 
398
#-------------------------------------------------------------------------------
399
# Function        : printArgsEnv
400
#
401
# Description     : Print my argumements nd environmen
402
#
403
# Inputs          : User arguments
404
#
405
# Returns         : Nothing
406
#
407
my $PSPLIT=':';
408
sub printArgsEnv
409
{
235 dpurdie 410
    Message "printargs....";
411
    Message "Program arguments", @ARGV;
412
 
413
    $PSPLIT = ';' if ( $ENV{GBE_MACHTYPE} eq 'win32' );
414
 
415
    sub penv
416
    {
417
        my ($var) = @_;
418
        pvar ($var, $ENV{$var} || '');
419
    }
420
    # Simple print of name and variable
421
    sub pvar
422
    {
423
        my ($text, $data) = @_;
424
        printf "%-17s= %s\n", $text, $data;
425
    }
426
 
427
    sub alist
428
    {
429
        my ($text, @var) = @_;
430
        my $sep = "=";
431
        for ( @var )
432
        {
433
            my $valid = ( -d $_ || -f $_ ) ? " " : "*";
434
            printf "%-17s%s%s%s\n", $text, $sep, $valid, $_;
435
            $text = "";
436
            $sep = " ";
437
        }
438
    }
439
 
440
    #   Display a ';' or ':' separated list, one entry per line
441
    sub dlist
442
    {
443
        my ($text, $var) = @_;
444
        alist( $text, split $PSPLIT, $var || " " );
445
    }
446
 
447
    Message ("Complete environment dump");
448
    foreach my $var ( sort keys(%ENV) )
449
    {
450
       penv  ($var);
451
    }
452
 
453
    dlist   "PATH"            , $ENV{PATH};
454
    exit (999);
455
}
456
 
457
1;