#!/usr/local/bin/perl
# 
# $Header: dte/DTE/scripts/jdev/MAIN/RTPATCH.pl akamaley_32481391_rt_patch_automation/2 2021/02/08 20:39:35 akamaley Exp $
#
# RTPATCH.pl
# 
# Copyright (c) 2021, Oracle and/or its affiliates. 
#
#    NAME
#      RTPATCH.pl - <one-line expansion of the name>
#
#    DESCRIPTION
#      <short description of component this file declares/defines>
#
#    NOTES
#      <other useful comments, qualifications, etc.>
#
#    MODIFIED   (MM/DD/YY)
#    akamaley    02/08/21 - Creation
# 
#Usage: RTPATCH.pl  import.txt export.txt runtime.txt

use File::Copy;
use File::Basename;
use LWP::UserAgent;

# BEGIN SUPPORTING FILES
##  otes in this "BEGIN END comments" section:
## a line starting with ## will be ignored when parsing by DTE runtime
## a line starting with single # will be used to get supporting files of the block
## all the paths must be relative to DTE's AUTO_HOME, no need to list DTE CORE files here
## to include a specific file foo.bar: # scripts/product/release/foo.bar  
## to include all files with extension xyz, use wildcard *: 
# scripts/product/release/*.xyz  
## to include all files in subdir 'foodir' (non-recursive): 
# scripts/product/release/foodir/*              
## to include everything under subdir 'bardir' recursively: 
# scripts/product/release/bardir                
# END SUPPORTING FILES 

# use BEGIN block to add DTE.pm into @INC
BEGIN
{
  use File::Basename;
  use Cwd;

  $orignalDir = getcwd();

  $scriptDir = dirname($0);
  chdir($scriptDir);
  $scriptDir =  getcwd();

  $plibDir = "$scriptDir/../../../plib";
  chdir($plibDir);
  $plibDir = getcwd();

  # add $plibDir into INC
  unshift  (@INC,"$plibDir");

  chdir($orignalDir);
}

require DTE;

if ( $#ARGV < 2)
{
  print ("Usage: perl $0 import.txt export.txt runtime.txt\n");
  exit 1;
}

$importfile  = $ARGV[0];
$exportfile  = $ARGV[1];
$runtimefile = $ARGV[2];
$JAVA_HOME="%JAVA_HOME%";

######## Initialize Global Variables #################

# Import Parameters will be put into hashtable %ImportParamTable
%ImportParamTable = ();
# The keys of hashtable %ImportParamTable are as follows:
# HOSTNAME
# MW_HOME
# RT_ZIP_FILENAME


# Runtime Parameters will be put into hashtable %RuntimeParamTable
# When adding your own code below, you may find these variables are useful and handy 
%RuntimeParamTable = ();
# The keys of hashtable %RuntimeParamTable are as follows:
# WORKDIR   - the workdir of the current task(block)
# AUTO_HOME - the AUTO_HOME dir
# AUTO_WORK - the AUTO_WORK dir
# ENVFILE   - the property file which has all the ENV variables dump 


# Export Parameters should be put into hashtable %ExportParamTable
# The operation of the block must determine the value of each export parameter
# with the exception that $ExportParamTable{HOSTNAME} always equals to 
# $ImportParamTable{HOSTNAME}
%ExportParamTable = ();
# The export parameters are listed as follows:
# HOSTNAME
# EXIT_STATUS
# TARGETLOC
# RT_ZIP_FILENAME

# the exit_value for this program
$exit_value=0;


#################### Program Main Logic ###################

############ Parse Runtime File runtime.txt  #######
%RuntimeParamTable=DTE::parse_runtime_file($runtimefile);

############ Parse Import File import.txt ##########
## All import parameters are in hashtable %ImportParamTable
%ImportParamTable = DTE::parse_import_file($importfile, %RuntimeParamTable);


############ Set Initial/Default Values for Mandatory Export Params ####
# Value for EXIT_STATUS ought to be changed based on operation outcome!
$ExportParamTable{HOSTNAME} = $ImportParamTable{HOSTNAME};
$ExportParamTable{EXIT_STATUS}="SUCCESS";


############### Here is the Operation of the block #########
# To do what the block is supposed to do and generate values for export parameters
operation();


############### Populate Export file with export param info ##############
DTE::populate_export_file($exportfile, %ExportParamTable);

# End the Main Logic here
exit $exit_value;


################# Program Subroutines For Block Logic ################
#
# ADD YOUR CODE BELOW
#
# Subroutine operation() is to accomplish the goal of this block - do the real job
# and generate values for all the exported parameters listed in block definition.
# The workdir of this block is $RuntimeParamTable{WORKDIR}, temporary files and subdir
# can be created in this workdir as needed
#
sub operation
{
  $HOSTNAME = $ImportParamTable{HOSTNAME};
  $MW_HOME = $ImportParamTable{MW_HOME};
  $RTZIP_FILENAME = $ImportParamTable{RTZIP_FILENAME};
  $RTZIP_FILENAME_2 = $ImportParamTable{RTZIP_FILENAME_2};

  $WORKDIR = $RuntimeParamTable{WORKDIR};
  #$JAVA_HOME = $RuntimeParamTable{JAVA_HOME};
  $AUTO_HOME = $RuntimeParamTable{AUTO_HOME};
  $ENVFILE = $RuntimeParamTable{ENVFILE};
  
  print "ENVFILE=$ENVFILE\n";
  if ( -f "$ENVFILE" ) {
    print "ENVFILE=$ENVFILE\n";
    DTE::loadEnvPrpFile($ENVFILE);
  }
	
 
  
  $PLATFORM = DTE::getOS();
  #$DIRSEP = '/' ;
  if ( $PLATFORM eq 'nt' ) {
    $DIRSEP = '\\';
	$UNZIP = "\"C:\\Program Files\\WinZip\\wzunzip.exe\" -yb -o";
    $COPY = 'cp -rf';
  }
  else {
	$DIRSEP = '/' ;
	$UNZIP = 'unzip -o';
    $COPY = 'cp -rfL';
  }
  if ( $PLATFORM eq 'linux' ) {
    $UNZIP = '/usr/bin/unzip -o';
  }
  if ( $PLATFORM eq 'solaris' || $PLATFORM eq 'aix' || $PLATFORM eq 'hp-ia64' || $PLATFORM eq 'hp-ux' || $PLATFORM eq 'hpux') {
    $UNZIP = '/usr/local/bin/unzip -o';
  }
   else {
	$DIRSEP = '/' ;
	$UNZIP = 'unzip -o';
    $COPY = 'cp -rfL';
  }

  
  # STEP 2: DOWNLOAD RT PATCH ARTIFACT
  print "\nSTART DOWNLOAD RT PATCH ARTIFACT\n\n";
  @UNZIP_INTERNAL1 = `mkdir -p $WORKDIR/RTPATCH 2>&1`;
  print @UNZIP_INTERNAL1;
  
  # STEP 3:UNZIP FILES	
  #
  $zipfile="$RTZIP_FILENAME.zip";
  if  (-e $zipfile) 
  {
	@UNZIP_INTERNAL2 = `cp $RTZIP_FILENAME.zip $WORKDIR/RTPATCH 2>&1`;
	print @UNZIP_INTERNAL2;
	@UNZIP_INTERNAL3 = `cp $RTZIP_FILENAME_2.zip $WORKDIR/RTPATCH 2>&1`;
	print @UNZIP_INTERNAL3;
	@uz1=`find $WORKDIR/RTPATCH -name '*.zip' -exec sh -c 'unzip -d $WORKDIR/RTPATCH {}' ';'`;
	print @uz1;
	print"unzip the pullsan zip files in the RTpatch folder\n";
	$exit_value = 0;
   $ExportParamTable{EXIT_STATUS} = "SUCCESS";
  }
  else
  {
        print "zip file doesnot exist\n";
        $exit_value = 1;
        $ExportParamTable{EXIT_STATUS} = "FAILURE";
  }
  
  # STEP :4 APPLY PATCH
  if  (-d "$WORKDIR/RTPATCH/oracle_common")
  {
       @applypatch=`$COPY $WORKDIR/RTPATCH/oracle_common/* $MW_HOME/oracle_common/ 2>&1`;
       print @applypatch;
       print "replacing the oracle_common folder in mw_home\n";
       $exit_value = 0;
       $ExportParamTable{EXIT_STATUS} = "SUCCESS";
  }
 else
  {
      print"no unzipped files from zip file\n";
      $exit_value = 1;
     $ExportParamTable{EXIT_STATUS} = "FAILURE";
  }

  
}



populate_export_file();

sub populate_export_file
{
  if ( ! open (EXPFILE, ">$exportfile") ) 
  {
    print "ERROR: failed to write to $exportfile\n"; 
    $exit_value = 1;
  }
  else 
  {
    if ( $exit_value eq 0 )
    {
      $EXIT_STATUS="SUCCESS";
    } else
    {
      $EXIT_STATUS="FAILURE";
    }

    print EXPFILE "HOSTNAME=$HOSTNAME\n";
    print EXPFILE "EXIT_STATUS=$EXIT_STATUS\n";
    close (EXPFILE);
  }
}

sub parse_import_file
{
  if ( open(IN, "$importfile") )
  {
    while(my $my_line = <IN>) 
    {
      chomp $my_line;
      $my_line =~ s/^\s+//;
      $my_line =~ s/\s+$//;

      my @tmp_token = split("=",$my_line);

      # need to handle if the value contains '=' itself
      my $token = $tmp_token[0] ;
      my $value = $my_line ;
      $value =~ s/$token\s*=\s*//g ;

      # handle string %ADE_VIEW_ROOT% found in any import parameter here
      if ( $ENV{ADE_VIEW_ROOT} ) {
        $value =~ s/%ADE_VIEW_ROOT%/$ENV{ADE_VIEW_ROOT}/g;
      }
      # handle string %T_WORK% found in any import parameter here
      if ( $ENV{T_WORK} ) {
        $value =~ s/%T_WORK%/$ENV{T_WORK}/g;
      }

      if($token eq "HOSTNAME" ) {
        $HOSTNAME = $value;
      }
      elsif ($token eq "MW_HOME" ) {
        $MW_HOME = $value;
      }
	  elsif ($token eq "COMMON_PROPERTYFILE" ) {
        $COMMON_PROPERTYFILE = $value;
      }
    }
    close (IN);
  }
  else
  {
    print "ERROR: failed to open $importfile\n";
    $exit_value = 1;
  }
}

sub parse_common_property_file
{
  if ( open(IN, "$COMMON_PROPERTYFILE") )
  {
    while(my $my_line = <IN>) 
    {
      chomp $my_line;
      $my_line =~ s/^\s+//;
      $my_line =~ s/\s+$//;

      my @tmp_token = split("=",$my_line);
      # need to handle if the value contains '=' itself
      my $token = $tmp_token[0] ;
      my $value = $my_line ;
      $value =~ s/$token\s*=\s*//g ;
      print "value=$value\n";

      if($token =~ /JDEV.HOSTNAME/ && ($HOSTNAME eq "%HOSTNAME%" || $HOSTNAME eq "")) {
        $HOSTNAME = $value;
      }
      elsif ($token =~ /JDEV.MW_HOME/ && ($MW_HOME eq "%MW_HOME%" || $MW_HOME eq "") ) {
        $MW_HOME = $value;
      }
	  elsif ($token =~ /DB_PROPERTYFILE/ && ($DB_PROPERTIES_FILE eq "%DB_PROPERTIES_FILE%" || $DB_PROPERTIES_FILE eq "") ) {
        $DB_PROPERTIES_FILE = $value;
      }
	  else{
	  ;
      }
    }
    close (IN);
  }
  else
  {
    print "ERROR: failed to open $COMMON_PROPERTYFILE\n";
    $exit_value = 1;
  }
}

exit $exit_value
 

Perl Online Compiler

Write, Run & Share Perl code online using OneCompiler's Perl online compiler for free. It's one of the robust, feature-rich online compilers for Perl language, running on the latest version 5.22.1. Getting started with the OneCompiler's Perl compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Perl and start coding.

Taking inputs (stdin)

OneCompiler's Perl online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Perl program which takes name as input and prints hello message with your name.

my $name = <STDIN>;             
print "Hello $name.\n";          

About Perl

Perl(Practical Extraction and Report Language) is especially desined for text processing by Larry Wall.

Key features

  • Cross-platform
  • Efficient for mission critical applications.
  • Open-source
  • Supports both procedural and object-oriented programming.
  • Perl interpreter is embeddable with other systems.
  • Loosely typed language

Syntax help

Data types

There is no need to specify the type of the data in Perl as it is loosely typed language.

TypeDescriptionUsage
ScalarScalar is either a number or a string or an address of a variable(reference)$var
ArraysArray is an ordered list of scalars, you can access arrays with indexes which starts from 0@arr = (1,2,3)
HashHash is an unordered set of key/value pairs%ul = (1,'foo', 2, 'bar)

Variables

In Perl, there is no need to explicitly declare variables to reserve memory space. When you assign a value to a variable, declaration happens automatically.

$var-name =value; #scalar-variable
@arr-name = (values); #Array-variables
%hashes = (key-value pairs); # Hash-variables 

Loops

1. If family:

If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.

If

if(conditional-expression){    
//code    
} 

If-else

if(conditional-expression){  
//code if condition is true  
}else{  
//code if condition is false  
} 

Nested-If-else

if(condition-expression1){  
//code if above condition is true  
}else if(condition-expression2){  
//code if above condition is true  
}  
else if(condition-expression3){  
//code if above condition is true  
}  
...  
else{  
//code if all the conditions are false  
}  

2. Switch:

There is no case or switch in perl, instead we use given and when to check the code for multiple conditions.

given(expr){    
when (value1)  
{//code if above value is matched;}    
when (value2)  
{//code if above value is matched;}   
when (value3)  
{//code if above value is matched;}  
default  
{//code if all the above cases are not matched.}     
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  // code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition) {  
 // code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {
  // code 
} while (condition); 

Sub-routines

Sub-routines are similar to functions which contains set of statements. Usually sub-routines are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

How to define a sub-routine

sub subroutine_name 
{
	# set of Statements
}

How to call a sub-routine

subroutine_name();
subroutine_name(arguments-list); // if arguments are present