Subversion Repositories DevTools

Rev

Rev 227 | Rev 261 | 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
########################################################################
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
230
#
231
# Returns         : Path name of the file
232
#
233
sub LocateProgInPath
234
{
235
    my ($prog, @args ) = @_;
236
    my $all = 0;
237
    my $stop_dir;
238
 
239
    #
240
    #   Extract arguments
241
    #
242
    foreach ( @args )
243
    {
244
        if ( m/^--All/ ) {
245
            $all = 1;
246
        }
247
    }
248
 
249
    #
250
    #   Stop at the JATS BIN directory, unless requested otherwise
251
    #
252
    unless ( $all )
253
    {
254
        $stop_dir = "$ENV{GBE_CORE}/BIN.";
255
        $stop_dir =~ tr~\\/~/~s;
256
    }
257
 
258
    #
259
    #   A list of known extensions to scan
260
    #   Basically present so that we can use .exe files without the .exe name
261
    #
262
    my @elist;
263
    push @elist, '.exe' if ( $ScmHost ne "Unix" );
264
    push @elist, '.pl', '.sh', '.ksh', '';
265
 
266
    #
267
    #   If elist is empty then insert a defined entry
268
    #
269
    push @elist, '' unless ( @elist );
270
 
271
    #
272
    #   Scan all toolset directories
273
    #   for the program
274
    #
275
    for my $dir ( split ( $ScmPathSep, $ENV{PATH} ) )
276
    {
277
        for my $ext ( @elist )
278
        {
279
            my $tool = "$dir/$prog$ext";
280
            Debug2( "LocateProgInPath: Look for: $tool" );
281
 
282
            return $tool if ( -f $tool );
283
        }
284
 
285
        #
286
        #   Don't process any dirs beyond the JATS BIN directory
287
        #   The program MUST be provided by the JATS framework and not
288
        #   random user configuration
289
        #
290
        if (  $stop_dir )
291
        {
292
            $dir =~ tr~\\/~/~s;
293
            if ( $dir =~ /^$stop_dir/i)
294
            {
295
                Message ("LocateProgInPath: Stopped at JATS BIN");
296
                last;
297
            }
298
        }
299
    }
300
}
301
 
302
 
303
#-------------------------------------------------------------------------------
304
# Function        : QuoteCommand
305
#
306
# Description     : Return a string command that is quoted
307
#                   Do not quote empty elements
308
#
309
# Inputs          : Array of element to quote
310
#
311
# Returns         : A string
312
#
313
sub QuoteCommand
314
{
315
    my $cmd = '';
316
    my $pad = '';
317
    foreach  ( @_ )
318
    {
319
        next unless ( $_ );
320
        $cmd .= $pad . '"' . $_ . '"';
321
        $pad = ' ';
322
    }
323
    return $cmd;
324
}
325
 
326
 
327
1;
328