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 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()
def __get_manifest(self, source_path, from_archive): """ Returns the manifest object for the specified path. The source path may be a jar, or an exploded path. :param source_path: the source path to be checked :param from_archive: if True, use the manifest from the archive, otherwise from the file system :return: the manifest, or None if it is not present :raises: IOException: if there are problems reading an existing manifest """ manifest = None if string_utils.is_empty(source_path): return manifest source_path = self.model_context.replace_token_string(source_path) if from_archive and deployer_utils.is_path_into_archive(source_path): return self.archive_helper.get_manifest(source_path) else: if not os.path.isabs(source_path): # if this was in archive, it has been expanded under domain home. # or it may be a relative file intentionally placed under domain home. source_file = File(File(self.model_context.get_domain_home()), source_path) else: source_file = File(source_path) if source_file.isDirectory(): # read the manifest directly from the file system manifest_file = File(source_file, MANIFEST_NAME) if manifest_file.exists(): stream = None try: stream = FileInputStream(manifest_file) manifest = Manifest(stream) finally: if stream is not None: try: stream.close() except IOException: # nothing to report pass else: # read the manifest from the deployable ear/jar/war on the file system archive = JarFile(source_file.getAbsolutePath()) manifest = archive.getManifest() return manifest
def addManifestFile(self, manifestFile): __doc__ = """only one manifest file can be added""" self.addManifest(Manifest(FileInputStream(manifestFile)))