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
• 6,890 points

recategorized Jun 22, 2018 by shubham 53 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,280 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 19, 2018 in Other DevOps Questions by Alan
438 views
0 votes
1 answer

IS app pool import fails with error message.configuration error…insufficient permissions

There is a config file at %SystemRoot%\System32\inetsrv\config\redirection.config Removed the ...READ MORE

answered Jun 19, 2018 in Other DevOps Questions by shubham
• 6,890 points
116 views
0 votes
1 answer

How to deal with non-fast forward errors?

This error is faced when git cannot ...READ MORE

answered Jul 3, 2018 in Other DevOps Questions by Sophie may
• 9,870 points
3,097 views
+13 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 26, 2018 in DevOps & Agile by DragonLord999
• 8,380 points
211 views
0 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,280 points
63 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,280 points
40 views