当前位置: 首页>>代码示例>>Python>>正文


Python Shader.assemble_binary_rules方法代码示例

本文整理汇总了Python中pants.backend.jvm.subsystems.shader.Shader.assemble_binary_rules方法的典型用法代码示例。如果您正苦于以下问题:Python Shader.assemble_binary_rules方法的具体用法?Python Shader.assemble_binary_rules怎么用?Python Shader.assemble_binary_rules使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pants.backend.jvm.subsystems.shader.Shader的用法示例。


在下文中一共展示了Shader.assemble_binary_rules方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: ShaderTest

# 需要导入模块: from pants.backend.jvm.subsystems.shader import Shader [as 别名]
# 或者: from pants.backend.jvm.subsystems.shader.Shader import assemble_binary_rules [as 别名]
class ShaderTest(unittest.TestCase):
  def setUp(self):
    self.jarjar = '/not/really/jarjar.jar'
    with subsystem_instance(DistributionLocator):
      executor = SubprocessExecutor(DistributionLocator.cached())
      self.shader = Shader(jarjar=self.jarjar, executor=executor)
    self.output_jar = '/not/really/shaded.jar'

  def populate_input_jar(self, *entries):
    fd, input_jar_path = tempfile.mkstemp()
    os.close(fd)
    self.addCleanup(safe_delete, input_jar_path)
    with open_zip(input_jar_path, 'w') as jar:
      for entry in entries:
        jar.writestr(entry, '0xCAFEBABE')
    return input_jar_path

  def test_assemble_default_rules(self):
    input_jar = self.populate_input_jar('org/pantsbuild/tools/fake/Main.class',
                                        'com/google/common/base/Function.class')

    rules = self.shader.assemble_binary_rules('org.pantsbuild.tools.fake.Main', input_jar)

    self.assertEqual(Shader.exclude_package('org.pantsbuild.tools.fake'), rules[0])
    self.assertIn(Shader.exclude_package('javax.annotation'), rules[1:-1])
    self.assertEqual(Shader.shade_package('com.google.common.base'), rules[-1])

  def test_assemble_default_rules_default_package(self):
    input_jar = self.populate_input_jar('main.class', 'com/google/common/base/Function.class')

    rules = self.shader.assemble_binary_rules('main', input_jar)

    self.assertEqual(Shader.exclude_package(), rules[0])
    self.assertIn(Shader.exclude_package('javax.annotation'), rules[1:-1])
    self.assertEqual(Shader.shade_package('com.google.common.base'), rules[-1])

  def test_assemble_custom_rules(self):
    input_jar = self.populate_input_jar('main.class')

    rules = self.shader.assemble_binary_rules('main', input_jar,
                                              custom_rules=[Shader.shade_class('bob'),
                                                            Shader.exclude_class('fred')])

    self.assertEqual(Shader.shade_class('bob'), rules[0])
    self.assertEqual(Shader.exclude_class('fred'), rules[1])
    self.assertEqual(Shader.exclude_package(), rules[2])
    self.assertIn(Shader.exclude_package('javax.annotation'), rules[3:])

  def test_runner_command(self):
    input_jar = self.populate_input_jar('main.class', 'com/google/common/base/Function.class')
    custom_rules = [Shader.exclude_package('log4j', recursive=True)]

    with self.shader.binary_shader(self.output_jar, 'main', input_jar,
                                   custom_rules=custom_rules) as shader:
      command = shader.command

      self.assertTrue(command.pop(0).endswith('java'))

      jar_or_cp = command.pop(0)
      self.assertIn(jar_or_cp, {'-cp', 'classpath', '-jar'})
      self.assertEqual(self.jarjar, os.path.abspath(command.pop(0)))

      if jar_or_cp != '-jar':
        # We don't really care what the name of the jarjar main class is - shader.command[2]
        command.pop(0)

      self.assertEqual('process', command.pop(0))

      rules_file = command.pop(0)
      self.assertTrue(os.path.exists(rules_file))
      with open(rules_file) as fp:
        lines = fp.read().splitlines()
        self.assertEqual('rule log4j.** [email protected]', lines[0])  # The custom rule.
        self.assertEqual('rule * @1', lines[1])  # Exclude main's package.
        self.assertIn('rule javax.annotation.* [email protected]', lines)  # Exclude system.
        self.assertEqual('rule com.google.common.base.* {}[email protected]'
                         .format(Shading.SHADE_PREFIX), lines[-1])  # Shade the rest.

      self.assertEqual(input_jar, command.pop(0))
      self.assertEqual(self.output_jar, command.pop(0))

  def test_sanitize_package_name(self):
    def assert_sanitize(name, sanitized):
      self.assertEqual(sanitized, Shading.Relocate._sanitize_package_name(name))

    assert_sanitize('hello', 'hello')
    assert_sanitize('hello.goodbye', 'hello.goodbye')
    assert_sanitize('.hello.goodbye', 'hello.goodbye')
    assert_sanitize('hello.goodbye.', 'hello.goodbye')
    assert_sanitize('123', '_123')
    assert_sanitize('123.456', '_123._456')
    assert_sanitize('123.v2', '_123.v2')
    assert_sanitize('hello-goodbye', 'hello_goodbye')
    assert_sanitize('hello-/.goodbye.?', 'hello__.goodbye._')
    assert_sanitize('one.two..three....four.', 'one.two.three.four')

  def test_infer_shaded_pattern(self):
    def assert_inference(from_pattern, prefix, to_pattern):
      result = ''.join(Shading.Relocate._infer_shaded_pattern_iter(from_pattern, prefix))
      self.assertEqual(to_pattern, result)
#.........这里部分代码省略.........
开发者ID:Gabriel439,项目名称:pants,代码行数:103,代码来源:test_shader.py

示例2: ShaderTest

# 需要导入模块: from pants.backend.jvm.subsystems.shader import Shader [as 别名]
# 或者: from pants.backend.jvm.subsystems.shader.Shader import assemble_binary_rules [as 别名]
class ShaderTest(unittest.TestCase):
  def setUp(self):
    self.jarjar = '/not/really/jarjar.jar'
    init_subsystem(DistributionLocator)
    executor = SubprocessExecutor(DistributionLocator.cached())
    self.shader = Shader(jarjar_classpath=[self.jarjar], executor=executor)
    self.output_jar = '/not/really/shaded.jar'

  def populate_input_jar(self, *entries):
    fd, input_jar_path = tempfile.mkstemp()
    os.close(fd)
    self.addCleanup(safe_delete, input_jar_path)
    with open_zip(input_jar_path, 'w') as jar:
      for entry in entries:
        jar.writestr(entry, '0xCAFEBABE')
    return input_jar_path

  def test_assemble_default_rules(self):
    input_jar = self.populate_input_jar('org/pantsbuild/tools/fake/Main.class',
                                        'com/google/common/base/Function.class')

    rules = self.shader.assemble_binary_rules('org.pantsbuild.tools.fake.Main', input_jar)

    self.assertEqual(Shader.exclude_package('org.pantsbuild.tools.fake'), rules[0])
    self.assertIn(Shader.exclude_package('javax.annotation'), rules[1:-1])
    self.assertEqual(Shader.shade_package('com.google.common.base'), rules[-1])

  def test_assemble_default_rules_default_package(self):
    input_jar = self.populate_input_jar('main.class', 'com/google/common/base/Function.class')

    rules = self.shader.assemble_binary_rules('main', input_jar)

    self.assertEqual(Shader.exclude_package(), rules[0])
    self.assertIn(Shader.exclude_package('javax.annotation'), rules[1:-1])
    self.assertEqual(Shader.shade_package('com.google.common.base'), rules[-1])

  def test_assemble_custom_rules(self):
    input_jar = self.populate_input_jar('main.class')

    rules = self.shader.assemble_binary_rules('main', input_jar,
                                              custom_rules=[Shader.shade_class('bob'),
                                                            Shader.exclude_class('fred')])

    self.assertEqual(Shader.shade_class('bob'), rules[0])
    self.assertEqual(Shader.exclude_class('fred'), rules[1])
    self.assertEqual(Shader.exclude_package(), rules[2])
    self.assertIn(Shader.exclude_package('javax.annotation'), rules[3:])

  def test_runner_command(self):
    input_jar = self.populate_input_jar('main.class', 'com/google/common/base/Function.class')
    custom_rules = [Shader.exclude_package('log4j', recursive=True)]

    with self.shader.binary_shader(self.output_jar, 'main', input_jar,
                                   custom_rules=custom_rules) as shader:
      command = shader.command

      self.assertTrue(command.pop(0).endswith('java'))

      jar_or_cp = command.pop(0)
      self.assertIn(jar_or_cp, {'-cp', 'classpath', '-jar'})
      self.assertEqual(self.jarjar, os.path.abspath(command.pop(0)))

      if jar_or_cp != '-jar':
        # We don't really care what the name of the jarjar main class is - shader.command[2]
        command.pop(0)

      self.assertEqual('process', command.pop(0))

      rules_file = command.pop(0)
      self.assertTrue(os.path.exists(rules_file))
      with open(rules_file) as fp:
        lines = fp.read().splitlines()
        self.assertEqual('rule log4j.** [email protected]', lines[0])  # The custom rule.
        self.assertEqual('rule * @1', lines[1])  # Exclude main's package.
        self.assertIn('rule javax.annotation.* [email protected]', lines)  # Exclude system.
        self.assertEqual('rule com.google.common.base.* {}[email protected]'
                         .format(Shading.SHADE_PREFIX), lines[-1])  # Shade the rest.

      self.assertEqual(input_jar, command.pop(0))
      self.assertEqual(self.output_jar, command.pop(0))

  def test_infer_shaded_pattern(self):
    def assert_inference(from_pattern, prefix, to_pattern):
      result = ''.join(RelocateRule._infer_shaded_pattern_iter(from_pattern, prefix))
      self.assertEqual(to_pattern, result)

    assert_inference('com.foo.bar.Main', None, 'com.foo.bar.Main')
    assert_inference('com.foo.bar.', None, 'com.foo.bar.')
    assert_inference('com.foo.bar.', '__prefix__.', '__prefix__.com.foo.bar.')
    assert_inference('com.*.bar.', None, '[email protected]')
    assert_inference('com.*.bar.*.', None, '[email protected]@2.')
    assert_inference('com.*.bar.**', None, '[email protected]@2')
    assert_inference('*', None, '@1')
    assert_inference('**', None, '@1')
    assert_inference('**', '__prefix__.', '[email protected]')

  def test_shading_exclude(self):
    def assert_exclude(from_pattern, to_pattern):
      self.assertEqual((from_pattern, to_pattern), Shading.create_exclude(from_pattern))

#.........这里部分代码省略.........
开发者ID:CaitieM20,项目名称:pants,代码行数:103,代码来源:test_shader.py

示例3: ShaderTest

# 需要导入模块: from pants.backend.jvm.subsystems.shader import Shader [as 别名]
# 或者: from pants.backend.jvm.subsystems.shader.Shader import assemble_binary_rules [as 别名]
class ShaderTest(unittest.TestCase):
    def setUp(self):
        self.jarjar = "/not/really/jarjar.jar"
        with subsystem_instance(DistributionLocator):
            executor = SubprocessExecutor(DistributionLocator.cached())
            self.shader = Shader(jarjar_classpath=[self.jarjar], executor=executor)
        self.output_jar = "/not/really/shaded.jar"

    def populate_input_jar(self, *entries):
        fd, input_jar_path = tempfile.mkstemp()
        os.close(fd)
        self.addCleanup(safe_delete, input_jar_path)
        with open_zip(input_jar_path, "w") as jar:
            for entry in entries:
                jar.writestr(entry, "0xCAFEBABE")
        return input_jar_path

    def test_assemble_default_rules(self):
        input_jar = self.populate_input_jar(
            "org/pantsbuild/tools/fake/Main.class", "com/google/common/base/Function.class"
        )

        rules = self.shader.assemble_binary_rules("org.pantsbuild.tools.fake.Main", input_jar)

        self.assertEqual(Shader.exclude_package("org.pantsbuild.tools.fake"), rules[0])
        self.assertIn(Shader.exclude_package("javax.annotation"), rules[1:-1])
        self.assertEqual(Shader.shade_package("com.google.common.base"), rules[-1])

    def test_assemble_default_rules_default_package(self):
        input_jar = self.populate_input_jar("main.class", "com/google/common/base/Function.class")

        rules = self.shader.assemble_binary_rules("main", input_jar)

        self.assertEqual(Shader.exclude_package(), rules[0])
        self.assertIn(Shader.exclude_package("javax.annotation"), rules[1:-1])
        self.assertEqual(Shader.shade_package("com.google.common.base"), rules[-1])

    def test_assemble_custom_rules(self):
        input_jar = self.populate_input_jar("main.class")

        rules = self.shader.assemble_binary_rules(
            "main", input_jar, custom_rules=[Shader.shade_class("bob"), Shader.exclude_class("fred")]
        )

        self.assertEqual(Shader.shade_class("bob"), rules[0])
        self.assertEqual(Shader.exclude_class("fred"), rules[1])
        self.assertEqual(Shader.exclude_package(), rules[2])
        self.assertIn(Shader.exclude_package("javax.annotation"), rules[3:])

    def test_runner_command(self):
        input_jar = self.populate_input_jar("main.class", "com/google/common/base/Function.class")
        custom_rules = [Shader.exclude_package("log4j", recursive=True)]

        with self.shader.binary_shader(self.output_jar, "main", input_jar, custom_rules=custom_rules) as shader:
            command = shader.command

            self.assertTrue(command.pop(0).endswith("java"))

            jar_or_cp = command.pop(0)
            self.assertIn(jar_or_cp, {"-cp", "classpath", "-jar"})
            self.assertEqual(self.jarjar, os.path.abspath(command.pop(0)))

            if jar_or_cp != "-jar":
                # We don't really care what the name of the jarjar main class is - shader.command[2]
                command.pop(0)

            self.assertEqual("process", command.pop(0))

            rules_file = command.pop(0)
            self.assertTrue(os.path.exists(rules_file))
            with open(rules_file) as fp:
                lines = fp.read().splitlines()
                self.assertEqual("rule log4j.** [email protected]", lines[0])  # The custom rule.
                self.assertEqual("rule * @1", lines[1])  # Exclude main's package.
                self.assertIn("rule javax.annotation.* [email protected]", lines)  # Exclude system.
                self.assertEqual(
                    "rule com.google.common.base.* {}[email protected]".format(Shading.SHADE_PREFIX), lines[-1]
                )  # Shade the rest.

            self.assertEqual(input_jar, command.pop(0))
            self.assertEqual(self.output_jar, command.pop(0))

    def test_infer_shaded_pattern(self):
        def assert_inference(from_pattern, prefix, to_pattern):
            result = "".join(Shading.RelocateRule._infer_shaded_pattern_iter(from_pattern, prefix))
            self.assertEqual(to_pattern, result)

        assert_inference("com.foo.bar.Main", None, "com.foo.bar.Main")
        assert_inference("com.foo.bar.", None, "com.foo.bar.")
        assert_inference("com.foo.bar.", "__prefix__.", "__prefix__.com.foo.bar.")
        assert_inference("com.*.bar.", None, "[email protected]")
        assert_inference("com.*.bar.*.", None, "[email protected]@2.")
        assert_inference("com.*.bar.**", None, "[email protected]@2")
        assert_inference("*", None, "@1")
        assert_inference("**", None, "@1")
        assert_inference("**", "__prefix__.", "[email protected]")

    def test_shading_exclude(self):
        def assert_exclude(from_pattern, to_pattern):
            self.assertEqual((from_pattern, to_pattern), Shading.create_exclude(from_pattern))
#.........这里部分代码省略.........
开发者ID:ahamilton55,项目名称:pants,代码行数:103,代码来源:test_shader.py


注:本文中的pants.backend.jvm.subsystems.shader.Shader.assemble_binary_rules方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。