def register_options(cls, register): super(JUnit, cls).register_options(register) cls.register_jvm_tool(register, 'junit_library', classpath=[ cls.LIBRARY_JAR, ]) cls.register_jvm_tool(register, 'junit', classpath=[ cls._RUNNER_JAR, ], main=cls.RUNNER_MAIN, # TODO(John Sirois): Investigate how much less we can get away with. # Clearly both tests and the runner need access to the same @Test, # @Before, as well as other annotations, but there is also the Assert # class and some subset of the @Rules, @Theories and @RunWith APIs. custom_rules=[ Shader.exclude_package('junit.framework', recursive=True), Shader.exclude_package('org.junit', recursive=True), Shader.exclude_package('org.hamcrest', recursive=True), Shader.exclude_package('org.pantsbuild.junit.annotations', recursive=True), ])
def register_options(cls, register): super(JUnit, cls).register_options(register) cls.register_jvm_tool(register, 'junit_library', classpath=[ cls.LIBRARY_JAR, ]) cls.register_jvm_tool(register, 'junit', classpath=[ cls._RUNNER_JAR, ], main=cls.RUNNER_MAIN, # TODO(John Sirois): Investigate how much less we can get away with. # Clearly both tests and the runner need access to the same @Test, # @Before, as well as other annotations, but there is also the Assert # class and some subset of the @Rules, @Theories and @RunWith APIs. custom_rules=[ Shader.exclude_package('junit.framework', recursive=True), Shader.exclude_package('org.junit', recursive=True), Shader.exclude_package('org.hamcrest', recursive=True), Shader.exclude_package('org.pantsbuild.junit.annotations', recursive=True), ])
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 register_options(cls, register): super(Zinc.Factory, cls).register_options(register) zinc_rev = '1.0.3' shader_rules = [ # The compiler-interface and compiler-bridge tool jars carry xsbt and # xsbti interfaces that are used across the shaded tool jar boundary so # we preserve these root packages wholesale along with the core scala # APIs. Shader.exclude_package('scala', recursive=True), Shader.exclude_package('xsbt', recursive=True), Shader.exclude_package('xsbti', recursive=True), # Unfortunately, is loaded reflectively by the compiler. Shader.exclude_package('org.apache.logging.log4j', recursive=True), ] cls.register_jvm_tool(register, Zinc.ZINC_COMPILER_TOOL_NAME, classpath=[ JarDependency('org.pantsbuild', 'zinc-compiler_2.11', '0.0.5'), ], main=Zinc.ZINC_COMPILE_MAIN, custom_rules=shader_rules) cls.register_jvm_tool(register, 'compiler-bridge', classpath=[ ScalaJarDependency( org='org.scala-sbt', name='compiler-bridge', rev=zinc_rev, classifier='sources', intransitive=True), ]) cls.register_jvm_tool( register, 'compiler-interface', classpath=[ JarDependency(org='org.scala-sbt', name='compiler-interface', rev=zinc_rev), ], # NB: We force a noop-jarjar'ing of the interface, since it is now # broken up into multiple jars, but zinc does not yet support a sequence # of jars for the interface. main='no.such.main.Main', custom_rules=shader_rules) cls.register_jvm_tool(register, Zinc.ZINC_EXTRACTOR_TOOL_NAME, classpath=[ JarDependency('org.pantsbuild', 'zinc-extractor_2.11', '0.0.4') ])
def register_options(cls, register): super(JUnitRun, cls).register_options(register) register('--batch-size', advanced=True, type=int, default=sys.maxint, help='Run at most this many tests in a single test process.') register('--test', type=list, help='Force running of just these tests. Tests can be specified using any of: ' '[classname], [classname]#[methodname], [filename] or [filename]#[methodname]') register('--per-test-timer', type=bool, help='Show progress and timer for each test.') register('--default-concurrency', advanced=True, choices=junit_tests.VALID_CONCURRENCY_OPTS, default=junit_tests.CONCURRENCY_SERIAL, help='Set the default concurrency mode for running tests not annotated with' ' @TestParallel or @TestSerial.') register('--default-parallel', advanced=True, type=bool, removal_hint='Use --concurrency instead.', removal_version='1.1.0', help='Run classes without @TestParallel or @TestSerial annotations in parallel.') register('--parallel-threads', advanced=True, type=int, default=0, help='Number of threads to run tests in parallel. 0 for autoset.') register('--test-shard', advanced=True, help='Subset of tests to run, in the form M/N, 0 <= M < N. ' 'For example, 1/3 means run tests number 2, 5, 8, 11, ...') register('--output-mode', choices=['ALL', 'FAILURE_ONLY', 'NONE'], default='NONE', help='Specify what part of output should be passed to stdout. ' 'In case of FAILURE_ONLY and parallel tests execution ' 'output can be partial or even wrong. ' 'All tests output also redirected to files in .pants.d/test/junit.') register('--cwd', advanced=True, help='Set the working directory. If no argument is passed, use the build root. ' 'If cwd is set on a target, it will supersede this argument.') register('--strict-jvm-version', type=bool, advanced=True, help='If true, will strictly require running junits with the same version of java as ' 'the platform -target level. Otherwise, the platform -target level will be ' 'treated as the minimum jvm to run.') register('--failure-summary', type=bool, default=True, help='If true, includes a summary of which test-cases failed at the end of a failed ' 'junit run.') register('--allow-empty-sources', type=bool, advanced=True, help='Allows a junit_tests() target to be defined with no sources. Otherwise,' 'such a target will raise an error during the test run.') register('--use-experimental-runner', type=bool, advanced=True, help='Use experimental junit-runner logic for more options for parallelism.') cls.register_jvm_tool(register, 'junit', classpath=[ JarDependency(org='org.pantsbuild', name='junit-runner', rev='1.0.7'), ], main=JUnitRun._MAIN, # TODO(John Sirois): Investigate how much less we can get away with. # Clearly both tests and the runner need access to the same @Test, # @Before, as well as other annotations, but there is also the Assert # class and some subset of the @Rules, @Theories and @RunWith APIs. custom_rules=[ Shader.exclude_package('junit.framework', recursive=True), Shader.exclude_package('org.junit', recursive=True), Shader.exclude_package('org.hamcrest', recursive=True), Shader.exclude_package('org.pantsbuild.junit.annotations', recursive=True), ]) # TODO: Yuck, but will improve once coverage steps are in their own tasks. for c in [Coverage, Cobertura]: c.register_options(register, cls.register_jvm_tool)
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, binary_package_excludes=['javax']) self.output_jar = '/not/really/shaded.jar'
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_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 register_options(cls, register): super().register_options(register) register('--force-compiler-tag-prefix', default='use-compiler', metavar='<tag>', help='Always compile targets marked with this tag with rsc, unless the workflow is ' 'specified on the cli.') register('--workflow', type=cls.JvmCompileWorkflowType, choices=list(cls.JvmCompileWorkflowType), default=cls.JvmCompileWorkflowType.zinc_only, metavar='<workflow>', help='The default workflow to use to compile JVM targets. This is overriden on a per-target basis with the force-compiler-tag-prefix tag.', fingerprint=True) register('--scala-workflow-override', type=cls.JvmCompileWorkflowType, choices=list(cls.JvmCompileWorkflowType), default=None, metavar='<workflow_override>', help='Experimental option. The workflow to use to compile Scala targets, overriding the "workflow" option as well as any force-compiler-tag-prefix tags applied to targets. An example use case is to quickly turn off outlining workflows in case of errors.', fingerprint=True) register('--extra-rsc-args', type=list, default=[], help='Extra arguments to pass to the rsc invocation.') register('--zinc-outline', type=bool, default=False, help='Outline via Zinc when workflow is outline-and-zinc instead of a standalone scalac tool. This allows outlining to happen in the same nailgun instance as zinc compiles.', fingerprint=True) cls.register_jvm_tool( register, 'rsc', classpath=[ JarDependency( org='com.twitter', name='rsc_2.12', rev='0.0.0-768-7357aa0a', ), ], custom_rules=[ Shader.exclude_package('rsc', recursive=True), ] ) scalac_outliner_version = '2.12.10' cls.register_jvm_tool( register, 'scalac-outliner', classpath=[ JarDependency( org='org.scala-lang', name='scala-compiler', rev=scalac_outliner_version, ), ], custom_rules=[ Shader.exclude_package('scala', recursive=True), Shader.exclude_package('xsbt', recursive=True), Shader.exclude_package('xsbti', recursive=True), # Unfortunately, is loaded reflectively by the compiler. Shader.exclude_package('org.apache.logging.log4j', recursive=True), ] )
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(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_classes_in_meta_inf(self): input_jar = self.populate_input_jar('org/pantsbuild/tools/fake/Main.class', 'META-INF/versions/9/javax/xml/bind/ModuleInfo.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', recursive=True), rules[1:]) self.assertNotIn(Shading.create_relocate('META-INF.versions.9.javax.xml.bind.*'), 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", recursive=True), rules[1:-1]) self.assertEqual(Shader.shade_package("com.google.common.base"), rules[-1])
def register_options(cls, register): super(Zinc.Factory, cls).register_options(register) zinc_rev = '1.0.3' shader_rules = [ # The compiler-interface and compiler-bridge tool jars carry xsbt and # xsbti interfaces that are used across the shaded tool jar boundary so # we preserve these root packages wholesale along with the core scala # APIs. Shader.exclude_package('scala', recursive=True), Shader.exclude_package('xsbt', recursive=True), Shader.exclude_package('xsbti', recursive=True), # Unfortunately, is loaded reflectively by the compiler. Shader.exclude_package('org.apache.logging.log4j', recursive=True), ] cls.register_jvm_tool(register, Zinc.ZINC_COMPILER_TOOL_NAME, classpath=[ JarDependency('org.pantsbuild', 'zinc-compiler_2.11', '0.0.5'), ], main=Zinc.ZINC_COMPILE_MAIN, custom_rules=shader_rules) cls.register_jvm_tool(register, 'compiler-bridge', classpath=[ ScalaJarDependency(org='org.scala-sbt', name='compiler-bridge', rev=zinc_rev, classifier='sources', intransitive=True), ]) cls.register_jvm_tool(register, 'compiler-interface', classpath=[ JarDependency(org='org.scala-sbt', name='compiler-interface', rev=zinc_rev), ], # NB: We force a noop-jarjar'ing of the interface, since it is now # broken up into multiple jars, but zinc does not yet support a sequence # of jars for the interface. main='no.such.main.Main', custom_rules=shader_rules) cls.register_jvm_tool(register, Zinc.ZINC_EXTRACTOR_TOOL_NAME, classpath=[ JarDependency('org.pantsbuild', 'zinc-extractor_2.11', '0.0.4') ])
def register_options_for(jvm_tool_mixin_cls, register, **kwargs): """Register options for the zinc tool in the context of the given JvmToolMixin. TODO: Move into the classmethod after zinc registration has been removed from `zinc_compile` in `1.6.0.dev0`. """ cls = jvm_tool_mixin_cls def sbt_jar(name, **kwargs): return JarDependency(org='org.scala-sbt', name=name, rev='1.0.0-X5', **kwargs) shader_rules = [ # The compiler-interface and compiler-bridge tool jars carry xsbt and # xsbti interfaces that are used across the shaded tool jar boundary so # we preserve these root packages wholesale along with the core scala # APIs. Shader.exclude_package('scala', recursive=True), Shader.exclude_package('xsbt', recursive=True), Shader.exclude_package('xsbti', recursive=True), ] cls.register_jvm_tool(register, 'zinc', classpath=[ JarDependency('org.pantsbuild', 'zinc_2.10', '0.0.5'), ], main=Zinc.ZINC_COMPILE_MAIN, custom_rules=shader_rules, **kwargs) cls.register_jvm_tool(register, 'compiler-bridge', classpath=[ sbt_jar(name='compiler-bridge_2.10', classifier='sources', intransitive=True) ], **kwargs) cls.register_jvm_tool(register, 'compiler-interface', classpath=[ sbt_jar(name='compiler-interface') ], # NB: We force a noop-jarjar'ing of the interface, since it is now broken # up into multiple jars, but zinc does not yet support a sequence of jars # for the interface. main='no.such.main.Main', custom_rules=shader_rules, **kwargs)
def register_options(cls, register): super(ZincCompile, cls).register_options(register) register('--plugins', advanced=True, action='append', fingerprint=True, help='Use these scalac plugins.') register('--plugin-args', advanced=True, type=dict_option, default={}, fingerprint=True, help='Map from plugin name to list of arguments for that plugin.') register('--name-hashing', advanced=True, action='store_true', default=False, fingerprint=True, help='Use zinc name hashing.') cls.register_jvm_tool(register, 'zinc', classpath=[ JarDependency('org.pantsbuild', 'zinc', '1.0.8') ], main=cls._ZINC_MAIN, custom_rules=[ # The compiler-interface and sbt-interface tool jars carry xsbt and # xsbti interfaces that are used across the shaded tool jar boundary so # we preserve these root packages wholesale along with the core scala # APIs. Shader.exclude_package('scala', recursive=True), Shader.exclude_package('xsbt', recursive=True), Shader.exclude_package('xsbti', recursive=True), ]) def sbt_jar(name, **kwargs): return JarDependency(org='com.typesafe.sbt', name=name, rev='0.13.9', **kwargs) cls.register_jvm_tool(register, 'compiler-interface', classpath=[ sbt_jar(name='compiler-interface', classifier='sources', # We just want the single compiler-interface jar and not its # dep on scala-lang intransitive=True) ]) cls.register_jvm_tool(register, 'sbt-interface', classpath=[ sbt_jar(name='sbt-interface', # We just want the single sbt-interface jar and not its dep # on scala-lang intransitive=True) ]) # By default we expect no plugin-jars classpath_spec is filled in by the user, so we accept an # empty classpath. cls.register_jvm_tool(register, 'plugin-jars', classpath=[])
def register_options(cls, register): super(JUnitRun, cls).register_options(register) register('--batch-size', advanced=True, type=int, default=sys.maxint, help='Run at most this many tests in a single test process.') register('--test', action='append', help='Force running of just these tests. Tests can be specified using any of: ' '[classname], [classname]#[methodname], [filename] or [filename]#[methodname]') register('--per-test-timer', action='store_true', help='Show progress and timer for each test.') register('--default-parallel', advanced=True, action='store_true', help='Run classes without @TestParallel or @TestSerial annotations in parallel.') register('--parallel-threads', advanced=True, type=int, default=0, help='Number of threads to run tests in parallel. 0 for autoset.') register('--test-shard', advanced=True, help='Subset of tests to run, in the form M/N, 0 <= M < N. ' 'For example, 1/3 means run tests number 2, 5, 8, 11, ...') register('--suppress-output', action='store_true', default=True, help='Redirect test output to files in .pants.d/test/junit.') register('--cwd', advanced=True, help='Set the working directory. If no argument is passed, use the build root. ' 'If cwd is set on a target, it will supersede this argument.') register('--strict-jvm-version', action='store_true', default=False, advanced=True, help='If true, will strictly require running junits with the same version of java as ' 'the platform -target level. Otherwise, the platform -target level will be ' 'treated as the minimum jvm to run.') register('--failure-summary', action='store_true', default=True, help='If true, includes a summary of which test-cases failed at the end of a failed ' 'junit run.') register('--allow-empty-sources', action='store_true', default=False, advanced=True, help='Allows a junit_tests() target to be defined with no sources. Otherwise,' 'such a target will raise an error during the test run.') cls.register_jvm_tool(register, 'junit', classpath=[ JarDependency(org='org.pantsbuild', name='junit-runner', rev='0.0.12'), ], main=JUnitRun._MAIN, # TODO(John Sirois): Investigate how much less we can get away with. # Clearly both tests and the runner need access to the same @Test, # @Before, as well as other annotations, but there is also the Assert # class and some subset of the @Rules, @Theories and @RunWith APIs. custom_rules=[ Shader.exclude_package('junit.framework', recursive=True), Shader.exclude_package('org.junit', recursive=True), Shader.exclude_package('org.hamcrest', recursive=True), Shader.exclude_package('org.pantsbuild.junit.annotations', recursive=True), ]) # TODO: Yuck, but will improve once coverage steps are in their own tasks. for c in [Coverage, Cobertura]: c.register_options(register, cls.register_jvm_tool)
def register_options(cls, register): super(RscCompile, cls).register_options(register) rsc_toolchain_version = '0.0.0-446-c64e6937' scalameta_toolchain_version = '4.0.0' cls.register_jvm_tool( register, 'rsc', classpath=[ JarDependency( org='com.twitter', name='rsc_2.11', rev=rsc_toolchain_version, ), ], custom_rules=[ Shader.exclude_package('rsc', recursive=True), ] ) cls.register_jvm_tool( register, 'metacp', classpath=[ JarDependency( org='org.scalameta', name='metacp_2.11', rev=scalameta_toolchain_version, ), ], custom_rules=[ Shader.exclude_package('scala', recursive=True), ] ) cls.register_jvm_tool( register, 'metai', classpath=[ JarDependency( org='org.scalameta', name='metai_2.11', rev=scalameta_toolchain_version, ), ], custom_rules=[ Shader.exclude_package('scala', recursive=True), ] )
def register_options(cls, register): super().register_options(register) register('--configuration', advanced=True, type=file_option, fingerprint=True, help='Path to the checkstyle configuration file.') register('--properties', advanced=True, type=dict_with_files_option, default={}, fingerprint=True, help='Dictionary of property mappings to use for checkstyle.properties.') register('--confs', advanced=True, type=list, default=['default'], help='One or more ivy configurations to resolve for this target.') register('--include-user-classpath', type=bool, fingerprint=True, help='Add the user classpath to the checkstyle classpath') cls.register_jvm_tool(register, 'checkstyle', # Note that checkstyle 7.0 does not run on Java 7 runtimes or below. classpath=[ JarDependency(org='com.puppycrawl.tools', name='checkstyle', rev='6.19'), ], main=cls._CHECKSTYLE_MAIN, custom_rules=[ # Checkstyle uses reflection to load checks and has an affordance that # allows leaving off a check classes' package name. This affordance # breaks for built-in checkstyle checks under shading so we ensure all # checkstyle packages are excluded from shading such that just its # third party transitive deps (guava and the like), are shaded. # See the module configuration rules here which describe this: # http://checkstyle.sourceforge.net/config.html#Modules Shader.exclude_package('com.puppycrawl.tools.checkstyle', recursive=True), ])
def register_options(cls, register): super(FindBugs, cls).register_options(register) register('--skip', action='store_true', default=False, fingerprint=True, help='Skip findbugs.') register('--jvm-options', advanced=True, action='append', metavar='<option>...', help='Run findbugs with these extra jvm options.') register('--effort', default='default', choices=['min', 'less', 'default', 'more', 'max'], help='Effort of the bug finders.') register('--threshold', default='medium', choices=['low', 'medium', 'high', 'experimental'], help='Effort of the bug finders.') register('--fail-on-error', action='store_true', help='Fail the build on an error.') register('--max-rank', type=int, help='Maximum bug ranking to record [1..20].') register('--relaxed', action='store_true', default=False, help='Relaxed reporting mode') register('--nested', action='store_true', default=True, help='Analyze nested jar/zip archives') register('--exclude-filter-file', help='Exclude bugs matching given filter') register('--include-filter-file', help='Include only bugs matching given filter') register('--exclude-patterns', action='append', default=[], help='Adds patterns for targets to be excluded from analysis.') cls.register_jvm_tool(register, 'findbugs', classpath=[ JarDependency(org='com.google.code.findbugs', name='findbugs', rev='3.0.1'), ], main=cls._FINDBUGS_MAIN, custom_rules=[ Shader.exclude_package('edu.umd.cs.findbugs', recursive=True), ])
def register_options(cls, register): super(ErrorProne, cls).register_options(register) register('--skip', type=bool, help='Skip Error Prone.') register('--transitive', default=False, type=bool, help='Run Error Prone against transitive dependencies of targets ' 'specified on the command line.') register('--command-line-options', type=list, default=[], fingerprint=True, help='Command line options passed to Error Prone') register('--exclude-patterns', type=list, default=[], fingerprint=True, help='Patterns for targets to be excluded from analysis.') cls.register_jvm_tool(register, 'errorprone', classpath=[ JarDependency(org='com.google.errorprone', name='error_prone_core', rev='2.3.1'), ], main=cls._ERRORPRONE_MAIN, custom_rules=[ Shader.exclude_package('com.google.errorprone', recursive=True) ] ) # The javac version should be kept in sync with the version used by errorprone above. cls.register_jvm_tool(register, 'errorprone-javac', classpath=[ JarDependency(org='com.google.errorprone', name='javac', rev='9+181-r4173-1'), ])
def register_options(cls, register): super(ErrorProne, cls).register_options(register) register('--skip', type=bool, help='Skip Error Prone.') register('--transitive', default=False, type=bool, help='Run Error Prone against transitive dependencies of targets ' 'specified on the command line.') register('--command-line-options', type=list, default=[], fingerprint=True, help='Command line options passed to Error Prone') register('--exclude-patterns', type=list, default=[], fingerprint=True, help='Patterns for targets to be excluded from analysis.') cls.register_jvm_tool(register, 'errorprone', classpath=[ JarDependency(org='com.google.errorprone', name='error_prone_core', rev='2.3.1'), ], main=cls._ERRORPRONE_MAIN, custom_rules=[ Shader.exclude_package('com.google.errorprone', recursive=True) ] ) # The javac version should be kept in sync with the version used by errorprone above. cls.register_jvm_tool(register, 'errorprone-javac', classpath=[ JarDependency(org='com.google.errorprone', name='javac', rev='9+181-r4173-1'), ])
def register_options(cls, register): super(IvyOutdated, cls).register_options(register) register( '--confs', type=list, default=['default'], help='Pass a configuration to ivy in addition to the default ones.' ) register( '--exclude-patterns', type=list, default=[], help= 'Regular expressions matching jars to be excluded from outdated report.' ) cls.register_jvm_tool(register, 'dependency-update-checker', classpath=[ JarDependency( org='org.pantsbuild', name='ivy-dependency-update-checker', rev='0.0.1'), ], main=cls._IVY_DEPENDENCY_UPDATE_MAIN, custom_rules=[ Shader.exclude_package('org.apache.ivy', recursive=True) ])
def register_options(cls, register): super(ErrorProne, cls).register_options(register) register('--skip', type=bool, help='Skip Error Prone.') register('--transitive', default=False, type=bool, help='Run Error Prone against transitive dependencies of targets ' 'specified on the command line.') register('--command-line-options', type=list, default=[], fingerprint=True, help='Command line options passed to Error Prone') register('--exclude-patterns', type=list, default=[], fingerprint=True, help='Patterns for targets to be excluded from analysis.') cls.register_jvm_tool(register, 'errorprone', classpath=[ JarDependency(org='com.google.errorprone', name='error_prone_core', rev='2.0.17'), ], main=cls._ERRORPRONE_MAIN, custom_rules=[ Shader.exclude_package('com.google.errorprone', recursive=True), Shading.create_exclude('*'), # https://github.com/pantsbuild/pants/issues/4288 ] )
def register_options(cls, register): super(RscCompile, cls).register_options(register) register( '--force-compiler-tag-prefix', default='use-compiler', metavar='<tag>', help= 'Always compile targets marked with this tag with rsc, unless the workflow is ' 'specified on the cli.') register( '--default-workflow', type=_CompileWorkflowChoice, default=_CompileWorkflowChoice.guess, metavar='<workflow>', help= 'Defines how a compile workflow is chosen when a tag is not present.' ) cls.register_jvm_tool(register, 'rsc', classpath=[ JarDependency( org='com.twitter', name='rsc_2.11', rev='0.0.0-734-e57e96eb', ), ], custom_rules=[ Shader.exclude_package('rsc', recursive=True), ])
def register_options(cls, register): super(Checkstyle, cls).register_options(register) register('--skip', type=bool, fingerprint=True, help='Skip checkstyle.') register('--configuration', advanced=True, type=file_option, fingerprint=True, help='Path to the checkstyle configuration file.') register('--properties', advanced=True, type=dict_with_files_option, default={}, fingerprint=True, help='Dictionary of property mappings to use for checkstyle.properties.') register('--confs', advanced=True, type=list, default=['default'], help='One or more ivy configurations to resolve for this target.') register('--include-user-classpath', type=bool, fingerprint=True, help='Add the user classpath to the checkstyle classpath') cls.register_jvm_tool(register, 'checkstyle', # Note that checkstyle 7.0 does not run on Java 7 runtimes or below. classpath=[ JarDependency(org='com.puppycrawl.tools', name='checkstyle', rev='6.19'), ], main=cls._CHECKSTYLE_MAIN, custom_rules=[ # Checkstyle uses reflection to load checks and has an affordance that # allows leaving off a check classes' package name. This affordance # breaks for built-in checkstyle checks under shading so we ensure all # checkstyle packages are excluded from shading such that just its # third party transitive deps (guava and the like), are shaded. # See the module configuration rules here which describe this: # http://checkstyle.sourceforge.net/config.html#Modules Shader.exclude_package('com.puppycrawl.tools.checkstyle', recursive=True), ])
def register_options(cls, register): super(Checkstyle, cls).register_options(register) register('--skip', action='store_true', fingerprint=True, help='Skip checkstyle.') register('--configuration', advanced=True, type=file_option, fingerprint=True, help='Path to the checkstyle configuration file.') register( '--properties', advanced=True, type=dict_option, default={}, fingerprint=True, help= 'Dictionary of property mappings to use for checkstyle.properties.' ) register( '--confs', advanced=True, default=['default'], help='One or more ivy configurations to resolve for this target.') register('--jvm-options', advanced=True, action='append', metavar='<option>...', help='Run checkstyle with these extra jvm options.') register('--include-user-classpath', action='store_true', default=False, fingerprint=True, help='Add the user classpath to the checkstyle classpath') cls.register_jvm_tool( register, 'checkstyle', classpath=[ # Pants still officially supports java 6 as a tool; the supported # development environment for a pants hacker is based on that. As # such, we use 6.1.1 here since its the last checkstyle version # compiled to java 6. See the release notes here: # http://checkstyle.sourceforge.net/releasenotes.html JarDependency(org='com.puppycrawl.tools', name='checkstyle', rev='6.1.1'), ], main=cls._CHECKSTYLE_MAIN, custom_rules=[ # Checkstyle uses reflection to load checks and has an affordance that # allows leaving off a check classes' package name. This affordance # breaks for built-in checkstyle checks under shading so we ensure all # checkstyle packages are excluded from shading such that just its # third party transitive deps (guava and the like), are shaded. # See the module configuration rules here which describe this: # http://checkstyle.sourceforge.net/config.html#Modules Shader.exclude_package('com.puppycrawl.tools.checkstyle', recursive=True), ])
def register_options(cls, register): super().register_options(register) register( "--confs", type=list, default=["default"], help="Pass a configuration to ivy in addition to the default ones.", ) register( "--exclude-patterns", type=list, default=[], help= "Regular expressions matching jars to be excluded from outdated report.", ) cls.register_jvm_tool( register, "dependency-update-checker", classpath=[ JarDependency(org="org.pantsbuild", name="ivy-dependency-update-checker", rev="0.0.5"), ], main=cls._IVY_DEPENDENCY_UPDATE_MAIN, custom_rules=[ Shader.exclude_package("org.apache.ivy", recursive=True) ], )
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.** log4j.@1', lines[0]) # The custom rule. self.assertEqual('rule * @1', lines[1]) # Exclude main's package. self.assertIn('rule javax.annotation.* javax.annotation.@1', lines) # Exclude system. self.assertEqual('rule com.google.common.base.* {}com.google.common.base.@1' .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_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.** log4j.@1', lines[0]) # The custom rule. self.assertEqual('rule * @1', lines[1]) # Exclude main's package. self.assertIn('rule javax.annotation.* javax.annotation.@1', lines) # Exclude system. self.assertEqual('rule com.google.common.base.* {}com.google.common.base.@1' .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 register_options(cls, register): super().register_options(register) register( '--force-compiler-tag-prefix', default='use-compiler', metavar='<tag>', help= 'Always compile targets marked with this tag with rsc, unless the workflow is ' 'specified on the cli.') register('--workflow', type=cls.JvmCompileWorkflowType, default=cls.JvmCompileWorkflowType.zinc_only, metavar='<workflow>', help='The workflow to use to compile JVM targets.', fingerprint=True) register('--extra-rsc-args', type=list, default=[], help='Extra arguments to pass to the rsc invocation.') cls.register_jvm_tool(register, 'rsc', classpath=[ JarDependency( org='com.twitter', name='rsc_2.12', rev='0.0.0-768-7357aa0a', ), ], custom_rules=[ Shader.exclude_package('rsc', recursive=True), ])
def register_options(cls, register): super().register_options(register) cls.register_jvm_tool(register, "kythe-java-indexer", main=cls._KYTHE_JAVA_INDEXER_MAIN) cls.register_jvm_tool( register, "javac9", custom_rules=[Shader.exclude_package("com.sun", recursive=True)], main="com.sun.tools.javac.main.Main", )
def register_options(cls, register): super(Zinc.Factory, cls).register_options(register) def sbt_jar(name, **kwargs): return JarDependency(org='org.scala-sbt', name=name, rev='1.0.0-X5', **kwargs) shader_rules = [ # The compiler-interface and compiler-bridge tool jars carry xsbt and # xsbti interfaces that are used across the shaded tool jar boundary so # we preserve these root packages wholesale along with the core scala # APIs. Shader.exclude_package('scala', recursive=True), Shader.exclude_package('xsbt', recursive=True), Shader.exclude_package('xsbti', recursive=True), ] cls.register_jvm_tool(register, 'zinc', classpath=[ JarDependency('org.pantsbuild', 'zinc_2.10', '0.0.5'), ], main=Zinc.ZINC_COMPILE_MAIN, custom_rules=shader_rules) cls.register_jvm_tool(register, 'compiler-bridge', classpath=[ sbt_jar(name='compiler-bridge_2.10', classifier='sources', intransitive=True) ]) cls.register_jvm_tool( register, 'compiler-interface', classpath=[sbt_jar(name='compiler-interface')], # NB: We force a noop-jarjar'ing of the interface, since it is now # broken up into multiple jars, but zinc does not yet support a sequence # of jars for the interface. main='no.such.main.Main', custom_rules=shader_rules)
def register_options(cls, register): super(ExtractJava, cls).register_options(register) cls.register_jvm_tool(register, 'kythe-extractor', custom_rules=[ # These need to remain unshaded so that Kythe can interact with the # javac embedded in its jar. Shader.exclude_package('com.sun', recursive=True), ], main=cls._KYTHE_EXTRACTOR_MAIN)
def register_options(cls, register): super(ExtractJava, cls).register_options(register) cls.register_jvm_tool(register, 'kythe-java-extractor', custom_rules=[ # These need to remain unshaded so that Kythe can interact with the # javac embedded in its jar. Shader.exclude_package('com.sun', recursive=True), ], main=cls._KYTHE_JAVA_EXTRACTOR_MAIN)
def register_options(cls, register): super(IndexJava, cls).register_options(register) cls.register_jvm_tool(register, 'kythe-java-indexer', main=cls._KYTHE_JAVA_INDEXER_MAIN) cls.register_jvm_tool(register, 'javac9', custom_rules=[ Shader.exclude_package('com.sun', recursive=True), ], main='com.sun.tools.javac.main.Main')
def register_options(cls, register): super().register_options(register) cls.register_jvm_tool( register, "kythe-java-extractor", custom_rules=[], main=cls._KYTHE_JAVA_EXTRACTOR_MAIN ) cls.register_jvm_tool( register, "javac9", custom_rules=[Shader.exclude_package("com.sun", recursive=True),], main="com.sun.tools.javac.main.Main", )
def register_options(cls, register): super(Zinc.Factory, cls).register_options(register) def sbt_jar(name, **kwargs): return JarDependency(org='org.scala-sbt', name=name, rev='1.0.0-X5', **kwargs) shader_rules = [ # The compiler-interface and compiler-bridge tool jars carry xsbt and # xsbti interfaces that are used across the shaded tool jar boundary so # we preserve these root packages wholesale along with the core scala # APIs. Shader.exclude_package('scala', recursive=True), Shader.exclude_package('xsbt', recursive=True), Shader.exclude_package('xsbti', recursive=True), ] cls.register_jvm_tool(register, 'zinc', classpath=[ JarDependency('org.pantsbuild', 'zinc_2.10', '0.0.5'), ], main=Zinc.ZINC_COMPILE_MAIN, custom_rules=shader_rules) cls.register_jvm_tool(register, 'compiler-bridge', classpath=[ sbt_jar(name='compiler-bridge_2.10', classifier='sources', intransitive=True) ]) cls.register_jvm_tool(register, 'compiler-interface', classpath=[ sbt_jar(name='compiler-interface') ], # NB: We force a noop-jarjar'ing of the interface, since it is now # broken up into multiple jars, but zinc does not yet support a sequence # of jars for the interface. main='no.such.main.Main', custom_rules=shader_rules)
def register_options(cls, register): super().register_options(register) cls.register_jvm_tool(register, 'kythe-java-indexer', main=cls._KYTHE_JAVA_INDEXER_MAIN) cls.register_jvm_tool(register, 'javac9', custom_rules=[ Shader.exclude_package('com.sun', recursive=True), ], main='com.sun.tools.javac.main.Main')
def register_options(cls, register, register_jvm_tool): register('--skip', action='store_true', help='Skip running junit.') register('--fail-fast', action='store_true', help='Fail fast on the first test failure in a suite.') register('--batch-size', advanced=True, type=int, default=sys.maxint, help='Run at most this many tests in a single test process.') register('--test', action='append', help='Force running of just these tests. Tests can be specified using any of: ' '[classname], [classname]#[methodname], [filename] or [filename]#[methodname]') register('--per-test-timer', action='store_true', help='Show progress and timer for each test.') register('--default-parallel', advanced=True, action='store_true', help='Run classes without @TestParallel or @TestSerial annotations in parallel.') register('--parallel-threads', advanced=True, type=int, default=0, help='Number of threads to run tests in parallel. 0 for autoset.') register('--test-shard', advanced=True, help='Subset of tests to run, in the form M/N, 0 <= M < N. ' 'For example, 1/3 means run tests number 2, 5, 8, 11, ...') register('--suppress-output', action='store_true', default=True, help='Redirect test output to files in .pants.d/test/junit.') register('--cwd', advanced=True, help='Set the working directory. If no argument is passed, use the build root. ' 'If cwd is set on a target, it will supersede this argument.') register('--strict-jvm-version', action='store_true', default=False, advanced=True, help='If true, will strictly require running junits with the same version of java as ' 'the platform -target level. Otherwise, the platform -target level will be ' 'treated as the minimum jvm to run.') register_jvm_tool(register, 'junit', classpath=[ JarDependency(org='org.pantsbuild', name='junit-runner', rev='0.0.8'), ], main=JUnitRun._MAIN, # TODO(John Sirois): Investigate how much less we can get away with. # Clearly both tests and the runner need access to the same @Test, @Before, # as well as other annotations, but there is also the Assert class and some # subset of the @Rules, @Theories and @RunWith APIs. custom_rules=[ Shader.exclude_package('org.junit', recursive=True), Shader.exclude_package('org.hamcrest', recursive=True) ])
def register_options(cls, register): super(ExtractJava, cls).register_options(register) cls.register_jvm_tool(register, 'kythe-java-extractor', custom_rules=[ ], main=cls._KYTHE_JAVA_EXTRACTOR_MAIN) cls.register_jvm_tool(register, 'javac9', custom_rules=[ Shader.exclude_package('com.sun', recursive=True), ], main='com.sun.tools.javac.main.Main')
def register_options(cls, register): super(ExtractJava, cls).register_options(register) cls.register_jvm_tool(register, 'kythe-java-extractor', custom_rules=[], main=cls._KYTHE_JAVA_EXTRACTOR_MAIN) cls.register_jvm_tool(register, 'javac9', custom_rules=[ Shader.exclude_package('com.sun', recursive=True), ], main='com.sun.tools.javac.main.Main')
def register_options(cls, register): super().register_options(register) register("--skip", type=bool, help="Skip Error Prone.") register( "--transitive", default=False, type=bool, help="Run Error Prone against transitive dependencies of targets " "specified on the command line.", ) register( "--command-line-options", type=list, default=[], fingerprint=True, help="Command line options passed to Error Prone", ) register( "--exclude-patterns", type=list, default=[], fingerprint=True, help="Patterns for targets to be excluded from analysis.", ) cls.register_jvm_tool( register, "errorprone", classpath=[ JarDependency(org="com.google.errorprone", name="error_prone_core", rev="2.3.1"), ], main=cls._ERRORPRONE_MAIN, custom_rules=[ Shader.exclude_package("com.google.errorprone", recursive=True) ], ) # The javac version should be kept in sync with the version used by errorprone above. cls.register_jvm_tool( register, "errorprone-javac", classpath=[ JarDependency(org="com.google.errorprone", name="javac", rev="9+181-r4173-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_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_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 register_options(cls, register): super(RscCompile, cls).register_options(register) cls.register_jvm_tool( register, 'rsc', classpath=[ JarDependency( org='com.twitter', name='rsc_2.11', rev='0.0.0-734-e57e96eb', ), ], custom_rules=[ Shader.exclude_package('rsc', recursive=True), ] )
def register_options(cls, register): super(IvyOutdated, cls).register_options(register) register('--confs', type=list, default=['default'], help='Pass a configuration to ivy in addition to the default ones.') register('--exclude-patterns', type=list, default=[], help='Regular expressions matching jars to be excluded from outdated report.') cls.register_jvm_tool(register, 'dependency-update-checker', classpath=[ JarDependency(org='org.pantsbuild', name='ivy-dependency-update-checker', rev='0.0.1'), ], main=cls._IVY_DEPENDENCY_UPDATE_MAIN, custom_rules=[ Shader.exclude_package('org.apache.ivy', recursive=True) ] )
def register_options(cls, register): super(FindBugs, cls).register_options(register) register('--skip', type=bool, help='Skip findbugs.') register('--transitive', default=True, type=bool, help='Run findbugs against transitive dependencies. Disable to run findbugs only on ' 'targets specified on the command line.') register('--effort', default='default', fingerprint=True, choices=['min', 'less', 'default', 'more', 'max'], help='Effort of the bug finders.') register('--threshold', default='medium', fingerprint=True, choices=['low', 'medium', 'high', 'experimental'], help='Effort of the bug finders.') register('--fail-on-error', type=bool, fingerprint=True, help='Fail the build on an error.') register('--max-rank', type=int, fingerprint=True, help='Maximum bug ranking to record [1..20].') register('--relaxed', type=bool, fingerprint=True, help='Relaxed reporting mode') register('--nested', type=bool, default=True, fingerprint=True, help='Analyze nested jar/zip archives') register('--exclude-filter-file', type=file_option, fingerprint=True, help='Exclude bugs matching given filter') register('--include-filter-file', type=file_option, fingerprint=True, help='Include only bugs matching given filter') register('--exclude-patterns', type=list, default=[], fingerprint=True, help='Adds patterns for targets to be excluded from analysis.') cls.register_jvm_tool(register, 'findbugs', classpath=[ JarDependency(org='com.google.code.findbugs', name='findbugs', rev='3.0.1'), ], main=cls._FINDBUGS_MAIN, custom_rules=[ Shader.exclude_package('edu.umd.cs.findbugs', recursive=True), ])
def register_options(cls, register): super(Checkstyle, cls).register_options(register) register('--skip', type=bool, fingerprint=True, help='Skip checkstyle.') register('--configuration', advanced=True, type=file_option, fingerprint=True, help='Path to the checkstyle configuration file.') register('--properties', advanced=True, type=dict_with_files_option, default={}, fingerprint=True, help='Dictionary of property mappings to use for checkstyle.properties.') register('--confs', advanced=True, type=list, default=['default'], help='One or more ivy configurations to resolve for this target.') register('--include-user-classpath', type=bool, fingerprint=True, help='Add the user classpath to the checkstyle classpath') cls.register_jvm_tool(register, 'checkstyle', classpath=[ # Pants still officially supports java 6 as a tool; the supported # development environment for a pants hacker is based on that. As # such, we use 6.1.1 here since its the last checkstyle version # compiled to java 6. See the release notes here: # http://checkstyle.sourceforge.net/releasenotes.html JarDependency(org='com.puppycrawl.tools', name='checkstyle', rev='6.1.1'), ], main=cls._CHECKSTYLE_MAIN, custom_rules=[ # Checkstyle uses reflection to load checks and has an affordance that # allows leaving off a check classes' package name. This affordance # breaks for built-in checkstyle checks under shading so we ensure all # checkstyle packages are excluded from shading such that just its # third party transitive deps (guava and the like), are shaded. # See the module configuration rules here which describe this: # http://checkstyle.sourceforge.net/config.html#Modules Shader.exclude_package('com.puppycrawl.tools.checkstyle', recursive=True), ])
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.** log4j.@1', lines[0]) # The custom rule. self.assertEqual('rule * @1', lines[1]) # Exclude main's package. self.assertIn('rule javax.annotation.* javax.annotation.@1', lines) # Exclude system. self.assertEqual('rule com.google.common.base.* {}com.google.common.base.@1' .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__.', '__prefix__.@1') def test_shading_exclude(self): def assert_exclude(from_pattern, to_pattern): self.assertEqual((from_pattern, to_pattern), Shading.create_exclude(from_pattern)) assert_exclude('com.foo.bar.Main', 'com.foo.bar.Main') assert_exclude('com.foo.bar.**', 'com.foo.bar.@1') assert_exclude('com.*.bar.**', '[email protected].@2') def test_shading_exclude_package(self): self.assertEqual(('com.foo.bar.**', 'com.foo.bar.@1'), Shading.create_exclude_package('com.foo.bar')) self.assertEqual(('com.foo.bar.*', 'com.foo.bar.@1'), Shading.create_exclude_package('com.foo.bar', recursive=False)) def test_relocate(self): self.assertEqual(('com.foo.bar.**', '{}com.foo.bar.@1'.format(Shading.SHADE_PREFIX)), Shading.create_relocate(from_pattern='com.foo.bar.**')) self.assertEqual(('com.foo.bar.**', '{}com.foo.bar.@1'.format('__my_prefix__.')), Shading.create_relocate(from_pattern='com.foo.bar.**', shade_prefix='__my_prefix__.')) self.assertEqual(('com.foo.bar.**', 'org.biz.baz.@1'.format('__my_prefix__.')), Shading.create_relocate(from_pattern='com.foo.bar.**', shade_prefix='__my_prefix__.', shade_pattern='org.biz.baz.@1')) def test_relocate_package(self): self.assertEqual(('com.foo.bar.**', '{}com.foo.bar.@1'.format(Shading.SHADE_PREFIX)), Shading.create_relocate_package('com.foo.bar')) self.assertEqual(('com.foo.bar.*', '{}com.foo.bar.@1'.format(Shading.SHADE_PREFIX)), Shading.create_relocate_package('com.foo.bar', recursive=False)) self.assertEqual(('com.foo.bar.**', '__p__.com.foo.bar.@1'), Shading.create_relocate_package('com.foo.bar', shade_prefix='__p__.')) def test_zap_package(self): self.assertEqual(('zap', 'com.foo.bar.**'), Shading.create_zap_package('com.foo.bar', True)) self.assertEqual(('zap', 'com.foo.bar.*'), Shading.create_zap_package('com.foo.bar', False)) def test_keep_package(self): self.assertEqual(('keep', 'com.foo.bar.**'), Shading.create_keep_package('com.foo.bar', True)) self.assertEqual(('keep', 'com.foo.bar.*'), Shading.create_keep_package('com.foo.bar', False)) def test_rules_file(self): expected = [ 'rule a.b.c.** d.e.f.@1\n', 'rule a.*.b [email protected]\n', 'rule x.y.z.** shaded.x.y.z.@1\n', 'zap com.foo.bar.Main\n', 'zap com.baz.*\n', 'keep org.foobar.vegetable.Potato\n', 'keep org.foobar.fruit.**\n', ] rules = [ Shading.create_relocate('a.b.c.**', 'd.e.f.@1'), Shading.create_relocate('a.*.b', shade_prefix='shaded.'), Shading.create_relocate_package('x.y.z', shade_prefix='shaded.'), Shading.create_zap('com.foo.bar.Main'), Shading.create_zap_package('com.baz', recursive=False), Shading.create_keep('org.foobar.vegetable.Potato'), Shading.create_keep_package('org.foobar.fruit'), ] with self.shader.temporary_rules_file(rules) as fname: with open(fname, 'r') as f: received = f.readlines() self.assertEqual(received, expected)
def register_options(cls, register): super(BaseZincCompile, cls).register_options(register) register( "--name-hashing", advanced=True, type=bool, fingerprint=True, removal_hint="Name hashing is required for operation in zinc 1.0.0-X: this " "option no longer has any effect.", removal_version="1.4.0", help="Use zinc name hashing.", ) register( "--whitelisted-args", advanced=True, type=dict, default={"-S.*": False, "-C.*": False, "-file-filter": True, "-msg-filter": True}, help="A dict of option regexes that make up pants' supported API for zinc. " "Options not listed here are subject to change/removal. The value of the dict " "indicates that an option accepts an argument.", ) register( "--incremental", advanced=True, type=bool, default=True, help="When set, zinc will use sub-target incremental compilation, which dramatically " "improves compile performance while changing large targets. When unset, " "changed targets will be compiled with an empty output directory, as if after " "running clean-all.", ) register( "--incremental-caching", advanced=True, type=bool, help="When set, the results of incremental compiles will be written to the cache. " "This is unset by default, because it is generally a good precaution to cache " "only clean/cold builds.", ) def sbt_jar(name, **kwargs): return JarDependency(org="org.scala-sbt", name=name, rev="1.0.0-X5", **kwargs) shader_rules = [ # The compiler-interface and compiler-bridge tool jars carry xsbt and # xsbti interfaces that are used across the shaded tool jar boundary so # we preserve these root packages wholesale along with the core scala # APIs. Shader.exclude_package("scala", recursive=True), Shader.exclude_package("xsbt", recursive=True), Shader.exclude_package("xsbti", recursive=True), ] cls.register_jvm_tool( register, "zinc", classpath=[JarDependency("org.pantsbuild", "zinc_2.10", "0.0.5")], main=cls._ZINC_MAIN, custom_rules=shader_rules, ) cls.register_jvm_tool( register, "compiler-bridge", classpath=[sbt_jar(name="compiler-bridge_2.10", classifier="sources", intransitive=True)], ) cls.register_jvm_tool( register, "compiler-interface", classpath=[sbt_jar(name="compiler-interface")], # NB: We force a noop-jarjar'ing of the interface, since it is now broken # up into multiple jars, but zinc does not yet support a sequence of jars # for the interface. main="no.such.main.Main", custom_rules=shader_rules, )
def register_options(cls, register): super(Zinc.Factory, cls).register_options(register) zinc_rev = '1.0.3' shader_rules = [ # The compiler-interface and compiler-bridge tool jars carry xsbt and # xsbti interfaces that are used across the shaded tool jar boundary so # we preserve these root packages wholesale along with the core scala # APIs. Shader.exclude_package('scala', recursive=True), Shader.exclude_package('xsbt', recursive=True), Shader.exclude_package('xsbti', recursive=True), # Unfortunately, is loaded reflectively by the compiler. Shader.exclude_package('org.apache.logging.log4j', recursive=True), ] cls.register_jvm_tool(register, Zinc.ZINC_BOOTSTRAPPER_TOOL_NAME, classpath=[ JarDependency('org.pantsbuild', 'zinc-bootstrapper_2.11', '0.0.4'), ], main=Zinc.ZINC_BOOTSTRAPER_MAIN, custom_rules=shader_rules, ) cls.register_jvm_tool(register, Zinc.ZINC_COMPILER_TOOL_NAME, classpath=[ JarDependency('org.pantsbuild', 'zinc-compiler_2.11', '0.0.9'), ], main=Zinc.ZINC_COMPILE_MAIN, custom_rules=shader_rules) cls.register_jvm_tool(register, 'compiler-bridge', classpath=[ ScalaJarDependency(org='org.scala-sbt', name='compiler-bridge', rev=zinc_rev, classifier='sources', intransitive=True), ]) cls.register_jvm_tool(register, 'compiler-interface', classpath=[ JarDependency(org='org.scala-sbt', name='compiler-interface', rev=zinc_rev), ], # NB: We force a noop-jarjar'ing of the interface, since it is now # broken up into multiple jars, but zinc does not yet support a sequence # of jars for the interface. main='no.such.main.Main', custom_rules=shader_rules) cls.register_jvm_tool(register, Zinc.ZINC_EXTRACTOR_TOOL_NAME, classpath=[ JarDependency('org.pantsbuild', 'zinc-extractor_2.11', '0.0.6') ]) # Register scalac for fixed versions of Scala, 2.10, 2.11 and 2.12. # Relies on ScalaPlatform to get the revision version from the major.minor version. # The tool with the correct scala version will be retrieved later, # taking the user-passed option into account. supported_scala_versions=['2.10', '2.11', '2.12'] wanted_jars = ['scala-compiler', 'scala-library', 'scala-reflect'] for scala_version in supported_scala_versions: cls.register_jvm_tool(register, ScalaPlatform.versioned_tool_name('scalac', scala_version), classpath=[ ScalaPlatform.create_jardep(jar, scala_version) for jar in wanted_jars ]) # Register custom scalac tool. cls.register_jvm_tool(register, ScalaPlatform.versioned_tool_name('scalac', 'custom'), classpath=[JarDependency('missing spec', ' //:scalac')])
def register_options(cls, register): super(BaseZincCompile, cls).register_options(register) register('--name-hashing', advanced=True, type=bool, fingerprint=True, removal_hint='Name hashing is required for operation in zinc 1.0.0-X: this ' 'option no longer has any effect.', removal_version='1.4.0.dev0', help='Use zinc name hashing.') register('--whitelisted-args', advanced=True, type=dict, default={ '-S.*': False, '-C.*': False, '-file-filter': True, '-msg-filter': True, }, help='A dict of option regexes that make up pants\' supported API for zinc. ' 'Options not listed here are subject to change/removal. The value of the dict ' 'indicates that an option accepts an argument.') register('--incremental', advanced=True, type=bool, default=True, help='When set, zinc will use sub-target incremental compilation, which dramatically ' 'improves compile performance while changing large targets. When unset, ' 'changed targets will be compiled with an empty output directory, as if after ' 'running clean-all.') register('--incremental-caching', advanced=True, type=bool, help='When set, the results of incremental compiles will be written to the cache. ' 'This is unset by default, because it is generally a good precaution to cache ' 'only clean/cold builds.') def sbt_jar(name, **kwargs): return JarDependency(org='org.scala-sbt', name=name, rev='1.0.0-X5', **kwargs) shader_rules = [ # The compiler-interface and compiler-bridge tool jars carry xsbt and # xsbti interfaces that are used across the shaded tool jar boundary so # we preserve these root packages wholesale along with the core scala # APIs. Shader.exclude_package('scala', recursive=True), Shader.exclude_package('xsbt', recursive=True), Shader.exclude_package('xsbti', recursive=True), ] cls.register_jvm_tool(register, 'zinc', classpath=[ JarDependency('org.pantsbuild', 'zinc_2.10', '0.0.5'), ], main=cls._ZINC_MAIN, custom_rules=shader_rules) cls.register_jvm_tool(register, 'compiler-bridge', classpath=[ sbt_jar(name='compiler-bridge_2.10', classifier='sources', intransitive=True) ]) cls.register_jvm_tool(register, 'compiler-interface', classpath=[ sbt_jar(name='compiler-interface') ], # NB: We force a noop-jarjar'ing of the interface, since it is now broken # up into multiple jars, but zinc does not yet support a sequence of jars # for the interface. main='no.such.main.Main', custom_rules=shader_rules)
def register_options(cls, register): super(BaseZincCompile, cls).register_options(register) # TODO: disable by default because it breaks dependency parsing: # https://github.com/pantsbuild/pants/issues/2224 # ...also, as of sbt 0.13.9, it is significantly slower for cold builds. register('--name-hashing', advanced=True, type=bool, fingerprint=True, help='Use zinc name hashing.') register('--whitelisted-args', advanced=True, type=dict, default={ '-S.*': False, '-C.*': False, '-file-filter': True, '-msg-filter': True, }, help='A dict of option regexes that make up pants\' supported API for zinc. ' 'Options not listed here are subject to change/removal. The value of the dict ' 'indicates that an option accepts an argument.') register('--incremental', advanced=True, type=bool, default=True, help='When set, zinc will use sub-target incremental compilation, which dramatically ' 'improves compile performance while changing large targets. When unset, ' 'changed targets will be compiled with an empty output directory, as if after ' 'running clean-all.') # TODO: Defaulting to false due to a few upstream issues for which we haven't pulled down fixes: # https://github.com/sbt/sbt/pull/2085 # https://github.com/sbt/sbt/pull/2160 register('--incremental-caching', advanced=True, type=bool, help='When set, the results of incremental compiles will be written to the cache. ' 'This is unset by default, because it is generally a good precaution to cache ' 'only clean/cold builds.') cls.register_jvm_tool(register, 'zinc', classpath=[ # NB: This is explicitly a `_2.10` JarDependency rather than a # ScalaJarDependency. The latter would pick up the platform in a users' # repo, whereas this binary is shaded and independent of the target # platform version. JarDependency('org.pantsbuild', 'zinc_2.10', '0.0.3') ], main=cls._ZINC_MAIN, custom_rules=[ # The compiler-interface and sbt-interface tool jars carry xsbt and # xsbti interfaces that are used across the shaded tool jar boundary so # we preserve these root packages wholesale along with the core scala # APIs. Shader.exclude_package('scala', recursive=True), Shader.exclude_package('xsbt', recursive=True), Shader.exclude_package('xsbti', recursive=True), ]) def sbt_jar(name, **kwargs): return JarDependency(org='com.typesafe.sbt', name=name, rev='0.13.9', **kwargs) cls.register_jvm_tool(register, 'compiler-interface', classpath=[ sbt_jar(name='compiler-interface', classifier='sources', # We just want the single compiler-interface jar and not its # dep on scala-lang intransitive=True) ]) cls.register_jvm_tool(register, 'sbt-interface', classpath=[ sbt_jar(name='sbt-interface', # We just want the single sbt-interface jar and not its dep # on scala-lang intransitive=True) ])
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'