Beispiel #1
0
 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))
Beispiel #2
0
 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))
Beispiel #3
0
 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())
Beispiel #4
0
 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())
Beispiel #5
0
 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'))
Beispiel #6
0
 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'))
Beispiel #7
0
  def monolithic_jar(self, binary, path, with_external_deps):
    """Creates a jar containing the class files for a jvm_binary target and all its deps.

    Yields a handle to the open jarfile, so the caller can add to the jar if needed.

    :param binary: The jvm_binary target to operate on.
    :param path: Write the output jar here, overwriting an existing file, if any.
    :param with_external_deps: If True, unpack external jar deps and add their classes to the jar.
    """
    # TODO(benjy): There's actually nothing here that requires 'binary' to be a jvm_binary.
    # It could be any target. And that might actually be useful.
    # TODO(benjy): Get the classfiles directly from the class products, instead of unpacking
    # the per-target jars?

    jarmap = self.context.products.get('jars')

    with open_jar(path, 'w', compression=zipfile.ZIP_DEFLATED) as jar:
      def add_jars(target):
        generated = jarmap.get(target)
        if generated:
          for base_dir, jars in generated.items():
            for internal_jar in jars:
              self._dump(os.path.join(base_dir, internal_jar), jar)

      with self.context.new_workunit(name='add-generated-jars'):
        binary.walk(add_jars)

      if with_external_deps:
        with self.context.new_workunit(name='add-dependency-jars'):
          for basedir, external_jar in self.list_jar_dependencies(binary):
            self._dump(os.path.join(basedir, external_jar), jar)

      yield jar
Beispiel #8
0
 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
Beispiel #9
0
 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
Beispiel #10
0
 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())
Beispiel #11
0
 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 _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))
Beispiel #13
0
  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' % (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])
Beispiel #14
0
    def monolithic_jar(self, binary, path, with_external_deps):
        """Creates a jar containing the class files for a jvm_binary target and all its deps.

    Yields a handle to the open jarfile, so the caller can add to the jar if needed.

    :param binary: The jvm_binary target to operate on.
    :param path: Write the output jar here, overwriting an existing file, if any.
    :param with_external_deps: If True, unpack external jar deps and add their classes to the jar.
    """
        # TODO(benjy): There's actually nothing here that requires 'binary' to be a jvm_binary.
        # It could be any target. And that might actually be useful.
        # TODO(benjy): Get the classfiles directly from the class products, instead of unpacking
        # the per-target jars?

        jarmap = self.context.products.get('jars')

        with open_jar(path, 'w', compression=zipfile.ZIP_DEFLATED) as jar:

            def add_jars(target):
                generated = jarmap.get(target)
                if generated:
                    for base_dir, jars in generated.items():
                        for internal_jar in jars:
                            self._dump(os.path.join(base_dir, internal_jar),
                                       jar)

            with self.context.new_workunit(name='add-generated-jars'):
                binary.walk(add_jars)

            if with_external_deps:
                with self.context.new_workunit(name='add-dependency-jars'):
                    for basedir, external_jar in self.list_jar_dependencies(
                            binary):
                        self._dump(os.path.join(basedir, external_jar), jar)

            yield jar
Beispiel #15
0
 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())
Beispiel #16
0
 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())