def buildLarchJar(outputStream,
                  additionalFilesAsNameBytesPairs,
                  filterFn=None,
                  larchJarURL=None):
    """
	Build a JAR from an existing Larch JAR, along with additional files to make a packaged program

	:param outputStream: The output stream to which the JAR is to be written
	:param additionalFilesAsNameBytesPairs: Additional files in the form of a sequence of tuples consisting of (path, bytes)
	:param filterFn: (optional) A filter function that can be used to exclude files from the existing Larch JAR; takes the form of function(name) -> boolean, should return True if file should be included
	:param larchJarURL: (optional) A URL at which the existing Larch JAR can be obtained. If None, it will use the JAR from which Larch was started. Raises an error if no URL provided and Larch was not started from a JAR
	"""
    if larchJarURL is None:
        larchJarURL = _larchJarURL

    if larchJarURL is None:
        raise RuntimeError, 'Larch was not loaded from a JAR file and no Larch JAR file was provided'

    jarIn = JarInputStream(larchJarURL.openStream())

    manifestIn = jarIn.getManifest()
    manifestOut = Manifest(manifestIn)

    jarOut = JarOutputStream(outputStream, manifestOut)

    bytesBuffer = jarray.zeros(_BYTES_BUFFER_SIZE, 'b')

    entryIn = jarIn.getNextJarEntry()
    while entryIn is not None:
        name = entryIn.getName()

        if filterFn is None or filterFn(name):
            bufferStream = ByteArrayOutputStream()
            while True:
                bytesRead = jarIn.read(bytesBuffer, 0, _BYTES_BUFFER_SIZE)
                if bytesRead == -1:
                    break
                bufferStream.write(bytesBuffer, 0, bytesRead)

            entryOut = ZipEntry(name)
            entryOut.setSize(bufferStream.size())
            jarOut.putNextEntry(entryOut)
            bufferStream.writeTo(jarOut)
            jarOut.closeEntry()

        entryIn = jarIn.getNextJarEntry()

    for name, bytes in additionalFilesAsNameBytesPairs:
        size = len(bytes)
        entryOut = ZipEntry(name)
        entryOut.setSize(size)
        jarOut.putNextEntry(entryOut)
        jarOut.write(bytes, 0, size)
        jarOut.closeEntry()

    jarOut.finish()
Example #2
0
def fake_plugin_jar(name, plugins_config):
	output = FileOutputStream(name)
	zip = ZipOutputStream(output)
	entry = ZipEntry('plugins.config')
	zip.putNextEntry(entry)
	zip.write(plugins_config)
	zip.closeEntry()
	zip.close()
Example #3
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 #4
0
 def add_folder(zip, folder):
     for file in os.listdir(folder):
         file = folder + '/' + file
         if os.path.isdir(file):
             add_folder(zip, file)
         elif os.path.isfile(file):
             if verbose:
                 print file
             entry = ZipEntry(file)
             zip.putNextEntry(entry)
             f = open(file, "rb")
             zip.write(f.read())
             f.close()
             zip.closeEntry()
Example #5
0
def add_folder(zos, folder_name, base_folder_name):
    f = File(folder_name)
    if not f.exists():
        return
    if f.isDirectory():
        for f2 in f.listFiles():
            add_folder(zos, f2.absolutePath, base_folder_name)
        return
    entry_name = folder_name[len(base_folder_name) + 1:len(folder_name)]
    ze = ZipEntry(entry_name)
    zos.putNextEntry(ze)
    input_stream = FileInputStream(folder_name)
    buffer = zeros(1024, 'b')
    rlen = input_stream.read(buffer)
    while (rlen > 0):
        zos.write(buffer, 0, rlen)
        rlen = input_stream.read(buffer)
    input_stream.close()
    zos.closeEntry()
Example #6
0
    def __createEpub(self):
        title = self.__manifest.getString(None, "title")

        self.vc("response").setHeader(
            "Content-Disposition",
            "attachment; filename=%s.epub" % urllib.quote(title))
        out = self.vc("response").getOutputStream("application/epub+zip")
        zipOutputStream = ZipOutputStream(out)

        #save mimetype... and the rest of standard files in epub
        zipOutputStream.putNextEntry(ZipEntry("mimetype"))
        epubMimetypeStream = self.__getResourceAsStream("/epub/mimetype")
        IOUtils.copy(epubMimetypeStream, zipOutputStream)
        zipOutputStream.closeEntry()

        zipOutputStream.putNextEntry(ZipEntry("META-INF/container.xml"))
        epubContainerStream = self.__getResourceAsStream("/epub/container.xml")
        IOUtils.copy(epubContainerStream, zipOutputStream)
        zipOutputStream.closeEntry()

        zipOutputStream.putNextEntry(ZipEntry("OEBPS/epub.css"))
        epubcss = self.__getResourceAsStream("/epub/epub.css")
        IOUtils.copy(epubcss, zipOutputStream)
        zipOutputStream.closeEntry()

        #### Creating toc.ncx ####
        tocXml = ElementTree.Element(
            "ncx", {
                "version": "2005-1",
                "xml:lang": "en",
                "xmlns": "http://www.daisy.org/z3986/2005/ncx/"
            })
        headNode = ElementTree.Element("head")
        tocXml.append(headNode)

        headNode.append(
            ElementTree.Element("meta", {
                "name": "dtb:uid",
                "content": "1"
            }))
        headNode.append(
            ElementTree.Element("meta", {
                "name": "dtb:depth",
                "content": "1"
            }))
        headNode.append(
            ElementTree.Element("meta", {
                "name": "dtb:totalPageCount",
                "content": "1"
            }))
        headNode.append(
            ElementTree.Element("meta", {
                "name": "dtb:maxPageNumber",
                "content": "1"
            }))
        headNode.append(
            ElementTree.Element("meta", {
                "name": "dtb:generator",
                "content": "ICE v2"
            }))

        #docTitle
        docTitle = ElementTree.Element("docTitle")
        textNode = ElementTree.Element("text")
        textNode.text = title
        docTitle.append(textNode)
        tocXml.append(docTitle)

        #docAuthor
        docAuthor = ElementTree.Element("docAuthor")
        textNode = ElementTree.Element("text")
        textNode.text = "ICE v2"
        docAuthor.append(textNode)
        tocXml.append(docAuthor)

        #navMap
        navMap = ElementTree.Element("navMap")
        tocXml.append(navMap)

        #### Creating content.opf ####
        contentXml = ElementTree.Element(
            "package", {
                "version": "2.0",
                "xmlns": "http://www.idpf.org/2007/opf",
                "unique-identifier": "BookId"
            })

        metadataNode = ElementTree.Element(
            "metadata", {
                "xmlns:dc": "http://purl.org/dc/elements/1.1/",
                "xmlns:opf": "http://www.idpf.org/2007/opf"
            })
        contentXml.append(metadataNode)

        #metadata information
        metadata = ElementTree.Element("dc:title")
        metadata.text = title
        metadataNode.append(metadata)

        metadata = ElementTree.Element("dc:language")
        metadata.text = "en-AU"
        metadataNode.append(metadata)

        metadata = ElementTree.Element("dc:creator", {"opf:role": "aut"})
        metadata.text = "ICE"
        metadataNode.append(metadata)

        metadata = ElementTree.Element("dc:publisher")
        metadata.text = "University of Southern Queensland"
        metadataNode.append(metadata)

        metadata = ElementTree.Element("dc:identifier", {"id": "BookId"})
        metadata.text = title
        metadataNode.append(metadata)

        #manifest
        manifest = ElementTree.Element("manifest")
        contentXml.append(manifest)

        spine = ElementTree.Element("spine", {"toc": "ncx"})
        contentXml.append(spine)

        item = ElementTree.Element("item", {
            "id": "ncx",
            "href": "toc.ncx",
            "media-type": "text/xml"
        })
        manifest.append(item)
        css = ElementTree.Element("item", {
            "id": "style",
            "href": "epub.css",
            "media-type": "text/css"
        })
        manifest.append(css)

        count = 1
        for itemHash in self.__orderedItem:
            id, title, htmlFileName, payloadDict, isImage = self.__itemRefDict[
                itemHash]

            for payloadId in payloadDict:
                payload, payloadType = payloadDict[payloadId]
                if isinstance(payload, Payload):
                    payloadId = payloadId.lower()
                    zipEntryId = payloadId.replace(" ", "_").replace("\\", "/")
                    if payloadType == "application/xhtml+xml":
                        zipOutputStream.putNextEntry(
                            ZipEntry("OEBPS/%s" % zipEntryId))
                        ##process the html....
                        saxReader = SAXReader(False)
                        try:
                            saxDoc = saxReader.read(payload.open())
                            payload.close()
                            #                            ## remove class or style nodes
                            #                            classOrStyleNodes = saxDoc.selectNodes("//@class | //@style ")
                            #                            for classOrStyleNode in classOrStyleNodes:
                            #                                node = classOrStyleNode
                            #                                if classOrStyleNode.getParent():
                            #                                    node = classOrStyleNode.getParent()
                            #                                if node.getQualifiedName() == "img":
                            #                                    attr = node.attribute(QName("class"))
                            #                                    attr = node.attribute(QName("class"))
                            #                                    if attr:
                            #                                        node.remove(attr)
                            #                                    attr = node.attribute(QName("style"))
                            #                                    if attr:
                            #                                        node.remove(attr)

                            ## remove name in a tags
                            ahrefs = saxDoc.selectNodes(
                                "//*[local-name()='a' and @name!='']")
                            for a in ahrefs:
                                attr = a.attribute(QName("name"))
                                if attr:
                                    a.remove(attr)

                            ## fix images src name.... replace space with underscore and all lower case
                            imgs = saxDoc.selectNodes(
                                "//*[local-name()='img' and contains(@src, '_files')]"
                            )
                            for img in imgs:
                                srcAttr = img.attribute(QName("src"))
                                if srcAttr:
                                    src = srcAttr.getValue()
                                    #hash the sourcename
                                    filepath, filename = os.path.split(src)
                                    filename, ext = os.path.splitext(filename)
                                    filename = hashlib.md5(
                                        filename).hexdigest()
                                    src = os.path.join(
                                        filepath.lower().replace(" ", "_"),
                                        "node-%s%s" % (filename, ext))
                                    img.addAttribute(QName("src"),
                                                     src.replace(" ", "_"))

                            bodyNode = saxDoc.selectSingleNode(
                                "//*[local-name()='div' and @class='body']")
                            bodyNode.setName("div")

                            out = ByteArrayOutputStream()
                            format = OutputFormat.createPrettyPrint()
                            format.setSuppressDeclaration(True)
                            writer = XMLWriter(out, format)
                            writer.write(bodyNode)
                            writer.flush()
                            contentStr = out.toString("UTF-8")

                            htmlString = """<?xml version="1.0" encoding="UTF-8"?>
                                            <html xmlns="http://www.w3.org/1999/xhtml"><head><title>%s</title>
                                            <link rel="stylesheet" href="epub.css"/>
                                            </head><body>%s</body></html>"""
                            htmlString = htmlString % (title, contentStr)
                            self.__copyString(htmlString, zipOutputStream)
                            includeFile = False
                        except:
                            traceback.print_exc()
                    else:
                        #images....
                        zipOutputStream.putNextEntry(
                            ZipEntry("OEBPS/%s" % zipEntryId))
                        IOUtils.copy(payload.open(), zipOutputStream)
                        payload.close()
                        zipOutputStream.closeEntry()
                else:
                    zipOutputStream.putNextEntry(
                        ZipEntry("OEBPS/%s" % zipEntryId))
                    IOUtils.copy(payload, zipOutputStream)
                    zipOutputStream.closeEntry()

                itemNode = ElementTree.Element("item", {
                    "media-type": payloadType,
                    "href": zipEntryId
                })
                if payloadId == htmlFileName.lower():
                    itemNode.set("id", itemHash)
                else:
                    itemNode.set("id", payloadId.replace("/", "_"))
                manifest.append(itemNode)

            if not isImage:
                navPoint = ElementTree.Element(
                    "navPoint", {
                        "class": "chapter",
                        "id": "%s" % itemHash,
                        "playOrder": "%s" % count
                    })
            else:
                navPoint = ElementTree.Element(
                    "navPoint", {
                        "class": "chapter",
                        "id": "%s" % htmlFileName,
                        "playOrder": "%s" % count
                    })
            navMap.append(navPoint)
            navLabel = ElementTree.Element("navLabel")
            navPoint.append(navLabel)
            textNode = ElementTree.Element("text")
            textNode.text = title
            navLabel.append(textNode)
            content = ElementTree.Element("content")
            navPoint.append(content)
            content.set("src", htmlFileName)
            count += 1

            itemRefNode = ElementTree.Element("itemref")
            spine.append(itemRefNode)
            itemRefNode.set("idref", itemHash)

        #saving content.opf...
        zipOutputStream.putNextEntry(ZipEntry("OEBPS/content.opf"))
        self.__copyString(ElementTree.tostring(contentXml), zipOutputStream)
        zipOutputStream.closeEntry()

        #saving toc.ncx
        zipOutputStream.putNextEntry(ZipEntry("OEBPS/toc.ncx"))
        self.__copyString(ElementTree.tostring(tocXml), zipOutputStream)
        zipOutputStream.closeEntry()

        zipOutputStream.close()
Example #7
0
 def add(self, name, data):
     self.zipOutputStream.putNextEntry(ZipEntry(name))
     self.__copyString(data, self.zipOutputStream)
     self.zipOutputStream.closeEntry()
Example #8
0
 def addStream(self, name, s):
     self.zipOutputStream.putNextEntry(ZipEntry(name))
     IOUtils.copy(s, self.zipOutputStream)
     self.zipOutputStream.closeEntry()
Example #9
0
 def getFile(self, name):
     fname = self.getName(name)
     self.zipfile.putNextEntry(ZipEntry(fname))
     return self.zipfile