def parse(self, file): doc = FromXmlStream(file) filter = ScreenNodeFilter({}) iter = doc.createTreeWalker(doc, filter.SHOW_ELEMENT, filter, 0) node = iter.nextNode() while node: if node.nodeName == 'roll': self.handle_rollChild(node) node = iter.nextNode()
def run(self): if self.args: file = self.args[0] else: file = sys.stdin doc = FromXmlStream(file) filter = MyNodeFilter() iter = doc.createTreeWalker(doc, NodeFilter.SHOW_ELEMENT, filter, 0) node = iter.nextNode() while node: eval('self.handle_%s(node)' % (node.nodeName)) node = iter.nextNode() self.createReport()
def parse(self, file): doc = FromXmlStream(file) filter = ScreenNodeFilter(self.getArch()) iter = doc.createTreeWalker(doc, filter.SHOW_ELEMENT, filter, 0) node = iter.nextNode() while node: if node.nodeName == "screen": self.screens.append({}) child = iter.firstChild() while child: self.handle_screenChild(child) child = iter.nextSibling() node = iter.nextNode()
def parse(self, file): doc = FromXmlStream(file) filter = ScreenNodeFilter(self.getArch()) iter = doc.createTreeWalker(doc, filter.SHOW_ELEMENT, filter, 0) node = iter.nextNode() while node: if node.nodeName == 'screen': self.screens.append({}) child = iter.firstChild() while child: self.handle_screenChild(child) child = iter.nextSibling() node = iter.nextNode()
def _retrieveTask(self, location): try: return FromXmlStream(location) except: raise TaskerError( "Task file error\nReading file '%s'. It doesn't exist or it is a malformed XML file." % location)
#!/usr/bin/env python import sys from xml.dom.ext.reader.Sax2 import FromXmlStream from xml.dom.ext import PrettyPrint # get DOM object doc = FromXmlStream(sys.stdin) PrettyPrint(doc)
def parse(self, astr_inputStream): #sys.stdout.write("%40s" % "Parsing XML document...") sys.stdout.flush() self.mXMLdoc = FromXmlStream(astr_inputStream) self.mstr_XMLfile = astr_inputStream
class C_DOM_kXMLHandler(C_kXMLHandler): # # Member variables # # - Core variables mstr_obj = 'C_DOM_kXMLHandler'; # name of object class mstr_name = 'void'; # name of object variable m_id = -1; # id of agent m_iter = 0; # current iteration in an # arbitrary processing # scheme m_verbosity = 0; # debug related value for # object m_warnings = 0; # show warnings # (and warnings level) # # - Class variables # Core variables - specific mXMLdoc = None # # Methods # # Core methods - construct, initialise, id def core_construct( self, astr_obj = 'C_DOM_kXMLHandler', astr_name = 'void', a_id = -1, a_iter = 0, a_verbosity = 0, a_warnings = 0) : self.mstr_obj = astr_obj self.mstr_name = astr_name self.m_id = a_id self.m_iter = a_iter self.m_verbosity = a_verbosity self.m_warnings = a_warnings self.internals_reset() def internals_reset(self): for key in self.mC_struct.ml_keys : self.mdict_element[key] = '' self.mbdict_inTag[key] = False def __str__(self): print 'mstr_obj\t\t= %s' % self.mstr_obj print 'mstr_name\t\t= %s' % self.mstr_name print 'm_id\t\t\t= %d' % self.m_id print 'm_iter\t\t\t= %d' % self.m_iter print 'm_verbosity\t\t= %d' % self.m_verbosity print 'm_warnings\t\t= %d' % self.m_warnings return 'This class is a DOM specialisation of the XML kXMLHandler.' def __init__(self, aC_struct, astr_mainTag): C_kXMLHandler.__init__(self, aC_struct, astr_mainTag) self.core_construct() def parse(self, astr_inputStream): #sys.stdout.write("%40s" % "Parsing XML document...") sys.stdout.flush() self.mXMLdoc = FromXmlStream(astr_inputStream) self.mstr_XMLfile = astr_inputStream #print "%40s\n" % "[ ok ]" def elements_listAll(self, alstr_tag): # # PRECONDITIONS # o alstr_tag is a list of text tag names # # POSTCONDITIONS # o Each tag in the passed list is found in the XML database # and its text value printed. This is across all the # entries in the XML database. # for database in self.mXMLdoc.childNodes: for dbChild in database.childNodes: for record in dbChild.childNodes: if record.nodeType == record.ELEMENT_NODE: str_tag = record.tagName if str_tag in alstr_tag: for value in record.childNodes: if value.nodeType == value.TEXT_NODE: str_data = value.data print "%s:\t%s" % (str_tag, str_data) def element_extract(self, astr_tag, astr_value, ab_removeElement = 0): # # PRECONDITIONS # o str_tag is a tag name to search for # o str_value is the value of the tag to extract # # POSTCONDITIONS # - element_extract('idCode', 'LSP-001') # will extract the node (and its children) containing # an 'idCode' of 'LSP-001' # - if <ab_removeElement>, then the dbChild containing the # target tag value is removed from the database. # b_targetFound = 0 for database in self.mXMLdoc.childNodes: for dbChild in database.childNodes: for record in dbChild.childNodes: if record.nodeType == record.ELEMENT_NODE: str_tag = record.tagName for value in record.childNodes: if value.nodeType == value.TEXT_NODE: str_data = value.data self.mdict_element[str_tag] = str_data if str_tag == astr_tag and str_data == astr_value: b_targetFound = 1 if b_targetFound: for key in self.mC_struct.ml_keys: self.mC_struct.mdict_sgmlCore[key].value_set(self.mdict_element[key]) if ab_removeElement: database.removeChild(dbChild) str_edittedDBName = "%s.edt" % self.mstr_XMLfile file_edittedDBName = open(str_edittedDBName, 'w') PrettyPrint(self.mXMLdoc, file_edittedDBName) return b_targetFound return b_targetFound def element_extractFirst(self): # # PRECONDITIONS # o Assumes primarily that the XML database consists of a single entry # as in the case of the distributed database. Will still work on an # input monolithic database, though. # # o Design was derived from the monolithic case, so some extra looping # and redundant flags are present. # # POSTCONDITIONS # o Reads the first element in the XML database and populates internal # kedasa structure. # # NOTE: # o Does not properly handle attribute lists. # b_targetFound = 0 b_tagValueFound = 0 dataCount = 0 for database in self.mXMLdoc.childNodes: for dbChild in database.childNodes: for record in dbChild.childNodes: if record.nodeType == record.ELEMENT_NODE: str_tag = record.tagName for value in record.childNodes: if value.nodeType == value.TEXT_NODE: str_data = value.data b_tagValueFound = 1 if b_tagValueFound: self.mdict_element[str_tag] = str_data self.mC_struct.mdict_sgmlCore[str_tag].value_set(str_data) b_tagValueFound = 0 dataCount += 1 b_targetFound = 1 return dataCount def element_listFirst(self, alstr_tag): # # PRECONDITIONS # o alstr_tag is a list of text tag names # # POSTCONDITIONS # o Each tag in the passed list is found in the XML document # and its text value printed. # l_data = [] i = 0 for str_tag in alstr_tag: l_data.append([]) for tag in self.mXMLdoc.getElementsByTagName(str_tag): b_hit = 1 tag.normalize() l_data[i].append(tag.firstChild.data) i = i + 1 for j in range(0, i): print l_data[j][0]
def __init__(self, config_file): fp = open(config_file,'r') self.dom = FromXmlStream(fp) fp.close() self.config_file=config_file
class C_DOM_kXMLHandler(C_kXMLHandler): # # Member variables # # - Core variables mstr_obj = 'C_DOM_kXMLHandler' # name of object class mstr_name = 'void' # name of object variable m_id = -1 # id of agent m_iter = 0 # current iteration in an # arbitrary processing # scheme m_verbosity = 0 # debug related value for # object m_warnings = 0 # show warnings # (and warnings level) # # - Class variables # Core variables - specific mXMLdoc = None # # Methods # # Core methods - construct, initialise, id def core_construct(self, astr_obj='C_DOM_kXMLHandler', astr_name='void', a_id=-1, a_iter=0, a_verbosity=0, a_warnings=0): self.mstr_obj = astr_obj self.mstr_name = astr_name self.m_id = a_id self.m_iter = a_iter self.m_verbosity = a_verbosity self.m_warnings = a_warnings self.internals_reset() def internals_reset(self): for key in self.mC_struct.ml_keys: self.mdict_element[key] = '' self.mbdict_inTag[key] = False def __str__(self): print 'mstr_obj\t\t= %s' % self.mstr_obj print 'mstr_name\t\t= %s' % self.mstr_name print 'm_id\t\t\t= %d' % self.m_id print 'm_iter\t\t\t= %d' % self.m_iter print 'm_verbosity\t\t= %d' % self.m_verbosity print 'm_warnings\t\t= %d' % self.m_warnings return 'This class is a DOM specialisation of the XML kXMLHandler.' def __init__(self, aC_struct, astr_mainTag): C_kXMLHandler.__init__(self, aC_struct, astr_mainTag) self.core_construct() def parse(self, astr_inputStream): #sys.stdout.write("%40s" % "Parsing XML document...") sys.stdout.flush() self.mXMLdoc = FromXmlStream(astr_inputStream) self.mstr_XMLfile = astr_inputStream #print "%40s\n" % "[ ok ]" def elements_listAll(self, alstr_tag): # # PRECONDITIONS # o alstr_tag is a list of text tag names # # POSTCONDITIONS # o Each tag in the passed list is found in the XML database # and its text value printed. This is across all the # entries in the XML database. # for database in self.mXMLdoc.childNodes: for dbChild in database.childNodes: for record in dbChild.childNodes: if record.nodeType == record.ELEMENT_NODE: str_tag = record.tagName if str_tag in alstr_tag: for value in record.childNodes: if value.nodeType == value.TEXT_NODE: str_data = value.data print "%s:\t%s" % (str_tag, str_data) def element_extract(self, astr_tag, astr_value, ab_removeElement=0): # # PRECONDITIONS # o str_tag is a tag name to search for # o str_value is the value of the tag to extract # # POSTCONDITIONS # - element_extract('idCode', 'LSP-001') # will extract the node (and its children) containing # an 'idCode' of 'LSP-001' # - if <ab_removeElement>, then the dbChild containing the # target tag value is removed from the database. # b_targetFound = 0 for database in self.mXMLdoc.childNodes: for dbChild in database.childNodes: for record in dbChild.childNodes: if record.nodeType == record.ELEMENT_NODE: str_tag = record.tagName for value in record.childNodes: if value.nodeType == value.TEXT_NODE: str_data = value.data self.mdict_element[str_tag] = str_data if str_tag == astr_tag and str_data == astr_value: b_targetFound = 1 if b_targetFound: for key in self.mC_struct.ml_keys: self.mC_struct.mdict_sgmlCore[key].value_set( self.mdict_element[key]) if ab_removeElement: database.removeChild(dbChild) str_edittedDBName = "%s.edt" % self.mstr_XMLfile file_edittedDBName = open(str_edittedDBName, 'w') PrettyPrint(self.mXMLdoc, file_edittedDBName) return b_targetFound return b_targetFound def element_extractFirst(self): # # PRECONDITIONS # o Assumes primarily that the XML database consists of a single entry # as in the case of the distributed database. Will still work on an # input monolithic database, though. # # o Design was derived from the monolithic case, so some extra looping # and redundant flags are present. # # POSTCONDITIONS # o Reads the first element in the XML database and populates internal # kedasa structure. # # NOTE: # o Does not properly handle attribute lists. # b_targetFound = 0 b_tagValueFound = 0 dataCount = 0 for database in self.mXMLdoc.childNodes: for dbChild in database.childNodes: for record in dbChild.childNodes: if record.nodeType == record.ELEMENT_NODE: str_tag = record.tagName for value in record.childNodes: if value.nodeType == value.TEXT_NODE: str_data = value.data b_tagValueFound = 1 if b_tagValueFound: self.mdict_element[str_tag] = str_data self.mC_struct.mdict_sgmlCore[str_tag].value_set( str_data) b_tagValueFound = 0 dataCount += 1 b_targetFound = 1 return dataCount def element_listFirst(self, alstr_tag): # # PRECONDITIONS # o alstr_tag is a list of text tag names # # POSTCONDITIONS # o Each tag in the passed list is found in the XML document # and its text value printed. # l_data = [] i = 0 for str_tag in alstr_tag: l_data.append([]) for tag in self.mXMLdoc.getElementsByTagName(str_tag): b_hit = 1 tag.normalize() l_data[i].append(tag.firstChild.data) i = i + 1 for j in range(0, i): print l_data[j][0]
def main(): global spinlogfile buildstatus = 'Pass' builderror = 'Build success' try: # Parse Job description XML file to get buildscript list try: agent_scripts = {} Parse_XML.GetAgentScriptInfo(spinxmlname, agent_scripts) #debugprint ( status) except: status = "Error --- parsing Job Description file. " #debugprint ( status ) pass # Create directories to store log and build output information. # Directories: 'logs', 'build\release' and 'build\engineering' dir = os.getcwd() try: if spinutil.mswindows: if not os.path.isdir('logs'): os.mkdir("logs") if not os.path.isdir('build'): os.mkdir("build") os.mkdir(".\\build\\release") os.mkdir('.\\build\\engineering') if not os.path.isdir('.\\build\\release'): os.mkdir(".\\build\\release") if not os.path.isdir('.\\build\\engineering'): os.mkdir('.\\build\\engineering') else: if not os.path.isdir('logs'): os.mkdir("logs") if not os.path.isdir('build'): os.makedirs("build/release") os.makedirs("build/engineering") if not os.path.isdir('build/release'): os.makedirs("build/release") if not os.path.isdir('build/engineering'): os.makedirs("build/engineering") spinutil.run('chmod -R 755 ./* ') except: spinutil.logprint("Error --- Create 'agentroot/build' and/or 'agentroot/logs' directories.") pass # Generate compressed log file name and clean that file, if it exists in advance. projectname = string.strip(agent_scripts['projectname']) zipf = projectname + '_' + string.strip(agent_scripts['buildname']) + '_logs.zip' zipf = os.path.join('logs', zipf) if os.path.exists(zipf): os.remove(zipf) # Start Builing ... ... spinutil.setstatus('Building') buildscriptlist = agent_scripts['scriptlist']['buildscript'] # Variables to preserve log file list. #pre_filelist = [] filelist = [] for buildscript in buildscriptlist: # Check 'cancel.lock' file, it exists, then break out of the loop for 'soft' cancel. if os.path.isfile(cancellockfile): os.remove(cancellockfile) spinutil.logprint ('Build job cancelled') break spinutil.setstatus(buildscript['displaystatus']) # Setup <status> based on <displaystatus> ## buildcommand = '%s %s %s'% (buildscript['name'], agent_scripts['buildnumber'], buildscript['arguments']) buildcommand = '%s %s %s'% (buildscript['name'], agent_scripts['buildname'], buildscript['arguments']) if pytp.findall(buildscript['name']) <> [] and spinutil.mswindows: buildcommand = os.path.join(sys.prefix, 'python.exe ') + buildcommand #print "Run buildscript %s ..." % buildcommand spinutil.logprint("Run buildscript %s ..." % buildcommand) spinutil.run(buildcommand) # Tracking log file generating sequence for log files insertion into 'spin.xml' later # Othe processing # After each buildscript is executed, log files are to be processed. os.chdir(dir) # Logg files may have other extension name. It need to change into the 'logs' # directory for following processing. filelist = os.listdir('logs') # Remove '.zip' file names from the file list. path = os.path.join('logs', '*.zip') zipfilelist = glob.glob(path) if zipfilelist <> []: for f in zipfilelist: ff = os.path.basename(f) if ff in filelist: filelist.remove(ff) # 'Substrct' 'pre_filelist' from the current 'filelist'. # for f in pre_filelist: # if f in filelist: # filelist.remove(f) # Update 'pre_filelist' to the current filelist. #pre_filelist = pre_filelist.extend(filelist) path = os.path.join('logs', '*.xml') xmlfilelist = glob.glob(path) fdict = {} #print filelist if xmlfilelist == []: # Branch Successful --- # No XML format generated by build script, free style log files are to be parsed to generate # XML format log files, then to insert in to 'spin.xml' file. os.chdir('logs') # If using 'glob' module, this will be moved in front of the next loop. for f in filelist: list = [] try: ctime = os.path.getctime(f) #print ctime if ctime in fdict.keys(): fdict[ctime].append(os.path.basename(f)) else: list.append(os.path.basename(f)) fdict[ctime] = list except: spinutil.logprint("Error --- get file creation time") pass ctimelist = fdict.keys() ctimelist.sort() #os.chdir('logs') logxmlfilelist = [] for ctime in ctimelist: for logfile in fdict[ctime]: #print f (lname,lext) = os.path.splitext(logfile) logxmlfile = lname + "_log.xml" # Generate log XML file from each free style log file. logtype = None if string.lower(agent_scripts['projectname']) == 'ws5000': logtype = 'ws5000build' elif string.lower(agent_scripts['projectname']) == 'tntfusion': logtype = 'tntfusionbuild' if logfile == 'agent_script.log': st = spinutil.makexmllog(logxmlfile, 'agentscriptlog', logfile, logtype) else: st = spinutil.makexmlfile(logxmlfile, 'buildlog', logfile, logtype) if st == 2: buildstatus = 'Failed' builderror = 'Build failed' # Track xmlfile name into the list. logxmlfilelist.append(logxmlfile) else: # Branch Fail --- # XML format generated by build script, free style log files need NOT to be parsed to # generateXML format log files; Those XML log files could be inserted in to 'spin.xml' file directly. # Those xml logfile name need to be removed from 'pre_filelist' # Remove the XML file list from 'filelist' for f in xmlfilelist: #if f in pre_filelist: # pre_filelist.remove(f) ff = os.path.basename(f) if ff in filelist: filelist.remove(ff) for f in xmlfilelist: list = [] try: ctime = os.path.getctime(f) #print ctime if ctime in fdict.keys(): fdict[ctime].append(os.path.basename(f)) else: list.append(os.path.basename(f)) fdict[ctime] = list except: spinutil.logprint("Error --- get file creation time") pass ctimelist = fdict.keys() ctimelist.sort() logxmlfilelist = [] for ctime in ctimelist: for logfile in fdict[ctime]: # Track xmlfile name into the list. ## logxmlfilelist.append(logfile) # open the xml file and parse in to a DOM tree so we can use the XPath functions try: lf = os.path.join('logs', logfile) fp = open(lf,'r') dom = FromXmlStream(fp) fp.close() # Check 'status', if it is NOT 'Pass', then 'buildstatus' is set to 'Failed' status = strip(Evaluate('/buildlog/status/text()', dom.documentElement)[0].nodeValue) if string.lower(status) <> 'pass': buildstatus = 'Failed' builderror = 'Build Failed' # Or a tag's content in the XML file. # Track xmlfile name into the list, when it's format is succssfully parsed. logxmlfilelist.append(logfile) except: spinutil.logprint( "Error in format of XML log file: '%s'" % lf) os.remove(lf) # Remove this error format XML file pass #raise os.chdir(dir) # Insert each XML format log file content into 'spin.xml' # and remove each '.xml' log file after it is inserted. for logxmlfile in logxmlfilelist: lxf = logxmlfile # Add this specific log xml file into 'spin.xml' if sys.platform == "win32": logxmlfile = os.path.join('logs', logxmlfile) #changed this -DM 7/26 #logxmlfile = 'logs\\\\\\%s' % logxmlfile #print logxmlfile else: logxmlfile = os.path.join('logs', logxmlfile) #logxmlfile = 'logs//////%s' % logxmlfile if lxf == 'agent_script_log.xml': spinutil.addlog(logxmlfile, 'agentscriptloglist') else: spinutil.addlog(logxmlfile, 'buildloglist') #spinutil.logprint_local ("Insert log XML file %s ..." % logxmlfile, spinlogfile) # Remove the XML format log file after its content is inserted into the 'spin.xml' file. os.remove(logxmlfile) # Zip all free style log files, Then, PutBuildfiles() will responsible to upload # all build '.zip' and log '.zip' files to the local FTP repository. os.chdir(dir) for logfile in filelist: logfile = os.path.join('logs', logfile) #print logfile Compress(zipf,logfile) os.remove(logfile) # Copyt 'agent_script.log' into 'logs' directory, thus, intermedia agent_script.log could be seen #if os.path.isfile('agent_script.log'): # srcfile = 'agent_script.log' # dstfile = os.path.join('logs', 'agent_script.log') # Copy agent script log file into 'logs' directory # shutil.copy(srcfile, dstfile) # upload the build to ftp server spinutil.setstatus('Uploading Build and Log files') ChangeBuildName(projectname) try: projectname_version = string.strip(agent_scripts['projectname'])+ '_' + string.strip(agent_scripts['projectversion']) buildtypestr = string.strip(agent_scripts['buildtype']) if buildtypestr == 'X' or buildtypestr == 'x': buildtype = 'engineering' else: buildtype = 'official' destinationpath = "/%s/%s/%s/%s" %(string.strip(agent_scripts['productfamily']), projectname_version, buildtype, string.strip(agent_scripts['buildname'])) spinutil.logprint(destinationpath) st = spinutil.putbuildfiles(destinationpath) if st == 2: buildstatus = 'Failed' builderror = 'Build failed' except: buildstatus = 'Failed' builderror = 'Upload Build Files Failed' spinutil.logprint(builderror) pass # Remove 'agent_script.log' from 'logs' directory, otherwise, this log file would be compressed into log ZIP file. #dstfile = os.path.join('logs', 'agent_script.log') #if os.path.isfile(dstfile): # Remove agent script log file into 'logs' directory # os.remove(dstfile) # Upload 'agent_script.log' to FTP repository, thus, intermedia 'agent_script.log' could be seen destagentlog = '%s/logs/%s' % (destinationpath, agentlog) if os.path.isfile(agentlog): try: spinutil.putbuild(agentlog, destagentlog) except: #buildstatus = 'Failed' uperror = 'Upload Agent Script Log File Failed' spinutil.logprint(uperror) pass # clean up spinutil.setstatus('Cleaning up') spinutil.setbuildstatus(buildstatus, builderror) # These command would clean all files and directories under the directory # The clean function is commented out. 01/18/05. These dirs will be cleaned before # new job is initiated. #if spinutil.mswindows: # spinutil.run('rmdir /q /s logs') # spinutil.run('rmdir /q /s build') #else: # spinutil.run('rm -rf logs') # spinutil.run('rm -rf build') #spinutil.setstatus('Complete') # This stautus will be set in Agent('runscript.py') except: status = "Error --- Execute Agent Build Script ('build.py'). " spinutil.logprint(status) raise