Ejemplo n.º 1
0
  def addJarFile(self, jarFile):
    __doc__ = """if you want to add a .jar file with a MANIFEST, add it first"""
    jarJarFile = JarFile(jarFile)
    self.addManifest(jarJarFile.getManifest())
    jarJarFile.close()

    jarInputStream = JarInputStream(FileInputStream(jarFile))
    jarEntry = jarInputStream.getNextJarEntry()
    while jarEntry:
      self.getJarOutputStream().putNextEntry(jarEntry)
      buffer = jarray.zeros(self._bufsize, 'b')
      read = jarInputStream.read(buffer)
      while read <> -1:
        self.getJarOutputStream().write(buffer, 0, read)
        read = jarInputStream.read(buffer)
      self.getJarOutputStream().closeEntry()
      jarEntry = jarInputStream.getNextJarEntry()
def scanLarchJar():
    if _larchJarURL is None:
        raise RuntimeError, 'Larch was not loaded from a JAR file'

    jar = JarInputStream(_larchJarURL.openStream())

    entry = jar.getNextJarEntry()
    while entry is not None:
        name = entry.getName()

        for handler in _jarEntryHandlers:
            if handler.test_fn(name):

                def _reader():
                    return FileUtil.readBytes(jar)

                handler.handle_fn(name, _reader)

        entry = jar.getNextJarEntry()
Ejemplo n.º 3
0
 def copy_jars(self, jars):
     """Consumes a sequence of jar paths, fixing up paths as necessary"""
     seen = set()
     for jar_path in jars:
         normed_path = os.path.realpath(os.path.normpath(jar_path))
         if os.path.splitext(normed_path)[1] != ".jar":
             log.warn("Will only copy jars, not %s", normed_path)
             next
         if normed_path in seen:
             next
         seen.add(normed_path)
         log.debug("Copying %s", normed_path)
         with open(normed_path) as f:
             with closing(JarInputStream(f)) as input_jar:
                 self.copy_zip_input_stream(input_jar)
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()
Ejemplo n.º 5
0
    def addJarFile(self, jarFile):
        __doc__ = """if you want to add a .jar file with a MANIFEST, add it first"""
        jarJarFile = JarFile(jarFile)
        self.addManifest(jarJarFile.getManifest())
        jarJarFile.close()

        jarInputStream = JarInputStream(FileInputStream(jarFile))
        jarEntry = jarInputStream.getNextJarEntry()
        while jarEntry:
            self.getJarOutputStream().putNextEntry(jarEntry)
            buffer = jarray.zeros(self._bufsize, 'b')
            read = jarInputStream.read(buffer)
            while read <> -1:
                self.getJarOutputStream().write(buffer, 0, read)
                read = jarInputStream.read(buffer)
            self.getJarOutputStream().closeEntry()
            jarEntry = jarInputStream.getNextJarEntry()
Ejemplo n.º 6
0
Archivo: alter.py Proyecto: ofek/jpype
# Utility used to alter a Java jar file to date it in the future.
# This was needed to intentionally trigger a version error.
import jpype
import jpype.imports
jpype.startJVM()

from java.util.jar import JarOutputStream
from java.util.jar import JarInputStream
from java.util.jar import JarFile
from java.util.zip import CRC32
from java.io import File
from java.io import FileInputStream
from java.io import FileOutputStream

jar = JarInputStream(FileInputStream(File("build/lib/org.jpype.jar")))
manifest = jar.getManifest()
target = JarOutputStream(FileOutputStream(File("build/lib/org.jpype2.jar")),
                         manifest)

while 1:
    entry = jar.getNextEntry()
    if not entry:
        break
    out = []
    l3 = 512
    while 1:
        bt = jpype.JArray(jpype.JByte)(l3)
        l = jar.read(bt, 0, l3)
        if l == -1:
            break
        out.append((l, bt))
Ejemplo n.º 7
0
			visitClass(klass)
		except ClassNotFoundException e):
			raise RuntimeException(e)
		}
	}
	
	private void visitClassesInJar( URL url):
		try (InputStream stream = url.openStream()):
			visitClassesInJarInputStream(stream)
		} catch(IOException e):
			raise RuntimeException(e)
		}
	}
	
	private void visitClassesInJarInputStream( InputStream stream):
		try (JarInputStream jar = new JarInputStream(stream)):
			visitClassesInJarInputStream( jar)
		} catch(IOException e):
			raise RuntimeException(e)
		}
	}

	private void visitClassesInJarInputStream( JarInputStream jar):
		try:
			JarEntry entry = jar.getNextJarEntry()
			while(entry is not None):
				visitClassInJarEntry( entry)
				entry = jar.getNextJarEntry()
			}
		} catch(IOException e):
			raise RuntimeException(e)