def execute(self): self.customize(self.options) connection = None try: connection = Overthere.getConnection(CifsConnectionBuilder.CIFS_PROTOCOL, self.options) connection.setWorkingDirectory(connection.getFile(self.remotePath)) # upload the script and pass it to powershell targetFile = connection.getTempFile("uploaded-powershell-script", ".ps1") OverthereUtils.write(String(self.script).getBytes(), targetFile) targetFile.setExecutable(True) scriptCommand = CmdLine.build( "powershell", "-NoLogo", "-NonInteractive", "-InputFormat", "None", "-ExecutionPolicy", "Unrestricted", "-Command", targetFile.getPath(), ) return connection.execute(self.stdout, self.stderr, scriptCommand) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1
def getPayloadContent(self): format = self.__metadata.getField("dc_format") slash = self.__oid.rfind("/") pid = self.__oid[slash+1:] print " *** payload content, format: %s, pid: %s *** " % (format, pid) contentStr = "" if format.startswith("text"): contentStr = "<pre>" payload = self.__storage.getPayload(self.__oid, pid) str = StringWriter() IOUtils.copy(payload.getInputStream(), str) contentStr += str.toString() contentStr += "</pre>" elif format.find("vnd.ms-")>-1 or format.find("vnd.oasis.opendocument.")>-1: #get the html version if exist.... pid = pid[:pid.find(".")] + ".htm" payload = self.__storage.getPayload(self.__oid, pid) saxReader = SAXReader() document = saxReader.read(payload.getInputStream()) slideNode = document.selectSingleNode("//div[@class='body']") #linkNodes = slideNode.selectNodes("//img") #contentStr = slideNode.asXML(); # encode character entities correctly out = ByteArrayOutputStream() format = OutputFormat.createPrettyPrint() format.setSuppressDeclaration(True) writer = XMLWriter(out, format) writer.write(slideNode) writer.close() contentStr = out.toString("UTF-8") return contentStr
def register_script(self): """ Registers a pig scripts with its variables substituted. raises: IOException If a temp file containing the pig script could not be created. raises: ParseException The pig script could not have all its variables substituted. todo: Refactor this processes that result in calling this method. This method gets called twice for a single assert as every method that needs the data assumes no one else has called it (even methods that call other methods that call it (assertOutput() calls get_alias() which both call this method). """ pigIStream = BufferedReader(StringReader(self.orig_pig_code)) pigOStream = StringWriter() ps = ParameterSubstitutionPreprocessor(50) # Where does 50 come from? ps.genSubstitutedFile(pigIStream, pigOStream, self.args, self.arg_files) substitutedPig = pigOStream.toString() f = File.createTempFile("tmp", "pigunit") pw = PrintWriter(f) pw.println(substitutedPig) pw.close() pigSubstitutedFile = f.getCanonicalPath() self._temp_pig_script = pigSubstitutedFile self.pig.registerScript(pigSubstitutedFile, self.alias_overrides)
def getPayloadContent(self): mimeType = self.__mimeType print " * single.py: payload content mimeType=%s" % mimeType contentStr = "" if mimeType.startswith("text/"): if mimeType == "text/html": contentStr = '<iframe class="iframe-preview" src="%s/%s/download/%s"></iframe>' % ( contextPath, portalId, self.__oid, ) else: pid = self.__oid[self.__oid.rfind("/") + 1 :] payload = self.__storage.getPayload(self.__oid, pid) print " * single.py: pid=%s payload=%s" % (pid, payload) if payload is not None: sw = StringWriter() sw.write("<pre>") IOUtils.copy(payload.getInputStream(), sw) sw.write("</pre>") sw.flush() contentStr = sw.toString() elif ( mimeType == "application/pdf" or mimeType.find("vnd.ms") > -1 or mimeType.find("vnd.oasis.opendocument.") > -1 ): # get the html version if exist... pid = os.path.splitext(self.__pid)[0] + ".htm" print " * single.py: pid=%s" % pid # contentStr = '<iframe class="iframe-preview" src="%s/%s/download/%s/%s"></iframe>' % \ # (contextPath, portalId, self.__oid, pid) payload = self.__storage.getPayload(self.__oid, pid) saxReader = SAXReader(Boolean.parseBoolean("false")) try: document = saxReader.read(payload.getInputStream()) slideNode = document.selectSingleNode("//*[local-name()='body']") # linkNodes = slideNode.selectNodes("//img") # contentStr = slideNode.asXML(); # encode character entities correctly slideNode.setName("div") out = ByteArrayOutputStream() format = OutputFormat.createPrettyPrint() format.setSuppressDeclaration(True) format.setExpandEmptyElements(True) writer = XMLWriter(out, format) writer.write(slideNode) writer.close() contentStr = out.toString("UTF-8") except: traceback.print_exc() contentStr = '<p class="error">No preview available</p>' elif mimeType.startswith("image/"): src = "%s/%s" % (self.__oid, self.__pid) contentStr = ( '<a class="image" href="%(src)s" style="max-width:98%%">' '<img src="%(src)s" style="max-width:100%%" /></a>' % {"src": self.__pid} ) return contentStr
def exec_script(self, engine_name, script, post_script="", attr={}): r = "" l2sem = L2ScriptEngineManager.getInstance() context = SimpleScriptContext() sw = StringWriter() pw = PrintWriter(sw, True) context.setAttribute("out_writer", pw, ScriptContext.ENGINE_SCOPE) for k in attr: context.setAttribute(str(k), attr[k], ScriptContext.ENGINE_SCOPE) context.setWriter(pw) context.setErrorWriter(pw) try: l2sem.eval(engine_name, script, context) r += sw.toString() except ScriptException, e: r += sw.toString() r += e.getMessage()
def __toXMLString(self, document): domSource = DOMSource(document) writer = StringWriter() result = StreamResult(writer) tf = TransformerFactory.newInstance() transformer = tf.newTransformer() transformer.transform(domSource, result) return writer.toString()
def __init__(self, description): self.protocol = current_protocol self.test = current_test self.description = description sw = StringWriter() Exception('Stack trace').printStackTrace(PrintWriter(sw)) self.stack_trace = sw.toString() self.tb = '\n'.join( (line.strip() for line in traceback.format_stack()))
def handleStructs(): sio = StringWriter() sio.write("a") writer = ghidra.program.model.data.DataTypeWriter(dataTypeManager, sio) mon = ghidra.util.task.DummyCancellableTaskMonitor() writer.write( dataTypeManager.getCategory( ghidra.program.model.data.CategoryPath("/MH")), mon) json_dict["dataTypes"] = sio.toString()
def getResourceContent(self, plugin, field): resource = self.getMetadata(plugin, field) stream = self.pageService.getResource(resource) if stream: writer = StringWriter() IOUtils.copy(stream, writer, "UTF-8") html = writer.toString() print " *** html:", html return html return "<em>'%s' not found!</em>" % (field)
def getPayloadContent(self): mimeType = self.__mimeType print " * detail.py: payload content mimeType=%s" % mimeType contentStr = "" if mimeType == "application/octet-stream": dcFormat = self.__json.get("response/docs/dc_format") if dcFormat is not None: dcFormat = dcFormat[1:-1] print dcFormat, mimeType if dcFormat != mimeType: return "<div><em>(File not found)</em></div>" else: return "<div><em>(Binary file)</em></div>" elif mimeType.startswith("text/"): if mimeType == "text/html": contentStr = '<iframe class="iframe-preview" src="%s/%s/download/%s"></iframe>' % \ (contextPath, portalId, self.__oid) else: pid = self.__oid[self.__oid.rfind("/")+1:] payload = self.__storage.getPayload(self.__oid, pid) #print " * detail.py: pid=%s payload=%s" % (pid, payload) if payload is not None: sw = StringWriter() sw.write("<pre>") IOUtils.copy(payload.getInputStream(), sw) sw.write("</pre>") sw.flush() contentStr = sw.toString() elif mimeType == "application/pdf" or mimeType.find("vnd.ms")>-1 or mimeType.find("vnd.oasis.opendocument.")>-1: # get the html version if exist... pid = os.path.splitext(self.__pid)[0] + ".htm" print " * detail.py: pid=%s" % pid #contentStr = '<iframe class="iframe-preview" src="%s/%s/download/%s/%s"></iframe>' % \ # (contextPath, portalId, self.__oid, pid) payload = self.__storage.getPayload(self.__oid, pid) saxReader = SAXReader(Boolean.parseBoolean("false")) try: document = saxReader.read(payload.getInputStream()) slideNode = document.selectSingleNode("//*[local-name()='body']") #linkNodes = slideNode.selectNodes("//img") #contentStr = slideNode.asXML(); # encode character entities correctly slideNode.setName("div") out = ByteArrayOutputStream() format = OutputFormat.createPrettyPrint() format.setSuppressDeclaration(True) format.setExpandEmptyElements(True) writer = XMLWriter(out, format) writer.write(slideNode) writer.close() contentStr = out.toString("UTF-8") except: traceback.print_exc() contentStr = "<p class=\"error\">No preview available</p>" return contentStr
def parseOutput(self, person): template = self.findBy("Template", "name", "'user-information'") ve = VelocityEngine() ve.init() context = VelocityContext() context.put("person", person) writer = StringWriter() ve.evaluate(context, writer, template.getName(), unicode(template.getSource())) evaluatedTemplate = writer.toString() return evaluatedTemplate
def render(context, logString, template): sWriter = StringWriter() fIS = None try: sWriter.append(MSGTEMPLATE_PREFIX) fIS = FileInputStream(File(MSGTEMPLATE_DIR, template)) Velocity.evaluate(context, sWriter, logString, fIS) sWriter.append(MSGTEMPLATE_SUFFIX) return sWriter.toString() finally: if fIS: fIS.close()
def _get_details(self): # OOME.printStackTrace seems to throw NullPointerException if self._is_out_of_memory_error(self._exc_type): return '' output = StringWriter() self._exc_value.printStackTrace(PrintWriter(output)) details = '\n'.join(line for line in output.toString().splitlines() if not self._is_ignored_stack_trace_line(line)) msg = unic(self._exc_value.getMessage() or '') if msg: details = details.replace(msg, '', 1) return details
def getPayloadContent(self): mimeType = self.__mimeType print " * single.py: payload content mimeType=%s" % mimeType contentStr = "" if mimeType.startswith("text/"): if mimeType == "text/html": contentStr = '<iframe class="iframe-preview" src="%s/download/%s"></iframe>' % \ (portalPath, self.__oid) else: pid = self.__oid[self.__oid.rfind("/") + 1:] payload = self.__storage.getPayload(self.__oid, pid) print " * single.py: pid=%s payload=%s" % (pid, payload) if payload is not None: sw = StringWriter() sw.write("<pre>") IOUtils.copy(payload.getInputStream(), sw) sw.write("</pre>") sw.flush() contentStr = sw.toString() elif mimeType == "application/pdf" or mimeType.find( "vnd.ms") > -1 or mimeType.find( "vnd.oasis.opendocument.") > -1: # get the html version if exist... pid = os.path.splitext(self.__pid)[0] + ".htm" print " * single.py: pid=%s" % pid #contentStr = '<iframe class="iframe-preview" src="%s/download/%s/%s"></iframe>' % \ # (portalPath, self.__oid, pid) payload = self.__storage.getPayload(self.__oid, pid) saxReader = SAXReader(Boolean.parseBoolean("false")) try: document = saxReader.read(payload.getInputStream()) slideNode = document.selectSingleNode( "//*[local-name()='body']") #linkNodes = slideNode.selectNodes("//img") #contentStr = slideNode.asXML(); # encode character entities correctly slideNode.setName("div") out = ByteArrayOutputStream() format = OutputFormat.createPrettyPrint() format.setSuppressDeclaration(True) format.setExpandEmptyElements(True) writer = XMLWriter(out, format) writer.write(slideNode) writer.close() contentStr = out.toString("UTF-8") except: traceback.print_exc() contentStr = "<p class=\"error\">No preview available</p>" elif mimeType.startswith("image/"): src = "%s/%s" % (self.__oid, self.__pid) contentStr = '<a class="image" href="%(src)s" style="max-width:98%%">' \ '<img src="%(src)s" style="max-width:100%%" /></a>' % { "src": self.__pid } return contentStr
def execute(self): connection = None try: connection = LocalConnection.getLocalConnection() exitCode = connection.execute(self.stdout, self.stderr, self.cmdLine) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1
def getAboutPage(self, plugin, type): if type is None or plugin is None: return "<em>'plugin/%s/%s/about.html' not found!</em>" % (type, plugin) pid = plugin.replace("-", "_") resource = "plugin/%s/%s/about.html" % (type, pid) stream = self.pageService.getResource(resource) if stream: writer = StringWriter() IOUtils.copy(stream, writer, "UTF-8") html = writer.toString() return html return "<em>'plugin/%s/%s/about.html' not found!</em>" % (type, pid)
def getAboutPage(self, plugin, type): if type is None or plugin is None: return "<em>This plugin has provided no information about itself.</em>" pid = plugin.replace("-", "_") resource = "plugin/%s/%s/about.html" % (type, pid) stream = self.pageService.getResource(resource) if stream: writer = StringWriter() IOUtils.copy(stream, writer, "UTF-8") html = writer.toString() return html return "<em>This plugin has provided no information about itself.</em>"
def generateTemplate(self, calculator): self._template = self.findBy("Template", "name", "'possession-account-state'") self._context = VelocityContext() self._context.put('stateRent', calculator.calculateCurrentRentState()) self._context.put('stateRF', calculator.calculateCurrentRFState()) self._context.put('chargingRent', calculator.calculateRentCharging()) self._context.put('chargingRT', calculator.calculateRFCharging()) writer = StringWriter() self._ve = VelocityEngine() self._ve.init() self._ve.evaluate(self._context, writer, self._template.getName(), unicode(self._template.getSource())) evaluatedTemplate = writer.toString() self._svars.put('output', evaluatedTemplate)
def _get_java_details(exc_value): # OOME.printStackTrace seems to throw NullPointerException if isinstance(exc_value, OutOfMemoryError): return '' output = StringWriter() exc_value.printStackTrace(PrintWriter(output)) lines = [ line for line in output.toString().splitlines() if line and not _is_ignored_stacktrace_line(line) ] details = '\n'.join(lines) msg = unic(exc_value.getMessage() or '') if msg: details = details.replace(msg, '', 1) return details
def test_logger(self): # Setup writer stringWriter = StringWriter() pyFileWriter = PyFileWriter(stringWriter) oldWriter = sys.stdout sys.stdout = pyFileWriter # Run logger.log('123') output = stringWriter.toString() sys.stdout = oldWriter # Assert self.assertTrue(output.index('123') > 0)
def execute(self): self.customize(self.options) connection = None try: connection = Overthere.getConnection(CifsConnectionBuilder.CIFS_PROTOCOL, self.options) connection.setWorkingDirectory(connection.getFile(self.TestPath)) # upload the ResultName and pass it to csResultName.exe ResultNameCommand = CmdLine.build(WlrunExe, "-Run", "-TestPath", TestPath, "-ResultName", ResultName) return connection.execute(self.stdout, self.stderr, ResultNameCommand) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1
def evaluate(self, template): ve = VelocityEngine() ve.init() context = VelocityContext() context.put('_formatter', SimpleDateFormat("dd-MM-yyyy")) self._logger.info('Date formatter stored as _formatter') self._logger.info('Template contains %d variables' % len(template.getTemplateVariableCollection())) for var in template.getTemplateVariableCollection(): self._logger.info('Preparing variable %s' % var.getName()) context.put(var.getName(), self.loadData(var.getData())) self._logger.info('Variable %s stored' % var.getName()) writer = StringWriter() ve.evaluate(context, writer, template.getName(), unicode(template.getSource())) evaluatedTemplate = writer.toString() return evaluatedTemplate
def compileTemplate(self, template, restrictions): ve = VelocityEngine() ve.init() context = VelocityContext() context.put("restrictions", self._output) for restriction in restrictions: restriction.calculate() self._output[restriction.getTemplateName()] = [ restriction.getTemplateName(), restriction.getResult(), restriction.getMessage() ] writer = StringWriter() ve.evaluate(context, writer, template.getName(), unicode(template.getSource())) evaluatedTemplate = writer.toString() return evaluatedTemplate
def writeDom(doc): try: # set up a transformer transfac = TransformerFactory.newInstance() trans = transfac.newTransformer() trans.setOutputProperty(OutputKeys.ENCODING, "utf-8") trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no") trans.setOutputProperty(OutputKeys.INDENT, "yes") trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2") sw1 = StringWriter() result = StreamResult(sw1) source = DOMSource(doc) trans.transform(source, result) xmlString = sw1.toString() print xmlString except Exception, e: print e
def execute(self): self.customize(self.options) connection = None try: connection = Overthere.getConnection(SshConnectionBuilder.CONNECTION_TYPE, self.options) # upload the script and pass it to python exeFile = connection.getTempFile('f5_disable', '.py') OverthereUtils.write(String(self.script).getBytes(), targetFile) exeFile.setExecutable(True) # run cscript in batch mode scriptCommand = CmdLine.build( '/usr/bin/python', exeFile.getPath() ) return connection.execute(self.stdout, self.stderr, scriptCommand) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1
def __getPayloadContent(self, oid, pid): print " * combined.py: oid='%s' pid='%s'" % (oid, pid) payload = self.__storage.getPayload(oid, pid) if payload is None: return "<div>Error: No content for '%s'</div>" % oid mimeType = payload.contentType contentStr = "" if mimeType.startswith("text/"): if mimeType == "text/html": contentStr = '<iframe class="iframe-preview" src="%s/download/%s"></iframe>' % \ (portalPath, oid) else: sw = StringWriter() sw.write("<pre>") IOUtils.copy(payload.getInputStream(), sw) sw.write("</pre>") sw.flush() contentStr = sw.toString() elif mimeType == "application/pdf" or mimeType.find("vnd.ms")>-1 or mimeType.find("vnd.oasis.opendocument.")>-1: # get the html version if exist... pid = os.path.splitext(pid)[0] + ".htm" print " * combined.py: pid=%s" % pid payload = self.__storage.getPayload(oid, pid) saxReader = SAXReader(False) try: document = saxReader.read(payload.getInputStream()) slideNode = document.selectSingleNode("//*[local-name()='body']") slideNode.setName("div") out = ByteArrayOutputStream() format = OutputFormat.createPrettyPrint() format.setSuppressDeclaration(True) format.setExpandEmptyElements(True) writer = XMLWriter(out, format) writer.write(slideNode) writer.close() contentStr = out.toString("UTF-8") except: traceback.print_exc() contentStr = "<p class=\"error\">No preview available</p>" elif mimeType.startswith("image/"): src = "%s/%s" % (oid, pid) contentStr = '<a class="image" href="%(src)s" style="max-width:98%%">' \ '<img src="%(src)s" style="max-width:100%%" /></a>' % { "src": pid } return contentStr
def getPayloadContent(self): mimeType = self.__mimeType print " * detail.py: payload content mimeType=%s" % mimeType contentStr = "" if mimeType.startswith("text/"): if mimeType == "text/html": contentStr = '<iframe class="iframe-preview" src="%s/%s/download/%s"></iframe>' % \ (contextPath, portalId, self.__oid) else: pid = self.__oid[self.__oid.rfind("/")+1:] payload = self.__storage.getPayload(self.__oid, pid) print " * detail.py: pid=%s payload=%s" % (pid, payload) if payload is not None: sw = StringWriter() sw.write("<pre>") IOUtils.copy(payload.getInputStream(), sw) sw.write("</pre>") sw.flush() contentStr = sw.toString() elif mimeType == "application/pdf" or mimeType.find("vnd")>-1 or mimeType.find("vnd.oasis.opendocument.")>-1: # get the html version if exist... pid = os.path.splitext(self.__pid)[0] + ".htm" print " * detail.py: pid=%s" % pid #contentStr = '<iframe class="iframe-preview" src="%s/%s/download/%s/%s"></iframe>' % \ # (contextPath, portalId, self.__oid, pid) payload = self.__storage.getPayload(self.__oid, pid) saxReader = SAXReader(Boolean.parseBoolean("false")) try: document = saxReader.read(payload.getInputStream()) except: traceback.print_exc() #slideNode = document.selectSingleNode("//div[@class='body']") slideNode = document.selectSingleNode("//*[local-name()='body']") #linkNodes = slideNode.selectNodes("//img") #contentStr = slideNode.asXML(); # encode character entities correctly out = ByteArrayOutputStream() format = OutputFormat.createPrettyPrint() format.setSuppressDeclaration(True) writer = XMLWriter(out, format) writer.write(slideNode) writer.close() contentStr = out.toString("UTF-8") return contentStr
def execute(self): self.customize(self.options) connection = None try: connection = Overthere.getConnection(CifsConnectionBuilder.CIFS_PROTOCOL, self.options) connection.setWorkingDirectory(connection.getFile(self.remotePath)) # upload the script and pass it to cscript.exe targetFile = connection.getTempFile('uploaded-script', '.vbs') OverthereUtils.write(String(self.script).getBytes(), targetFile) targetFile.setExecutable(True) # run cscript in batch mode scriptCommand = CmdLine.build(cscriptExecutable, '//B', '//nologo', targetFile.getPath()) return connection.execute(self.stdout, self.stderr, scriptCommand) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1
def __createPackage(self, outputFile=None): title = self.__manifest.getString(None, "title") manifest = self.__createManifest() context = JAXBContext.newInstance("com.googlecode.fascinator.ims") m = context.createMarshaller() m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, True) writer = StringWriter() jaxbElem = ObjectFactory.createManifest(ObjectFactory(), manifest) m.marshal(jaxbElem, writer) writer.close() if outputFile is not None: print "writing to %s..." % outputFile out = FileOutputStream(outputFile) else: print "writing to http output stream..." filename = urllib.quote(title.replace(" ", "_")) response.setHeader("Content-Disposition", "attachment; filename=%s.zip" % filename) out = response.getOutputStream("application/zip") zipOut = ZipOutputStream(out) zipOut.putNextEntry(ZipEntry("imsmanifest.xml")) IOUtils.write(writer.toString(), zipOut) zipOut.closeEntry() oidList = self.__manifest.search("id") for oid in oidList: obj = Services.getStorage().getObject(oid) for pid in obj.getPayloadIdList(): payload = obj.getPayload(pid) if not PayloadType.Annotation.equals(payload.getType()): zipOut.putNextEntry( ZipEntry("resources/%s/%s" % (oid, pid))) IOUtils.copy(payload.open(), zipOut) payload.close() zipOut.closeEntry() obj.close() zipOut.close() out.close()
def execute(self): self.customize(self.options) connection = None try: connection = Overthere.getConnection( CifsConnectionBuilder.CIFS_PROTOCOL, self.options) connection.setWorkingDirectory(connection.getFile(self.remotePath)) # upload the script and pass it to cscript.exe targetFile = connection.getTempFile('uploaded-script', '.vbs') OverthereUtils.write(String(self.script).getBytes(), targetFile) targetFile.setExecutable(True) # run cscript in batch mode scriptCommand = CmdLine.build(cscriptExecutable, '//B', '//nologo', targetFile.getPath()) return connection.execute(self.stdout, self.stderr, scriptCommand) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1
def __createPackage(self, outputFile=None): title = self.__manifest.getString(None, "title") manifest = self.__createManifest() context = JAXBContext.newInstance("au.edu.usq.fascinator.ims") m = context.createMarshaller() m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, True) writer = StringWriter() jaxbElem = ObjectFactory.createManifest(ObjectFactory(), manifest) m.marshal(jaxbElem, writer) writer.close() if outputFile is not None: print "writing to %s..." % outputFile out = FileOutputStream(outputFile) else: print "writing to http output stream..." filename = urllib.quote(title.replace(" ", "_")) response.setHeader("Content-Disposition", "attachment; filename=%s.zip" % filename) out = response.getOutputStream("application/zip") zipOut = ZipOutputStream(out) zipOut.putNextEntry(ZipEntry("imsmanifest.xml")) IOUtils.write(writer.toString(), zipOut) zipOut.closeEntry() oidList = self.__manifest.search("id") for oid in oidList: obj = Services.getStorage().getObject(oid) for pid in obj.getPayloadIdList(): payload = obj.getPayload(pid) if not PayloadType.Annotation.equals(payload.getType()): zipOut.putNextEntry(ZipEntry("resources/%s/%s" % (oid, pid))) IOUtils.copy(payload.open(), zipOut) payload.close() zipOut.closeEntry() obj.close() zipOut.close() out.close()
def execute(self): self.customize(self.options) connection = None try: connection = Overthere.getConnection(CifsConnectionBuilder.CIFS_PROTOCOL, self.options) connection.setWorkingDirectory(connection.getFile(self.remotePath)) # upload the script and pass it to cscript.exe targetFile = connection.getTempFile('parameters', '.txt') OverthereUtils.write(String(self.script).getBytes(), targetFile) targetFile.setExecutable(True) exeFile = connection.getTempFile('HpToolsLauncher', '.exe') sysloader = ClassLoader.getSystemClassLoader() OverthereUtils.write(sysloader.getResourceAsStream("HpTools/HpToolsLauncher.exe"), exeFile) exeFile.setExecutable(True) # run cscript in batch mode scriptCommand = CmdLine.build(exeFile.getPath(), '-paramfile', targetFile.getPath()) return connection.execute(self.stdout, self.stderr, scriptCommand) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1
def execute( self ): connection = None try: connection = LocalConnection.getLocalConnection() scriptFile = connection.getTempFile('xlrScript', '.py') OverthereUtils.write( String( self.script ).getBytes(), scriptFile ) scriptFile.setExecutable(True) self.cmdLine.addArgument( '-source' ) self.cmdLine.addArgument( scriptFile.getPath() ) if ( len( self.options ) > 1 ): self.cmdLine.addArgument( '--' ) optionsList = self.options.split(' ') for opt in optionsList: self.cmdLine.addArgument( opt ) # End for # End if exitCode = connection.execute( self.stdout, self.stderr, self.cmdLine ) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1
def executeFile( self ): connection = None try: connection = LocalConnection.getLocalConnection() scriptFile = self.script print "using script %s" % ( scriptFile ) #scriptFile.setExecutable(True) self.cmdLine.addArgument( '-source' ) self.cmdLine.addArgument( scriptFile ) if ( len( self.options ) > 1 ): self.cmdLine.addArgument( '--' ) optionsList = self.options.split(' ') for opt in optionsList: self.cmdLine.addArgument( opt ) # End for # End if exitCode = connection.execute( self.stdout, self.stderr, self.cmdLine ) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1
def __createPackage(self, outputFile=None): manifest = self.__createManifest() context = JAXBContext.newInstance("au.edu.usq.fascinator.ims") m = context.createMarshaller() m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, True) writer = StringWriter() jaxbElem = ObjectFactory.createManifest(ObjectFactory(), manifest) m.marshal(jaxbElem, writer); writer.close() if outputFile is not None: print " * imscp.py: writing to %s..." % outputFile out = FileOutputStream(outputFile) else: print " * imscp.py: writing to http output stream..." response.setHeader("Content-Disposition", "attachment; filename=%s.zip" % self.__portal.getName()) out = response.getOutputStream("application/zip") zipOut = ZipOutputStream(out) zipOut.putNextEntry(ZipEntry("imsmanifest.xml")) IOUtils.write(writer.toString(), zipOut) zipOut.closeEntry() for key in self.__portalManifest.keySet(): item = self.__portalManifest.get(key) oid = item.get("id") obj = Services.getStorage().getObject(oid) for payload in obj.getPayloadList(): pid = payload.getId() if pid != "SOF-META": zipOut.putNextEntry(ZipEntry("%s/%s" % (key[5:], pid))) IOUtils.copy(payload.getInputStream(), zipOut) zipOut.closeEntry() zipOut.close() out.close()
def execute(self): connection = None try: connection = LocalConnection.getLocalConnection() scriptFile = connection.getTempFile('xlrScript', '.py') OverthereUtils.write(String(self.script).getBytes(), scriptFile) scriptFile.setExecutable(True) self.cmdLine.addArgument('-source') self.cmdLine.addArgument(scriptFile.getPath()) if len(self.options) > 1: self.cmdLine.addArgument('--') optionsList = self.options.split(' ') for opt in optionsList: self.cmdLine.addArgument(opt) # End for # End if exitCode = connection.execute(self.stdout, self.stderr, self.cmdLine) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1
def executeFile(self): connection = None try: connection = LocalConnection.getLocalConnection() scriptFile = self.script print "using script %s" % (scriptFile) #scriptFile.setExecutable(True) self.cmdLine.addArgument('-source') self.cmdLine.addArgument(scriptFile) if (len(self.options) > 1): self.cmdLine.addArgument('--') optionsList = self.options.split(' ') for opt in optionsList: self.cmdLine.addArgument(opt) # End for # End if exitCode = connection.execute(self.stdout, self.stderr, self.cmdLine) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1
connection = self.getConnection() # upload the script and pass it to python exeFile = connection.getTempFile('script', '.py') OverthereUtils.write(String(self.script).getBytes(), exeFile) exeFile.setExecutable(True) scriptCommand = CmdLine.build('/usr/bin/python', exeFile.getPath()) return connection.execute(self.stdout, self.stderr, scriptCommand) except AttributeError, e: self.logger.error(str(e)) except NameError, e: self.logger.error(str(e)) except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) self.stderr.handleLine(stacktrace.toString()) return 1 finally: if connection is not None: connection.close() def getConnection(self): if (self.connectionType): return Overthere.getConnection("ssh", self.options) else: return LocalConnection.getLocalConnection() def getStdout(self): return self.stdout.getOutput() def getStdoutLines(self):
def __getPayloadAsString(self, payload): sw = StringWriter() IOUtils.copy(payload.getInputStream(), sw) sw.flush() return sw.toString()
# End if URLSource.request('GET', uri, {}, {}) response = URLSource.getresponse() print response.status, response.reason script = response.read() cliScript = localCliScript(cli['cliHome'], cli['xldHost'], cli['xldPort'], cli['xldContext'], cli['xldProxyHost'], cli['xldProxyPort'], cli['xldSocketTimeout'], cli['xldUserName'], cli['xldPassword'], script, cli['cliExecutable'], options) exitCode = cliScript.execute() output = cliScript.getStdout() err = cliScript.getStderr() except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) err = stacktrace.toString() exitCode = 1 finally: URLSource.close() # End try if (exitCode == 0 ): print output else: print print "### Exit code " print exitCode print print "### Output:" print output
def generateReport(self): writer = StringWriter() self._ve.evaluate(self._context, writer, self._template.getName(), unicode(self._template.getSource())) evaluatedTemplate = writer.toString() return evaluatedTemplate
print "No JMS Servers are configured within this domain."; v_didyoufindit = ''; # This piece of javascript enabled the Tabs to work print "Finished scanning JMS info" # CLOSE output file, program end model.add("JMS Resource Information", jmsResourceArray) "Writing JSON to file" modelobj = model.build() ## now to print stWriter = StringWriter(); jsonWriter = Json.createWriter(stWriter); jsonWriter.writeObject(modelobj); jsonWriter.close(); jsonData = stWriter.toString(); print >>f, jsonData f.close() closeDomain(); print "Finished Scan report - output file completed" exit();
class UploadXMLHandler(DefaultHandler2): """Класс SAX-парсера, производящий разбор xml и вставку данных в таблицу""" parentTag = None currentCell = None flag = 0 currentString = u'' currentEncoding = u"utf8" def __init__(self, tableInstance, action): self.tableInstance = tableInstance # возможность настраивать, нужно ли обновлять записи и вставлять новые def actionUI(ins): if not ins.tryInsert(): ins.update() def actionU(ins): ins.tryUpdate() def actionI(ins): ins.tryInsert() # определяем, какую функцию нам нужно использовать self.funcAction = locals()['action%s' % action.upper()] def startElement(self, namespaceURI, lname, qname, attrs): if self.parentTag == 'field': if self.flag == 0: self.stringWriter = StringWriter() self.xmlWriter = XMLOutputFactory.newInstance( ).createXMLStreamWriter(self.stringWriter, "UTF-8") self.flag += 1 self.xmlWriter.writeStartElement(qname) for i in range(0, attrs.getLength()): self.xmlWriter.writeAttribute(attrs.getQName(i), attrs.getValue(i)) elif qname == 'table' and self.flag == 0: if self.parentTag is not None: raise CelestaException(u"Неверный формат файла") self.parentTag = qname if not attrs.getValue('name'): raise CelestaException( u"Атрибут 'name' отсутствует в теге 'table'") elif attrs.getValue('name') != self.tableInstance.meta().getName(): raise CelestaException( u"Имя таблицы %s не соответствует значению атрибута 'name'" % self.tableInstance.meta().getName()) elif qname == 'row' and self.flag == 0: if self.parentTag != 'table': raise CelestaException(u"Неверный формат файла") self.parentTag = qname elif qname == 'field' and self.flag == 0: if self.parentTag != 'row': raise CelestaException(u"Неверный формат файла") self.currentEncoding = attrs.getValue('encoding') or u"utf8" self.currentCell = attrs.getValue('name') self.parentTag = qname self.currentString = u'' else: raise CelestaException(u"Неверный формат файла") def endElement(self, uri, lname, qname): if qname == 'table' and self.flag == 0: self.parentTag = None elif qname == 'row' and self.flag == 0: # обновляем или вставляем записи self.funcAction(self.tableInstance) self.tableInstance.clear() self.parentTag = 'table' elif qname == 'field' and self.flag == 0: # Вставка данных в поле таблицы, отдельно рассмотрен случай если данные в формате XML if hasattr(self, 'stringWriter') and self.stringWriter: self.xmlWriter.close() # Вставка данных в поле типа blob if self.tableInstance.meta().columns[ self.currentCell].getCelestaType() == 'BLOB': getattr(self.tableInstance, "calc%s" % self.currentCell)() blobField = self.tableInstance.__dict__[ self.currentCell].getOutStream() blobField.write(self.stringWriter.strip()) else: self.tableInstance.__setattr__( self.currentCell, self.stringWriter.toString()) self.stringWriter.flush() self.stringWriter = None self.xmlWriter = None else: # проверка на None if self.currentString.strip() != 'None': self.currentString = self.currentString.strip() # Вставка данных в поле типа blob if self.tableInstance.meta().columns[ self.currentCell].getCelestaType() == 'BLOB': getattr(self.tableInstance, "calc%s" % self.currentCell)() blobField = self.tableInstance.__dict__[ self.currentCell].getOutStream() if self.currentEncoding == u"utf8": blobField.write(self.currentString) elif self.currentEncoding == u"base64": blobField.write( base64.b64decode(self.currentString)) else: raise CelestaException(u"Неверная кодировка") elif self.tableInstance.meta().columns[ self.currentCell].getCelestaType() == 'DATETIME': sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") self.tableInstance.__setattr__( self.currentCell, sdf.parse(self.currentString)) elif self.tableInstance.meta().columns[ self.currentCell].getCelestaType() == 'BIT': self.tableInstance.__setattr__( self.currentCell, self.currentString.lower() == "true") else: if self.currentCell == 'prefix': pass self.tableInstance.__setattr__(self.currentCell, self.currentString) self.parentTag = 'row' self.currentCell = None self.currentString = None elif self.parentTag == 'field': self.xmlWriter.writeEndElement() self.flag -= 1 def characters(self, ch, start, length): if self.parentTag == 'field' and self.flag > 0: self.xmlWriter.writeCharacters(ch, start, length) elif self.currentCell: self.currentString += unicode(String(ch[start:start + length])) def comment(self, ch, start, length): if self.parentTag == 'field' and self.flag > 0: self.xmlWriter.writeComment(ch.tostring()[start:start + length]) def startPrefixMapping(self, prefix, uri): if self.parentTag == 'field' and self.flag > 0: if prefix == "": self.xmlWriter.setDefaultNamespace(uri) else: self.xmlWriter.setPrefix(prefix, uri) def processingInstruction(self, target, data): if self.parentTag == 'field' and self.flag > 0: self.xmlWriter.writeProcessingInstruction(target, data) def skippedEntity(self, name): if self.parentTag == 'field' and self.flag > 0: self.xmlWriter.writeEntityRef(name)
source.setAttribute("cpu-frequency-in-hz", df.format(mon.cpuFrequencyInHz())) source.setAttribute("current-pid", df.format(mon.currentPid())) source.setAttribute("num-cpus", df.format(mon.numCpus())) source.setAttribute("os-name", mon.osName()) source.setAttribute("uptime-in-seconds", df.format(mon.uptimeInSeconds())) cpu = mon.cpuTimes() source.setAttribute("idle-millis", df.format(cpu.getIdleMillis())) source.setAttribute("system-millis", df.format(cpu.getSystemMillis())) source.setAttribute("total-millis", df.format(cpu.getTotalMillis())) source.setAttribute("user-millis", df.format(cpu.getUserMillis())) props = doc.createElement("properties") source.appendChild(props) sw = StringWriter() System.getProperties().store(sw, None) props.setTextContent(sw.toString()) req_map = dict( [ [entry.getKey(), entry.getValue()] for entry in inv.getMonad() .getServletConfig() .getServletContext() .getAttribute("net.sf.chellow.request_map") .entrySet() ] ) for entry in Thread.getAllStackTraces().entrySet(): thread_element = doc.createElement("thread") source.appendChild(thread_element)
print(str(round(t1 - t0, 3)) + " seconds") vertex_increment += vertex_count #meshData_str = meshData.toString() #meshData_inc_str = meshData.toString() # Save .obj file full_objfilename = filepath + objFileName objfile = open(full_objfilename, "w") objfile.write(meshData_str) objfile.close() # Save .mtl file full_mtlfilename = filepath + materialFileName mtlfile = open(full_mtlfilename, "w") mtlfile.write(materialData.toString()) mtlfile.close() # Append files collecing all objects objfile_all.write(meshData_inc_str) mtlfile_all.write(materialData.toString()) # Close files objfile_all.close() mtlfile_all.close() print("done writing files!") # Display the mesh in Meshlab # note: sys.platform = "java" here, cannot extract OS automatically if open_in_meshlab == "yes" or open_in_OBJ_Viewer == "yes":
stdout = CapturingOverthereExecutionOutputHandler.capturingHandler() stderr = CapturingOverthereExecutionOutputHandler.capturingHandler() try: connection = LocalConnection.getLocalConnection() targetScript = connection.getTempFile('oc-script', '.bat') OverthereUtils.write( String(script).getBytes(), targetScript) targetScript.setExecutable(True) cmd = CmdLine.build( targetScript.getPath() ) connection.execute( stdout, stderr, cmd ) except Exception, e: stacktrace = StringWriter() writer = PrintWriter( stacktrace, True ) e.printStackTrace(writer) stderr.hadleLine(stacktrace.toString()) # set variables output = stdout.getOutput() error = stderr.getOutput() if len(output) > 0: print "```" print output print "```" else: print "----" print "#### Output:" print "```" print output
print "No JMS Servers are configured within this domain." v_didyoufindit = '' # This piece of javascript enabled the Tabs to work print "Finished scanning JMS info" # CLOSE output file, program end model.add("JMS Resource Information", jmsResourceArray) "Writing JSON to file" modelobj = model.build() ## now to print stWriter = StringWriter() jsonWriter = Json.createWriter(stWriter) jsonWriter.writeObject(modelobj) jsonWriter.close() jsonData = stWriter.toString() print >> f, jsonData f.close() closeDomain() print "Finished Scan report - output file completed" exit()
def __reportSearch(self): self.reportId = self.request.getParameter("id") self.format = self.request.getParameter("format") self.report = self.reportManager.getReports().get(self.reportId) self.reportQuery = self.report.getQueryAsString() self.log.debug("Report query: " + self.reportQuery) #Get a total number of records try: out = ByteArrayOutputStream() recnumreq = SearchRequest(self.reportQuery) recnumreq.setParam("rows", "0") self.indexer.search(recnumreq, out) recnumres = SolrResult(ByteArrayInputStream(out.toByteArray())) self.__rowsFoundSolr = "%s" % recnumres.getNumFound() except: self.errorMsg = "Query failure. The issue has been logged (%s - %s)." % ( sys.exc_info()[0], sys.exc_info()[1]) self.log.error( "Reporting threw an exception (report was %s): %s - %s" % (self.report.getLabel(), sys.exc_info()[0], sys.exc_info()[1])) return #Setup the main query req = SearchRequest(self.reportQuery) req.setParam("fq", 'item_type:"object"') req.setParam("fq", 'workflow_id:"dataset"') req.setParam("rows", self.__rowsFoundSolr) try: #Now do the master search out = ByteArrayOutputStream() self.indexer.search(req, out) self.__reportResult = SolrResult( ByteArrayInputStream(out.toByteArray())) self.__checkResults() except: self.errorMsg = "Query failure. The issue has been logged (%s - %s)." % ( sys.exc_info()[0], sys.exc_info()[1]) self.log.error( "Reporting threw an exception (report was %s): %s - %s" % (self.report.getLabel(), sys.exc_info()[0], sys.exc_info()[1])) return #At this point the display template has enough to go with. #We just need to handle the CSV now if (self.format == "csv"): #Setup the main query - we need to requery to make sure we return #only the required fields. We'll use the specific IDs that met the #__checkResults check req = SearchRequest(self.reportQuery) req.setParam("fq", 'item_type:"object"') req.setParam("fq", 'workflow_id:"dataset"') req.setParam("rows", self.__rowsFoundSolr) req.setParam("csv.mv.separator", ";") #we need to get a list of the matching IDs from Solr #this doesn't work for long queries so it's abandoned #but left here commented to make sure we don't try it again #idQry = "" #for item in self.getProcessedResultsList(): # idQry += item.get("id") + " OR " #req.setParam("fq", 'id:(%s)' % idQry[:len(idQry)-4]) #Create a list of IDs for reference when preparing the CSV idQryList = [] for item in self.getProcessedResultsList(): idQryList.append(item.get("id")) #Setup SOLR query with the required fields self.fields = self.systemConfig.getArray("redbox-reports", "csv-output-fields") #We must have an ID field and it must be the first field fieldString = "id," if self.fields is not None: for field in self.fields: fieldString = fieldString + field.get("field-name") + "," fieldString = fieldString[:-1] req.setParam("fl", fieldString) out = ByteArrayOutputStream() try: self.indexer.search(req, out, self.format) except: #We can't get the result back from SOLR so fail back to the template display self.errorMsg = "Query failure. Failed to load the data - this issue has been logged (%s - %s)." % ( sys.exc_info()[0], sys.exc_info()[1]) self.log.error( "Reporting threw an exception (report was %s); Error: %s - %s" % (self.report.getLabel(), sys.exc_info()[0], sys.exc_info()[1])) return try: csvResponseString = String(out.toByteArray(), "utf-8") csvResponseLines = csvResponseString.split("\n") except: #We can't get the result back from SOLR so fail back to the template display self.errorMsg = "Query failure. Failed to prepare the CSV - this issue has been logged (%s - %s)." % ( sys.exc_info()[0], sys.exc_info()[1]) self.log.error( "Reporting threw an exception (report was %s); Error: %s - %s" % (self.report.getLabel(), sys.exc_info()[0], sys.exc_info()[1])) return fileName = self.urlEncode(self.report.getLabel()) self.log.debug("Generating CSV report with file name: " + fileName) self.response.setHeader("Content-Disposition", "attachment; filename=%s.csv" % fileName) sw = StringWriter() parser = CSVParser() writer = CSVWriter(sw) count = 0 prevLine = "" badRowFlag = False for line in csvResponseLines: if badRowFlag: #In this section of code we'll handle errors by either trying to fix the problem #or by adding an error line in the CSV. We'll then move to the next row and keep going try: self.log.debug( "Reporting - trying to append the previous line with the previous faulty one. Line appears as: %s" % prevLine + line) csvLine = parser.parseLine(prevLine + line) badRowFlag = False prevLine = "" self.log.debug( "Reporting - remedy appears to have worked. Line appears as: %s" % prevLine + line) except: #We tried to rescue the file but failed on the second run so give up writer.writeNext( ["Failed to transfer record to CSV - check logs"]) self.log.error( "Reporting threw an exception (report was %s); Error: %s - %s; Result line: %s" % (self.report.getLabel(), sys.exc_info()[0], sys.exc_info()[1], prevLine + line)) else: try: csvLine = parser.parseLine(line) badRowFlag = False prevLine = "" except: #This can happen if there's a newline in the index data #so we raise the badRowFlag and see if we can join this #row to the next one to fix it self.log.debug( "Reporting threw an exception but I'll see if it's just a formatting issue (report was %s); Error: %s - %s; Result line: %s" % (self.report.getLabel(), sys.exc_info()[0], sys.exc_info()[1], line)) badRowFlag = True prevLine = line continue if count == 0: #Header row count += 1 for idx, csvValue in enumerate(csvLine): csvLine[idx] = self.findDisplayLabel(csvValue) elif csvLine[0] not in idQryList: #ignore continue writer.writeNext(csvLine) #Now send off the CSV self.out = self.response.getOutputStream("text/csv") self.out.print(sw.toString()) self.out.close()
def __reportSearch(self): self.reportId = self.request.getParameter("id") self.format = self.request.getParameter("format") self.report = self.reportManager.getReports().get(self.reportId) self.reportQuery = self.report.getQueryAsString() self.log.debug("Report query: " + self.reportQuery) #Get a total number of records try: out = ByteArrayOutputStream() recnumreq = SearchRequest(self.reportQuery) recnumreq.setParam("rows", "0") self.indexer.search(recnumreq, out) recnumres = SolrResult(ByteArrayInputStream(out.toByteArray())) self.__rowsFoundSolr = "%s" % recnumres.getNumFound() except: self.errorMsg = "Query failure. The issue has been logged (%s - %s)." % (sys.exc_info()[0], sys.exc_info()[1]) self.log.error("Reporting threw an exception (report was %s): %s - %s" % (self.report.getLabel(), sys.exc_info()[0], sys.exc_info()[1])) return #Setup the main query req = SearchRequest(self.reportQuery) req.setParam("fq", 'item_type:"object"') req.setParam("fq", 'workflow_id:"dataset"') req.setParam("rows", self.__rowsFoundSolr) try: #Now do the master search out = ByteArrayOutputStream() self.indexer.search(req, out) self.__reportResult = SolrResult(ByteArrayInputStream(out.toByteArray())) self.__checkResults() except: self.errorMsg = "Query failure. The issue has been logged (%s - %s)." % (sys.exc_info()[0], sys.exc_info()[1]) self.log.error("Reporting threw an exception (report was %s): %s - %s" % (self.report.getLabel(), sys.exc_info()[0], sys.exc_info()[1])) return #At this point the display template has enough to go with. #We just need to handle the CSV now if (self.format == "csv"): #Setup the main query - we need to requery to make sure we return #only the required fields. We'll use the specific IDs that met the #__checkResults check req = SearchRequest(self.reportQuery) req.setParam("fq", 'item_type:"object"') req.setParam("fq", 'workflow_id:"dataset"') req.setParam("rows", self.__rowsFoundSolr) req.setParam("csv.mv.separator",";") #we need to get a list of the matching IDs from Solr #this doesn't work for long queries so it's abandoned #but left here commented to make sure we don't try it again #idQry = "" #for item in self.getProcessedResultsList(): # idQry += item.get("id") + " OR " #req.setParam("fq", 'id:(%s)' % idQry[:len(idQry)-4]) #Create a list of IDs for reference when preparing the CSV idQryList = [] for item in self.getProcessedResultsList(): idQryList.append(item.get("id")) #Setup SOLR query with the required fields self.fields = self.systemConfig.getArray("redbox-reports","csv-output-fields") #We must have an ID field and it must be the first field fieldString = "id," if self.fields is not None: for field in self.fields: fieldString = fieldString+ field.get("field-name")+"," fieldString = fieldString[:-1] req.setParam("fl",fieldString) out = ByteArrayOutputStream() try: self.indexer.search(req, out, self.format) except: #We can't get the result back from SOLR so fail back to the template display self.errorMsg = "Query failure. Failed to load the data - this issue has been logged (%s - %s)." % (sys.exc_info()[0], sys.exc_info()[1]) self.log.error("Reporting threw an exception (report was %s); Error: %s - %s" % (self.report.getLabel(), sys.exc_info()[0], sys.exc_info()[1])) return try: csvResponseString = String(out.toByteArray(),"utf-8") csvResponseLines = csvResponseString.split("\n") except: #We can't get the result back from SOLR so fail back to the template display self.errorMsg = "Query failure. Failed to prepare the CSV - this issue has been logged (%s - %s)." % (sys.exc_info()[0], sys.exc_info()[1]) self.log.error("Reporting threw an exception (report was %s); Error: %s - %s" % (self.report.getLabel(), sys.exc_info()[0], sys.exc_info()[1])) return fileName = self.urlEncode(self.report.getLabel()) self.log.debug("Generating CSV report with file name: " + fileName) self.response.setHeader("Content-Disposition", "attachment; filename=%s.csv" % fileName) sw = StringWriter() parser = CSVParser() writer = CSVWriter(sw) count = 0 prevLine = "" badRowFlag = False for line in csvResponseLines: if badRowFlag: #In this section of code we'll handle errors by either trying to fix the problem #or by adding an error line in the CSV. We'll then move to the next row and keep going try: self.log.debug("Reporting - trying to append the previous line with the previous faulty one. Line appears as: %s" % prevLine + line) csvLine = parser.parseLine(prevLine + line) badRowFlag = False prevLine = "" self.log.debug("Reporting - remedy appears to have worked. Line appears as: %s" % prevLine + line) except: #We tried to rescue the file but failed on the second run so give up writer.writeNext(["Failed to transfer record to CSV - check logs"]) self.log.error("Reporting threw an exception (report was %s); Error: %s - %s; Result line: %s" % (self.report.getLabel(), sys.exc_info()[0], sys.exc_info()[1], prevLine + line)) else: try: csvLine = parser.parseLine(line) badRowFlag = False prevLine = "" except: #This can happen if there's a newline in the index data #so we raise the badRowFlag and see if we can join this #row to the next one to fix it self.log.debug("Reporting threw an exception but I'll see if it's just a formatting issue (report was %s); Error: %s - %s; Result line: %s" % (self.report.getLabel(), sys.exc_info()[0], sys.exc_info()[1], line)) badRowFlag = True prevLine = line continue if count == 0 : #Header row count += 1 for idx, csvValue in enumerate(csvLine): csvLine[idx] = self.findDisplayLabel(csvValue) elif csvLine[0] not in idQryList: #ignore continue writer.writeNext(csvLine) #Now send off the CSV self.out = self.response.getOutputStream("text/csv") self.out.print(sw.toString()) self.out.close()
def render(self, template, context): t = Template("inline", StringReader(template), self._conf) sw = StringWriter() t.process(context, sw) return sw.toString()
# End if URLSource.request('GET', uri, {}, {}) response = URLSource.getresponse() print response.status, response.reason script = response.read() cliScript = Localcliscript(cli['cliHome'], cli['xldHost'], cli['xldPort'], cli['xldSecure'], cli['xldContext'], cli['xldProxyHost'], cli['xldProxyPort'], cli['xldSocketTimeout'], cli['xldUserName'], cli['xldPassword'], script, cli['cliExecutable'], options) exitCode = cliScript.execute() output = cliScript.getStdout() err = cliScript.getStderr() except Exception, e: stacktrace = StringWriter() writer = PrintWriter(stacktrace, True) e.printStackTrace(writer) err = stacktrace.toString() exitCode = 1 finally: URLSource.close() # End try if exitCode == 0: print output else: print print "### Exit code " print exitCode print print "### Output:" print output
def doPost(self, request, response): c = NomjycContainer() c.parameters = request.getParameterMap() log = open("/var/log/nomjyc/sandbox.log","a") safelog = dict(c.parameters) if "pass" in safelog: safelog["pass"]=len(safelog["pass"]) log.write(("[[%s]] %s %s\n" % (request.getRemoteAddr(), datetime.utcnow(), self.yaml.dump(safelog))).encode("utf-8")) log.close() output = "<div class=\"infobox\"><span class=\"gh\">nomjyc 0.1</span>\n" c.session = {} if len(c.parameters)==0: output += "<pre>%s</pre>" % self.__doc__ output += "</div>" c.salt="dckx" try: c.data = self.yaml.load(FileInputStream("/var/lib/tomcat6/webapps/nomjyc/data/nomjyc.yaml")) except: output += self.explainException("Error while initiating game state") # Print some debug information - for now. output += self.divHideCode("infobox", "Request", "reqi", self.dumpYaml(c.parameters), visible=True) output += self.divHideCode("infobox", "Data read", "dri", self.dumpYaml(c.data)) # Add a final rule, if the test parameter is set if "test" in c.parameters: for code in c.parameters["test"]: c.data["rules"].add({"author":"impromptu", "code":code, "creation":datetime.utcnow(), "title":"test rule"}) cycles = 1 if "cycles" in c.parameters: try: cycles = int(c.parameters["cycles"][0]) except: pass if cycles<0 or cycles>3: cycles = 1 # Execute all rules against the user input brain = PythonInterpreter() c.sandbox = True # a flag that gives away that we are in a sandbox for i in range(cycles): c.cycle = i # a counter for the cycle we are in for rule in c.data["rules"][:]: # we are going to modify the rules. a lot. this prevents concurrent modification. try: output += self.divHideCode("rulebox", "Executing rule '%s'" % rule["title"], "id"+str(random()), self.dumpPython(rule["code"]), openend=True) err = StringWriter() out = StringWriter() checksum = hashlib.md5(self.yaml.dump(c.data)).hexdigest() brain.set("self", c) brain.setErr(err) brain.setOut(out) before = time.time() timeout (brain.exec,(rule["code"],),timeout_duration=30) runtime = int((time.time()-before) * 1000) changes = (checksum != hashlib.md5(self.yaml.dump(c.data)).hexdigest()) output += "<div class=\"ruleoutput\">" if changes: output += "<div class=\"erroroutput\">This rule changed the game data.</div>" if (err.getBuffer().length()): output += "<div class=\"erroroutput\">Err:<br />%s</div>" % self.dumpPythonTB(err.toString().strip()) if (out.getBuffer().length()): output += "<div class=\"gu\">Out:</div>"+out.toString().strip() output += "<div>(runtime: %sms)</div></div></div>" % runtime except Exception, ex: output += self.explainException("Execution failed") + "</div>"