Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove @rule Select syntax #7477

Merged
merged 4 commits into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
from pants.engine.addressable import BuildFileAddresses
from pants.engine.console import Console
from pants.engine.rules import console_rule
from pants.engine.selectors import Get, Select
from pants.engine.selectors import Get
from pants.option.scope import Scope, ScopedOptions
from pants.rules.core.exceptions import GracefulTerminationException


@console_rule('list-and-die-for-testing', [Select(Console), Select(BuildFileAddresses)])
@console_rule('list-and-die-for-testing', [Console, BuildFileAddresses])
def fast_list_and_die_for_testing(console, addresses):
"""A fast and deadly variant of `./pants list`."""
options = yield Get(ScopedOptions, Scope(str('list')))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from pants.backend.native.config.environment import Assembler, Linker
from pants.binaries.binary_tool import NativeTool
from pants.engine.rules import rule
from pants.engine.selectors import Select


class Binutils(NativeTool):
Expand Down Expand Up @@ -38,12 +37,12 @@ def linker(self):
)


@rule(Assembler, [Select(Binutils)])
@rule(Assembler, [Binutils])
def get_as(binutils):
return binutils.assembler()


@rule(Linker, [Select(Binutils)])
@rule(Linker, [Binutils])
def get_ld(binutils):
return binutils.linker()

Expand Down
5 changes: 2 additions & 3 deletions src/python/pants/backend/native/subsystems/binaries/gcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from pants.backend.native.subsystems.utils.archive_file_mapper import ArchiveFileMapper
from pants.binaries.binary_tool import NativeTool
from pants.engine.rules import rule
from pants.engine.selectors import Select
from pants.util.memo import memoized_method, memoized_property


Expand Down Expand Up @@ -96,12 +95,12 @@ def cpp_compiler(self, platform):
extra_args=[])


@rule(CCompiler, [Select(GCC), Select(Platform)])
@rule(CCompiler, [GCC, Platform])
def get_gcc(gcc, platform):
return gcc.c_compiler(platform)


@rule(CppCompiler, [Select(GCC), Select(Platform)])
@rule(CppCompiler, [GCC, Platform])
def get_gplusplus(gcc, platform):
return gcc.cpp_compiler(platform)

Expand Down
7 changes: 3 additions & 4 deletions src/python/pants/backend/native/subsystems/binaries/llvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from pants.binaries.binary_tool import NativeTool
from pants.binaries.binary_util import BinaryToolUrlGenerator
from pants.engine.rules import RootRule, rule
from pants.engine.selectors import Select
from pants.util.dirutil import is_readable_dir
from pants.util.memo import memoized_method, memoized_property

Expand Down Expand Up @@ -123,17 +122,17 @@ def cpp_compiler(self):


# TODO(#5663): use this over the XCode linker!
@rule(Linker, [Select(Platform), Select(LLVM)])
@rule(Linker, [Platform, LLVM])
def get_lld(platform, llvm):
return llvm.linker(platform)


@rule(CCompiler, [Select(LLVM)])
@rule(CCompiler, [LLVM])
def get_clang(llvm):
return llvm.c_compiler()


@rule(CppCompiler, [Select(LLVM)])
@rule(CppCompiler, [LLVM])
def get_clang_plusplus(llvm):
return llvm.cpp_compiler()

Expand Down
26 changes: 13 additions & 13 deletions src/python/pants/backend/native/subsystems/native_toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from pants.backend.native.subsystems.native_build_step import ToolchainVariant
from pants.backend.native.subsystems.xcode_cli_tools import XCodeCLITools
from pants.engine.rules import RootRule, rule
from pants.engine.selectors import Get, Select
from pants.engine.selectors import Get
from pants.subsystem.subsystem import Subsystem
from pants.util.memo import memoized_property
from pants.util.objects import datatype
Expand Down Expand Up @@ -98,7 +98,7 @@ class LLVMCToolchain(datatype([('c_toolchain', CToolchain)])): pass
class LLVMCppToolchain(datatype([('cpp_toolchain', CppToolchain)])): pass


@rule(LibcObjects, [Select(Platform), Select(NativeToolchain)])
@rule(LibcObjects, [Platform, NativeToolchain])
def select_libc_objects(platform, native_toolchain):
# We use lambdas here to avoid searching for libc on osx, where it will fail.
paths = platform.resolve_for_enum_variant({
Expand All @@ -108,7 +108,7 @@ def select_libc_objects(platform, native_toolchain):
yield LibcObjects(paths)


@rule(Assembler, [Select(Platform), Select(NativeToolchain)])
@rule(Assembler, [Platform, NativeToolchain])
def select_assembler(platform, native_toolchain):
if platform == Platform.darwin:
assembler = yield Get(Assembler, XCodeCLITools, native_toolchain._xcode_cli_tools)
Expand All @@ -125,7 +125,7 @@ class BaseLinker(datatype([('linker', Linker)])):


# TODO: select the appropriate `Platform` in the `@rule` decl using variants!
@rule(BaseLinker, [Select(Platform), Select(NativeToolchain)])
@rule(BaseLinker, [Platform, NativeToolchain])
def select_base_linker(platform, native_toolchain):
if platform == Platform.darwin:
# TODO(#5663): turn this into LLVM when lld works.
Expand All @@ -136,7 +136,7 @@ def select_base_linker(platform, native_toolchain):
yield base_linker


@rule(GCCLinker, [Select(NativeToolchain)])
@rule(GCCLinker, [NativeToolchain])
def select_gcc_linker(native_toolchain):
base_linker = yield Get(BaseLinker, NativeToolchain, native_toolchain)
linker = base_linker.linker
Expand All @@ -145,7 +145,7 @@ def select_gcc_linker(native_toolchain):
yield GCCLinker(linker_with_libc)


@rule(LLVMLinker, [Select(BaseLinker)])
@rule(LLVMLinker, [BaseLinker])
def select_llvm_linker(base_linker):
return LLVMLinker(base_linker.linker)

Expand All @@ -163,12 +163,12 @@ def as_clang_argv(self):
return ['--gcc-toolchain={}'.format(self.toolchain_dir)]


@rule(GCCInstallLocationForLLVM, [Select(GCC)])
@rule(GCCInstallLocationForLLVM, [GCC])
def select_gcc_install_location(gcc):
return GCCInstallLocationForLLVM(gcc.select())


@rule(LLVMCToolchain, [Select(Platform), Select(NativeToolchain)])
@rule(LLVMCToolchain, [Platform, NativeToolchain])
def select_llvm_c_toolchain(platform, native_toolchain):
provided_clang = yield Get(CCompiler, LLVM, native_toolchain._llvm)

Expand All @@ -194,7 +194,7 @@ def select_llvm_c_toolchain(platform, native_toolchain):
yield LLVMCToolchain(CToolchain(working_c_compiler, working_linker))


@rule(LLVMCppToolchain, [Select(Platform), Select(NativeToolchain)])
@rule(LLVMCppToolchain, [Platform, NativeToolchain])
def select_llvm_cpp_toolchain(platform, native_toolchain):
provided_clangpp = yield Get(CppCompiler, LLVM, native_toolchain._llvm)

Expand Down Expand Up @@ -238,7 +238,7 @@ def select_llvm_cpp_toolchain(platform, native_toolchain):
yield LLVMCppToolchain(CppToolchain(working_cpp_compiler, working_linker))


@rule(GCCCToolchain, [Select(Platform), Select(NativeToolchain)])
@rule(GCCCToolchain, [Platform, NativeToolchain])
def select_gcc_c_toolchain(platform, native_toolchain):
provided_gcc = yield Get(CCompiler, GCC, native_toolchain._gcc)

Expand All @@ -263,7 +263,7 @@ def select_gcc_c_toolchain(platform, native_toolchain):
yield GCCCToolchain(CToolchain(working_c_compiler, working_linker))


@rule(GCCCppToolchain, [Select(Platform), Select(NativeToolchain)])
@rule(GCCCppToolchain, [Platform, NativeToolchain])
def select_gcc_cpp_toolchain(platform, native_toolchain):
provided_gpp = yield Get(CppCompiler, GCC, native_toolchain._gcc)

Expand Down Expand Up @@ -301,7 +301,7 @@ class ToolchainVariantRequest(datatype([
])): pass


@rule(CToolchain, [Select(ToolchainVariantRequest)])
@rule(CToolchain, [ToolchainVariantRequest])
def select_c_toolchain(toolchain_variant_request):
native_toolchain = toolchain_variant_request.toolchain
# TODO(#5933): make an enum exhaustiveness checking method that works with `yield Get(...)`!
Expand All @@ -316,7 +316,7 @@ def select_c_toolchain(toolchain_variant_request):
yield toolchain_resolved.c_toolchain


@rule(CppToolchain, [Select(ToolchainVariantRequest)])
@rule(CppToolchain, [ToolchainVariantRequest])
def select_cpp_toolchain(toolchain_variant_request):
native_toolchain = toolchain_variant_request.toolchain
# TODO(#5933): make an enum exhaustiveness checking method that works with `yield Get(...)`!
Expand Down
9 changes: 4 additions & 5 deletions src/python/pants/backend/native/subsystems/xcode_cli_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from pants.backend.native.config.environment import Assembler, CCompiler, CppCompiler, Linker
from pants.engine.rules import rule
from pants.engine.selectors import Select
from pants.subsystem.subsystem import Subsystem
from pants.util.dirutil import is_readable_dir
from pants.util.memo import memoized_method, memoized_property
Expand Down Expand Up @@ -167,22 +166,22 @@ def cpp_compiler(self):
extra_args=[MIN_OSX_VERSION_ARG])


@rule(Assembler, [Select(XCodeCLITools)])
@rule(Assembler, [XCodeCLITools])
def get_assembler(xcode_cli_tools):
return xcode_cli_tools.assembler()


@rule(Linker, [Select(XCodeCLITools)])
@rule(Linker, [XCodeCLITools])
def get_ld(xcode_cli_tools):
return xcode_cli_tools.linker()


@rule(CCompiler, [Select(XCodeCLITools)])
@rule(CCompiler, [XCodeCLITools])
def get_clang(xcode_cli_tools):
return xcode_cli_tools.c_compiler()


@rule(CppCompiler, [Select(XCodeCLITools)])
@rule(CppCompiler, [XCodeCLITools])
def get_clang_plusplus(xcode_cli_tools):
return xcode_cli_tools.cpp_compiler()

Expand Down
4 changes: 2 additions & 2 deletions src/python/pants/backend/python/rules/python_test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
FallibleExecuteProcessResult)
from pants.engine.legacy.graph import TransitiveHydratedTarget
from pants.engine.rules import optionable_rule, rule
from pants.engine.selectors import Get, Select
from pants.engine.selectors import Get
from pants.rules.core.core_test_model import Status, TestResult


Expand All @@ -27,7 +27,7 @@ class PyTestResult(TestResult):

# TODO: Support deps
# TODO: Support resources
@rule(PyTestResult, [Select(TransitiveHydratedTarget), Select(PyTest)])
@rule(PyTestResult, [TransitiveHydratedTarget, PyTest])
def run_python_test(transitive_hydrated_target, pytest):
target_root = transitive_hydrated_target.root

Expand Down
22 changes: 11 additions & 11 deletions src/python/pants/engine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ As a very simple example, you might register the following `@rule` that can comp
Product given a single `Int` input.

```python
@rule(StringType, [Select(IntType)])
@rule(StringType, [IntType])
def int_to_str(an_int):
return '{}'.format(an_int)
```

The first argument to the `@rule` decorator is the Product (ie, return) type for the `@rule`. The
second argument is a list of `Selector`s that declare the types of the input arguments to the
`@rule`. In this case, because the Product type is `StringType` and there is one `Selector`
(`Select(IntType)`), this `@rule` represents a conversion from `IntType` to `StrType`, with no
second argument is a list of parameter selectors that declare the types of the input parameters for
the `@rule`. In this case, because the Product type is `StringType` and there is one parameter
selector (for `IntType`), this `@rule` represents a conversion from `IntType` to `StrType`, with no
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"parameter selectors" is a very good edit, this leaves the opportunity to expand it later as needed while making it extremely clear what those types are doing there.

other inputs.

When the engine statically checks whether it can use this `@rule` to create a string for a
Expand All @@ -73,7 +73,7 @@ definitions to provide a unique and descriptive type is strongly recommended:
```python
class FormattedInt(datatype(['content'])): pass

@rule(FormattedInt, [Select(IntType)])
@rule(FormattedInt, [IntType])
def int_to_str(an_int):
return FormattedInt('{}'.format(an_int))

Expand Down Expand Up @@ -113,15 +113,15 @@ formalize the assumptions made about the value of an object into a specific type
just wraps a single field. The `datatype()` function makes it simple and efficient to apply that
strategy.

### Selectors and Gets
### Parameter selectors and Gets

As demonstrated above, the `Selector` classes select `@rule` inputs in the context of a particular
As demonstrated above, parameter selectors select `@rule` inputs in the context of a particular
`Subject` (and its `Variants`: discussed below). But it is frequently necessary to "change" the
subject and request products for subjects other than the one that the `@rule` is running for.

In cases where this is necessary, `@rule`s may be written as coroutines (ie, using the python
`yield` statement) that yield "`Get` requests" that request products for other subjects. Just like
`@rule` parameter Selectors, `Get` requests instantiated in the body of an `@rule` are statically
`@rule` parameter selectors, `Get` requests instantiated in the body of an `@rule` are statically
checked to be satisfiable in the set of installed `@rule`s.

#### Example
Expand All @@ -130,7 +130,7 @@ For example, you could declare an `@rule` that requests FileContent for each ent
and then concatentates that content into a (typed) string:

```python
@rule(ConcattedFiles, [Select(Files)])
@rule(ConcattedFiles, [Files])
def concat(files):
file_content_list = yield [Get(FileContent, File(f)) for f in files]
yield ConcattedFiles(''.join(fc.content for fc in file_content_list))
Expand Down Expand Up @@ -184,8 +184,8 @@ given set of rules.
In general, there are three types of rules you can define:

1. an `@rule`, which has a single product type and selects its inputs as described above.
2. a `SingletonRule`, which matches a product type with a value so the type can then be `Select`ed
in an `@rule`.
2. a `SingletonRule`, which matches a product type with a value so the type can then be selected
as a parameter to an `@rule`.
3. a `RootRule`, which declares a type that can be used as a *subject*, which means it can be
provided as an input to a `product_request()`.

Expand Down
10 changes: 5 additions & 5 deletions src/python/pants/engine/build_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from pants.engine.objects import Locatable, SerializableFactory, Validatable
from pants.engine.parser import TargetAdaptorContainer
from pants.engine.rules import RootRule, SingletonRule, rule
from pants.engine.selectors import Get, Select
from pants.engine.selectors import Get
from pants.engine.struct import Struct
from pants.util.collections_abc_backport import MutableMapping, MutableSequence
from pants.util.objects import TypeConstraintError, datatype
Expand All @@ -40,7 +40,7 @@ def _key_func(entry):
return key


@rule(AddressFamily, [Select(AddressMapper), Select(Dir)])
@rule(AddressFamily, [AddressMapper, Dir])
def parse_address_family(address_mapper, directory):
"""Given an AddressMapper and a directory, return an AddressFamily.

Expand Down Expand Up @@ -89,7 +89,7 @@ def _raise_did_you_mean(address_family, name, source=None):
raise resolve_error


@rule(UnhydratedStruct, [Select(AddressMapper), Select(Address)])
@rule(UnhydratedStruct, [AddressMapper, Address])
def resolve_unhydrated_struct(address_mapper, address):
"""Given an AddressMapper and an Address, resolve an UnhydratedStruct.

Expand Down Expand Up @@ -135,7 +135,7 @@ def collect_dependencies(item):
dependencies)


@rule(TargetAdaptorContainer, [Select(AddressMapper), Select(UnhydratedStruct)])
@rule(TargetAdaptorContainer, [AddressMapper, UnhydratedStruct])
def hydrate_struct(address_mapper, unhydrated_struct):
"""Hydrates a Struct from an UnhydratedStruct and its satisfied embedded addressable deps.

Expand Down Expand Up @@ -208,7 +208,7 @@ def _hydrate(item_type, spec_path, **kwargs):
return item


@rule(BuildFileAddresses, [Select(AddressMapper), Select(Specs)])
@rule(BuildFileAddresses, [AddressMapper, Specs])
def addresses_from_address_families(address_mapper, specs):
"""Given an AddressMapper and list of Specs, return matching BuildFileAddresses.

Expand Down
3 changes: 1 addition & 2 deletions src/python/pants/engine/isolated_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from pants.engine.fs import Digest
from pants.engine.rules import RootRule, rule
from pants.engine.selectors import Select
from pants.util.objects import Exactly, TypeCheckError, datatype


Expand Down Expand Up @@ -113,7 +112,7 @@ def __init__(self, exit_code, stdout, stderr, process_description):
super(ProcessExecutionFailure, self).__init__(msg)


@rule(ExecuteProcessResult, [Select(FallibleExecuteProcessResult), Select(ExecuteProcessRequest)])
@rule(ExecuteProcessResult, [FallibleExecuteProcessResult, ExecuteProcessRequest])
def fallible_to_exec_result_or_raise(fallible_result, request):
"""Converts a FallibleExecuteProcessResult to a ExecuteProcessResult or raises an error."""

Expand Down
Loading