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)
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()
#!/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 __init__(self, config_file): fp = open(config_file,'r') self.dom = FromXmlStream(fp) fp.close() self.config_file=config_file
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
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