Back to home page

Project CMSSW displayed by LXR

 
 

    


Warning, /DQM/Integration/scripts/XMLcfgfiles/ExtractAppInfoFromXML is written in an unsupported language. File is not indexed.

0001 #!/usr/bin/env python3
0002 """Syntax:
0003         ExtracAppInfoFromXML [-sapc] file
0004 Parameters:
0005         file    file from where to read a RCMS configuration
0006         -s      list application servers found in the XML file
0007         -p      list the ports used found in the XML file
0008         -a      list the names of the applications configured in the XML file
0009         -c      list the cfg (eg dqmfu09-1_cfg.py) files
0010 Notes:  
0011         The default behavior is to present a table organized in the following way
0012                 SERVER PORT CFG_FILE APP_NAME
0013         which is equivalent to using -sapc
0014         
0015         The options selected and their order will affect teeh fields shown and their
0016         respective sorting. eg.
0017                 -sa will only show SERVER and APP_NAME and will sort first by SERVER and
0018                  then by APP_NAME 
0019                  
0020         OUTPUT is always unique in a per row bases
0021 """
0022 ################################################################################
0023 import sys, os.path,re
0024 from xml.dom import minidom
0025 ################################################################################
0026 # Some module's global variables.
0027 xmldoc=""
0028 
0029 def printXMLtree(head,l=0,bn=0):
0030         tabs=""
0031         for a in range(l):
0032                 tabs+="\t"
0033         try:
0034                 print "[%d-%d-%d]"%(l,bn,head.nodeType)+tabs+"+++++>"+head.tagName
0035         except AttributeError, e:
0036                 print "[%d-%d-%d]"%(l,bn,head.nodeType)+tabs+"+++++>"+str(e)
0037         print "[%d-%d-%d-v]"%(l,bn,head.nodeType)+tabs+"."+ (head.nodeValue or "None")
0038         try:
0039                 for katt,vatt in head.attributes.items():
0040                         if katt!="environmentString":
0041                                 print tabs+"%s=%s"%(katt,vatt)
0042                         else:
0043                                 print tabs+"%s= 'Some Stuff'"%(katt,)
0044         except:
0045                 pass    
0046         i=0
0047         for node in head.childNodes:
0048                 printXMLtree(node,l+1,i)
0049                 i+=1
0050 ################################################################################
0051 def compactNodeValue(head):
0052         firstborne=None
0053         for item in head.childNodes:
0054                 if item.nodeType == 3:
0055                         firstborne = item
0056                         break
0057         if not firstborne:
0058                 return
0059         for item in head.childNodes[1:]:
0060                 if item.nodeType == 3:
0061                         firstborne.nodeValue+=item.nodeValue
0062                         item.nodeValue=None
0063                         
0064 ################################################################################
0065 def appendDataXML(head):
0066         """Parses information that's XML format from value to the Docuemnt tree"""
0067         compactNodeValue(head)
0068         if head.firstChild.nodeValue:
0069                 newNode=minidom.parseString(head.firstChild.nodeValue)
0070                 for node in newNode.childNodes:
0071                         head.appendChild(node.cloneNode(True))
0072                 newNode.unlink()                        
0073 ################################################################################
0074 def getAppNameFromCfg(filename):
0075         """it searches for the line containing the string consumerName, usually
0076         found as a property of the process, and returns the set value found.
0077         eg. 
0078         matches line:
0079                 process.EventStreamHttpReader.consumerName = 'EcalEndcap DQM Consumer' 
0080         returns:
0081                 EcalEndcap DQM Consumer 
0082         """
0083         try:
0084                 f = open(filename)
0085                 consumer = f.readline()
0086                 name=""
0087                 while consumer :
0088                         consumer=consumer.strip()
0089                         if "consumerName" in consumer:
0090                                 name=re.search(r"(\"|\').*(\"|\')",consumer).group(0)
0091                                 name=name.strip("\"")
0092                                 name=name.strip("\'")
0093                                 #consumer[consumer.index("'")+1:consumer.index("'",consumer.index("'")+1)] 
0094                                 break
0095                         consumer = f.readline()
0096                 f.close()
0097         except IOError:
0098                 sys.stderr.write("WARNING: Unable to open file: " + filename + " from <configFile> section of XML\n")
0099                 name = "CONFIG FILE IS M.I.A"        
0100         return name
0101 ################################################################################
0102 def getProcNameFromCfg(filename):
0103         """it searches for the line containing the string consumerName, usually
0104         found as a property of the process, and returns the set value found.
0105         eg. 
0106         matches line:
0107                 process = cms.Process ("ECALDQM") 
0108         returns:
0109                 ECALDQM
0110         """
0111         try:
0112                 f = open(filename)
0113         except:
0114                 sys.stderr.write("Unable to open file: " + filename + " from <configFile> section of XML\n")
0115                 raise IOError
0116         consumer = f.readline()
0117         name=""
0118         while consumer :
0119                 consumer=consumer.strip()
0120                 if "cms.Process(" in consumer:
0121                         name=consumer[consumer.index("(")+2:consumer.index(")")-1] 
0122                         break
0123                 consumer = f.readline()
0124         f.close()
0125         return name
0126 ################################################################################
0127 def filterNodeList(branch1,nodeList):
0128         if len(branch1) > 0:
0129                 branch=branch1[:len(branch1)]
0130                 idx=0
0131                 for item in range(len(nodeList)):
0132                         vals=[v for (k,v) in nodeList[idx].attributes.items()]
0133                         if branch[0] not in vals:
0134                                 del nodeList[idx]
0135                         else:
0136                                 idx=idx+1
0137                 del branch[0]
0138         elif len(branch1)==0:
0139                 return nodeList 
0140         return filterNodeList(branch,nodeList)
0141                 
0142 ################################################################################
0143 def fillTable(order,branch=[]):
0144         global xmldoc
0145         table={} 
0146         if len(order)==0: 
0147                 return table
0148         key=min(order.keys())   
0149         k=order[key]
0150         order.pop(key)
0151         if k=="s":
0152                 lista=xmldoc.getElementsByTagName("XdaqExecutive")
0153                 lista=filterNodeList(branch,lista)
0154                 for item in lista:
0155                         table[item.attributes["hostname"].value]=""
0156                 for item in table.keys():
0157                         table[item]=fillTable(order.copy(),branch + [item])
0158         elif k=="a":
0159                 lista=xmldoc.getElementsByTagName("XdaqExecutive")
0160                 lista=filterNodeList(branch,lista)
0161                 for item in lista:
0162                         pset=item.getElementsByTagName("parameterSet")
0163                         if len(pset):
0164                                 arch=pset[0].firstChild.nodeValue[5:]
0165                                 appname=getAppNameFromCfg(arch) or getProcNameFromCfg(arch)
0166                                 table[appname]=""
0167                         else:
0168                                 App=item.getElementsByTagName("xc:Application")
0169                                 table[App[0].attributes["class"].value]=""
0170                 for item in table.keys():
0171                         table[item]=fillTable(order.copy(),branch)
0172         elif k=="p":
0173                 lista=xmldoc.getElementsByTagName("XdaqExecutive")
0174                 lista=filterNodeList(branch,lista)
0175                 for item in lista:
0176                         table[item.attributes["port"].value]=""
0177                 for item in table.keys():
0178                         table[item]=fillTable(order.copy(),branch + [item])
0179         elif k=="c":
0180                 lista=xmldoc.getElementsByTagName("XdaqExecutive")
0181                 lista=filterNodeList(branch,lista)
0182                 for item in lista:
0183                         pset=item.getElementsByTagName("parameterSet")
0184                         if not len(pset):
0185                                 table["No additional file"]=""  
0186                         else:
0187                                 table[pset[0].firstChild.nodeValue]=""
0188                 for item in table.keys():
0189                         table[item]=fillTable(order.copy(),branch)
0190         else:
0191                 pass
0192         return table
0193 ################################################################################
0194 def SortAndGrid(table,order):
0195         """table => {s:{p:{c:{a:{}}}}}"""
0196         grid=[]
0197         for (server,ports) in table.items():
0198                 for (port,configfiles) in ports.items():
0199                         for (configfile,appnames) in configfiles.items():
0200                                 for appname in appnames.keys():
0201                                         line=[]
0202                                         for col in order.values():
0203                                                 if col=="s":
0204                                                         line.append(server)
0205                                                 if col=="p":
0206                                                         line.append(port)
0207                                                 if col=="c":
0208                                                         line.append(configfile)
0209                                                 if col=="a":
0210                                                         line.append(appname)
0211                                         grid.append(line)
0212         grid.sort()
0213         return grid
0214 ################################################################################
0215 def printGrid(grid):
0216         numcols=len(grid[0])
0217         PPGrid=grid[:]
0218         maxs=[]
0219         for col in range(numcols):
0220                 maxs.append(0)
0221         for line in grid:
0222                 for col in range(numcols):
0223                         if len(line[col])>maxs[col]:
0224                                 maxs[col]=len(line[col])
0225         for line in PPGrid:
0226                 pline=""
0227                 for col in range(numcols):
0228                         pline+=line[col].ljust(maxs[col]+2)
0229                 print pline
0230                         
0231 ################################################################################        
0232 #getAppInfo                                                                    #
0233 ################################################################################
0234 def getAppInfo(XMLf,s=0,a=2,p=1,c=3):
0235         """     getAppInfo(XMLf,s=0,a=2,p=1,c=3) takes the file name of a valid RCMS 
0236                 configuration and 4     variables that represent which fields are desired 
0237                 and in which order. 
0238                 
0239                 It returns a touple containing a directory that contains all the 
0240                 relevant information in the XMLf file and a list of rows each row 
0241                 containing the fiels specified by the other four variables in the r
0242                 espective order
0243                 
0244                 The fields are Servers (s) ports(p) Appnames a.k.a. consumer names(a) 
0245                 and consumer config file. (Note: The consumerName is directly extracted 
0246                 from the config file.) if one field is not desired it should be assigned
0247                 a value of -1 eg s=-1. other wise their value is mapped from smallest to
0248                 largest ==> left to right. Note the default values, they will take 
0249                 precedence if not specifyed giving unexpected results
0250         """
0251         global xmldoc
0252         try: 
0253                 os.path.exists(XMLf)
0254         except:
0255                 sys.stderr.write('File doesn\'t exist\n')
0256                 sys.exit(2)
0257         try:
0258                 xmldoc = minidom.parse(XMLf)
0259         except IOError:
0260                 sys.stderr.write('Unable to locate file ' +XMLf +'\n')
0261                 return ({},[])
0262         except:
0263                 sys.stderr.write('Parser error\n')
0264                 return ({},[])
0265                 
0266         configFileNodes=xmldoc.getElementsByTagName("configFile")
0267         for node in configFileNodes:
0268                 appendDataXML(node)
0269         ## The table is always filled in a specific order, to properly get the data
0270         order={0:"s",1:"p",3:"a",2:"c"}
0271         #try:
0272         table=fillTable(order)
0273         #except:
0274         #       return ({},[])
0275         del order
0276         order={}
0277         if a != -1:
0278                 order[a]="a"
0279         if c != -1:
0280                 order[c]="c"
0281         if s != -1:
0282                 order[s]="s"
0283         if p != -1:
0284                 order[p]="p"
0285         grid=SortAndGrid(table,order)
0286         #printXMLtree(xmldoc)   
0287         #Clean Up
0288         xmldoc.unlink()
0289         return (table,grid)
0290                                                                   
0291 ################################################################################
0292 if __name__ == "__main__":             
0293         XMLfile=""
0294         args=sys.argv
0295         args.remove(args[0])
0296         options=""
0297         for arg in args:
0298                 if arg.startswith("-"):
0299                         options+=arg.strip("-")
0300                 else:
0301                         XMLfile=arg
0302         if options.count("s")+options.count("a")+options.count("p")+options.count("c")!=len(options):
0303                 sys.stderr.write(  "Sintax Error unrecognised option" )
0304                 sys.stderr.write( __doc__ )
0305                 sys.exit(2)
0306         if options.count("s")+options.count("a")+options.count("p")+options.count("c")==0:
0307                 (apptable,appinfo)=getAppInfo(XMLfile)
0308         else:
0309                 (apptable,appinfo)=getAppInfo(XMLfile,options.find("s"),options.find("a"),options.find("p"),options.find("c"))
0310         if appinfo != []:
0311                 printGrid(appinfo)
0312         apptable