Subversion Repositories DevTools

Rev

Rev 235 | Rev 321 | Go to most recent revision | 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
51
              rm_rf
52
              rm_f
53
              mkpath
54
              printenv
55
              printargs
56
            );
57
 
58
use File::Path qw(rmtree);
59
 
60
#BEGIN
61
#{
62
#    print "-------jats_runtime initiated\n";
63
#}
64
 
65
#-------------------------------------------------------------------------------
66
# Function        : rmlitter
67
#
68
# Description     : Remove litter from a build directory
69
#
70
# Inputs          : ARGV    A list of files (with wildcards) to delete in the
71
#                           current, and named, directories.
72
#
73
#                           Options:    -f File list follows (default)
74
#                                       -d Dir  list follows
75
#
76
#                           Example:    *.err -d OBJ BIN
77
#                                       Will delete *.err OBJ/*.err BIN/*.err
78
#
79
# Returns         : 0
80
#
81
sub rmlitter
82
{
83
    my @flist;
84
    my @dlist = '.';
85
 
86
    #
87
    #   Parse arguments
88
    #   Collect filenames and dirnames. Switch between the two collection lists
89
    #
90
    #
91
    my $listp = \@flist;
92
    foreach my $ii ( @ARGV )
93
    {
94
        if ( $ii eq '-f' ) {
95
            $listp = \@flist;
96
 
97
        } elsif ( $ii eq '-d' ) {
98
            $listp = \@dlist;
99
 
100
        } else {
101
            push @$listp, $ii;
102
        }
103
    }
104
 
105
    #
106
    #   Process all directories looking for matching files
107
    #   Delete files
108
    #
109
    foreach my $dir ( @dlist )
110
    {
111
        foreach my $file ( @flist )
112
        {
113
            my $path = "$dir/$file";
114
            $path =~ s~ ~\\ ~g;
115
            my @del = glob ( $path );
116
            if ( @del )
117
            {
118
                chmod '777', @del;
119
                unlink @del;
120
            }
121
        }
122
    }
123
}
124
 
125
#-------------------------------------------------------------------------------
126
# Function        : expand_wildcards
127
#
128
# Description     : Expand argument wildcards
129
#                   Replace @ARGV with an expanded list of files to process
130
#                   This is a helper function
131
#
132
#
133
# Inputs          : @ARGV
134
#
135
# Returns         : @ARGV
136
#
137
sub expand_wildcards
138
{
139
    #
140
    #   Replace spaces with escaped spaces to assist the 'glob'
141
    #
142
    sub escape_space
143
    {
144
        my ($item) = @_;
145
        $item =~ s~ ~\\ ~g;
146
        return $item;
147
    }
148
    @ARGV = map(/[*?]/o ? glob (escape_space($_)) : $_ , @ARGV);
149
}
150
 
151
#-------------------------------------------------------------------------------
152
# Function        : rm_rf
153
#
154
# Description     : Remove all files and directories specified
155
#
156
# Inputs          : @ARGV       - A list of files and directories
157
#
158
# Returns         : Nothing
159
#
160
sub rm_rf
161
{
162
    expand_wildcards();
163
    my @dirs =  grep -e $_,@ARGV;
164
    if ( @dirs )
165
    {
166
        rmtree(\@dirs,0,0);
167
    }
168
}
169
 
170
#-------------------------------------------------------------------------------
171
# Function        : rm_f
172
#
173
# Description     : Remove all named files
174
#                   Will not remove directores - even if named
175
#
176
# Inputs          : @ARGV       - A list of files to delete
177
#
178
# Returns         :
179
#
180
sub rm_f {
181
    expand_wildcards();
182
 
183
    foreach my $file (@ARGV) {
184
        next unless -f $file;
185
 
186
        next if _unlink($file);
187
 
188
        chmod(0777, $file);
189
 
190
        next if _unlink($file);
191
        Warning "Cannot delete $file: $!";
192
    }
193
}
194
 
195
#-------------------------------------------------------------------------------
196
# Function        : mkpath
197
#
198
# Description     : Create a directory tree
199
#                   This will create all the parent directories in the path
200
#
201
# Inputs          : @ARGV   - An array of paths to create
202
#
203
# Returns         :
204
#
205
sub mkpath
206
{
207
    expand_wildcards();
208
    File::Path::mkpath([@ARGV],0,0777);
209
}
210
 
211
#-------------------------------------------------------------------------------
212
# Function        : _unlink
213
#
214
# Description     : Helper function
215
#                   Unlink a list of files
216
#
217
# Inputs          : A list of files to delete
218
#
219
# Returns         : The number of files that have been deleted
220
#
221
sub _unlink {
222
    my $files_unlinked = 0;
223
 
224
    foreach my $file (@_)
225
    {
226
        my $delete_count = 0;
227
        $delete_count++ while unlink $file;
228
        $files_unlinked++ if $delete_count;
229
    }
230
    return $files_unlinked;
231
}
232
 
233
#-------------------------------------------------------------------------------
234
# Function        : printenv
235
#
236
# Description     : 
237
#
238
# Inputs          : 
239
#
240
# Returns         : 
241
#
242
sub printenv
243
{
244
    foreach my $entry ( sort keys %ENV )
245
    {
246
        print "------------------- $entry=$ENV{$entry}\n";
247
    }
248
}
249
 
250
#-------------------------------------------------------------------------------
251
# Function        : printargs
252
#
253
# Description     : Print my argumements
254
#
255
# Inputs          : User arguments
256
#
257
# Returns         : Nothing
258
#
259
my $PSPLIT=':';
260
sub printargs
261
{
262
    Message "printargs....";
263
    Message "Program arguments", @ARGV;
264
 
265
    $PSPLIT = ';' if ( $ENV{GBE_MACHTYPE} eq 'win32' );
266
 
267
    sub penv
268
    {
269
        my ($var) = @_;
270
        pvar ($var, $ENV{$var} || '');
271
    }
272
    # Simple print of name and variable
273
    sub pvar
274
    {
275
        my ($text, $data) = @_;
276
        printf "%-17s= %s\n", $text, $data;
277
    }
278
 
279
    sub alist
280
    {
281
        my ($text, @var) = @_;
282
        my $sep = "=";
283
        for ( @var )
284
        {
285
            my $valid = ( -d $_ || -f $_ ) ? " " : "*";
286
            printf "%-17s%s%s%s\n", $text, $sep, $valid, $_;
287
            $text = "";
288
            $sep = " ";
289
        }
290
    }
291
 
292
    #   Display a ';' or ':' separated list, one entry per line
293
    sub dlist
294
    {
295
        my ($text, $var) = @_;
296
        alist( $text, split $PSPLIT, $var || " " );
297
    }
298
 
299
    Message ("Complete environment dump");
300
    foreach my $var ( sort keys(%ENV) )
301
    {
302
       penv  ($var);
303
    }
304
 
305
    dlist   "PATH"            , $ENV{PATH};
306
    exit (999);
307
}
308
 
309
1;