Issue with the perl script

0 votes

I have written a perl script to check if the all the entries in a log file show status "--BuildSuccessful--". The Log file moves from the current folder after every successful build. It was working fine, but now sometimes its not working as expected. Below is my log file template and the script. Please suggest what I can do better?

log file template:

Build Number::  21.9004.5200.0  BUILT TYPE::     Nightly build
===========================================================
Progress                Time                Build Step
BuildStarted CosmoRR        Tue Apr 11 20:05:38 2017        0
CheckChanges            Tue Apr 11 20:05:38 2017            0
PreBuildToolsSLN        Tue Apr 11 20:14:07 2017        0
ReportDevEnvErrs        Tue Apr 11 20:14:12 2017        0
CheckDevenvErrs     Tue Apr 11 20:14:12 2017        0
ReplaceUCBVersionNumber Tue Apr 11 20:14:27 2017    1
BuildVPresent           Tue Apr 11 20:14:27 2017            2
BuildFlashAndFlex       Tue Apr 11 20:14:35 2017        3
BuildFlexTop            Tue Apr 11 20:15:00 2017            4
ObfuscatePHP            Tue Apr 11 20:15:07 2017            5
ReplaceSTVersionNumber      Tue Apr 11 20:15:11 2017        6
BuildRubyDirector2      Tue Apr 11 21:07:52 2017        24
CreateTAPISDK           Tue Apr 11 21:13:30 2017            25
BuildDMuiInstaller      Tue Apr 11 21:47:51 2017        34
BuildMsiInstaller       Tue Apr 11 21:47:51 2017        34
CheckSignCodeErrors     Tue Apr 11 21:49:54 2017        34
CheckSignCodeErrors     Tue Apr 11 21:49:54 2017        34
BuildRemoteServer       Tue Apr 11 21:49:54 2017        35
BuildMsiInstaller       Tue Apr 11 21:49:54 2017        35
BuildPlatformInstaller      Tue Apr 11 22:13:15 2017        37
BuildMsiInstaller       Tue Apr 11 22:13:15 2017        37
BuildServerInstaller        Tue Apr 11 22:13:15 2017        37
BuildMsiInstaller       Tue Apr 11 22:13:15 2017        37
CheckSignCodeErrors     Tue Apr 11 22:59:44 2017        37
CheckSignCodeErrors     Tue Apr 11 22:59:46 2017        37
CheckSignCodeErrors     Tue Apr 11 22:59:46 2017        37
CheckSignCodeErrors     Tue Apr 11 22:59:47 2017        37
CheckSignCodeErrors     Tue Apr 11 22:59:47 2017        37
RunpostInstall          Tue Apr 11 22:59:47 2017            38
ReportVersions          Tue Apr 11 23:00:03 2017            39
BuildAllSims            Tue Apr 11 23:00:04 2017            40
ReportSIMDevEnvErrors       Tue Apr 11 23:02:41 2017        40
CheckDevenvErrs     Tue Apr 11 23:02:41 2017        40
BuildAllSimsVS2010      Tue Apr 11 23:03:44 2017        41
ReportSIMDevEnvErrors       Tue Apr 11 23:03:57 2017        41
CheckDevenvErrs     Tue Apr 11 23:03:57 2017        41
===========================================================
--BuildSuccessful--
Successful          Tue Apr 11 23:05:55 2017            41

script:

opendir DIR1, "C:\\BuildStatus\\Current\\" or die "cannot open dir $dir: $!";
my @files=  grep ! /^\.+$/, readdir DIR1;
foreach my $files (@files) 
{ 
    $searchsucs = "--BuildSuccessful--";
    $oldLoc2 = "C:\\BuildStatus\\Current\\".$files;
    $newLocS2 = "C:\\BuildStatus\\History\\Successful\\".$files;
    $newLocF2 = "C:\\BuildStatus\\History\\Failed\\".$files;
    open(E_FILE,"C:\\BuildStatus\\Current\\".$files);
    @valf2 = <E_FILE>;
    my $newLoc2 = "";
    foreach $searchf2 (@valf2)
    {
        if ($searchf2=~/$searchsucs/)
            { print "matched";
                $num1 =$num1 + 1;
            }else
            { print "not matched";
                $num1 = $num1 + 0;
            }
    }
    close E_FILE;
    print "moving build status file......\n";
    if($num1 == 1) {
      fmove($oldLoc2, $newLocS2) or warn "$file Warning: Not able to move build status files \n ";
      system(qq(E:\\depot\\builds\\PassedBuilds.bat));
    } else {
      fmove($oldLoc2, $newLocF2) or warn "$file Warning: Not able to move build status files \n ";
    }
}
closedir DIR1;
Jun 22, 2018 in Other DevOps Questions by shubham
• 7,340 points

recategorized Jun 22, 2018 by shubham 906 views

1 answer to this question.

0 votes

I don't really know what might the problem be, but I can certainly give you a more optimized code. Tell me if this helps at all:

# Lexical variable for directory handle
# Single-quoted string
# Reverse slash direction to improve readability
opendir my $dir_h, 'C:/BuildStatus/Current/'
  or die "cannot open dir $dir: $!";

my @files =  grep ! /^\.+$/, readdir $dir_h;

# This doesn't change. Move it out of the loop.
my $searchsucs = '--BuildSuccessful--';

# Rename $files to $file to improve readability
foreach my $files (@files) {
  # Lexical variables (with "my")
  # Use variable interpolation instead of string concatenation
  # Reverse slash durection for readability
  # Don't define variables until you need them
  my $oldLoc2  = "C:/BuildStatus/Current/$file";


  # Lexical variable for file-handle
  # Three args in open() call
  # Use $oldLoc2 variable
  # Check return value from open()
  open(my $fh, '<', $oldLoc2)
    or die "Cannot open $file: $!";

  # Use a $found variable
  my $found;

  # Remove unused $newLoc2 variable
  # Remove unnecessary @valf2 variable
  # Process input file a line at a time (use while, not foreach)
  # Use Perl's default variable $_ for clarity
  while (<$fh>) {
    if (/$searchsucs/) {
      print "matched";
      $found = 1;
      # Skip rest of loop
      last;
    } else {
      # This will be a bit noisy!
      print "not matched"
    }
  }

  # Lexical filehandle means no need to explicitly close file

  print "moving build status file......\n";

  # Simpler logic check
  if ($found) {
    my $newLocS2 = "C:/BuildStatus/History/Successful/$file";
    fmove($oldLoc2, $newLocS2)
      or warn "$file Warning: Not able to move build status files \n ";
    system(qq(E:\\depot\\builds\\PassedBuilds.bat));
  } else {
    my $newLocF2 = "C:/BuildStatus/History/Failed/$file";
    fmove($oldLoc2, $newLocF2)
      or warn "$file Warning: Not able to move build status files \n ";
  }
}

Does that help at all?

answered Jun 22, 2018 by ajs3033
• 7,300 points

Related Questions In Other DevOps Questions

0 votes
1 answer

Issue while using the vagrant up command

I think the issue is with your ...READ MORE

answered Dec 20, 2018 in Other DevOps Questions by Alan
3,783 views
0 votes
1 answer

How to integrate OnPrem Azure DevOps Server with the cloud one?

When I go to Project > Boards ...READ MORE

answered Feb 11, 2022 in Other DevOps Questions by Bhavitha
• 1,000 points
631 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,059 views
+2 votes
1 answer
0 votes
1 answer

Automating Oracle script with nolio

Depending upon the details of your script ...READ MORE

answered Jul 17, 2018 in Other DevOps Questions by ajs3033
• 7,300 points
1,076 views
0 votes
1 answer

Command not working in .bat script but works in CLI

powershell /? If you run the above command ...READ MORE

answered Jul 19, 2018 in Other DevOps Questions by ajs3033
• 7,300 points
1,550 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP