Example #1
0
    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)        
Example #2
0
def mainproc():
    u'''Функция для склейки карточки с блоками правил'''
    try:
        rootPath = AppInfoSingleton.getAppInfo().getCurUserData().getPath(
        ) + '/xforms/'
    except:
        rootPath = 'E:/Projects/celesta/ssmmd/userscore/ssmmd/xforms/file/'

    templatePath = rootPath + add  #путь к карточке, в которую необходимо вставить праила
    rulePath = rootPath + 'ruleTemplate.xml'  #путь к блоку с правилами
    bindPath = rootPath + 'bindTemplate.xml'  #путь к блоку с биндами для правил

    stringWriter = StringWriter()
    xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(
        stringWriter)

    parser = XMLReaderFactory.createXMLReader()
    handler = XformsAddRules(rulePath, bindPath, xmlWriter)
    parser.setContentHandler(handler)
    parser.setErrorHandler(handler)
    parser.setFeature("http://xml.org/sax/features/namespace-prefixes", True)
    parser.setProperty("http://xml.org/sax/properties/lexical-handler",
                       handler)

    stream = FileInputStream(templatePath)
    parser.parse(InputSource(stream))
    xmlWriter.close()
    stringWriter.close()
    return unicode(stringWriter)
 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
Example #4
0
    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)
Example #5
0
 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 __toXMLString(self, document):
     domSource = DOMSource(document)
     writer = StringWriter()
     result = StreamResult(writer)
     tf = TransformerFactory.newInstance()
     transformer = tf.newTransformer()
     transformer.transform(domSource, result)
     return writer.toString()
Example #7
0
 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()
Example #8
0
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()
Example #9
0
 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()))
Example #10
0
 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)
Example #11
0
 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
Example #12
0
 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 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
Example #14
0
 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
Example #15
0
 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
Example #16
0
 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)
Example #17
0
 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)
Example #19
0
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
Example #20
0
 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)
Example #21
0
 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
Example #22
0
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 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 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
Example #25
0
 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
Example #26
0
 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()
Example #27
0
 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
Example #28
0
 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()
Example #29
0
 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 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
Example #31
0
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
Example #32
0
 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
Example #33
0
def build_velocity_context(template_file):
    inputs = VelocityContext()
    fInputs = None
    try:
        fInputs = FileInputStream(template_file)
        Velocity.evaluate(inputs, StringWriter(), "inputs", fInputs)
        return inputs
    finally:
        if fInputs:
            fInputs.close()
Example #34
0
    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()
Example #35
0
 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
Example #36
0
 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
Example #37
0
    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()
Example #38
0
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()
Example #39
0
 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
Example #40
0
 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 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
Example #42
0
    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"Неверный формат файла")
Example #43
0
 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 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 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
Example #46
0
def mainproc():
    global xmlWriter, statusUid

    parser = XMLUtils.createSAXParser()
    stream = StringBufferInputStream(data.encode("utf-8"))
    stringWriter = StringWriter()
    xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(
        stringWriter)
    parser.parse(stream, formReader())
    xmlWriter.close()

    if (statusUid != ''):
        if JythonPoolTest.getStorage().contains(statusUid):
            return UserMessage("test1", u"повтор!")
        JythonPoolTest.getStorage().add(statusUid)
    return None
Example #47
0
 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
Example #48
0
    def printXMLfile(self, doc):
        try:
            from javax.xml.transform import TransformerFactory
            from javax.xml.transform import OutputKeys
            from javax.xml.transform.stream import StreamSource
            from javax.xml.transform.stream import StreamResult
            from javax.xml.transform.dom import DOMSource
            from java.io import StringWriter

            xmlInput = DOMSource(doc)
            xmlOutput = StreamResult(StringWriter())

            tranFactory = TransformerFactory.newInstance()
            aTransformer = tranFactory.newTransformer()
            aTransformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8")
            aTransformer.setOutputProperty(OutputKeys.INDENT, "yes")
            aTransformer.transform(xmlInput, xmlOutput)

            print xmlOutput.getWriter().toString()
        except:
            print "exception: %s" % traceback.format_exception(*sys.exc_info())
Example #49
0
def ncdumpToString(path, output_format='cdl', show_values='c', vars=None):
    """Returns contents of a given netCDF file as a string.
    
    Please be aware that the resulting string CANNOT (currently) be used as
    input for ncgen due to netCDF-Java limitations.
    
    Args:
        path: Path to an existing netCDF file.
        
        output_format: Optional. Understands 'cdl' and 'ncml'. Defaults to 'cdl'
        
        show_values: Optional. Understands 'c' and 'vall'. Defaults to 'c'.
        
        vars: Optional. Allows you to dump specified variable(s) or variable section(s).
        
    Returns:
        String representation of "ncdump" output (or an empty string if there
        was a problem within netCDF).
    """
    
    from jarray import array
    from java.io import IOException
    from java.io import StringWriter
    from ucar.nc2 import NCdumpW
    
    # build up commandline args to send off to netCDF-land (it wants 'em as a
    # string for some reason)
    args = '%s -%s -%s ' % (path, output_format, show_values)
    if vars:
        for var in vars:
            args = '%s -v %s' % (args, var)
            
    writer = StringWriter()
    
    try:
        NCdumpW.print(args, writer, None)
    except IOException, ioe:
        print 'Error attempting to list contents of', path
        print ioe
Example #50
0
def handler(environ, start_response):
    reload(sys)
    sys.setdefaultencoding('UTF8')
    welcomeString = "<html><head><title>atom2rss converter &bull; Atom to RSS2 converter</title><style>body { padding: 20px 40px; font-family: Verdana, Helvetica, Sans-Serif; font-size: medium; }</style></head><body><form method=\"post\"><h1>atom2rss converter written with Jython</h1><p>This tool will let you convert your atom 1.0 feed into an RSS2 feed that can be imported into WordPress.</p><p>Please read the full instructions before starting.</p><p><input type=\"text\" name=\"atom\" style=\"width: 400px;\"><input type=\"submit\" value=\"Convert\"></p><h2>Instructions</h2><p><strong>Step 1</strong>: Enter the url of an atom feed urls, e.g. <i>http://yoursite.com/atom.xml</i><br /></p><p><strong>Step 2</strong>: After clicking the \"Convert\" button, head to File / Save As... and save the file as rss.xml to your desktop.</p><p><strong>Step 3</strong>: Use this rss.xml file to import your data into WordPress under Import / RSS.</p><h2>About</h2><p>This tool is online as a convenience designed by <a href=\"http://kevin.9511.net/\">Kevin Li</a>.</p><p>Your can use it to import GoogleReader or Blogger data into Wordpress.</p></form></body></html>"
    response_parts = []
    header_str = ''
    if environ['REQUEST_METHOD'] == 'POST':
        post = environ['j2ee.request']
        if post.getParameterValues('atom') is None:
            bs = welcomeString
        else:
            try:
                atom = post.getParameterValues('atom')[0]

                atomUrl = URL(str(atom))
                atomSource = StreamSource(
                    InputStreamReader(atomUrl.openStream(), "utf-8"))

                file = open(os.path.join(os.getcwd(), 'webapp/atom2rss.xsl'))
                xsltString = file.read()
                file.close()
                xsltSource = StreamSource(StringReader(xsltString))

                outputBuffer = StringWriter()

                transFactory = javax.xml.transform.TransformerFactory.newInstance(
                    "org.apache.xalan.processor.TransformerFactoryImpl",
                    Thread.currentThread().getContextClassLoader())

                transformer = transFactory.newTransformer(xsltSource)
                transformer.transform(atomSource, StreamResult(outputBuffer))
                bs = outputBuffer.buffer.toString()
                header_str = 'text/xml;charset=utf-8'
            except MalformedURLException, mue:
                bs = "url format error"
            except TransformerException, te:
                bs = "transforme failed"
            except IOException, ie:
                bs = ie.getMessage()
Example #51
0
    def startElement(self, namespaceURI, lname, qname, attrs):
        global stringEmployeesWriter, statusUid

        if (self.currentState != self.states[0] or qname == 'info'):
            if (self.currentState == self.states[0] and qname == 'info'):
                self.currentState = self.states[1]

            elif (self.currentState == self.states[1] and qname == 'status'):
                self.currentState = self.states[2]
                statusUid = attrs.getValue(attrs.getIndex("uid"))

            elif (self.currentState == self.states[1]
                  and qname == 'employees'):
                self.currentState = self.states[3]

                stringEmployeesWriter = StringWriter()
                self.employeesWriter = XMLOutputFactory.newInstance(
                ).createXMLStreamWriter(stringEmployeesWriter)
                self.employeesWriter.writeStartElement(qname)

            elif (self.currentState == self.states[3]):
                self.employeesWriter.writeStartElement(qname)
                for i in range(0, attrs.getLength()):
                    self.employeesWriter.writeAttribute(
                        attrs.getQName(i), attrs.getValue(i))

        else:
            if (attrs.getIndex("ftid") != -1):
                for i in range(0, attrs.getLength()):
                    if (attrs.getQName(i) != 'ftid'):
                        xmlWriter.writeStartElement('record')
                        xmlWriter.writeAttribute(
                            'ftid', attrs.getValue(attrs.getIndex("ftid")))
                        xmlWriter.writeAttribute('name', attrs.getQName(i))
                        xmlWriter.writeAttribute('value', attrs.getValue(i))
                        xmlWriter.writeEndElement()
Example #52
0
   else:
       print "Make HTTP connection"
       URLSource = httplib.HTTPConnection( host )
   # 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
Example #53
0
 def logStackTrace_0(cls, toFile, ex):
     """ generated source for method logStackTrace_0 """
     s = StringWriter()
     ex.printStackTrace(PrintWriter(s))
     cls.logError(toFile, s.__str__())
Example #54
0
    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()
Example #55
0
 def generateReport(self):
     writer = StringWriter()
     self._ve.evaluate(self._context, writer, self._template.getName(),
                       unicode(self._template.getSource()))
     evaluatedTemplate = writer.toString()
     return evaluatedTemplate
Example #56
0
    mon = JavaSysMon()
    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():
else:
    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();
Example #58
0
  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>"