def assert_writestr(path, contents, *entries): with self.jarfile() as jarfile: with open_jar(jarfile, 'w') as jar: jar.writestr(path, contents) with open_jar(jarfile) as jar: self.assertEquals(list(entries), jar.namelist()) self.assertEquals(contents, jar.read(path))
def test_write_dir(self): with temporary_dir() as chroot: dir = os.path.join(chroot, 'a/b/c') safe_mkdir(dir) with self.jarfile() as jarfile: with open_jar(jarfile, 'w') as jar: jar.write(dir, 'd/e') with open_jar(jarfile) as jar: self.assertEquals(['d/', 'd/e/'], jar.namelist())
def test_write_file(self): with temporary_dir() as chroot: dir = os.path.join(chroot, 'a/b/c') safe_mkdir(dir) data_file = os.path.join(dir, 'd.txt') with open(data_file, 'w') as fd: fd.write('e') with self.jarfile() as jarfile: with open_jar(jarfile, 'w') as jar: jar.write(data_file, 'f/g/h') with open_jar(jarfile) as jar: self.assertEquals(['f/', 'f/g/', 'f/g/h'], jar.namelist()) self.assertEquals('e', jar.read('f/g/h'))
def create_binary(self, binary): import platform safe_mkdir(self.outdir) jarmap = self.context.products.get('jars') binary_jarname = '%s.jar' % binary.basename binaryjarpath = os.path.join(self.outdir, binary_jarname) self.context.log.info('creating %s' % os.path.relpath(binaryjarpath, get_buildroot())) with open_jar(binaryjarpath, 'w', compression=self.compression, allowZip64=self.zip64) as jar: def add_jars(target): generated = jarmap.get(target) if generated: for basedir, jars in generated.items(): for internaljar in jars: self.dump(os.path.join(basedir, internaljar), jar) binary.walk(add_jars, lambda t: t.is_internal) if self.deployjar: for basedir, externaljar in self.list_jar_dependencies(binary): self.dump(os.path.join(basedir, externaljar), jar) manifest = Manifest() manifest.addentry(Manifest.MANIFEST_VERSION, '1.0') manifest.addentry( Manifest.CREATED_BY, 'python %s pants %s (Twitter, Inc.)' % (platform.python_version(), get_version()) ) main = binary.main or '*** java -jar not supported, please use -cp and pick a main ***' manifest.addentry(Manifest.MAIN_CLASS, main) jar.writestr(Manifest.PATH, manifest.contents()) jarmap.add(binary, self.outdir, [binary_jarname])
def create_jar(self, target, path): existing = self._jars.setdefault(path, target) if target != existing: raise TaskError( 'Duplicate name: target %s tried to write %s already mapped to target %s' % (target, path, existing)) self._jars[path] = target with open_jar(path, 'w', compression=self.compression) as jar: yield jar
def create_jar(self, target, path): existing = self._jars.setdefault(path, target) if target != existing: raise TaskError('Duplicate name: target %s tried to write %s already mapped to target %s' % ( target, path, existing )) self._jars[path] = target with open_jar(path, 'w', compression=self.compression) as jar: yield jar
def assert_jar_contents(self, context, product_type, target, *contents): jar_mapping = context.products.get(product_type).get(target) self.assertEqual(1, len(jar_mapping)) for basedir, jars in jar_mapping.items(): self.assertEqual(1, len(jars)) with open_jar(os.path.join(basedir, jars[0])) as jar: self.assertEqual(list(contents), jar.namelist()) for content in contents: if not content.endswith('/'): with closing(jar.open(content)) as fp: self.assertEqual(os.path.basename(content), fp.read())
def create_binary(self, binary): import platform safe_mkdir(self.outdir) jarmap = self.context.products.get('jars') binary_jarname = '%s.jar' % binary.basename binaryjarpath = os.path.join(self.outdir, binary_jarname) self.context.log.info('creating %s' % os.path.relpath(binaryjarpath, get_buildroot())) with open_jar(binaryjarpath, 'w', compression=self.compression, allowZip64=self.zip64) as jar: def add_jars(target): generated = jarmap.get(target) if generated: for basedir, jars in generated.items(): for internaljar in jars: self.dump(os.path.join(basedir, internaljar), jar) binary.walk(add_jars, lambda t: t.is_internal) if self.deployjar: for basedir, externaljar in self.list_jar_dependencies(binary): self.dump(os.path.join(basedir, externaljar), jar) def write_binary_data(product_type): data = self.context.products.get_data(product_type).get(binary) if data: for root, rel_paths in data.rel_paths(): for rel_path in rel_paths: jar.write(os.path.join(root, rel_path), arcname=rel_path) write_binary_data('classes_by_target') write_binary_data('resources_by_target') manifest = Manifest() manifest.addentry(Manifest.MANIFEST_VERSION, '1.0') manifest.addentry( Manifest.CREATED_BY, 'python %s pants %s (Twitter, Inc.)' % (platform.python_version(), get_version())) main = binary.main or '*** java -jar not supported, please use -cp and pick a main ***' manifest.addentry(Manifest.MAIN_CLASS, main) jar.writestr(Manifest.PATH, manifest.contents()) jarmap.add(binary, self.outdir, [binary_jarname])
def _addargs(self, lines, jarfile, target): def is_configurationinfo(line): line = line.strip() return line and not line.startswith('#') if any(filter(is_configurationinfo, lines)): resource = os.path.join(RESOURCE_RELDIR, '%s.%d' % (RESOURCE_BASENAME, self.resource_index)) content = '# Created by pants goal args-apt\n' content += ''.join(sorted(lines)) with open_jar(jarfile, 'a') as jar: jar.writestr(resource, content) self.context.log.debug('Added args-apt resource file %s for %s:' '\n%s' % (resource, target, content))
def _addargs(self, lines, jarfile, target): def is_configurationinfo(line): line = line.strip() return line and not line.startswith('#') if any(filter(is_configurationinfo, lines)): resource = os.path.join( RESOURCE_RELDIR, '%s.%d' % (RESOURCE_BASENAME, self.resource_index)) content = '# Created by pants goal args-apt\n' content += ''.join(sorted(lines)) with open_jar(jarfile, 'a') as jar: jar.writestr(resource, content) self.context.log.debug( 'Added args-apt resource file %s for %s:' '\n%s' % (resource, target, content))
def assert_mkdirs(path, *entries): with self.jarfile() as jarfile: with open_jar(jarfile, 'w') as jar: jar.mkdirs(path) with open_jar(jarfile) as jar: self.assertEquals(list(entries), jar.namelist())