Skip to content

Commit

Permalink
[internal] Rollout Options v2 to core/ (pantsbuild#14641)
Browse files Browse the repository at this point in the history
[ci skip-rust]
  • Loading branch information
thejcannon authored Feb 27, 2022
1 parent 54edbb1 commit 86157e1
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 432 deletions.
19 changes: 5 additions & 14 deletions src/python/pants/core/goals/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from pants.engine.rules import Get, MultiGet, QueryRule, collect_rules, goal_rule
from pants.engine.target import Targets
from pants.engine.unions import UnionMembership, union
from pants.option.option_types import StrListOption
from pants.util.logging import LogLevel
from pants.util.memo import memoized_property
from pants.util.meta import frozen_after_init
Expand Down Expand Up @@ -145,20 +146,10 @@ class CheckSubsystem(GoalSubsystem):
def activated(cls, union_membership: UnionMembership) -> bool:
return CheckRequest in union_membership

@classmethod
def register_options(cls, register) -> None:
super().register_options(register)
register(
"--only",
type=list,
member_type=str,
default=[],
help=only_option_help("check", "checkers", "mypy", "javac"),
)

@property
def only(self) -> tuple[str, ...]:
return tuple(self.options.only)
only = StrListOption(
"--only",
help=only_option_help("check", "checkers", "mypy", "javac"),
)


class Check(Goal):
Expand Down
38 changes: 12 additions & 26 deletions src/python/pants/core/goals/fmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import itertools
from collections import defaultdict
from dataclasses import dataclass
from typing import TypeVar, cast
from typing import TypeVar

from pants.core.goals.style_request import (
StyleRequest,
Expand All @@ -23,6 +23,7 @@
from pants.engine.rules import Get, MultiGet, collect_rules, goal_rule, rule
from pants.engine.target import SourcesField, Targets
from pants.engine.unions import UnionMembership, union
from pants.option.option_types import IntOption, StrListOption
from pants.util.collections import partition_sequentially
from pants.util.logging import LogLevel
from pants.util.strutil import strip_v2_chroot_path
Expand Down Expand Up @@ -135,31 +136,16 @@ class FmtSubsystem(GoalSubsystem):
def activated(cls, union_membership: UnionMembership) -> bool:
return FmtRequest in union_membership

@classmethod
def register_options(cls, register) -> None:
super().register_options(register)
register(
"--only",
type=list,
member_type=str,
default=[],
help=only_option_help("fmt", "formatter", "isort", "shfmt"),
)
register(
"--batch-size",
advanced=True,
type=int,
default=128,
help=style_batch_size_help(uppercase="Formatter", lowercase="formatter"),
)

@property
def only(self) -> tuple[str, ...]:
return tuple(self.options.only)

@property
def batch_size(self) -> int:
return cast(int, self.options.batch_size)
only = StrListOption(
"--only",
help=only_option_help("fmt", "formatter", "isort", "shfmt"),
)
batch_size = IntOption(
"--batch-size",
advanced=True,
default=128,
help=style_batch_size_help(uppercase="Formatter", lowercase="formatter"),
)


class Fmt(Goal):
Expand Down
71 changes: 29 additions & 42 deletions src/python/pants/core/goals/generate_lockfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
from collections import defaultdict
from dataclasses import dataclass
from enum import Enum
from typing import ClassVar, Iterable, Sequence, cast
from typing import ClassVar, Iterable, Sequence

from pants.engine.collection import Collection
from pants.engine.fs import Digest, MergeDigests, Workspace
from pants.engine.goal import Goal, GoalSubsystem
from pants.engine.internals.selectors import Get, MultiGet
from pants.engine.rules import collect_rules, goal_rule
from pants.engine.unions import UnionMembership, union
from pants.option.option_types import StrListOption, StrOption
from pants.util.docutil import bin_name

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -294,47 +295,33 @@ def activated(cls, union_membership: UnionMembership) -> bool:
or KnownUserResolveNamesRequest in union_membership
)

@classmethod
def register_options(cls, register) -> None:
super().register_options(register)
register(
"--resolve",
type=list,
member_type=str,
advanced=False,
help=(
"Only generate lockfiles for the specified resolve(s).\n\n"
"Resolves are the logical names for the different lockfiles used in your project. "
"For your own code's dependencies, these come from the option "
"`[python].resolves`. For tool lockfiles, resolve "
"names are the options scope for that tool such as `black`, `pytest`, and "
"`mypy-protobuf`.\n\n"
f"For example, you can run `{bin_name()} generate-lockfiles --resolve=black "
"--resolve=pytest --resolve=data-science` to only generate lockfiles for those "
"two tools and your resolve named `data-science`.\n\n"
"If you specify an invalid resolve name, like 'fake', Pants will output all "
"possible values.\n\n"
"If not specified, Pants will generate lockfiles for all resolves."
),
)
register(
"--custom-command",
advanced=True,
type=str,
default=None,
help=(
"If set, lockfile headers will say to run this command to regenerate the lockfile, "
f"rather than running `{bin_name()} generate-lockfiles --resolve=<name>` like normal."
),
)

@property
def resolve_names(self) -> tuple[str, ...]:
return tuple(self.options.resolve)

@property
def custom_command(self) -> str | None:
return cast("str | None", self.options.custom_command)
resolve_names = StrListOption(
"--resolve",
advanced=False,
help=(
"Only generate lockfiles for the specified resolve(s).\n\n"
"Resolves are the logical names for the different lockfiles used in your project. "
"For your own code's dependencies, these come from the option "
"`[python].resolves`. For tool lockfiles, resolve "
"names are the options scope for that tool such as `black`, `pytest`, and "
"`mypy-protobuf`.\n\n"
f"For example, you can run `{bin_name()} generate-lockfiles --resolve=black "
"--resolve=pytest --resolve=data-science` to only generate lockfiles for those "
"two tools and your resolve named `data-science`.\n\n"
"If you specify an invalid resolve name, like 'fake', Pants will output all "
"possible values.\n\n"
"If not specified, Pants will generate lockfiles for all resolves."
),
)
custom_command = StrOption(
"--custom-command",
advanced=True,
default=None,
help=(
"If set, lockfile headers will say to run this command to regenerate the lockfile, "
f"rather than running `{bin_name()} generate-lockfiles --resolve=<name>` like normal."
),
)


class GenerateLockfilesGoal(Goal):
Expand Down
36 changes: 11 additions & 25 deletions src/python/pants/core/goals/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from pants.engine.rules import Get, MultiGet, collect_rules, goal_rule
from pants.engine.target import FieldSet, Targets
from pants.engine.unions import UnionMembership, union
from pants.option.option_types import IntOption, StrListOption
from pants.util.collections import partition_sequentially
from pants.util.logging import LogLevel
from pants.util.memo import memoized_property
Expand Down Expand Up @@ -164,31 +165,16 @@ class LintSubsystem(GoalSubsystem):
def activated(cls, union_membership: UnionMembership) -> bool:
return LintTargetsRequest in union_membership or LintFilesRequest in union_membership

@classmethod
def register_options(cls, register) -> None:
super().register_options(register)
register(
"--only",
type=list,
member_type=str,
default=[],
help=only_option_help("lint", "linter", "flake8", "shellcheck"),
)
register(
"--batch-size",
advanced=True,
type=int,
default=128,
help=style_batch_size_help(uppercase="Linter", lowercase="linter"),
)

@property
def only(self) -> tuple[str, ...]:
return tuple(self.options.only)

@property
def batch_size(self) -> int:
return cast(int, self.options.batch_size)
only = StrListOption(
"--only",
help=only_option_help("lint", "linter", "flake8", "shellcheck"),
)
batch_size = IntOption(
"--batch-size",
advanced=True,
default=128,
help=style_batch_size_help(uppercase="Linter", lowercase="linter"),
)


class Lint(Goal):
Expand Down
20 changes: 7 additions & 13 deletions src/python/pants/core/goals/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async def publish_example(request: PublishToMyRepoRequest, ...) -> PublishProces
from abc import ABCMeta
from dataclasses import asdict, dataclass, field, is_dataclass, replace
from itertools import chain
from typing import ClassVar, Generic, Type, TypeVar, cast
from typing import ClassVar, Generic, Type, TypeVar

from typing_extensions import final

Expand All @@ -44,6 +44,7 @@ async def publish_example(request: PublishToMyRepoRequest, ...) -> PublishProces
TargetRootsToFieldSetsRequest,
)
from pants.engine.unions import UnionMembership, UnionRule, union
from pants.option.option_types import StrOption
from pants.util.frozendict import FrozenDict

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -170,18 +171,11 @@ class PublishSubsystem(GoalSubsystem):
def activated(cls, union_membership: UnionMembership) -> bool:
return PackageFieldSet in union_membership and PublishFieldSet in union_membership

@classmethod
def register_options(cls, register) -> None:
super().register_options(register)
register(
"--output",
type=str,
help="Filename for JSON structured publish information.",
)

@property
def output(self) -> str | None:
return cast("str|None", self.options.output)
output = StrOption(
"--output",
default=None,
help="Filename for JSON structured publish information.",
)


class Publish(Goal):
Expand Down
36 changes: 12 additions & 24 deletions src/python/pants/core/goals/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from abc import ABC
from dataclasses import dataclass
from pathlib import PurePath
from typing import ClassVar, Iterable, Mapping, Optional, Tuple, cast
from typing import ClassVar, Iterable, Mapping, Optional, Tuple

from pants.base.build_root import BuildRoot
from pants.engine.addresses import Addresses
Expand All @@ -20,6 +20,7 @@
from pants.engine.target import Targets
from pants.engine.unions import UnionMembership, union
from pants.option.global_options import GlobalOptions
from pants.option.option_types import BoolOption, StrOption
from pants.util.contextutil import temporary_dir
from pants.util.frozendict import FrozenDict
from pants.util.memo import memoized_property
Expand Down Expand Up @@ -55,29 +56,16 @@ class ReplSubsystem(GoalSubsystem):
def activated(cls, union_membership: UnionMembership) -> bool:
return ReplImplementation in union_membership

@classmethod
def register_options(cls, register) -> None:
super().register_options(register)
register(
"--shell",
type=str,
default=None,
help="Override the automatically-detected REPL program for the target(s) specified.",
)
register(
"--restartable",
type=bool,
default=False,
help="True if the REPL should be restarted if its inputs have changed.",
)

@property
def shell(self) -> Optional[str]:
return cast(Optional[str], self.options.shell)

@property
def restartable(self) -> bool:
return cast(bool, self.options.restartable)
shell = StrOption(
"--shell",
default=None,
help="Override the automatically-detected REPL program for the target(s) specified.",
)
restartable = BoolOption(
"--restartable",
default=False,
help="True if the REPL should be restarted if its inputs have changed.",
)


class Repl(Goal):
Expand Down
41 changes: 13 additions & 28 deletions src/python/pants/core/goals/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from abc import ABCMeta
from dataclasses import dataclass
from pathlib import PurePath
from typing import Iterable, Mapping, Optional, Tuple, cast
from typing import Iterable, Mapping, Optional, Tuple

from pants.base.build_root import BuildRoot
from pants.build_graph.address import Address
Expand All @@ -22,8 +22,8 @@
WrappedTarget,
)
from pants.engine.unions import UnionMembership, union
from pants.option.custom_types import shell_str
from pants.option.global_options import GlobalOptions
from pants.option.option_types import ArgsListOption, BoolOption
from pants.util.contextutil import temporary_dir
from pants.util.frozendict import FrozenDict
from pants.util.meta import frozen_after_init
Expand Down Expand Up @@ -81,32 +81,17 @@ class RunSubsystem(GoalSubsystem):
def activated(cls, union_membership: UnionMembership) -> bool:
return RunFieldSet in union_membership

@classmethod
def register_options(cls, register) -> None:
super().register_options(register)
register(
"--args",
type=list,
member_type=shell_str,
passthrough=True,
help="Arguments to pass directly to the executed target, e.g. "
'`--run-args="val1 val2 --debug"`',
)
register(
"--cleanup",
type=bool,
default=True,
help="Whether to clean up the temporary directory in which the binary is chrooted. "
"Set to false to retain the directory, e.g., for debugging.",
)

@property
def args(self) -> Tuple[str, ...]:
return tuple(self.options.args)

@property
def cleanup(self) -> bool:
return cast(bool, self.options.cleanup)
args = ArgsListOption(
passthrough=True,
help="Arguments to pass directly to the executed target, e.g. "
'`--run-args="val1 val2 --debug"`',
)
cleanup = BoolOption(
"--cleanup",
default=True,
help="Whether to clean up the temporary directory in which the binary is chrooted. "
"Set to false to retain the directory, e.g., for debugging.",
)


class Run(Goal):
Expand Down
Loading

0 comments on commit 86157e1

Please sign in to comment.