Subversion Repositories DevTools

Rev

Rev 6177 | 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
7009 dpurdie 192
    #   Note: -1 indicates the progarm didn't run at all
193
    #         Otherwise the upper 8 bits is the programs exit code
227 dpurdie 194
    #
195
    Verbose2 "System Result Code: $rv";
196
    Verbose2 "System Result Code: $!" if ($rv);
7009 dpurdie 197
    if ($rv eq -1) {
198
        $rv = -1;
199
    } else {
200
        $rv = $rv / 256;
201
    }
227 dpurdie 202
 
203
    #
204
    #   If ExitOnError is enabled, then force program termination
205
    #
206
    if ( $rv && $exit )
207
    {
208
        if ( $exit == 2 )
209
        {
210
            Error("Program terminated. Errors previously reported");
211
        }
212
 
213
        Error("System cmd failure. Exit Code: $rv",
214
              "Command: " . join ',', map ( "\"$_\"" , @cmd) );
215
    }
216
 
217
    return $rv;
218
}
219
 
220
#-------------------------------------------------------------------------------
221
# Function        : JatsCmd
222
#
223
# Description     : Issue a command to JATS.PL
224
#
225
# Inputs          : Command line
263 dpurdie 226
#                   This should be an array of arguments
227
#                   It will not be processed by a shell
227 dpurdie 228
# Returns         : Error code
229
#
230
sub JatsCmd
231
{
263 dpurdie 232
    EnvImport ('GBE_PERL');
233
    EnvImport ('GBE_CORE');
227 dpurdie 234
 
263 dpurdie 235
    System (  '--NoShell', $::GBE_PERL, "$::GBE_CORE/TOOLS/jats.pl", @_ );
227 dpurdie 236
}
237
 
263 dpurdie 238
#-------------------------------------------------------------------------------
239
# Function        : JatsTool
240
#
241
# Description     : Issue a command to JATS tool
242
#                   Don't invoke JATS wrapper - go straight to the tool
243
#
244
# Inputs          : Tool                        - With optional .pl extension
245
#                   Command line                - Tool command line
246
#
247
# Returns         : Error code
248
#
249
sub JatsTool
250
{
251
    EnvImport ('GBE_PERL');
252
    EnvImport ('GBE_CORE');
227 dpurdie 253
 
263 dpurdie 254
    my $cmd = shift;
255
    $cmd .= '.pl' unless ( $cmd =~ m~\.pl$~i );
256
 
257
    #
258
    #   Look in the standard places for a JATS tool
259
    #   These are all perl tools
260
    #
261
    my $path;
262
    foreach my $dir (  '/TOOLS/', '/TOOLS/DEPLOY/', '/TOOLS/LOCAL/', '')
263
    {
264
        Error ("JatsTool not found: $cmd") unless ( $dir );
265
        $path = $::GBE_CORE . $dir . $cmd;
266
        last if ( -f $path );
267
    }
268
 
269
    System ( '--NoShell', $::GBE_PERL, $path, @_ );
270
}
271
 
227 dpurdie 272
#-------------------------------------------------------------------------------
273
# Function        : LocateProgInPath
274
#
275
# Description     : Locate a program in the users path
276
#                   (Default) Stop if we get the the JATS bin directory
277
#                   The user should NOT be using programs that are not
278
#                   provided by JATS
279
#
280
# Inputs          : prog            - Program to locate
281
#                   args            - Options
282
#                                     --All : Search all of PATH
283
#                                             Used by build tools
261 dpurdie 284
#                                     --Path= User provided pathlist
227 dpurdie 285
#
286
# Returns         : Path name of the file
287
#
288
sub LocateProgInPath
289
{
290
    my ($prog, @args ) = @_;
291
    my $all = 0;
292
    my $stop_dir;
261 dpurdie 293
    my $upath = $ENV{PATH};
227 dpurdie 294
 
295
    #
296
    #   Extract arguments
297
    #
298
    foreach ( @args )
299
    {
261 dpurdie 300
        #
301
        #   Search in all paths: Don't limit ourselves to JATS
302
        #
227 dpurdie 303
        if ( m/^--All/ ) {
304
            $all = 1;
305
        }
261 dpurdie 306
 
307
        #
308
        #   User provided pathlist
309
        #   Allow for an empty list - which will use the default path
310
        #
311
        if ( m/^--Path=(.+)/ ) {
312
            if ( $1 ) {
313
                $upath = $1;
314
                $all = 1;
315
            }
316
        }
227 dpurdie 317
    }
318
 
319
    #
320
    #   Stop at the JATS BIN directory, unless requested otherwise
321
    #
322
    unless ( $all )
323
    {
324
        $stop_dir = "$ENV{GBE_CORE}/BIN.";
325
        $stop_dir =~ tr~\\/~/~s;
326
    }
327
 
328
    #
329
    #   A list of known extensions to scan
330
    #   Basically present so that we can use .exe files without the .exe name
331
    #
332
    my @elist;
333
    push @elist, '.exe' if ( $ScmHost ne "Unix" );
334
    push @elist, '.pl', '.sh', '.ksh', '';
335
 
336
    #
337
    #   If elist is empty then insert a defined entry
338
    #
339
    push @elist, '' unless ( @elist );
340
 
341
    #
342
    #   Scan all toolset directories
343
    #   for the program
344
    #
261 dpurdie 345
    for my $dir ( split ( $ScmPathSep, $upath ) )
227 dpurdie 346
    {
347
        for my $ext ( @elist )
348
        {
343 dpurdie 349
            my $tool = "$dir/$prog$ext";
227 dpurdie 350
            Debug2( "LocateProgInPath: Look for: $tool" );
351
 
352
            return $tool if ( -f $tool );
353
        }
354
 
355
        #
356
        #   Don't process any dirs beyond the JATS BIN directory
357
        #   The program MUST be provided by the JATS framework and not
358
        #   random user configuration
359
        #
360
        if (  $stop_dir )
361
        {
362
            $dir =~ tr~\\/~/~s;
363
            if ( $dir =~ /^$stop_dir/i)
364
            {
365
                Message ("LocateProgInPath: Stopped at JATS BIN");
366
                last;
367
            }
368
        }
369
    }
370
}
371
 
372
#-------------------------------------------------------------------------------
373
# Function        : QuoteCommand
374
#
375
# Description     : Return a string command that is quoted
376
#                   Do not quote empty elements
263 dpurdie 377
#                   Don't quote if already quoted
378
#                   Handle embedded quotes
227 dpurdie 379
#
380
# Inputs          : Array of element to quote
381
#
263 dpurdie 382
# Returns         : A string or an array
383
#                   Try to keep as an array
227 dpurdie 384
#
385
sub QuoteCommand
386
{
263 dpurdie 387
    my @cmd;
388
    foreach ( @_ )
227 dpurdie 389
    {
263 dpurdie 390
        next unless ( defined $_);             # Ignore empty
391
        next if ( $_ eq '' );
392
 
393
        if ( m~^"(.+)"$~ )                      # Ignore already quoted
394
        {
395
            push @cmd, $_;
396
            next;
397
        }
398
 
399
        my $data = $_;                          # Escape embedded "
400
        $data =~ s~"~\\"~g;
401
        push @cmd, '"' . $data . '"';           # Quote the argument
227 dpurdie 402
    }
263 dpurdie 403
 
404
    #
405
    #   Attempt to keep it as an array
406
    #
407
    return (wantarray) ? @cmd : join (' ', @cmd);
227 dpurdie 408
}
409
 
263 dpurdie 410
#-------------------------------------------------------------------------------
345 dpurdie 411
# Function        : EscapeCommand
263 dpurdie 412
#
345 dpurdie 413
# Description     : Escape input commands
347 dpurdie 414
#                   Can be called with two forms of arguments.
415
#                   If the there is only one item in the input list, then the
416
#                   command will be a single command that is to be processed
417
#                   by the shell. We cannot do escaping of space characters.
418
#
419
#                   If there is more than one item, then assume that each
420
#                   item will be a standalone command parameter - and we can
421
#                   quote spaces within the command stream.
422
#
263 dpurdie 423
#                   Must handle:
424
#                       Embedded "
345 dpurdie 425
#                       Embeded spaces
263 dpurdie 426
#                   Doesn't (yet) handle embedded \
427
#
428
# Inputs          : Array of elements to process
429
#
430
# Returns         : Return an escaped string
431
#
345 dpurdie 432
sub EscapeCommand
263 dpurdie 433
{
434
    my @cmd;
347 dpurdie 435
    my $arg_count = $#_;
263 dpurdie 436
 
437
    foreach ( @_ )
438
    {
439
        my $data = $_;
440
        next unless ( $data );
441
        $data =~ s~"~\\"~g;
347 dpurdie 442
        $data =~ s~ ~\\ ~g if ($arg_count > 0);
263 dpurdie 443
        push @cmd, $data;
444
    }
445
    return join (' ', @cmd);
446
}
447
 
227 dpurdie 448
1;
449