我有一个脚本来读取日志文件并搜索所有41步,如果它显示状态“--BuildSuccessful--”,日志文件将从文件夹当前移动到成功。它工作但最近,有时它无法正常工作。我不明白发生了什么,日志文件仍然在文件夹当前。请花点时间帮助我改进代码。
此处为日志文件模板:
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
RunGetEnums Tue Apr 11 20:15:11 2017 7
ReplaceClientVersionNum Tue Apr 11 20:15:11 2017 8
BuildAllSLN Tue Apr 11 20:15:11 2017 9
ReportDevEnvErrs Tue Apr 11 20:35:32 2017 9
CheckDevenvErrs Tue Apr 11 20:35:32 2017 9
ReportDevEnvErrs Tue Apr 11 20:35:32 2017 9
CheckDevenvErrs Tue Apr 11 20:35:32 2017 9
ReportDevEnvErrs Tue Apr 11 20:35:32 2017 9
CheckDevenvErrs Tue Apr 11 20:35:32 2017 9
ReportDevEnvErrs Tue Apr 11 20:35:32 2017 9
CheckDevenvErrs Tue Apr 11 20:35:32 2017 9
BuildSolution Tue Apr 11 20:35:32 2017 10
ReportDevEnvErrs Tue Apr 11 20:43:20 2017 10
CheckDevenvErrs Tue Apr 11 20:43:20 2017 10
BuildPresenterInstaller Tue Apr 11 20:43:20 2017 11
BuildMsiInstaller Tue Apr 11 20:43:20 2017 11
CheckSignCodeErrors Tue Apr 11 20:43:23 2017 11
BuildAcceleratorInstaller Tue Apr 11 20:43:23 2017 11
BuildMsiInstaller Tue Apr 11 20:43:23 2017 11
CheckSignCodeErrors Tue Apr 11 20:43:24 2017 11
CheckSignCodeErrors Tue Apr 11 20:44:24 2017 11
CheckSignCodeErrors Tue Apr 11 20:44:24 2017 11
GetPhoneInstaller Tue Apr 11 20:44:24 2017 12
GetManhattanClientExo Tue Apr 11 20:44:28 2017 13
GetManhattanClientEndo Tue Apr 11 20:44:29 2017 14
GetManhattanClient Tue Apr 11 20:44:29 2017 15
GetShoreTelPlayer Tue Apr 11 20:44:34 2017 16
GetConnectforMSFT Tue Apr 11 20:44:35 2017 17
GetCCClient Tue Apr 11 20:44:37 2017 18
SpawnLinuxBuild Tue Apr 11 20:44:38 2017 19
BuildSwitches Tue Apr 11 20:44:39 2017 20
BuildFilesys Tue Apr 11 21:07:07 2017 20
ReportDevEnvErrs Tue Apr 11 21:07:13 2017 20
CheckDevenvErrs Tue Apr 11 21:07:13 2017 20
BuildCASTestingTools Tue Apr 11 21:07:17 2017 21
ReportDevEnvErrs Tue Apr 11 21:07:33 2017 21
CheckDevenvErrs Tue Apr 11 21:07:33 2017 21
BuildPHR Tue Apr 11 21:07:33 2017 22
BuildRuby Tue Apr 11 21:07:36 2017 23
BuildRubyDirector2 Tue Apr 11 21:07:52 2017 24
CreateTAPISDK Tue Apr 11 21:13:30 2017 25
SignTDIMedia Tue Apr 11 21:13:31 2017 26
BuildAPPFolder Tue Apr 11 21:13:32 2017 27
BuildRubywin Tue Apr 11 21:13:40 2017 28
BuildRubymingw Tue Apr 11 21:18:06 2017 29
BuildNginx Tue Apr 11 21:22:05 2017 30
BuildNginx Tue Apr 11 21:22:06 2017 31
GetSalesforcePlugin Tue Apr 11 21:22:19 2017 32
RunPrep4Install Tue Apr 11 21:22:23 2017 33
RunPrep4Install_iteration_1 Tue Apr 11 21:27:23 2017 33
RunPrep4Install_iteration_2 Tue Apr 11 21:32:23 2017 33
RunPrep4Install_iteration_3 Tue Apr 11 21:37:23 2017 33
RunPrep4Install_iteration_4 Tue Apr 11 21:42:23 2017 33
RunPrep4Install_iteration_5 Tue Apr 11 21:47:23 2017 33
CheckSignCodeErrors Tue Apr 11 21:47:48 2017 33
Client_3rdParty_DiagMonitoring_Installer Tue Apr 11 21:47:48 2017 34
Build3rdPartyInstaller Tue Apr 11 21:47:48 2017 34
BuildMsiInstaller Tue Apr 11 21:47:48 2017 34
BuildDiagMonitoringInstaller Tue Apr 11 21:47:50 2017 34
BuildMsiInstaller Tue Apr 11 21:47:50 2017 34
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
CheckSignCodeErrors Tue Apr 11 22:01:25 2017 35
BuildRemoteServerDC Tue Apr 11 22:01:25 2017 36
BuildMsiInstaller Tue Apr 11 22:01:25 2017 36
CheckSignCodeErrors Tue Apr 11 22:08:12 2017 36
Server_LinuxDVX_Switch_Platform_DC_Installer Tue Apr 11 22:08:12 2017 37
BuildDataCenterServerInstaller Tue Apr 11 22:08:12 2017 37
BuildMsiInstaller Tue Apr 11 22:08:12 2017 37
BuildLinuxDVSInstaller Tue Apr 11 22:08:14 2017 37
BuildMsiInstaller Tue Apr 11 22:08:14 2017 37
BuildSwitchInstaller Tue Apr 11 22:08:14 2017 37
BuildMsiInstaller Tue Apr 11 22:08:14 2017 37
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
我的剧本:
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;
答案 0 :(得分:2)
我无法看到导致您所描述的问题的原因(“日志文件仍在文件夹当前”)但我当然可以帮助您解决问题的其他部分(“帮助我改进我的代码”)。我会写这样的代码:
# 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 ";
}
}
这有帮助吗?