根据xml内容修改配置文件
时间:2007-05-15 来源:maojj
#!/usr/bin/python
import shutil
import string
import os
import sys
import time
import re
import xml
import xml.etree.ElementTree
from xml.dom.minidom import Document
class UpdateCfg:
_modifiedDict = {}
_modifiedFilename = ''
def updateFile(self, asFilename, aoDict, asModifyMethod):
self._modifiedFilename = asFilename
self._modifiedDict = aoDict
#backup the file to be modified first.
shutil.copyfile(self._modifiedFilename, "%(filename)s_BAK"%{"filename":self._modifiedFilename}) #open the file to read
f = open(self._modifiedFilename)
loContentLst = f.readlines() #begin modify
for loKey, loValue in self._modifiedDict.iteritems():
loMethod = getattr(self, asModifyMethod)
loMethod(loContentLst, loKey, loValue) ##begin to save changes
f2 = open(self._modifiedFilename, 'w')
#f2 = open(self._modifiedFilename + ".txt",'w') # for test purpose
for lsLine in loContentLst:
f2.write(lsLine) f2.close()
print 'Process %(filename)s done.' % {"filename":self._modifiedFilename}
def replaceByKeyValue(self, aoContentLst, asKey, asValue):
#parameter style as key = value.
for i in range(len(aoContentLst)):
lsLine = aoContentLst[i]
#todo, use RE to do the replacement.
if lsLine.find(asKey)>=0:
#print lsLine
loList = aoContentLst[i].split("=")
lsLine = loList[0] + " = " + asValue + "\n"
aoContentLst[i] = lsLine
def replaceByLine(self, aoContentLst, asKey, asValue):
#parameter as data line.
for i in range(len(aoContentLst)):
lsLine = aoContentLst[i]
#todo, use RE to do the replacement.
if lsLine.find(asKey)>=0:
#print lsLine
lsLine = asValue + '\n'
aoContentLst[i] = lsLine def replaceAll(self, aoContentLst, asKey, asValue):
#do not care parameter style, repalce all key with value.
for i in range(len(aoContentLst)):
lsLine = aoContentLst[i]
#todo, use RE to do the replacement.
if lsLine.find(asKey)>=0:
#print lsLine
lsLine = lsLine.replace(asKey, asValue)
aoContentLst[i] = lsLine
def replaceAllAfterLine(self, aoContentLst, asKey, asValue):
for i in range(len(aoContentLst)):
lsLine = aoContentLst[i]
#todo, use RE to do the replacement.
if lsLine.find(asKey)>=0:
lst = i
#print str(lst)
#print aoContentLst
#lsLine = asValue
#aoContentLst.append(lsLine)
#print aoContentLst
try:
if lst:
ls = string.split(asValue, "\\n")
#print ls
del aoContentLst[lst:]
for ll in ls:
#print ll
aoContentLst.append(ll + "\n")
#aoContentLst=lst
#print lst
except UnboundLocalError:
print "replaceAllAfterLine Error:: " + asKey + " no find"
def append(self, aoContentLst, asKey, asValue):
#do not care parameter style, repalce all key with value.
for i in range(len(aoContentLst)):
lsLine = aoContentLst[i]
#todo, use RE to do the replacement.
if lsLine.find(asKey)>=0:
#print lsLine
#lsLine = lsLine.replace(asKey, asValue)
aoContentLst.insert(i+1, asValue+"\n")
break
#i = i+1 # skip one line
class ConfigHandler:
flagadd = False
flag = False
key = ""
childtuple = []
roottuple = []
def doc_order_iter(self, node):
yield node
for child in node.childNodes:
for cn in self.doc_order_iter(child):
yield cn
return
def pasernodecfg(self, node):
self.flag = False
self.flagadd = False
def pasernodefileName(self, node):
#print dir(node)
self.key = node.nodeName
self.flag = True
def pasernodemodify(self, node):
self.key = node.nodeName
self.flag = True def pasernodedict(self, node):
self.key = node.nodeName
self.flag = True
self.flagadd = True
def addValue(self, node):
print self.key + "=" + node.nodeValue
self.childtuple.append(node.nodeValue)
if (self.flagadd == True):
print "add++++"
self.roottuple.append(self.childtuple)
self.childtuple = []
def gettasknewxml(self, asConfigFilename):
tree = xml.etree.ElementTree.parse(asConfigFilename).getroot()
result = []
for el in tree.findall("ConfigurationFilename"):
confel = []
#print el.get("path")
#print dir(confel)
confel.append(el.get("path"))
for elc in el.findall("ModifyMethod"):
#print elc.tag
confel.append(elc.get("type"))
#print elc.get("type")
item = {}
for elitem in elc.findall("key"):
#print elitem.get("name")
#print elitem.text
item[elitem.get("name")] = elitem.text
#print item
confel.append(item)
result.append(confel)
return result def getTasksFromXML(self, asConfigFilename):
#lsContent = open(asConfigFilename).read()
f = open(asConfigFilename)
s = f.read()
f.close()
doc = xml.dom.minidom.parseString(s)
for node in self.doc_order_iter(doc): if self.flag == True:
self.addValue(node)
if node.nodeName == "cfg":
self.pasernodecfg(node)
elif node.nodeName == "ConfigurationFilename":
self.pasernodefileName(node)
elif node.nodeName == "ModifyMethod":
self.pasernodemodify(node)
elif node.nodeName == "ToBeModifiedSecion":
self.pasernodedict(node)
else:
self.flag = False
return self.roottuple
def getTasks(self, asConfigFilename):
#read file
lsContent = open(asConfigFilename).read()
#print lsContent
import re
#Allow space leading Configu..., must end with a ] at a line tail(leading only space allowed.
lsRE = "^[ ]*ConfigurationFilename[\s\S]*?^[ ]*}$"
loCpl = re.compile(lsRE, re.M)
loResults = loCpl.findall(lsContent)
#print loResults
#print "length is " + str(len(loResults))
#print loResults[0]
lsRE2 = "[^(\^\[ \]ConfigurationFilename)][\s\S]*(?=ModifyMethod)"
''' to process: get the filename
ConfigurationFilename = C:\Holibut\Workshop\Quest\Test case\ModifyCFGs\mm7p2a.properties
ToBeModifiedSecion = [
(MM7P2AHTTP_opco1.port, 7798,ReplaceByKeyValue),
(MM7P2AHTTPSSL_opco1.port, 7799,]ReplaceByKeyValue)
]
'''
lsRE3 = "ModifyMethod[\s\S]*(?=ToBeModifiedSecion)"
lsRE4 = "ToBeModifiedSecion[\s\S]*"
loRets = []
for lsItem in loResults:
'''
ConfigurationFilename = C:\Holibut\Workshop\Quest\Test case\ModifyCFGs\mgw.properties
ModifyMethod = replaceByKeyValue
ToBeModifiedSecion = {
"MM7P2ASOAPHANDLER_opco1.allowedMethods":"deliver deliveryreport readreply",
"MM7SOAPHANDLER_opco1.allowedMethods":"submit deliver deliveryreport readreply",
"DSMPSOAPHANDLER_opco1.allowedMethods":"MonthFeeReq MonthFeeReceiptReq AuthPriceReq",,
"DSMPCDRQP_opco1.dsmp_reply_port":"18888"
}
'''
#print lsItem
#get the filename
loCpl = re.compile(lsRE2)
loResults2 = loCpl.findall(lsItem)
#print loResults2
# ['= C:\\Holibut\\Workshop\\Quest\\Test case\\ModifyCFGs\\mm7p2a.properties\n']
lsFilename = loResults2[0].split("=")[1][:-1] # emit the trailling /n
lsFilename = lsFilename.strip()#removing white space
#print lsFilename
#get the modifyMethod
loCpl = re.compile(lsRE3)
loResults3 = loCpl.findall(lsItem)
lsModifyMethod = loResults3[0].split("=")[1][:-1] # emit the trailling /n
lsModifyMethod = lsModifyMethod.strip()#removing white space
#print lsModifyMethod #get the dict
loCpl = re.compile(lsRE4)
loResults4 = loCpl.findall(lsItem)
lsDict = loResults4[0].split("{")[1]
#The dict must start a line with {, and end with a } in a new line.
#print lsDict
#print "here1"
loDict = eval("{"+lsDict)
#print "Here2"
#print loDict
loRets.append((lsFilename, loDict, lsModifyMethod))
#return the result
return loRets
def doUpdate(config_file):
lsConfigFile = config_file
if os.path.isfile(lsConfigFile):
ch = ConfigHandler()
loArray = ch.getTasks(lsConfigFile)
for lsFilename, loDict, lsModifyMethod in loArray:
uc = UpdateCfg()
uc.updateFile(lsFilename, loDict, lsModifyMethod) def doxmlupdate(config_file, ipaddress):
lsConfigFile = config_file
if (ipaddress == "localhost"):
if os.path.isfile(lsConfigFile):
ch = ConfigHandler()
loArray = ch.gettasknewxml(lsConfigFile)
print loArray
for lsFilename, lsModifyMethod, loDict in loArray:
print "filename to be modified = " + lsFilename
print "modify items : " + str(loDict)
print "modify method: " + lsModifyMethod
#loDict = eval(loDict)
uc = UpdateCfg()
uc.updateFile(lsFilename, loDict, lsModifyMethod)
return True
else:
return False
else:
if (checkipadress(ipaddress)):
if os.path.isfile(lsConfigFile):
os.system("scp /root/updateCFG.py root@" + ipaddress + ":/tmp")
os.system("scp " + lsConfigFile + " root@" + ipaddress + ":/tmp")
os.system("ssh root@" + ipaddress + " '(/usr/local/gemini/gtf/bin/python /tmp/updateCFG.py /tmp/"+ lsConfigFile.split("/")[-1] + ")'")
return True
else:
return False
else:
return False def checkipadress(ipaddress):
return True
if __name__ == "__main__":
import sys
#print sys.argv[1]#the config file is import as command line arg.
if len(sys.argv)<2:
print "Usage: ModifyCFGs.py configFilePath"
sys.exit() lsConfigFile = sys.argv[1]
#loTest = MGWUpdateCfg()
#loTest.update()
#print "Configuration file modified done."
ch = ConfigHandler()
#loArray = ch.getTasks(r"C:\Holibut\Workshop\Quest\Test case\ModifyCFGs\ModifyCFGs.config.windows")#for windows os test
#loArray = ch.getTasks(r"/home/holibut/work/artifacts/ModifyCFGs.config")
#loArray = ch.getTasks(lsConfigFile)
if (lsConfigFile[-3:] == "txt"):
loArray = ch.getTasks(lsConfigFile)
#[(filename,method,dict),(filename,method,dict)]
#print loArray
for lsFilename, loDict, lsModifyMethod in loArray:
print "filename to be modified = " + lsFilename
print "modify items : " + str(loDict)
print "modify method: " + lsModifyMethod
#loDict = eval(loDict)
uc = UpdateCfg()
uc.updateFile(lsFilename, loDict, lsModifyMethod)
else:
loArray = ch.gettasknewxml(lsConfigFile)
#[(filename,method,dict),(filename,method,dict)]
print loArray for lsFilename, lsModifyMethod, loDict in loArray:
print "filename to be modified = " + lsFilename
print "modify items : " + str(loDict)
print "modify method: " + lsModifyMethod
#loDict = eval(loDict)
uc = UpdateCfg()
uc.updateFile(lsFilename, loDict, lsModifyMethod)
import shutil
import string
import os
import sys
import time
import re
import xml
import xml.etree.ElementTree
from xml.dom.minidom import Document
class UpdateCfg:
_modifiedDict = {}
_modifiedFilename = ''
def updateFile(self, asFilename, aoDict, asModifyMethod):
self._modifiedFilename = asFilename
self._modifiedDict = aoDict
#backup the file to be modified first.
shutil.copyfile(self._modifiedFilename, "%(filename)s_BAK"%{"filename":self._modifiedFilename}) #open the file to read
f = open(self._modifiedFilename)
loContentLst = f.readlines() #begin modify
for loKey, loValue in self._modifiedDict.iteritems():
loMethod = getattr(self, asModifyMethod)
loMethod(loContentLst, loKey, loValue) ##begin to save changes
f2 = open(self._modifiedFilename, 'w')
#f2 = open(self._modifiedFilename + ".txt",'w') # for test purpose
for lsLine in loContentLst:
f2.write(lsLine) f2.close()
print 'Process %(filename)s done.' % {"filename":self._modifiedFilename}
def replaceByKeyValue(self, aoContentLst, asKey, asValue):
#parameter style as key = value.
for i in range(len(aoContentLst)):
lsLine = aoContentLst[i]
#todo, use RE to do the replacement.
if lsLine.find(asKey)>=0:
#print lsLine
loList = aoContentLst[i].split("=")
lsLine = loList[0] + " = " + asValue + "\n"
aoContentLst[i] = lsLine
def replaceByLine(self, aoContentLst, asKey, asValue):
#parameter as data line.
for i in range(len(aoContentLst)):
lsLine = aoContentLst[i]
#todo, use RE to do the replacement.
if lsLine.find(asKey)>=0:
#print lsLine
lsLine = asValue + '\n'
aoContentLst[i] = lsLine def replaceAll(self, aoContentLst, asKey, asValue):
#do not care parameter style, repalce all key with value.
for i in range(len(aoContentLst)):
lsLine = aoContentLst[i]
#todo, use RE to do the replacement.
if lsLine.find(asKey)>=0:
#print lsLine
lsLine = lsLine.replace(asKey, asValue)
aoContentLst[i] = lsLine
def replaceAllAfterLine(self, aoContentLst, asKey, asValue):
for i in range(len(aoContentLst)):
lsLine = aoContentLst[i]
#todo, use RE to do the replacement.
if lsLine.find(asKey)>=0:
lst = i
#print str(lst)
#print aoContentLst
#lsLine = asValue
#aoContentLst.append(lsLine)
#print aoContentLst
try:
if lst:
ls = string.split(asValue, "\\n")
#print ls
del aoContentLst[lst:]
for ll in ls:
#print ll
aoContentLst.append(ll + "\n")
#aoContentLst=lst
#print lst
except UnboundLocalError:
print "replaceAllAfterLine Error:: " + asKey + " no find"
def append(self, aoContentLst, asKey, asValue):
#do not care parameter style, repalce all key with value.
for i in range(len(aoContentLst)):
lsLine = aoContentLst[i]
#todo, use RE to do the replacement.
if lsLine.find(asKey)>=0:
#print lsLine
#lsLine = lsLine.replace(asKey, asValue)
aoContentLst.insert(i+1, asValue+"\n")
break
#i = i+1 # skip one line
class ConfigHandler:
flagadd = False
flag = False
key = ""
childtuple = []
roottuple = []
def doc_order_iter(self, node):
yield node
for child in node.childNodes:
for cn in self.doc_order_iter(child):
yield cn
return
def pasernodecfg(self, node):
self.flag = False
self.flagadd = False
def pasernodefileName(self, node):
#print dir(node)
self.key = node.nodeName
self.flag = True
def pasernodemodify(self, node):
self.key = node.nodeName
self.flag = True def pasernodedict(self, node):
self.key = node.nodeName
self.flag = True
self.flagadd = True
def addValue(self, node):
print self.key + "=" + node.nodeValue
self.childtuple.append(node.nodeValue)
if (self.flagadd == True):
print "add++++"
self.roottuple.append(self.childtuple)
self.childtuple = []
def gettasknewxml(self, asConfigFilename):
tree = xml.etree.ElementTree.parse(asConfigFilename).getroot()
result = []
for el in tree.findall("ConfigurationFilename"):
confel = []
#print el.get("path")
#print dir(confel)
confel.append(el.get("path"))
for elc in el.findall("ModifyMethod"):
#print elc.tag
confel.append(elc.get("type"))
#print elc.get("type")
item = {}
for elitem in elc.findall("key"):
#print elitem.get("name")
#print elitem.text
item[elitem.get("name")] = elitem.text
#print item
confel.append(item)
result.append(confel)
return result def getTasksFromXML(self, asConfigFilename):
#lsContent = open(asConfigFilename).read()
f = open(asConfigFilename)
s = f.read()
f.close()
doc = xml.dom.minidom.parseString(s)
for node in self.doc_order_iter(doc): if self.flag == True:
self.addValue(node)
if node.nodeName == "cfg":
self.pasernodecfg(node)
elif node.nodeName == "ConfigurationFilename":
self.pasernodefileName(node)
elif node.nodeName == "ModifyMethod":
self.pasernodemodify(node)
elif node.nodeName == "ToBeModifiedSecion":
self.pasernodedict(node)
else:
self.flag = False
return self.roottuple
def getTasks(self, asConfigFilename):
#read file
lsContent = open(asConfigFilename).read()
#print lsContent
import re
#Allow space leading Configu..., must end with a ] at a line tail(leading only space allowed.
lsRE = "^[ ]*ConfigurationFilename[\s\S]*?^[ ]*}$"
loCpl = re.compile(lsRE, re.M)
loResults = loCpl.findall(lsContent)
#print loResults
#print "length is " + str(len(loResults))
#print loResults[0]
lsRE2 = "[^(\^\[ \]ConfigurationFilename)][\s\S]*(?=ModifyMethod)"
''' to process: get the filename
ConfigurationFilename = C:\Holibut\Workshop\Quest\Test case\ModifyCFGs\mm7p2a.properties
ToBeModifiedSecion = [
(MM7P2AHTTP_opco1.port, 7798,ReplaceByKeyValue),
(MM7P2AHTTPSSL_opco1.port, 7799,]ReplaceByKeyValue)
]
'''
lsRE3 = "ModifyMethod[\s\S]*(?=ToBeModifiedSecion)"
lsRE4 = "ToBeModifiedSecion[\s\S]*"
loRets = []
for lsItem in loResults:
'''
ConfigurationFilename = C:\Holibut\Workshop\Quest\Test case\ModifyCFGs\mgw.properties
ModifyMethod = replaceByKeyValue
ToBeModifiedSecion = {
"MM7P2ASOAPHANDLER_opco1.allowedMethods":"deliver deliveryreport readreply",
"MM7SOAPHANDLER_opco1.allowedMethods":"submit deliver deliveryreport readreply",
"DSMPSOAPHANDLER_opco1.allowedMethods":"MonthFeeReq MonthFeeReceiptReq AuthPriceReq",,
"DSMPCDRQP_opco1.dsmp_reply_port":"18888"
}
'''
#print lsItem
#get the filename
loCpl = re.compile(lsRE2)
loResults2 = loCpl.findall(lsItem)
#print loResults2
# ['= C:\\Holibut\\Workshop\\Quest\\Test case\\ModifyCFGs\\mm7p2a.properties\n']
lsFilename = loResults2[0].split("=")[1][:-1] # emit the trailling /n
lsFilename = lsFilename.strip()#removing white space
#print lsFilename
#get the modifyMethod
loCpl = re.compile(lsRE3)
loResults3 = loCpl.findall(lsItem)
lsModifyMethod = loResults3[0].split("=")[1][:-1] # emit the trailling /n
lsModifyMethod = lsModifyMethod.strip()#removing white space
#print lsModifyMethod #get the dict
loCpl = re.compile(lsRE4)
loResults4 = loCpl.findall(lsItem)
lsDict = loResults4[0].split("{")[1]
#The dict must start a line with {, and end with a } in a new line.
#print lsDict
#print "here1"
loDict = eval("{"+lsDict)
#print "Here2"
#print loDict
loRets.append((lsFilename, loDict, lsModifyMethod))
#return the result
return loRets
def doUpdate(config_file):
lsConfigFile = config_file
if os.path.isfile(lsConfigFile):
ch = ConfigHandler()
loArray = ch.getTasks(lsConfigFile)
for lsFilename, loDict, lsModifyMethod in loArray:
uc = UpdateCfg()
uc.updateFile(lsFilename, loDict, lsModifyMethod) def doxmlupdate(config_file, ipaddress):
lsConfigFile = config_file
if (ipaddress == "localhost"):
if os.path.isfile(lsConfigFile):
ch = ConfigHandler()
loArray = ch.gettasknewxml(lsConfigFile)
print loArray
for lsFilename, lsModifyMethod, loDict in loArray:
print "filename to be modified = " + lsFilename
print "modify items : " + str(loDict)
print "modify method: " + lsModifyMethod
#loDict = eval(loDict)
uc = UpdateCfg()
uc.updateFile(lsFilename, loDict, lsModifyMethod)
return True
else:
return False
else:
if (checkipadress(ipaddress)):
if os.path.isfile(lsConfigFile):
os.system("scp /root/updateCFG.py root@" + ipaddress + ":/tmp")
os.system("scp " + lsConfigFile + " root@" + ipaddress + ":/tmp")
os.system("ssh root@" + ipaddress + " '(/usr/local/gemini/gtf/bin/python /tmp/updateCFG.py /tmp/"+ lsConfigFile.split("/")[-1] + ")'")
return True
else:
return False
else:
return False def checkipadress(ipaddress):
return True
if __name__ == "__main__":
import sys
#print sys.argv[1]#the config file is import as command line arg.
if len(sys.argv)<2:
print "Usage: ModifyCFGs.py configFilePath"
sys.exit() lsConfigFile = sys.argv[1]
#loTest = MGWUpdateCfg()
#loTest.update()
#print "Configuration file modified done."
ch = ConfigHandler()
#loArray = ch.getTasks(r"C:\Holibut\Workshop\Quest\Test case\ModifyCFGs\ModifyCFGs.config.windows")#for windows os test
#loArray = ch.getTasks(r"/home/holibut/work/artifacts/ModifyCFGs.config")
#loArray = ch.getTasks(lsConfigFile)
if (lsConfigFile[-3:] == "txt"):
loArray = ch.getTasks(lsConfigFile)
#[(filename,method,dict),(filename,method,dict)]
#print loArray
for lsFilename, loDict, lsModifyMethod in loArray:
print "filename to be modified = " + lsFilename
print "modify items : " + str(loDict)
print "modify method: " + lsModifyMethod
#loDict = eval(loDict)
uc = UpdateCfg()
uc.updateFile(lsFilename, loDict, lsModifyMethod)
else:
loArray = ch.gettasknewxml(lsConfigFile)
#[(filename,method,dict),(filename,method,dict)]
print loArray for lsFilename, lsModifyMethod, loDict in loArray:
print "filename to be modified = " + lsFilename
print "modify items : " + str(loDict)
print "modify method: " + lsModifyMethod
#loDict = eval(loDict)
uc = UpdateCfg()
uc.updateFile(lsFilename, loDict, lsModifyMethod)
相关阅读 更多 +