Subversion Repositories DevTools

Rev

Rev 5710 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
227 dpurdie 1
#! perl
2
########################################################################
6177 dpurdie 3
# COPYRIGHT - VIX IP PTY LTD ("VIX"). ALL RIGHTS RESERVED.
227 dpurdie 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
 
263 dpurdie 20
#
21
#   System Wide Globals
22
#
23
our $GBE_BIN;                                   # From ENV
24
our $GBE_PERL;
25
our $GBE_CORE;
26
 
27
 
227 dpurdie 28
package JatsSystem;
29
use JatsError;
30
use FileUtils;
263 dpurdie 31
use JatsEnv;
227 dpurdie 32
 
33
 
34
our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS, $VERSION);
35
use Exporter;
36
 
37
$VERSION = 1.00;
38
@ISA = qw(Exporter);
39
 
40
# Symbols to autoexport (:DEFAULT tag)
41
@EXPORT = qw(
42
            System
43
            SystemConfig
44
            JatsCmd
263 dpurdie 45
            JatsTool
227 dpurdie 46
            LocateProgInPath
47
            QuoteCommand
48
            );
49
 
50
# Non exported package globals go here
51
 
52
my $opt_test_mode           = 0;                # Test Mode disabled
53
my $opt_use_shell           = 0;                # Force a shell to be used
54
my $opt_exit_on_error       = 0;                # Force exit on error
55
 
56
#-------------------------------------------------------------------------------
57
# Function        : SystemConfig
58
#
59
# Description     : Set the system command to TEST mode
60
#                   Command will not be executed. Only displayed
61
#
62
# Inputs          : Test        => Test Mode
63
#                   UseShell    => Default Shell Mode
64
#                   ExitOnError => Exit on Error Mode
65
#
66
# Returns         : Nothing
67
#
68
sub SystemConfig
69
{
70
    my %args = @_;
71
 
72
    while (my($key, $value) = each %args)
73
    {
74
        if (       $key =~ /^Test/i ) {
75
            $opt_test_mode = $value;
76
            Message("SystemTest Enabled") if $value;
77
 
78
        } elsif ( $key =~ /^UseShell/i ) {
79
            $opt_use_shell = $value;
80
 
81
        } elsif ( $key =~ /^ExitOnError/i ) {
82
            $opt_exit_on_error = $value;
83
 
84
        } else {
85
            Error("SystemConfig, Unknown option: $key");
86
        }
87
    }
88
}
89
 
90
#-------------------------------------------------------------------------------
91
# Function        : System
92
#
93
# Description     : Exec the specified command ...
94
#
95
# Inputs          : Options [Must be first]
96
#                       --Show              Force argument display
97
#                       --Shell             Force use of a Shell
98
#                       --NoShell           Force no Shell use
99
#                       --Exit              Force Exit on Error
100
#                       --ExitQuiet         Force Exit on Error, without display
101
#                       --NoExit            Force no exit on error
102
#                   Command
103
#                   Command args
104
#
105
# Returns         : Result code
106
#
107
sub System
108
{
109
    my @cmd;
110
    my( $rv );
111
    my $opt_show = 0;
112
    my $opt_prefix = "System:";
113
    my $shell = $opt_use_shell;
114
    my $exit = $opt_exit_on_error;
115
 
116
    #
117
    #   Strip off any leading options
118
    #
119
    my $just_collect;
120
    foreach ( @_ )
121
    {
4928 dpurdie 122
        # Ignore undefined item on the command line.
123
        # Makes use of ternary operators, for optional items, easier
124
        next unless defined $_;
125
 
227 dpurdie 126
        if ( $just_collect ) {
127
            push @cmd, $_;
128
            next;
129
 
130
        } elsif ( m/^--Show/ ) {
131
            $opt_show = 1;
132
        } elsif ( m/^--Shell/ ) {
133
            $shell = 1;
134
        } elsif ( m/^--NoShell/ ) {
135
            $shell = 0;
136
        } elsif ( m/^--ExitQuiet/ ) {
137
            $exit = 2;
138
        } elsif ( m/^--Exit/ ) {
139
            $exit = 1;
140
        } elsif ( m/^--NoExit/ ) {
141
            $exit = 0;
142
        } elsif ( m/^--/ ) {
143
            Warning("System: Unknown option(ignored): $_" );
144
        } else {
145
            $just_collect = 1;
146
            redo;
147
        }
148
    }
149
 
150
    #
151
    #   Prefix with Shell if required
152
    #
153
    if ( $shell )
154
    {
155
        #
156
        #   Fetch and cache GBE_BIN
157
        #
263 dpurdie 158
        EnvImport ('GBE_BIN')
159
            unless ( $::GBE_BIN );
227 dpurdie 160
 
263 dpurdie 161
        #
162
        #   Reform command
163
        #   With -c shell takes one argumemnt - not an array of args
345 dpurdie 164
        #   Escape the users command and enclose in quotes
263 dpurdie 165
        #
345 dpurdie 166
        @cmd = ( "$::GBE_BIN/sh", "-c", EscapeCommand(@cmd) );
227 dpurdie 167
    }
168
 
169
    #
170
    #   Display the command
171
    #
172
    $opt_prefix = "System TEST:" if ($opt_test_mode);
173
    if ( $opt_show || $::ScmVerbose >= 2  )
174
    {
175
        my $line = $opt_prefix . ' ' . join ',', map ( "\"$_\"" , @cmd);
176
        Verbose2 ( $line) ;
177
        Message  ( $line ) if ( $opt_show );
178
    }
179
 
180
    #
181
    #   Simply return OK if in test mode
182
    #
183
    return 0 if ( $opt_test_mode );
184
 
185
    #
186
    #   Now do the hard bit
187
    #
188
    $rv = system( @cmd );
189
 
190
    #
191
    #   Report the result code
192
    #
193
    Verbose2 "System Result Code: $rv";
194
    Verbose2 "System Result Code: $!" if ($rv);
195
    $rv = $rv / 256;
196
 
197
    #
198
    #   If ExitOnError is enabled, then force program termination
199
    #
200
    if ( $rv && $exit )
201
    {
202
        if ( $exit == 2 )
203
        {
204
            Error("Program terminated. Errors previously reported");
205
        }
206
 
207
        Error("System cmd failure. Exit Code: $rv",
208
              "Command: " . join ',', map ( "\"$_\"" , @cmd) );
209
    }
210
 
211
    return $rv;
212
}
213
 
214
#-------------------------------------------------------------------------------
215
# Function        : JatsCmd
216
#
217
# Description     : Issue a command to JATS.PL
218
#
219
# Inputs          : Command line
263 dpurdie 220
#                   This should be an array of arguments
221
#                   It will not be processed by a shell
227 dpurdie 222
# Returns         : Error code
223
#
224
sub JatsCmd
225
{
263 dpurdie 226
    EnvImport ('GBE_PERL');
227
    EnvImport ('GBE_CORE');
227 dpurdie 228
 
263 dpurdie 229
    System (  '--NoShell', $::GBE_PERL, "$::GBE_CORE/TOOLS/jats.pl", @_ );
227 dpurdie 230
}
231
 
263 dpurdie 232
#-------------------------------------------------------------------------------
233
# Function        : JatsTool
234
#
235
# Description     : Issue a command to JATS tool
236
#                   Don't invoke JATS wrapper - go straight to the tool
237
#
238
# Inputs          : Tool                        - With optional .pl extension
239
#                   Command line                - Tool command line
240
#
241
# Returns         : Error code
242
#
243
sub JatsTool
244
{
245
    EnvImport ('GBE_PERL');
246
    EnvImport ('GBE_CORE');
227 dpurdie 247
 
263 dpurdie 248
    my $cmd = shift;
249
    $cmd .= '.pl' unless ( $cmd =~ m~\.pl$~i );
250
 
251
    #
252
    #   Look in the standard places for a JATS tool
253
    #   These are all perl tools
254
    #
255
    my $path;
256
    foreach my $dir (  '/TOOLS/', '/TOOLS/DEPLOY/', '/TOOLS/LOCAL/', '')
257
    {
258
        Error ("JatsTool not found: $cmd") unless ( $dir );
259
        $path = $::GBE_CORE . $dir . $cmd;
260
        last if ( -f $path );
261
    }
262
 
263
    System ( '--NoShell', $::GBE_PERL, $path, @_ );
264
}
265
 
227 dpurdie 266
#-------------------------------------------------------------------------------
267
# Function        : LocateProgInPath
268
#
269
# Description     : Locate a program in the users path
270
#                   (Default) Stop if we get the the JATS bin directory
271
#                   The user should NOT be using programs that are not
272
#                   provided by JATS
273
#
274
# Inputs          : prog            - Program to locate
275
#                   args            - Options
276
#                                     --All : Search all of PATH
277
#                                             Used by build tools
261 dpurdie 278
#                                     --Path= User provided pathlist
227 dpurdie 279
#
280
# Returns         : Path name of the file
281
#
282
sub LocateProgInPath
283
{
284
    my ($prog, @args ) = @_;
285
    my $all = 0;
286
    my $stop_dir;
261 dpurdie 287
    my $upath = $ENV{PATH};
227 dpurdie 288
 
289
    #
290
    #   Extract arguments
291
    #
292
    foreach ( @args )
293
    {
261 dpurdie 294
        #
295
        #   Search in all paths: Don't limit ourselves to JATS
296
        #
227 dpurdie 297
        if ( m/^--All/ ) {
298
            $all = 1;
299
        }
261 dpurdie 300
 
301
        #
302
        #   User provided pathlist
303
        #   Allow for an empty list - which will use the default path
304
        #
305
        if ( m/^--Path=(.+)/ ) {
306
            if ( $1 ) {
307
                $upath = $1;
308
                $all = 1;
309
            }
310
        }
227 dpurdie 311
    }
312
 
313
    #
314
    #   Stop at the JATS BIN directory, unless requested otherwise
315
    #
316
    unless ( $all )
317
    {
318
        $stop_dir = "$ENV{GBE_CORE}/BIN.";
319
        $stop_dir =~ tr~\\/~/~s;
320
    }
321
 
322
    #
323
    #   A list of known extensions to scan
324
    #   Basically present so that we can use .exe files without the .exe name
325
    #
326
    my @elist;
327
    push @elist, '.exe' if ( $ScmHost ne "Unix" );
328
    push @elist, '.pl', '.sh', '.ksh', '';
329
 
330
    #
331
    #   If elist is empty then insert a defined entry
332
    #
333
    push @elist, '' unless ( @elist );
334
 
335
    #
336
    #   Scan all toolset directories
337
    #   for the program
338
    #
261 dpurdie 339
    for my $dir ( split ( $ScmPathSep, $upath ) )
227 dpurdie 340
    {
341
        for my $ext ( @elist )
342
        {
343 dpurdie 343
            my $tool = "$dir/$prog$ext";
227 dpurdie 344
            Debug2( "LocateProgInPath: Look for: $tool" );
345
 
346
            return $tool if ( -f $tool );
347
        }
348
 
349
        #
350
        #   Don't process any dirs beyond the JATS BIN directory
351
        #   The program MUST be provided by the JATS framework and not
352
        #   random user configuration
353
        #
354
        if (  $stop_dir )
355
        {
356
            $dir =~ tr~\\/~/~s;
357
            if ( $dir =~ /^$stop_dir/i)
358
            {
359
                Message ("LocateProgInPath: Stopped at JATS BIN");
360
                last;
361
            }
362
        }
363
    }
364
}
365
 
366
#-------------------------------------------------------------------------------
367
# Function        : QuoteCommand
368
#
369
# Description     : Return a string command that is quoted
370
#                   Do not quote empty elements
263 dpurdie 371
#                   Don't quote if already quoted
372
#                   Handle embedded quotes
227 dpurdie 373
#
374
# Inputs          : Array of element to quote
375
#
263 dpurdie 376
# Returns         : A string or an array
377
#                   Try to keep as an array
227 dpurdie 378
#
379
sub QuoteCommand
380
{
263 dpurdie 381
    my @cmd;
382
    foreach ( @_ )
227 dpurdie 383
    {
263 dpurdie 384
        next unless ( defined $_);             # Ignore empty
385
        next if ( $_ eq '' );
386
 
387
        if ( m~^"(.+)"$~ )                      # Ignore already quoted
388
        {
389
            push @cmd, $_;
390
            next;
391
        }
392
 
393
        my $data = $_;                          # Escape embedded "
394
        $data =~ s~"~\\"~g;
395
        push @cmd, '"' . $data . '"';           # Quote the argument
227 dpurdie 396
    }
263 dpurdie 397
 
398
    #
399
    #   Attempt to keep it as an array
400
    #
401
    return (wantarray) ? @cmd : join (' ', @cmd);
227 dpurdie 402
}
403
 
263 dpurdie 404
#-------------------------------------------------------------------------------
345 dpurdie 405
# Function        : EscapeCommand
263 dpurdie 406
#
345 dpurdie 407
# Description     : Escape input commands
347 dpurdie 408
#                   Can be called with two forms of arguments.
409
#                   If the there is only one item in the input list, then the
410
#                   command will be a single command that is to be processed
411
#                   by the shell. We cannot do escaping of space characters.
412
#
413
#                   If there is more than one item, then assume that each
414
#                   item will be a standalone command parameter - and we can
415
#                   quote spaces within the command stream.
416
#
263 dpurdie 417
#                   Must handle:
418
#                       Embedded "
345 dpurdie 419
#                       Embeded spaces
263 dpurdie 420
#                   Doesn't (yet) handle embedded \
421
#
422
# Inputs          : Array of elements to process
423
#
424
# Returns         : Return an escaped string
425
#
345 dpurdie 426
sub EscapeCommand
263 dpurdie 427
{
428
    my @cmd;
347 dpurdie 429
    my $arg_count = $#_;
263 dpurdie 430
 
431
    foreach ( @_ )
432
    {
433
        my $data = $_;
434
        next unless ( $data );
435
        $data =~ s~"~\\"~g;
347 dpurdie 436
        $data =~ s~ ~\\ ~g if ($arg_count > 0);
263 dpurdie 437
        push @cmd, $data;
438
    }
439
    return join (' ', @cmd);
440
}
441
 
227 dpurdie 442
1;
443