class OutputJar(object): # Derived, with heavy modifications, from # http://stackoverflow.com/questions/1281229/how-to-use-jaroutputstream-to-create-a-jar-file def __init__(self, jar=None, output_path="output.jar"): self.output_path = output_path if jar is not None: self.jar = jar self.output = None return self.runpy = None self.setup() def __enter__(self): return self def __exit__(self, type, value, tb): self.close() def setup(self): manifest = Manifest() manifest.getMainAttributes()[Attributes.Name.MANIFEST_VERSION] = "1.0" if self.runpy and os.path.exists(self.runpy): manifest.getMainAttributes()[ Attributes.Name.MAIN_CLASS] = "org.python.util.JarRunner" else: log.debug( "No __run__.py defined, so defaulting to Jython command line") manifest.getMainAttributes()[ Attributes.Name.MAIN_CLASS] = "org.python.util.jython" self.output = open(self.output_path, "wb") self.jar = JarOutputStream(self.output, manifest) self.created_paths = set() self.build_time = int(time.time() * 1000) def close(self): self.jar.close() if self.output: self.output.close() def create_ancestry(self, path_parts): for i in xrange(len(path_parts), 0, -1): # right to left ancestor = "/".join(path_parts[:-i]) + "/" if ancestor == "/": continue # FIXME shouldn't need to do this special casing if ancestor not in self.created_paths: entry = JarEntry(ancestor) entry.time = self.build_time try: self.jar.putNextEntry(entry) self.jar.closeEntry() except ZipException, e: if not "duplicate entry" in str(e): log.error("Problem in creating entry %r", entry, exc_info=True) raise self.created_paths.add(ancestor)
class OutputJar(object): # Derived, with heavy modifications, from # http://stackoverflow.com/questions/1281229/how-to-use-jaroutputstream-to-create-a-jar-file def __init__(self, jar=None, output_path="output.jar"): self.output_path = output_path if jar is not None: self.jar = jar self.output = None return self.runpy = None self.setup() def __enter__ (self): return self def __exit__ (self, type, value, tb): self.close() def setup(self): manifest = Manifest() manifest.getMainAttributes()[Attributes.Name.MANIFEST_VERSION] = "1.0" if self.runpy and os.path.exists(self.runpy): manifest.getMainAttributes()[Attributes.Name.MAIN_CLASS] = "org.python.util.JarRunner" else: log.debug("No __run__.py defined, so defaulting to Jython command line") manifest.getMainAttributes()[Attributes.Name.MAIN_CLASS] = "org.python.util.jython" self.output = open(self.output_path, "wb") self.jar = JarOutputStream(self.output, manifest) self.created_paths = set() self.build_time = int(time.time() * 1000) def close(self): self.jar.close() if self.output: self.output.close() def create_ancestry(self, path_parts): for i in xrange(len(path_parts), 0, -1): # right to left ancestor = "/".join(path_parts[:-i]) + "/" if ancestor == "/": continue # FIXME shouldn't need to do this special casing if ancestor not in self.created_paths: entry = JarEntry(ancestor) entry.time = self.build_time try: self.jar.putNextEntry(entry) self.jar.closeEntry() except ZipException, e: if not "duplicate entry" in str(e): log.error("Problem in creating entry %r", entry, exc_info=True) raise self.created_paths.add(ancestor)
def produce_jar(outdir, jarname): """ Produce a jar from a directory This function does not use the 'jar' utility, so it does work on the JRE. """ fout = FileOutputStream(jarname) jarOut = JarOutputStream(fout) add_to_jar(jarOut, outdir) jarOut.close() fout.close() return jarname
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)) if out: out[0][1][7] = 57 crc = CRC32() for v in out: crc.update(v[1], 0, v[0]) entry.setCrc(crc.getValue()) entry.setCompressedSize(-1) target.putNextEntry(entry) for v in out: target.write(v[1], 0, v[0]) target.closeEntry() target.close()