Alphamonk IT Systems Home | Articles | Contact Us | Services





Login
Username:

Password:

Remember me



Lost Password?

Register now!
Articles > Perl Infrastructure Automation > Perl Automation Script using a config file

Perl Automation Script using a config file

Published by Alphamonk on 2009/2/21 (1022 reads)
This is a perl script I wrote a while back to use a config file as the way to add arguments to the program. I like this idea a lot. I could not sell this to anyone.



#!/usr/bin/perl

use Getopt::Long;        # Core
use Cwd;                 # Core
use File::Copy;          # Core
use Config::IniFiles;    # Extra
use IPC::Run3;           # Extra
use warnings;            # Core Pragma
use strict;              # Core Pragma

# Variable declaration
my (
    
$input,  $sandboxdir,  $cfgfile$cfg$svn,   $command,
    
$rundir$run_sim_cfg$logdir,  $design$gosim$dir,
    
$ver,    $ConfigFile,  @text,    $cfgdir,
);
########## Main ##########

Config();
my (
    
$mksb,      $sandboxname$releasename$update_sb,
    
$simprep,   $buildlibs,   $dosim,       $doregress,
    
$version,
) = 
Arguments();

# Version Information
$ver "1.0.0";

if (
$version) {
    print 
"$vern";
    exit;
}

if (
$mksb) {
    if (   ( -
".bld_dir_locator" )
        || ( -
"../design" )
        || ( -
"../../$sandboxname" ) )
    {
        print 
"This directory seems to be a working sandboxn";
    }
    else {
        print 
"Running mksbn";
        if ( -
d $sandboxname ) {
            print <<
MESSAGE;
Warningsandbox $sandboxname already exists in current working directory
Continuing may cause undesireable behavior 
if the sandbox was not
generated using this script
The script will make a best effort to
update
/relink the directoriesRelinking will result in all changes
in the link paths to be deleted
!!!

Continue
Yes   2 No
MESSAGE

            $input 
= <STDIN>;
            if ( 
$input == ) {
                print 
"nWe're moving on...nn";
            }
            
elsif $input == ) {
                print 
"nAborting update per user requestnn";
                exit;
            }
            else {
                print 
"nProper respones are "1" or "2" - Abortingnn";
                exit;
            }
            
run3(`date`);
            if ( !
$releasename ) {
                print
"Creating sandbox $sandboxname with files from tip...n";
            }
            else {
                print
"Creating sandbox $sandboxname with files from $releasename...n";
            }
        }
        else {
            
mkdir("$sandboxname");
        }

        
$dir        getcwd();
        
$sandboxdir "$dir/$sandboxname";
        
chdir("$sandboxdir") == or die "Could not chdir to $sandboxdirn";
        print 
"Sandbox path is $dirn";
        if ( !-
"design" ) {
            
mkdir("design");
        }

        
chdir("design");
        
$design getcwd();
        
chomp($design);

        
copy"$dir/.dosim.cfg""$dir/$sandboxname/design" );
        
unlink("$dir/.dosim.cfg");
        
unlink("$dir/.tempfile ");

        if ( !-
".bld_dir_locator" ) {
            
run3("touch .bld_dir_locator");
            die 
"Could not create .bld_dir_locatorn" if $? != 0;
        }

        if ( !-
"$design/log" ) {
            
mkdir("log");
            
$logdir = `/bin/pwd`;
            
chomp($logdir);
            
$logdir "$logdir/log";
        }
        else {
            
$command "rm -rf $logdir/*";
            
run3($command);
        }

        if (
$releasename) {
            print 
"Link directories to project $releasename)n";
            }
            else {
                print 
"Release $releasename does not eixstn";
            }
        }
        else {
            print 
"Checkout from svnserver1n";

            
do_svn$cfg$logdir );

        }
    }
}
else {

    
$logdir = `/bin/pwd`;
    
chomp($logdir);
    
$logdir "$logdir/log";
}

if (
$update_sb) {
    print 
"Updating the sandboxn";

    
do_svn$cfg$logdir );
}

if (
$simprep) {
    print 
"Run simulation preparation scriptsn";

    
$design getcwd();
    
chomp($design);
    
$ENV{DESIGN} = $design;

    if(! -
"log"){
       
mkdir("log");
    }

    
$gosim $cfg->val'CMDS''sim.exe' );
    
chdir("$design") == or die "Could not chdir to $designn";
    
$command "$gosim --make sg -J";
    print 
"$commandn";
    
run3$commandundef"$logdir" $cfg->val'LOG''run' ) );
    die 
"Failed to successfully run make sg" if $? != 0;
}

if (
$buildlibs) {
    print 
"Building unit libsn";
    
rotate_config_param("UNITLIBS");
}
if (
$dosim) {
    print 
"Run simulationn";
    
rotate_config_param("GOSIM");
}

if (
$doregress) {
    print 
"Run regressionn";
    
rotate_config_param("GOREGRESS");
}

#===  FUNCTION  ================================================================
#         NAME:  rotate_config_param
#  DESCRIPTION:  ????
#===============================================================================
sub rotate_config_param {
    
unless ( ( -".bld_dir_locator" ) || ( -".dosim.cfg" ) ) {
        print
"Can not find .bld_dir_locator, Change directory to a working sandboxn";
    }
    
my $Section$design, @param$i$command );
    (
$Section) = @_;
    print 
"Section is $Sectionn";
    
unless ($design) {
        
$design getcwd();
        
chomp($design);
        
$ENV{DESIGN} = $design;
    }
    @
param $cfg->Parameters("$Section");
    print 
"Parameters in this section aren@paramn";
    foreach 
$i (@param) {
        
$command $cfg->val"$Section"$i );
        print 
"$commandn";
        
run3($command);
        die 
"$command failedn" if $? != 0;
    }
}

#===  FUNCTION  ================================================================
#         NAME:  do_svn
#  DESCRIPTION:  ????
#===============================================================================
sub do_svn {
    
my $value, @svnparam );
    
my $cfg$logdir ) = @_;
    @
svnparam $cfg->Parameters('SVN');
    
my $svnlogfile $cfg->val'LOG''svn' );
    foreach 
my $svndir (@svnparam) {
        
$value $cfg->val'SVN'$svndir );
        if ( -
"$svndir" ) {    # do update if the directory is there
            
$command $cfg->val'CMDS''update' ) . " $svndir";
            print 
"$commandn";
            
run3$commandundef"$logdir/" $cfg->val'LOG''svn' ) );
            die 
"$cfg update failed" if $? != 0;
        }
        else {
            
$command $cfg->val'CMDS''checkout' ) . " $value $svndir";
            print 
"$commandn";
            
run3$commandundef"$logdir/" $cfg->val'LOG''svn' ) );
            die 
"$value checkout failed" if $? != 0;
        }
    }
}

#===  FUNCTION  ================================================================
#         NAME:  Config
#  DESCRIPTION:  ????
#===============================================================================
sub Config {
    
## CoofigFile ##
        
$cfgdir getcwd();
    
#    print "$rundirn";
    
if ( -".dosim.cfg" ) {
        
$ConfigFile ".dosim.cfg";
        
openCONFIG"$ConfigFile" ) or die "Could not open $ConfigFilen";
        @
text = <CONFIG>;
    }
    else {
        @
text       = <DATA>;
        
$ConfigFile ".dosim.cfg";
        
openCONFIG">$ConfigFile" ) or die "Could not open $ConfigFilen";
        print 
CONFIG "@text";
        
close(CONFIG);
    }
    
$cfg Config::IniFiles->new( -file => "$ConfigFile" );
}

#===  FUNCTION  ================================================================
#         NAME:  Arguments
#  DESCRIPTION:  This are the possible argument that can be called from the
#                command line or gui
#===============================================================================
sub Arguments {
    
my $ok$mksb$sandboxname$releasename$dosim$doregress$debug,
        
$help );
    
$ok GetOptions(
        
"mksb"          => $mksb,
        
"sandboxname=s" => $sandboxname,
        
"releasename=s" => $releasename,
        
"update_sb"     => $update_sb,
        
"simprep"       => $simprep,
        
"buildlibs"     => $buildlibs,
        
"dosim"         => $dosim,
        
"doregress"     => $doregress,
        
"version"       => $version,
        
"help"          => $help
    
);

    
Usage()
      
unless ( ($mksb)
        || (
$update_sb)
        || (
$simprep)
        || (
$buildlibs)
        || (
$dosim)
        || (
$doregress)
        || (
$version) );
    
Usage() if ($help);
    
Usage() if ( $dosim && $doregress );
    
Usage() if ( ($mksb) && ( !$sandboxname ) );

    return (
        
$mksb,      $sandboxname,    $releasename,    $update_sb
        
$simprep,   $buildlibs,      $dosim,          $doregress
        
$version,  
    );
}

#===  FUNCTION  ================================================================
#         NAME:  Usage
#  DESCRIPTION:  Show users how to use this program
#===============================================================================
sub Usage {
    
my $usageoutput = <<USAGE;
Syntaxrun_sim.pl [options
--
mksb          Checkout Repository
--update_sb     Update Repository
--simprep       Compile libraries, and run make sg
--buildlibs     Build unit libraries
--sandboxname   Sandbox or Working Copy name
--releasenaem   Name of the release build to link to NOTEMust be run with --mksb
--dosim         Run simulations NOTECna not be run with --doregress
--doregress     Run regressions NOTECan not be run with --dosim
--version       Shows current version number
--help          Prints this help message

Example         
run_sim.pl --mksb --sandboxname tip
                
run_sim.pl --mksb --sandboxname tip --releasename be2_2.0.55.0
                
run_sim.pl --update_sb
                
run_sim.pl --simprep
                
run_sim.pl --buildlibs
                
run_sim.pl --dosim
                
run_sim.pl --doregress
                
run_sim.pl --mksb --sandboxname MySandbox --simprep --dosim
                
run_sim.pl --version
USAGE
    
print "$usageoutput";
    exit;
}

__DATA__
[SVN]
rtl=http://svnserver.mydomain:8080/svnroot/V1/trunk/rtl
sg=http://svnserver.mydomain:8080/svnroot/V1/trunk/sg
tb=http://svnserver.mydomain:8080/svnroot/V1/trunk/tb
models=http://svnserver.mydomain:8080/svnroot/V1/trunk/models
sw=http://svnserver.yourdomain:8080/svnroot/V1/trunk/sw
fw=http://svnserver.yourdomain:8080/svnroot/V1/trunk/fw

[LOG]
svn=svn.log
buildtcsh
=buildtcsh.log
run
=run.log

[CMDS]
checkout=svn co
update
=svn update
revert
=svn revert
sim
.exe=gosim
regress
.exe=go_regress

[UNITLIBS]

[
GOSIM]

[
GOREGRESS]

Navigate through the articles
Previous article hex2binary script Perl Modules and My Functions that I use over and over again Next article
Voters total: 0
Average: 0
The comments are owned by the poster. We aren't responsible for their content.
Main Menu
ads
AddTo Social bookmarking block




� 2008 All Rights Reserved