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) 2006 ERG Limited, All rights reserved
4
#
5
# Module name   : jats.sh
6
# Module type   : Makefile system
7
# Compiler(s)   : n/a
8
# Environment(s): jats
9
#
10
# Description   : This utility is used within the automated test environment
11
#                 to assist in the testing of packages that load shared
12
#                 libraries by name.
13
#
14
#                 The program will
15
#                   1) Locate the named library in the PATH
16
#                      PATH(Win32) LD_LIBRARY_PATH(unix)
17
#                   2) Rewrite a file replacing a tag with the full path
18
#                      to the file
19
# Usage:
20
#
21
# Version   Who      Date        Description
22
#
23
#......................................................................#
24
 
255 dpurdie 25
require 5.006_001;
227 dpurdie 26
use strict;
27
use warnings;
28
 
29
use JatsError;
30
use FileUtils;
31
use Getopt::Long;
32
use Pod::Usage;
33
 
34
#
35
#   Global variables
36
#
37
my $VERSION     = "1.0.0";
38
my $opt_debug   = $ENV{'GBE_DEBUG'};        # Allow global debug
39
my $opt_verbose = $ENV{'GBE_VERBOSE'};      # Allow global verbose
40
my $opt_type    = $ENV{'GBE_TYPE'};
41
my $opt_help = 0;
42
my $opt_manual = 0;
43
my $opt_smart_names = 1;
44
 
45
my $opt_inifile;
46
my $opt_tag;
47
my $opt_libname;
48
 
49
our $ScmHost;
50
our $ScmPathSep;
51
 
52
#-------------------------------------------------------------------------------
53
# Function        :
54
#
55
# Description     : Main entry point
56
#
57
# Inputs          : @ARGV
58
#
59
# Returns         : Non-zero if an error is detected
60
#                   Zero if all is well
61
#
62
 
63
#
64
#   Process user options
65
#
66
my $result = GetOptions (
67
                "help+"         => \$opt_help,              # flag, multiple use allowed
68
                "manual"        => \$opt_manual,            # flag
69
                "verbose+"      => \$opt_verbose,           # flag, multiple use allowed
70
                "type=s"        => \$opt_type,              # string
71
                "inifile=s"     => \$opt_inifile,           # string
72
                "library=s"     => \$opt_libname,           # string
73
                "tag=s"         => \$opt_tag,               # string
74
                "smart!"        => \$opt_smart_names,       # [No]Flag
75
 
76
                );
77
 
78
                #
79
                #   UPDATE THE DOCUMENTATION AT THE END OF THIS FILE !!!
80
                #
81
 
82
#
83
#   Process help and manual options
84
#
85
pod2usage(-verbose => 0, -message => "Version: $VERSION") if ($opt_help == 1 || ! $result);
86
pod2usage(-verbose => 1) if ($opt_help == 2 );
87
pod2usage(-verbose => 2) if ($opt_manual || ($opt_help > 2));
88
pod2usage(-verbose => 0, -message => "Version: $VERSION") if ( $#ARGV >= 0 );
89
 
90
#
91
#   Initialise used packages
92
#
93
ErrorConfig ('name' => 'LibIni',
94
             'verbose' => $opt_verbose,
95
             'debug' => $opt_debug );
96
InitFileUtils();
97
 
98
#
99
#   Sanity test the user parameters
100
#
101
Error("No ini file specified" ) unless ( $opt_inifile );
102
Error("Ini file not found: $opt_inifile") unless ( -f $opt_inifile );
103
 
104
Error("No 'tag' specified" ) unless ( $opt_tag );
105
 
106
Error("No Shared Library specified") unless ( $opt_libname );
107
 
108
Error("Type of library not known. GBE_TYPE has not been set") unless ( $opt_type );
109
$opt_type = '' if ( $opt_type eq 'none' );
110
 
111
#
112
#   Display user parameters
113
#
114
Verbose ("Ini: $opt_inifile" );
115
Verbose ("Tag:$opt_tag" );
116
Verbose ("Lib:$opt_libname");
117
Verbose ("Type:$opt_type");
118
 
119
#
120
#   Determine the path to search
121
#   Windows a linux are different
122
#
123
my $pname = ($ScmHost eq "Unix") ? 'LD_LIBRARY_PATH' : 'PATH';
124
my $ext   = ($ScmHost eq "Unix") ? '.so' : '.dll';
125
my $pre   = ($ScmHost eq "Unix") ? 'lib' : '';
126
my $libfile = "$pre$opt_libname$opt_type$ext";
127
 
128
Verbose("Library Path EnvVar: $pname");
129
Verbose("Library Ext : $ext");
130
Verbose("Library File: $libfile");
131
 
132
Error ("No library search path variable in environment: $pname")
133
    unless ( exists $ENV{$pname} );
134
 
135
#
136
#   Walk the path looking for the specified library
137
#   Detect multiple instances
138
#
139
my @found = ();
140
foreach my $dir ( split( $ScmPathSep, $ENV{$pname})  )
141
{
142
    Verbose2("Looking in: $dir");
143
    if ( -f "$dir/$libfile" || -l  "$dir/$libfile"  )
144
    {
145
        Verbose("Found library in: $dir");
146
        push @found, $dir;
147
    }
148
}
149
 
150
#
151
#   Did we get exactly one library
152
#
153
Error ("Library not found",
154
       "Library Name: $libfile",
155
       "Search path:", $ENV{$pname} ) if ($#found < 0);
156
 
157
Error ("Multiple instances of the library found",
158
       "Library Name: $libfile",
159
       "Found in", @found )if ($#found > 0);
160
 
161
#
162
#   Determine replacement string
163
#
164
my $replace = $found[0] . '/' . $libfile;
165
Verbose ("Tag: $opt_tag");
166
Verbose ("Replace: $replace");
167
 
168
#
169
#   Have one instance of the library
170
#   No for the ini file
171
#
172
#   Slurp in the entire ini file
173
#   Do not attempt to process line endings
174
#
175
undef $/;                    # slurp mode (undef)
176
open(my $fh, "<", $opt_inifile) or Error("Cannot open the ini file for reading");
177
my $ini_text = <$fh>;
178
close($fh);
179
 
180
#
181
#   Replace the tag within the file
182
#       Quote metacharacters in the tag string
183
#       Perform Windows filename translation
184
#
185
$opt_tag = quotemeta ($opt_tag);
186
$replace =~ s~/~\\~g if ($opt_smart_names && $ScmHost ne "Unix");
187
 
188
Verbose2 ("Inifile: Before replacement:\n",$ini_text);
189
unless ($ini_text =~ s~$opt_tag~$replace~g )
190
{
191
    Error ("No replacement performed for tag: $opt_tag");
192
}
193
Verbose2 ("Inifile: After replacement:\n",$ini_text);
194
 
195
#
196
#   Save the file out
197
#
198
open($fh, ">", $opt_inifile) or Error("Cannot open the ini file for writing");
199
print $fh $ini_text;
200
close($fh);
201
 
202
exit 0;
203
 
204
#-------------------------------------------------------------------------------
205
#   Documentation
206
#
207
 
208
=pod
209
 
210
=head1 NAME
211
 
212
jats_lib_scan - Scan for Shared Libraries and update an INI file
213
 
214
=head1 SYNOPSIS
215
 
216
jats jats_lib_scan [options]
217
 
218
 
219
 Options:
220
    -help              - brief help message
221
    -help -help        - Detailed help message
222
    -man               - Full documentation
223
    -inifile=name      - The name of an INI file to re-write
224
    -tag=tag_text      - Replacement tag used in the inifile
225
    -library=basename  - Base name of the library
226
    -type=zzz          - Library type ('none' is allowed)(Optional)
227
    -[no]smart         - Smart conversion of pathnames (Default:smart)
228
 
229
=head1 OPTIONS
230
 
231
=over 8
232
 
233
=item B<-help>
234
 
235
Print a brief help message and exits.
236
 
237
=item B<-help -help>
238
 
239
Print a detailed help message with an explanation for each option.
240
 
241
=item B<-man>
242
 
243
Prints the manual page and exits.
244
 
245
=item B<-inifile=name>
246
 
247
Mandatory: The name of an INI file to re-write.
248
 
249
=item B<-tag=tag_text>
250
 
251
Mandatory: Replacement tag used in the inifile. Choose the tag carefully as the
252
replacement operation is very simple.
253
 
254
=item B<-library=basename>
255
 
256
Mandatory: Base name of the library. The program will determine the required
257
library name on the OS type.
258
 
259
The utility will prepend and append OS appropriate text.
260
Under Unix it will prepend 'lib' and append '.so'.
261
 
262
=item B<-type=zzz>
263
 
264
Optional: The type of library. The default value is determined by the environment
265
variable GBE_TYPE that is setup by JATS.
266
 
267
A type of 'none' suppresses this operation.
268
 
269
Suggested values are "P", "D" or "none".
270
 
271
=item B<-smart>
272
 
273
This option controls the replacement of slashes in the replacement path.
274
The default operation is to use '\' on windows and '/' on unix platforms.
275
 
276
This option will disable this operation and only '/' will be used.
277
 
278
 
279
=head1 DESCRIPTION
280
 
281
This utility program is provided to support the test environment. The script
282
allows the generation, at test time, of INI files that specify the absolute
283
paths to shared library files.
284
 
285
The script will search for a named library in the same manner as the OS loader
286
and will then rewrite the INI file script replacing a TAG value with the full
287
path to the required library
288
 
289
=head1 EXAMPLE
290
 
291
Within the test makefile.pl
292
 
293
    Script  ('*', 'MyScript' );
294
    Src     ('*', 'inifile' );
295
    RunTest ('*', 'MyScript', '--CopyIn=inifile' );
296
 
297
Within a test script:
298
 
299
    jats jats_lib_scan.pl -inifile=inifile -tag=MyTag1 -library=CryptoClient
300
    if [ $? -gt 0 ]
301
    then
302
        echo "The LIB SCAN operation did not work"
303
        exit 1
304
    fi
305
 
306
    cat inifile
307
 
308
=cut
309