Last active
December 6, 2017 21:51
-
-
Save AKSarav/4e3f949157a4e3afc52d5a859f735d48 to your computer and use it in GitHub Desktop.
Weblogic Application Deployment Parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
import sys | |
import subprocess | |
from sys import argv | |
from os.path import exists | |
import re | |
from collections import deque | |
from itertools import chain | |
import xlsxwriter | |
FLAG = "OFF" | |
mylist = [] | |
repo = {} | |
parents = [] | |
servername = "" | |
row =0 | |
col =0 | |
founddomain = "false" | |
# Testing the Startup Arguments for the script | |
if not (len(argv) < 3 ): | |
print "Argument is must" | |
sys.exit() | |
filename = argv[1].rstrip(); | |
#Initiate EXCEL | |
xlsfilename = "app-deployment-info.xlsx"; | |
print "Writing data to",xlsfilename; | |
workbook = xlsxwriter.Workbook(xlsfilename) | |
sheet1 = workbook.add_worksheet('domaininfo') | |
# Create a format to use in the merged range. | |
merge_format = workbook.add_format({ | |
'bold': 1, | |
'align': 'center', | |
'valign': 'vcenter', | |
'font_size': 12, | |
'border': 1 | |
}) | |
border_format = workbook.add_format() | |
border_format.set_border(style=1) | |
#Merge for headline | |
sheet1.merge_range('A1:B1', 'APP-DEPLOYMENT PARSER - REPORT', merge_format) | |
row = row+1 | |
if exists(filename): | |
#with open(filename,"r+") as fo | |
fo = open(filename,"r+") | |
os.system("clear") | |
print "-"* 0 | |
print "..Reading the file..",filename | |
print "-"*70 | |
print "\t\t WEBLOGIC APP-DEPLOYMENT PARSER \t\t" | |
print "\t\t\t WWW.MWINVENTORY.IN \t \t\t" | |
print "-"*70 | |
filecontent = fo.readlines() | |
for str in filecontent: | |
#print " Processing Line: \n",str | |
matchdomainname = re.search(r'(^\<)(name)(\>)(.+)(\<\/)(name)(\>)', str.strip()) | |
matchdomainver = re.search(r'(^\<)(domain-version)(\>)(.+)(\<\/)(domain-version)(\>)', str.strip()) | |
if matchdomainname and founddomain == "false": | |
founddomain = "true" | |
sheet1.write(row, col, "DomainName", border_format) | |
sheet1.write(row,col+1, matchdomainname.group(4), border_format) | |
row = row+1 | |
if matchdomainver: | |
sheet1.write(row, col, "DomainVersion", border_format) | |
sheet1.write(row,col+1, matchdomainver.group(4), border_format) | |
row = row+1 | |
match1 = re.search(r'\<app-deployment\>', str) | |
if match1: | |
#print str | |
FLAG = "ON" | |
match2 = re.search(r'\<\/app-deployment\>', str) | |
if match2: | |
#print str | |
FLAG = "OFF" | |
#print str.strip() | |
mylist.append(str.strip()) | |
if (FLAG == "ON"): | |
#print str.strip() | |
mylist.append(str.strip()) | |
fo.close() | |
else: | |
print "File is not present" | |
sys.exit() | |
elements={} | |
class parent(object): | |
def addelement(self,key,value): | |
self.__dict__[key]=value | |
servers=[] | |
class server(object): | |
def addelement(self,key,value): | |
self.__dict__[key]=value | |
''' | |
Below steps are for creating Dynamic variables and dictionary | |
element[server]=parent() # New object will be created under the list | |
element[server].addelement(key,value) would add new element to the dictionary like JSON | |
''' | |
mode="parent" | |
#print "------------------------------------------------------------" | |
try: | |
for line in mylist: | |
if re.search(r'(^\<)(.+)(\>)(.+)(\<\/)(.+)(\>)',line): | |
mode="child" | |
match=re.search(r'(^\<)(.+)(\>)(.+)(\<\/)(.+)(\>)',line) | |
if match: | |
if not ( "/" in root ): | |
print root+"_"+match.group(2),":", match.group(4) | |
sheet1.write(row, col, match.group(2),border_format) | |
sheet1.write(row, col+1, match.group(4),border_format) | |
elements[root].addelement(match.group(2),match.group(4)) #adding new element to the dict | |
key=root+"_"+match.group(2) | |
repo[key]=match.group(4) | |
else: | |
print "DID IT EVER COME HERE" | |
root = root.split("/")[1].strip() | |
print root+"_"+match.group(2),":", match.group(4) | |
sheet1.write(row, col, match.group(2),border_format) | |
sheet1.write(row, col+1, match.group(4),border_format) | |
elements[root].addelement(match.group(2),match.group(4)) #adding new element to the dict | |
key=root+"_"+match.group(2) | |
repo[key]=match.group(4) | |
elif re.search(r'(^\<)(.+)(\>)(\<\/)(.+)(\>)',line): | |
mode="child" | |
match=re.search(r'(^\<)(.+)(\>)(\<\/)(.+)(\>)',line) | |
if match: | |
if not ( "/" in root ): | |
print root+"_"+match.group(2),": NO VALUE" | |
sheet1.write(row, col, match.group(2),border_format) | |
sheet1.write(row, col+1, "NO VALUE",border_format) | |
elements[root].addelement(match.group(2),"NO VALUE") | |
key=root+"_"+match.group(2) | |
repo[key]="NO VALUE" | |
else: | |
root = root.split("/")[1].strip() | |
print root+"_"+match.group(2),": NO VALUE" | |
sheet1.write(row, col, match.group(2),border_format) | |
sheet1.write(row, col+1, "NO VALUE",border_format) | |
elements[root].addelement(match.group(2),"NO VALUE") | |
key=root+"_"+match.group(2) | |
repo[key]="NO VALUE" | |
elif re.search(r'(^\<|^\<\/)(.+)(\>$)',line): | |
mode="parent" | |
match=re.search(r'(^\<)(.+)(\>)',line) | |
if match: | |
if not "/" in match.group(2) : | |
mode = "child" | |
root=match.group(2) | |
parents.append(root) | |
#print "ADDED NEW ELEMENT", parents | |
else: | |
root=match.group(2) | |
mode = "child" | |
tmpstr=match.group(2).split("/")[1] | |
#print tmpstr; | |
if tmpstr in parents: | |
parents.remove(tmpstr) | |
#print "REMOVED ELEMENT", parents | |
#print parents | |
if len(parents) > 0: | |
root=parents[len(parents)-1] | |
if root not in elements.keys(): | |
#print "Adding new element object", root | |
elements[root] = parent() | |
if root == "/app-deployment": | |
print "----------------------------------------------------------" | |
if "/app-deployment" not in line: | |
row = row+1 | |
col = 0 | |
print "-"*70 | |
workbook.close() | |
except IndexError: | |
print "- " |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment