Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BalakrishnaS1/bc2e119b1262114917a4bf0ffce97f9d to your computer and use it in GitHub Desktop.
Save BalakrishnaS1/bc2e119b1262114917a4bf0ffce97f9d to your computer and use it in GitHub Desktop.
import os
import Log_Splitter
import sys
"""This is a POC for extracting desired logs from folder level"""
"""Usage of this script: python LatestFolder.py "C:\\Program Files (x86)\\Jenkins\\jobs\\00_OPS\\jobs" "C:\\Users\\balakrishna\\Desktop\\ExtractedLogs"""
#Base directory where all our jobs are present
base_directory = sys.argv[1]
print("Base directory where all our jobs are present", base_directory)
#Folder where we copy our extracted logs
Output_folder = sys.argv[2]
print("Path of output folder where all our extracted logs are present", Output_folder)
#Navigate to the base directory
os.chdir(base_directory)
#Sort the folders in ascending order
folders = sorted(os.listdir(os.getcwd()), key=os.path.getmtime)
#Loop in to the build location, where our logs are present
for job in folders:
#Concatenate base directory and navigate to job names folder
os.chdir(os.path.join(base_directory,job))
#Navigate to builds folder
os.chdir("builds")
#Sort Latest builds# run from Jenkins
build_no = sorted(os.listdir(os.getcwd()), key=os.path.getmtime)
#Navigate inside the lastest build number
os.chdir(build_no[-1])
#Extract the desired logs and save it to output_folder
Log_Splitter.splitter(os.getcwd(),job+"_"+build_no[-1],Output_folder)
import re
def splitter(filein,out,outpath):
filein = filein+"\\"+"log"
#print("filein-->",filein)
#fileout = "C:\\Users\\balakrishna\\Desktop\\ExtractedLogs\\"+out+".txt"
fileout = outpath+"\\"+out+".txt"
outfile = open(fileout, 'w')
infile = open(filein,'r')
lines = infile.readlines()
for line in lines:
if re.search("Finished| Loading pro| \[Failed\] | * tests executed ", line):
outfile.write(line)
if __name__ == '__main__':
splitter(filein,out,outpath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment