Advertisement
Guest User

Python Code

a guest
Mar 15th, 2016
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3.  
  4. fileA='/home/gpadmin/explain_plan.log' ## This is where the original explain plan is stored
  5. fileB='/tmp/temp2' ## This temp file will be removed at the end of the script
  6. fileC='/tmp/temp3' ## This temp file will be removed at the end of the script
  7.  
  8.  
  9. #########################################################################################################################################
  10. ## fileB and fileC are there for temporary storage space, these are used to store the temp things so a lot of the reversed information ##
  11. ## when turning the explain plan upside down because thats hot it reads in the 'before' file of the Explain_plan ##
  12. #########################################################################################################################################
  13.  
  14. #### Reverse Initial Output ####
  15.  
  16. header1=[]
  17. file1=open(fileA, 'r')
  18. header1.extend(file1.readline() for h in xrange(2))
  19. file1.close()
  20.  
  21. ###############################################################################
  22. ## reading some data from the file and saving it as the parameter 'header1'? ##
  23. ###############################################################################
  24.  
  25. file1=open(fileA, 'r')
  26. read1=[line for line in file1 if line.strip()]
  27. read1.reverse()
  28.  
  29. file2=open(fileB, 'a+')
  30. file2.writelines(header1)
  31. file2.writelines([item for item in read1[:-2]])
  32. file2.close()
  33.  
  34. #### Insert Steps ####
  35.  
  36. start = 1
  37. header2 = []
  38. step = [' ----- Step' + str(start) + ' -----\n\n']
  39. file2 = open(fileB, 'r')
  40. header2.extend(file2.readline() for h in xrange(3))
  41.  
  42. for i, linecount in enumerate(file2):
  43. if i > 2:
  44. prevspace = -1
  45. for line in file2:
  46. spaces = len(line) - len(line.lstrip())
  47. if spaces > prevspace and prevspace != -1:
  48. step.append('\n ----- Step' + str(start+1) + ' -----\n\n')
  49. start = start + 1
  50. step.append(line)
  51. prevspace = spaces
  52. else:
  53. step.append(line)
  54. prevspace = spaces
  55. break
  56. file2.close()
  57.  
  58. with open(fileC,'a') as file3:
  59. file3.writelines(header2)
  60. for line in step:
  61. file3.write(line)
  62. file3.flush()
  63. header1.reverse()
  64. file3.writelines(header1)
  65. file3.close()
  66.  
  67. #### Print Output Explain Plan ####
  68.  
  69. file3=open(fileC, 'r')
  70. read3=file3.read()
  71. file3.close()
  72. print(read3)
  73.  
  74. #### Remove Temp Files ####
  75.  
  76. import os
  77. os.remove(fileB)
  78. os.remove(fileC)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement