Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python
- fileA='/home/gpadmin/explain_plan.log' ## This is where the original explain plan is stored
- fileB='/tmp/temp2' ## This temp file will be removed at the end of the script
- fileC='/tmp/temp3' ## This temp file will be removed at the end of the script
- #########################################################################################################################################
- ## fileB and fileC are there for temporary storage space, these are used to store the temp things so a lot of the reversed information ##
- ## when turning the explain plan upside down because thats hot it reads in the 'before' file of the Explain_plan ##
- #########################################################################################################################################
- #### Reverse Initial Output ####
- header1=[]
- file1=open(fileA, 'r')
- header1.extend(file1.readline() for h in xrange(2))
- file1.close()
- ###############################################################################
- ## reading some data from the file and saving it as the parameter 'header1'? ##
- ###############################################################################
- file1=open(fileA, 'r')
- read1=[line for line in file1 if line.strip()]
- read1.reverse()
- file2=open(fileB, 'a+')
- file2.writelines(header1)
- file2.writelines([item for item in read1[:-2]])
- file2.close()
- #### Insert Steps ####
- start = 1
- header2 = []
- step = [' ----- Step' + str(start) + ' -----\n\n']
- file2 = open(fileB, 'r')
- header2.extend(file2.readline() for h in xrange(3))
- for i, linecount in enumerate(file2):
- if i > 2:
- prevspace = -1
- for line in file2:
- spaces = len(line) - len(line.lstrip())
- if spaces > prevspace and prevspace != -1:
- step.append('\n ----- Step' + str(start+1) + ' -----\n\n')
- start = start + 1
- step.append(line)
- prevspace = spaces
- else:
- step.append(line)
- prevspace = spaces
- break
- file2.close()
- with open(fileC,'a') as file3:
- file3.writelines(header2)
- for line in step:
- file3.write(line)
- file3.flush()
- header1.reverse()
- file3.writelines(header1)
- file3.close()
- #### Print Output Explain Plan ####
- file3=open(fileC, 'r')
- read3=file3.read()
- file3.close()
- print(read3)
- #### Remove Temp Files ####
- import os
- os.remove(fileB)
- os.remove(fileC)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement