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 ) 2005 ERG Limited, All rights reserved
4
#
5
# Module name   : jats.sh
6
# Module type   : Perl Package
7
# Compiler(s)   : n/a
8
# Environment(s): jats
9
#
10
# Description   : This package contains functions to access system commands
11
#                 and programs.
12
#
13
#
14
#......................................................................#
15
 
255 dpurdie 16
use 5.006_001;
227 dpurdie 17
use strict;
18
use warnings;
19
 
20
package JatsSystem;
21
use JatsError;
22
use FileUtils;
23
 
24
 
25
our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS, $VERSION);
26
use Exporter;
27
 
28
$VERSION = 1.00;
29
@ISA = qw(Exporter);
30
 
31
# Symbols to autoexport (:DEFAULT tag)
32
@EXPORT = qw(
33
            System
34
            SystemConfig
35
            JatsCmd
36
            LocateProgInPath
37
            QuoteCommand
38
            );
39
 
40
# Non exported package globals go here
41
 
42
my $opt_test_mode           = 0;                # Test Mode disabled
43
my $opt_use_shell           = 0;                # Force a shell to be used
44
my $opt_exit_on_error       = 0;                # Force exit on error
45
 
46
my $GBE_BIN;                                    # From ENV
47
 
48
#-------------------------------------------------------------------------------
49
# Function        : SystemConfig
50
#
51
# Description     : Set the system command to TEST mode
52
#                   Command will not be executed. Only displayed
53
#
54
# Inputs          : Test        => Test Mode
55
#                   UseShell    => Default Shell Mode
56
#                   ExitOnError => Exit on Error Mode
57
#
58
# Returns         : Nothing
59
#
60
sub SystemConfig
61
{
62
    my %args = @_;
63
 
64
    while (my($key, $value) = each %args)
65
    {
66
        if (       $key =~ /^Test/i ) {
67
            $opt_test_mode = $value;
68
            Message("SystemTest Enabled") if $value;
69
 
70
        } elsif ( $key =~ /^UseShell/i ) {
71
            $opt_use_shell = $value;
72
 
73
        } elsif ( $key =~ /^ExitOnError/i ) {
74
            $opt_exit_on_error = $value;
75
 
76
        } else {
77
            Error("SystemConfig, Unknown option: $key");
78
        }
79
    }
80
}
81
 
82
#-------------------------------------------------------------------------------
83
# Function        : System
84
#
85
# Description     : Exec the specified command ...
86
#
87
# Inputs          : Options [Must be first]
88
#                       --Show              Force argument display
89
#                       --Shell             Force use of a Shell
90
#                       --NoShell           Force no Shell use
91
#                       --Exit              Force Exit on Error
92
#                       --ExitQuiet         Force Exit on Error, without display
93
#                       --NoExit            Force no exit on error
94
#                   Command
95
#                   Command args
96
#
97
# Returns         : Result code
98
#
99
sub System
100
{
101
    my @cmd;
102
    my( $rv );
103
    my $opt_show = 0;
104
    my $opt_prefix = "System:";
105
    my $shell = $opt_use_shell;
106
    my $exit = $opt_exit_on_error;
107
 
108
    #
109
    #   Strip off any leading options
110
    #
111
    my $just_collect;
112
    foreach ( @_ )
113
    {
114
        if ( $just_collect ) {
115
            push @cmd, $_;
116
            next;
117
 
118
        } elsif ( m/^--Show/ ) {
119
            $opt_show = 1;
120
        } elsif ( m/^--Shell/ ) {
121
            $shell = 1;
122
        } elsif ( m/^--NoShell/ ) {
123
            $shell = 0;
124
        } elsif ( m/^--ExitQuiet/ ) {
125
            $exit = 2;
126
        } elsif ( m/^--Exit/ ) {
127
            $exit = 1;
128
        } elsif ( m/^--NoExit/ ) {
129
            $exit = 0;
130
        } elsif ( m/^--/ ) {
131
            Warning("System: Unknown option(ignored): $_" );
132
        } else {
133
            $just_collect = 1;
134
            redo;
135
        }
136
    }
137
 
138
    #
139
    #   Prefix with Shell if required
140
    #
141
    if ( $shell )
142
    {
143
        #
144
        #   Fetch and cache GBE_BIN
145
        #
146
        unless ( $GBE_BIN )
147
        {
148
            $GBE_BIN = $ENV{GBE_BIN} || Error ("Environment variable GBE_BIN not set");
149
        }
150
 
151
        unshift @cmd, "$GBE_BIN/sh", "-c"
152
    }
153
 
154
    #
155
    #   Display the command
156
    #
157
    $opt_prefix = "System TEST:" if ($opt_test_mode);
158
    if ( $opt_show || $::ScmVerbose >= 2  )
159
    {
160
        my $line = $opt_prefix . ' ' . join ',', map ( "\"$_\"" , @cmd);
161
        Verbose2 ( $line) ;
162
        Message  ( $line ) if ( $opt_show );
163
    }
164
 
165
    #
166
    #   Simply return OK if in test mode
167
    #
168
    return 0 if ( $opt_test_mode );
169
 
170
    #
171
    #   Now do the hard bit
172
    #
173
    $rv = system( @cmd );
174
 
175
 
176
    #
177
    #   Report the result code
178
    #
179
    Verbose2 "System Result Code: $rv";
180
    Verbose2 "System Result Code: $!" if ($rv);
181
    $rv = $rv / 256;
182
 
183
    #
184
    #   If ExitOnError is enabled, then force program termination
185
    #
186
    if ( $rv && $exit )
187
    {
188
        if ( $exit == 2 )
189
        {
190
            Error("Program terminated. Errors previously reported");
191
        }
192
 
193
        Error("System cmd failure. Exit Code: $rv",
194
              "Command: " . join ',', map ( "\"$_\"" , @cmd) );
195
    }
196
 
197
    return $rv;
198
}
199
 
200
#-------------------------------------------------------------------------------
201
# Function        : JatsCmd
202
#
203
# Description     : Issue a command to JATS.PL
204
#
205
# Inputs          : Command line
206
#
207
# Returns         : Error code
208
#
209
sub JatsCmd
210
{
211
    my $GBE_PERL = $ENV{GBE_PERL} || Error ("Environment variable GBE_PERL not set");
212
    my $GBE_CORE = $ENV{GBE_CORE} || Error ("Environment variable GBE_CORE not set");
213
 
214
    System ( "$GBE_PERL $GBE_CORE/TOOLS/jats.pl @_" );
215
}
216
 
217
 
218
#-------------------------------------------------------------------------------
219
# Function        : LocateProgInPath
220
#
221
# Description     : Locate a program in the users path
222
#                   (Default) Stop if we get the the JATS bin directory
223
#                   The user should NOT be using programs that are not
224
#                   provided by JATS
225
#
226
# Inputs          : prog            - Program to locate
227
#                   args            - Options
228
#                                     --All : Search all of PATH
229
#                                             Used by build tools
261 dpurdie 230
#                                     --Path= User provided pathlist
227 dpurdie 231
#
232
# Returns         : Path name of the file
233
#
234
sub LocateProgInPath
235
{
236
    my ($prog, @args ) = @_;
237
    my $all = 0;
238
    my $stop_dir;
261 dpurdie 239
    my $upath = $ENV{PATH};
227 dpurdie 240
 
241
    #
242
    #   Extract arguments
243
    #
244
    foreach ( @args )
245
    {
261 dpurdie 246
        #
247
        #   Search in all paths: Don't limit ourselves to JATS
248
        #
227 dpurdie 249
        if ( m/^--All/ ) {
250
            $all = 1;
251
        }
261 dpurdie 252
 
253
        #
254
        #   User provided pathlist
255
        #   Allow for an empty list - which will use the default path
256
        #
257
        if ( m/^--Path=(.+)/ ) {
258
            if ( $1 ) {
259
                $upath = $1;
260
                $all = 1;
261
            }
262
        }
227 dpurdie 263
    }
264
 
265
    #
266
    #   Stop at the JATS BIN directory, unless requested otherwise
267
    #
268
    unless ( $all )
269
    {
270
        $stop_dir = "$ENV{GBE_CORE}/BIN.";
271
        $stop_dir =~ tr~\\/~/~s;
272
    }
273
 
274
    #
275
    #   A list of known extensions to scan
276
    #   Basically present so that we can use .exe files without the .exe name
277
    #
278
    my @elist;
279
    push @elist, '.exe' if ( $ScmHost ne "Unix" );
280
    push @elist, '.pl', '.sh', '.ksh', '';
281
 
282
    #
283
    #   If elist is empty then insert a defined entry
284
    #
285
    push @elist, '' unless ( @elist );
286
 
287
    #
288
    #   Scan all toolset directories
289
    #   for the program
290
    #
261 dpurdie 291
    for my $dir ( split ( $ScmPathSep, $upath ) )
227 dpurdie 292
    {
293
        for my $ext ( @elist )
294
        {
261 dpurdie 295
            my $tool = "$dir$ScmDirSep$prog$ext";
227 dpurdie 296
            Debug2( "LocateProgInPath: Look for: $tool" );
297
 
298
            return $tool if ( -f $tool );
299
        }
300
 
301
        #
302
        #   Don't process any dirs beyond the JATS BIN directory
303
        #   The program MUST be provided by the JATS framework and not
304
        #   random user configuration
305
        #
306
        if (  $stop_dir )
307
        {
308
            $dir =~ tr~\\/~/~s;
309
            if ( $dir =~ /^$stop_dir/i)
310
            {
311
                Message ("LocateProgInPath: Stopped at JATS BIN");
312
                last;
313
            }
314
        }
315
    }
316
}
317
 
318
 
319
#-------------------------------------------------------------------------------
320
# Function        : QuoteCommand
321
#
322
# Description     : Return a string command that is quoted
323
#                   Do not quote empty elements
324
#
325
# Inputs          : Array of element to quote
326
#
327
# Returns         : A string
328
#
329
sub QuoteCommand
330
{
331
    my $cmd = '';
332
    my $pad = '';
333
    foreach  ( @_ )
334
    {
335
        next unless ( $_ );
336
        $cmd .= $pad . '"' . $_ . '"';
337
        $pad = ' ';
338
    }
339
    return $cmd;
340
}
341
 
342
 
343
1;
344