Subversion Repositories DevTools

Rev

Rev 255 | Rev 361 | 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) 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
#
261 dpurdie 198
FileCreate ( $opt_inifile, $ini_text );
227 dpurdie 199
 
200
exit 0;
201
 
202
#-------------------------------------------------------------------------------
203
#   Documentation
204
#
205
 
206
=pod
207
 
208
=head1 NAME
209
 
210
jats_lib_scan - Scan for Shared Libraries and update an INI file
211
 
212
=head1 SYNOPSIS
213
 
214
jats jats_lib_scan [options]
215
 
216
 
217
 Options:
218
    -help              - brief help message
219
    -help -help        - Detailed help message
220
    -man               - Full documentation
221
    -inifile=name      - The name of an INI file to re-write
222
    -tag=tag_text      - Replacement tag used in the inifile
223
    -library=basename  - Base name of the library
224
    -type=zzz          - Library type ('none' is allowed)(Optional)
225
    -[no]smart         - Smart conversion of pathnames (Default:smart)
226
 
227
=head1 OPTIONS
228
 
229
=over 8
230
 
231
=item B<-help>
232
 
233
Print a brief help message and exits.
234
 
235
=item B<-help -help>
236
 
237
Print a detailed help message with an explanation for each option.
238
 
239
=item B<-man>
240
 
241
Prints the manual page and exits.
242
 
243
=item B<-inifile=name>
244
 
245
Mandatory: The name of an INI file to re-write.
246
 
247
=item B<-tag=tag_text>
248
 
249
Mandatory: Replacement tag used in the inifile. Choose the tag carefully as the
250
replacement operation is very simple.
251
 
252
=item B<-library=basename>
253
 
254
Mandatory: Base name of the library. The program will determine the required
255
library name on the OS type.
256
 
257
The utility will prepend and append OS appropriate text.
258
Under Unix it will prepend 'lib' and append '.so'.
259
 
260
=item B<-type=zzz>
261
 
262
Optional: The type of library. The default value is determined by the environment
263
variable GBE_TYPE that is setup by JATS.
264
 
265
A type of 'none' suppresses this operation.
266
 
267
Suggested values are "P", "D" or "none".
268
 
269
=item B<-smart>
270
 
271
This option controls the replacement of slashes in the replacement path.
272
The default operation is to use '\' on windows and '/' on unix platforms.
273
 
274
This option will disable this operation and only '/' will be used.
275
 
276
 
277
=head1 DESCRIPTION
278
 
279
This utility program is provided to support the test environment. The script
280
allows the generation, at test time, of INI files that specify the absolute
281
paths to shared library files.
282
 
283
The script will search for a named library in the same manner as the OS loader
284
and will then rewrite the INI file script replacing a TAG value with the full
285
path to the required library
286
 
287
=head1 EXAMPLE
288
 
289
Within the test makefile.pl
290
 
291
    Script  ('*', 'MyScript' );
292
    Src     ('*', 'inifile' );
293
    RunTest ('*', 'MyScript', '--CopyIn=inifile' );
294
 
295
Within a test script:
296
 
297
    jats jats_lib_scan.pl -inifile=inifile -tag=MyTag1 -library=CryptoClient
298
    if [ $? -gt 0 ]
299
    then
300
        echo "The LIB SCAN operation did not work"
301
        exit 1
302
    fi
303
 
304
    cat inifile
305
 
306
=cut
307