Subversion Repositories DevTools

Rev

Rev 361 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#! perl
########################################################################
# Copyright (C) 2006 ERG Limited, All rights reserved
#
# Module name   : jats.sh
# Module type   : Makefile system
# Compiler(s)   : n/a
# Environment(s): jats
#
# Description   : This utility is used within the automated test environment
#                 to assist in the testing of packages that load shared
#                 libraries by name.
#
#                 The program will
#                   1) Locate the named library in the PATH
#                      PATH(Win32) LD_LIBRARY_PATH(unix)
#                   2) Rewrite a file replacing a tag with the full path
#                      to the file
# Usage:
#
# Version   Who      Date        Description
#
#......................................................................#

require 5.006_001;
use strict;
use warnings;

use JatsError;
use FileUtils;
use Getopt::Long;
use Pod::Usage;

#
#   Global variables
#
my $VERSION     = "1.0.0";
my $opt_debug   = $ENV{'GBE_DEBUG'};        # Allow global debug
my $opt_verbose = $ENV{'GBE_VERBOSE'};      # Allow global verbose
my $opt_type    = $ENV{'GBE_TYPE'};
my $opt_help = 0;
my $opt_manual = 0;
my $opt_smart_names = 1;

my $opt_inifile;
my $opt_tag;
my $opt_libname;

our $ScmHost;
our $ScmPathSep;

#-------------------------------------------------------------------------------
# Function        :
#
# Description     : Main entry point
#
# Inputs          : @ARGV
#
# Returns         : Non-zero if an error is detected
#                   Zero if all is well
#

#
#   Process user options
#
my $result = GetOptions (
                "help+"         => \$opt_help,              # flag, multiple use allowed
                "manual"        => \$opt_manual,            # flag
                "verbose+"      => \$opt_verbose,           # flag, multiple use allowed
                "type=s"        => \$opt_type,              # string
                "inifile=s"     => \$opt_inifile,           # string
                "library=s"     => \$opt_libname,           # string
                "tag=s"         => \$opt_tag,               # string
                "smart!"        => \$opt_smart_names,       # [No]Flag

                );

                #
                #   UPDATE THE DOCUMENTATION AT THE END OF THIS FILE !!!
                #

#
#   Process help and manual options
#
pod2usage(-verbose => 0, -message => "Version: $VERSION") if ($opt_help == 1 || ! $result);
pod2usage(-verbose => 1) if ($opt_help == 2 );
pod2usage(-verbose => 2) if ($opt_manual || ($opt_help > 2));
pod2usage(-verbose => 0, -message => "Version: $VERSION") if ( $#ARGV >= 0 );

#
#   Initialise used packages
#
ErrorConfig ('name' => 'LibIni',
             'verbose' => $opt_verbose,
             'debug' => $opt_debug );
InitFileUtils();

#
#   Sanity test the user parameters
#
Error("No ini file specified" ) unless ( $opt_inifile );
Error("Ini file not found: $opt_inifile") unless ( -f $opt_inifile );

Error("No 'tag' specified" ) unless ( $opt_tag );

Error("No Shared Library specified") unless ( $opt_libname );

Error("Type of library not known. GBE_TYPE has not been set") unless ( $opt_type );
$opt_type = '' if ( $opt_type eq 'none' );

#
#   Display user parameters
#
Verbose ("Ini: $opt_inifile" );
Verbose ("Tag:$opt_tag" );
Verbose ("Lib:$opt_libname");
Verbose ("Type:$opt_type");

#
#   Determine the path to search
#   Windows a linux are different
#
my $pname = ($ScmHost eq "Unix") ? 'LD_LIBRARY_PATH' : 'PATH';
my $ext   = ($ScmHost eq "Unix") ? '.so' : '.dll';
my $pre   = ($ScmHost eq "Unix") ? 'lib' : '';
my $libfile = "$pre$opt_libname$opt_type$ext";

Verbose("Library Path EnvVar: $pname");
Verbose("Library Ext : $ext");
Verbose("Library File: $libfile");

Error ("No library search path variable in environment: $pname")
    unless ( exists $ENV{$pname} );

#
#   Walk the path looking for the specified library
#   Detect multiple instances
#
my @found = ();
foreach my $dir ( split( $ScmPathSep, $ENV{$pname})  )
{
    Verbose2("Looking in: $dir");
    if ( -f "$dir/$libfile" || -l  "$dir/$libfile"  )
    {
        Verbose("Found library in: $dir");
        push @found, $dir;
    }
}

#
#   Did we get exactly one library
#
Error ("Library not found",
       "Library Name: $libfile",
       "Search path:", $ENV{$pname} ) if ($#found < 0);

Error ("Multiple instances of the library found",
       "Library Name: $libfile",
       "Found in", @found )if ($#found > 0);

#
#   Determine replacement string
#
my $replace = $found[0] . '/' . $libfile;
Verbose ("Tag: $opt_tag");
Verbose ("Replace: $replace");

#
#   Have one instance of the library
#   No for the ini file
#
#   Slurp in the entire ini file
#   Do not attempt to process line endings
#
undef $/;                    # slurp mode (undef)
open(my $fh, "<", $opt_inifile) or Error("Cannot open the ini file for reading");
my $ini_text = <$fh>;
close($fh);

#
#   Replace the tag within the file
#       Quote metacharacters in the tag string
#       Perform Windows filename translation
#
$opt_tag = quotemeta ($opt_tag);
$replace =~ s~/~\\~g if ($opt_smart_names && $ScmHost ne "Unix");

Verbose2 ("Inifile: Before replacement:\n",$ini_text);
unless ($ini_text =~ s~$opt_tag~$replace~g )
{
    Error ("No replacement performed for tag: $opt_tag");
}
Verbose2 ("Inifile: After replacement:\n",$ini_text);

#
#   Save the file out
#
FileCreate ( $opt_inifile, $ini_text );

exit 0;

#-------------------------------------------------------------------------------
#   Documentation
#

=pod

=for htmltoc    MAKEUTIL::

=head1 NAME

jats_lib_scan - Scan for Shared Libraries and update an INI file

=head1 SYNOPSIS

jats jats_lib_scan [options]


 Options:
    -help              - brief help message
    -help -help        - Detailed help message
    -man               - Full documentation
    -inifile=name      - The name of an INI file to re-write
    -tag=tag_text      - Replacement tag used in the inifile
    -library=basename  - Base name of the library
    -type=zzz          - Library type ('none' is allowed)(Optional)
    -[no]smart         - Smart conversion of pathnames (Default:smart)

=head1 OPTIONS

=over 8

=item B<-help>

Print a brief help message and exits.

=item B<-help -help>

Print a detailed help message with an explanation for each option.

=item B<-man>

Prints the manual page and exits.

=item B<-inifile=name>

Mandatory: The name of an INI file to re-write.

=item B<-tag=tag_text>

Mandatory: Replacement tag used in the inifile. Choose the tag carefully as the
replacement operation is very simple.

=item B<-library=basename>

Mandatory: Base name of the library. The program will determine the required
library name on the OS type.

The utility will prepend and append OS appropriate text.
Under Unix it will prepend 'lib' and append '.so'.

=item B<-type=zzz>

Optional: The type of library. The default value is determined by the environment
variable GBE_TYPE that is setup by JATS.

A type of 'none' suppresses this operation.

Suggested values are "P", "D" or "none".

=item B<-smart>

This option controls the replacement of slashes in the replacement path.
The default operation is to use '\' on windows and '/' on unix platforms.

This option will disable this operation and only '/' will be used.

=back

=head1 DESCRIPTION

This utility program is provided to support the test environment. The script
allows the generation, at test time, of INI files that specify the absolute
paths to shared library files.

The script will search for a named library in the same manner as the OS loader
and will then rewrite the INI file script replacing a TAG value with the full
path to the required library

=head1 EXAMPLE

Within the test makefile.pl

    Script  ('*', 'MyScript' );
    Src     ('*', 'inifile' );
    RunTest ('*', 'MyScript', '--CopyIn=inifile' );

Within a test script:

    jats jats_lib_scan.pl -inifile=inifile -tag=MyTag1 -library=CryptoClient
    if [ $? -gt 0 ]
    then
        echo "The LIB SCAN operation did not work"
        exit 1
    fi

    cat inifile

=cut