forked from twitter/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert two existing concepts (RunTracker and the jar tool) to be sub…
…systems. - The RunTracker change is straightforward. - There is now a JarTool subsystem. But implementing this neatly required some changes to the JVM tool machinery: + The old JvmToolTaskMixin is now JvmToolMixin. I.e., you don't have to be a task to use JVM tools. Indeed, subsystems can mix this in in order to register and bootstrap JVM tools. + JvmToolTaskMixin is now a thin subclass of JvmToolMixin, adapting it for ease of use by tasks. In the future we might make all JVM tools (compilers etc.) into subsystems and then not need JvmToolTaskMixin at all. + The new JarTool subsystem does two things: 1) Bootstraps the jar tool and handles its options. 2) Actually runs the jar tool with given args in a given runner. This interface is slightly awkward perhaps, but very reusable. And we already pass runners around as arguments elsewhere anyway. - Removed some clunky code in JvmToolTaskTestBase that registers options for BootstrapJvmTools, which is not the task under test but is needed to bootstrap tools for the task under test. Instead overrides context() to add BootstrapJvmTools to the list of tasks the base class already clunkily registers options for (in addition to the task under test)... - TODO: JarTool is now tested indirectly in test_jar_task.py, but we might want to also test the now-standalone subsystem directly. Testing Done: CI passes: https://travis-ci.org/pantsbuild/pants/builds/58631095 Reviewed at https://rbcommons.com/s/twitter/r/2081/
- Loading branch information
Showing
19 changed files
with
208 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Copyright 2015 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
python_library( | ||
name = 'jvm_tool_mixin', | ||
sources = ['jvm_tool_mixin.py'], | ||
dependencies = [ | ||
'src/python/pants/base:exceptions', | ||
'src/python/pants/option', | ||
], | ||
) | ||
|
||
python_library( | ||
name = 'jar_tool', | ||
sources = ['jar_tool.py'], | ||
dependencies = [ | ||
':jvm_tool_mixin', | ||
'src/python/pants/base:workunit', | ||
'src/python/pants/option', | ||
'src/python/pants/subsystem', | ||
], | ||
) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# coding=utf-8 | ||
# Copyright 2015 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import (absolute_import, division, generators, nested_scopes, print_function, | ||
unicode_literals, with_statement) | ||
|
||
from pants.backend.jvm.subsystems.jvm_tool_mixin import JvmToolMixin | ||
from pants.base.workunit import WorkUnit | ||
from pants.option.options import Options | ||
from pants.subsystem.subsystem import Subsystem | ||
|
||
|
||
class JarTool(JvmToolMixin, Subsystem): | ||
@classmethod | ||
def scope_qualifier(cls): | ||
return 'jar-tool' | ||
|
||
@classmethod | ||
def register_options(cls, register): | ||
super(JarTool, cls).register_options(register) | ||
# TODO: All jvm tools will need this option, so might as well have register_jvm_tool add it? | ||
register('--jvm-options', advanced=True, type=Options.list, default=['-Xmx64M'], | ||
help='Run the jar tool with these JVM options.') | ||
cls.register_jvm_tool(register, 'jar-tool') | ||
|
||
def run(self, context, runjava, args): | ||
return runjava(self.tool_classpath_from_products(context.products, 'jar-tool', | ||
scope=self.options_scope), | ||
'com.twitter.common.jar.tool.Main', | ||
jvm_options=self.get_options().jvm_options, | ||
args=args, | ||
workunit_name='jar-tool', | ||
workunit_labels=[WorkUnit.TOOL, WorkUnit.JVM, WorkUnit.NAILGUN]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# coding=utf-8 | ||
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import (absolute_import, division, generators, nested_scopes, print_function, | ||
unicode_literals, with_statement) | ||
|
||
from pants.base.exceptions import TaskError | ||
from pants.option.options import Options | ||
|
||
|
||
class JvmToolMixin(object): | ||
"""A mixin for registering and accessing JVM-based tools.""" | ||
|
||
_tool_keys = [] # List of (scope, key, main, custom_rule) tuples. | ||
|
||
@classmethod | ||
def register_jvm_tool(cls, register, key, default=None, main=None, custom_rules=None): | ||
"""Registers a jvm tool under `key` for lazy classpath resolution. | ||
Classpaths can be retrieved in `execute` scope via `tool_classpath`. | ||
NB: If the tool's `main` class name is supplied the tool classpath will be shaded. | ||
:param register: A function that can register options with the option system. | ||
:param unicode key: The key the tool configuration should be registered under. | ||
:param list default: The default tool classpath target address specs to use. | ||
:param unicode main: The fully qualified class name of the tool's main class if shading of the | ||
tool classpath is desired. | ||
:param list custom_rules: An optional list of `Shader.Rule`s to apply before the automatically | ||
generated binary jar shading rules. This is useful for excluding | ||
classes shared between the tool and the code it runs over. The | ||
canonical example is the `org.junit.Test` annotation read by junit | ||
runner tools from user code. In this sort of case the shared code must | ||
have a uniform name between the tool and the user code and so the | ||
shared code must be excluded from shading. | ||
""" | ||
register('--{0}'.format(key), | ||
advanced=True, | ||
type=Options.list, | ||
default=default or ['//:{0}'.format(key)], | ||
help='Target specs for bootstrapping the {0} tool.'.format(key)) | ||
|
||
# TODO(John Sirois): Move towards requiring tool specs point to jvm_binary targets. | ||
# These already have a main and are a natural place to house any custom shading rules. That | ||
# would eliminate the need to pass main and custom_rules here. | ||
# It is awkward that jars can no longer be inlined as dependencies - this will require 2 targets | ||
# for every tool - the jvm_binary, and a jar_library for its dependencies to point to. It may | ||
# be worth creating a JarLibrary subclass - say JarBinary, or else mixing in a Binary interface | ||
# to JarLibrary to endow it with main and shade_rules attributes to allow for single-target | ||
# definition of resolvable jvm binaries. | ||
JvmToolMixin._tool_keys.append((register.scope, key, main, custom_rules)) | ||
|
||
@staticmethod | ||
def get_registered_tools(): | ||
return JvmToolMixin._tool_keys | ||
|
||
@staticmethod | ||
def reset_registered_tools(): | ||
"""Needed only for test isolation.""" | ||
JvmToolMixin._tool_keys = [] | ||
|
||
def tool_classpath_from_products(self, products, key, scope): | ||
"""Get a classpath for the tool previously registered under key in the given scope. | ||
Returns a list of paths. | ||
""" | ||
return self.lazy_tool_classpath_from_products(products, key, scope)() | ||
|
||
def lazy_tool_classpath_from_products(self, products, key, scope): | ||
"""Get a lazy classpath for the tool previously registered under the key in the given scope. | ||
Returns a no-arg callable. Invoking it returns a list of paths. | ||
""" | ||
callback_product_map = products.get_data('jvm_build_tools_classpath_callbacks') or {} | ||
callback = callback_product_map.get(scope, {}).get(key) | ||
if not callback: | ||
raise TaskError('No bootstrap callback registered for {key} in {scope}'.format( | ||
key=key, scope=scope)) | ||
return callback |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.