Skip to content

Commit

Permalink
Add PythonToolBase.to_pex_request() to DRY tool installation (#14610)
Browse files Browse the repository at this point in the history
[ci skip-rust]
[ci skip-build-wheels]
  • Loading branch information
Eric-Arellano authored Feb 25, 2022
1 parent c577a90 commit 8f62a1c
Show file tree
Hide file tree
Showing 21 changed files with 73 additions and 192 deletions.
10 changes: 1 addition & 9 deletions src/python/pants/backend/awslambda/python/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,8 @@ async def package_python_awslambda(
additional_lockfile_args=additional_pex_args,
)

lambdex_request = PexRequest(
output_filename="lambdex.pex",
internal_only=True,
requirements=lambdex.pex_requirements(),
interpreter_constraints=lambdex.interpreter_constraints,
main=lambdex.main,
)

lambdex_pex, pex_result, handler, transitive_targets = await MultiGet(
Get(VenvPex, PexRequest, lambdex_request),
Get(VenvPex, PexRequest, lambdex.to_pex_request()),
Get(Pex, PexFromTargetsRequest, pex_request),
Get(ResolvedPythonAwsHandler, ResolvePythonAwsHandlerRequest(field_set.handler)),
Get(TransitiveTargets, TransitiveTargetsRequest([field_set.address])),
Expand Down
9 changes: 2 additions & 7 deletions src/python/pants/backend/codegen/protobuf/python/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pants.backend.codegen.protobuf.target_types import ProtobufGrpcToggleField, ProtobufSourceField
from pants.backend.python.target_types import PythonSourceField
from pants.backend.python.util_rules import pex
from pants.backend.python.util_rules.pex import PexRequest, PexResolveInfo, VenvPex, VenvPexRequest
from pants.backend.python.util_rules.pex import PexResolveInfo, VenvPex, VenvPexRequest
from pants.backend.python.util_rules.pex_environment import PexEnvironment
from pants.core.util_rules.external_tool import DownloadedExternalTool, ExternalToolRequest
from pants.core.util_rules.source_files import SourceFilesRequest
Expand Down Expand Up @@ -98,14 +98,9 @@ async def generate_python_from_protobuf(
protoc_gen_mypy_script = "protoc-gen-mypy"
protoc_gen_mypy_grpc_script = "protoc-gen-mypy_grpc"
mypy_pex = None
mypy_request = PexRequest(
output_filename="mypy_protobuf.pex",
internal_only=True,
requirements=python_protobuf_mypy_plugin.pex_requirements(),
interpreter_constraints=python_protobuf_mypy_plugin.interpreter_constraints,
)

if python_protobuf_subsystem.mypy_plugin:
mypy_request = python_protobuf_mypy_plugin.to_pex_request()
mypy_pex = await Get(
VenvPex,
VenvPexRequest(bin_names=[protoc_gen_mypy_script], pex_request=mypy_request),
Expand Down
14 changes: 4 additions & 10 deletions src/python/pants/backend/docker/subsystems/dockerfile_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,29 +67,23 @@ async def setup_parser(dockerfile_parser: DockerfileParser) -> ParserSetup:
parser_script_content = pkgutil.get_data(_DOCKERFILE_PACKAGE, _DOCKERFILE_SANDBOX_TOOL)
if not parser_script_content:
raise ValueError(
"Unable to find source to {_DOCKERFILE_SANDBOX_TOOL!r} in {_DOCKERFILE_PACKAGE}."
f"Unable to find source to {_DOCKERFILE_SANDBOX_TOOL!r} in {_DOCKERFILE_PACKAGE}."
)

parser_content = FileContent(
path="__pants_df_parser.py",
content=parser_script_content,
is_executable=True,
)

parser_digest = await Get(Digest, CreateDigest([parser_content]))

parser_pex = await Get(
VenvPex,
PexRequest(
output_filename="dockerfile_parser.pex",
internal_only=True,
requirements=dockerfile_parser.pex_requirements(),
interpreter_constraints=dockerfile_parser.interpreter_constraints,
main=EntryPoint(PurePath(parser_content.path).stem),
sources=parser_digest,
PexRequest,
dockerfile_parser.to_pex_request(
main=EntryPoint(PurePath(parser_content.path).stem), sources=parser_digest
),
)

return ParserSetup(parser_pex)


Expand Down
11 changes: 1 addition & 10 deletions src/python/pants/backend/python/goals/coverage_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,16 +375,7 @@ class CoverageSetup:

@rule
async def setup_coverage(coverage: CoverageSubsystem) -> CoverageSetup:
pex = await Get(
VenvPex,
PexRequest(
output_filename="coverage.pex",
internal_only=True,
requirements=coverage.pex_requirements(),
interpreter_constraints=coverage.interpreter_constraints,
main=coverage.main,
),
)
pex = await Get(VenvPex, PexRequest, coverage.to_pex_request())
return CoverageSetup(pex)


Expand Down
10 changes: 3 additions & 7 deletions src/python/pants/backend/python/goals/lockfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,9 @@ async def generate_lockfile(

_poetry_pex = await Get(
VenvPex,
PexRequest(
output_filename="poetry.pex",
internal_only=True,
requirements=poetry_subsystem.pex_requirements(),
interpreter_constraints=poetry_subsystem.interpreter_constraints,
main=EntryPoint(PurePath(POETRY_LAUNCHER.path).stem),
sources=_launcher_digest,
PexRequest,
poetry_subsystem.to_pex_request(
main=EntryPoint(PurePath(POETRY_LAUNCHER.path).stem), sources=_launcher_digest
),
)

Expand Down
11 changes: 1 addition & 10 deletions src/python/pants/backend/python/goals/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,7 @@ async def twine_upload(
)

twine_pex, packages_digest, config_files = await MultiGet(
Get(
VenvPex,
PexRequest(
output_filename="twine.pex",
internal_only=True,
requirements=twine_subsystem.pex_requirements(),
interpreter_constraints=twine_subsystem.interpreter_constraints,
main=twine_subsystem.main,
),
),
Get(VenvPex, PexRequest, twine_subsystem.to_pex_request()),
Get(Digest, MergeDigests(pkg.digest for pkg in request.packages)),
Get(ConfigFiles, ConfigFilesRequest, twine_subsystem.config_request()),
)
Expand Down
9 changes: 1 addition & 8 deletions src/python/pants/backend/python/goals/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,7 @@ async def create_ipython_repl_request(
)

ipython_request = Get(
Pex,
PexRequest(
output_filename="ipython.pex",
main=ipython.main,
requirements=ipython.pex_requirements(),
interpreter_constraints=interpreter_constraints,
internal_only=True,
),
Pex, PexRequest, ipython.to_pex_request(interpreter_constraints=interpreter_constraints)
)

requirements_pex, sources, ipython_pex = await MultiGet(
Expand Down
11 changes: 1 addition & 10 deletions src/python/pants/backend/python/lint/autoflake/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,7 @@ def generate_argv(

@rule(level=LogLevel.DEBUG)
async def setup_autoflake(setup_request: SetupRequest, autoflake: Autoflake) -> Setup:
autoflake_pex_get = Get(
VenvPex,
PexRequest(
output_filename="autoflake.pex",
internal_only=True,
requirements=autoflake.pex_requirements(),
interpreter_constraints=autoflake.interpreter_constraints,
main=autoflake.main,
),
)
autoflake_pex_get = Get(VenvPex, PexRequest, autoflake.to_pex_request())

source_files_get = Get(
SourceFiles,
Expand Down
9 changes: 2 additions & 7 deletions src/python/pants/backend/python/lint/bandit/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,8 @@ async def bandit_lint_partition(partition: BanditPartition, bandit: Bandit) -> L

bandit_pex_get = Get(
VenvPex,
PexRequest(
output_filename="bandit.pex",
internal_only=True,
requirements=bandit.pex_requirements(),
interpreter_constraints=partition.interpreter_constraints,
main=bandit.main,
),
PexRequest,
bandit.to_pex_request(interpreter_constraints=partition.interpreter_constraints),
)

config_files_get = Get(ConfigFiles, ConfigFilesRequest, bandit.config_request)
Expand Down
9 changes: 2 additions & 7 deletions src/python/pants/backend/python/lint/black/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,8 @@ async def setup_black(

black_pex_get = Get(
VenvPex,
PexRequest(
output_filename="black.pex",
internal_only=True,
requirements=black.pex_requirements(),
interpreter_constraints=tool_interpreter_constraints,
main=black.main,
),
PexRequest,
black.to_pex_request(interpreter_constraints=tool_interpreter_constraints),
)

source_files_get = Get(
Expand Down
11 changes: 1 addition & 10 deletions src/python/pants/backend/python/lint/docformatter/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,7 @@ def generate_args(
@rule(level=LogLevel.DEBUG)
async def setup_docformatter(setup_request: SetupRequest, docformatter: Docformatter) -> Setup:

docformatter_pex_get = Get(
VenvPex,
PexRequest(
output_filename="docformatter.pex",
internal_only=True,
requirements=docformatter.pex_requirements(),
interpreter_constraints=docformatter.interpreter_constraints,
main=docformatter.main,
),
)
docformatter_pex_get = Get(VenvPex, PexRequest, docformatter.to_pex_request())
source_files_get = Get(
SourceFiles,
SourceFilesRequest(field_set.source for field_set in setup_request.request.field_sets),
Expand Down
10 changes: 3 additions & 7 deletions src/python/pants/backend/python/lint/flake8/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,10 @@ async def flake8_lint_partition(
) -> LintResult:
flake8_pex_get = Get(
VenvPex,
PexRequest(
output_filename="flake8.pex",
internal_only=True,
requirements=flake8.pex_requirements(
extra_requirements=first_party_plugins.requirement_strings,
),
PexRequest,
flake8.to_pex_request(
interpreter_constraints=partition.interpreter_constraints,
main=flake8.main,
extra_requirements=first_party_plugins.requirement_strings,
),
)
config_files_get = Get(ConfigFiles, ConfigFilesRequest, flake8.config_request)
Expand Down
11 changes: 1 addition & 10 deletions src/python/pants/backend/python/lint/isort/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,7 @@ def generate_argv(

@rule(level=LogLevel.DEBUG)
async def setup_isort(setup_request: SetupRequest, isort: Isort) -> Setup:
isort_pex_get = Get(
VenvPex,
PexRequest(
output_filename="isort.pex",
internal_only=True,
requirements=isort.pex_requirements(),
interpreter_constraints=isort.interpreter_constraints,
main=isort.main,
),
)
isort_pex_get = Get(VenvPex, PexRequest, isort.to_pex_request())
source_files_get = Get(
SourceFiles,
SourceFilesRequest(field_set.source for field_set in setup_request.request.field_sets),
Expand Down
9 changes: 3 additions & 6 deletions src/python/pants/backend/python/lint/pylint/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,10 @@ async def pylint_lint_partition(

pylint_pex_get = Get(
Pex,
PexRequest(
output_filename="pylint.pex",
internal_only=True,
requirements=pylint.pex_requirements(
extra_requirements=first_party_plugins.requirement_strings,
),
PexRequest,
pylint.to_pex_request(
interpreter_constraints=partition.interpreter_constraints,
extra_requirements=first_party_plugins.requirement_strings,
),
)

Expand Down
11 changes: 1 addition & 10 deletions src/python/pants/backend/python/lint/pyupgrade/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,7 @@ class PyUpgradeResult:

@rule(level=LogLevel.DEBUG)
async def run_pyupgrade(request: PyUpgradeRequest, pyupgrade: PyUpgrade) -> PyUpgradeResult:
pyupgrade_pex_get = Get(
VenvPex,
PexRequest(
output_filename="pyupgrade.pex",
internal_only=True,
requirements=pyupgrade.pex_requirements(),
interpreter_constraints=pyupgrade.interpreter_constraints,
main=pyupgrade.main,
),
)
pyupgrade_pex_get = Get(VenvPex, PexRequest, pyupgrade.to_pex_request())
source_files_get = Get(
SourceFiles,
SourceFilesRequest(field_set.source for field_set in request.field_sets),
Expand Down
11 changes: 1 addition & 10 deletions src/python/pants/backend/python/lint/yapf/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,7 @@ def generate_argv(source_files: SourceFiles, yapf: Yapf, check_only: bool) -> Tu

@rule(level=LogLevel.DEBUG)
async def setup_yapf(setup_request: SetupRequest, yapf: Yapf) -> Setup:
yapf_pex_get = Get(
VenvPex,
PexRequest(
output_filename="yapf.pex",
internal_only=True,
requirements=yapf.pex_requirements(),
interpreter_constraints=yapf.interpreter_constraints,
main=yapf.main,
),
)
yapf_pex_get = Get(VenvPex, PexRequest, yapf.to_pex_request())
source_files_get = Get(
SourceFiles,
SourceFilesRequest(field_set.source for field_set in setup_request.request.field_sets),
Expand Down
11 changes: 1 addition & 10 deletions src/python/pants/backend/python/packaging/pyoxidizer/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,7 @@ async def package_pyoxidizer_binary(
) -> BuiltPackage:
direct_deps, pyoxidizer_pex = await MultiGet(
Get(Targets, DependenciesRequest(field_set.dependencies)),
Get(
Pex,
PexRequest(
output_filename="pyoxidizer.pex",
internal_only=True,
requirements=pyoxidizer.pex_requirements(),
interpreter_constraints=pyoxidizer.interpreter_constraints,
main=pyoxidizer.main,
),
),
Get(Pex, PexRequest, pyoxidizer.to_pex_request()),
)

deps_field_sets = await Get(
Expand Down
35 changes: 34 additions & 1 deletion src/python/pants/backend/python/subsystems/python_tool_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@

from pants.backend.python.target_types import ConsoleScript, EntryPoint, MainSpecification
from pants.backend.python.util_rules.interpreter_constraints import InterpreterConstraints
from pants.backend.python.util_rules.pex import PexRequest
from pants.backend.python.util_rules.pex_requirements import (
PexRequirements,
ToolCustomLockfile,
ToolDefaultLockfile,
)
from pants.core.goals.generate_lockfiles import DEFAULT_TOOL_LOCKFILE, NO_TOOL_LOCKFILE
from pants.core.util_rules.lockfile_metadata import calculate_invalidation_digest
from pants.engine.fs import FileContent
from pants.engine.fs import Digest, FileContent
from pants.option.errors import OptionsError
from pants.option.subsystem import Subsystem
from pants.util.docutil import bin_name
Expand Down Expand Up @@ -193,6 +194,23 @@ def interpreter_constraints(self) -> InterpreterConstraints:
"""
return InterpreterConstraints(self.options.interpreter_constraints)

def to_pex_request(
self,
*,
interpreter_constraints: InterpreterConstraints | None = None,
extra_requirements: Iterable[str] = (),
main: MainSpecification | None = None,
sources: Digest | None = None,
) -> PexRequest:
return PexRequest(
output_filename=f"{self.options_scope.replace('-', '_')}.pex",
internal_only=True,
requirements=self.pex_requirements(extra_requirements=extra_requirements),
interpreter_constraints=interpreter_constraints or self.interpreter_constraints,
main=main,
sources=sources,
)


class PythonToolBase(PythonToolRequirementsBase):
"""Base class for subsystems that configure a python tool to be invoked out-of-process."""
Expand Down Expand Up @@ -242,3 +260,18 @@ def main(self) -> MainSpecification:
if not is_default_entry_point:
return EntryPoint.parse(cast(str, self.options.entry_point))
return self.default_main

def to_pex_request(
self,
*,
interpreter_constraints: InterpreterConstraints | None = None,
extra_requirements: Iterable[str] = (),
main: MainSpecification | None = None,
sources: Digest | None = None,
) -> PexRequest:
return super().to_pex_request(
interpreter_constraints=interpreter_constraints,
extra_requirements=extra_requirements,
main=main or self.main,
sources=sources,
)
10 changes: 3 additions & 7 deletions src/python/pants/backend/python/typecheck/mypy/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,10 @@ async def mypy_typecheck_partition(

mypy_pex_get = Get(
VenvPex,
PexRequest(
output_filename="mypy.pex",
internal_only=True,
main=mypy.main,
requirements=mypy.pex_requirements(
extra_requirements=first_party_plugins.requirement_strings,
),
PexRequest,
mypy.to_pex_request(
interpreter_constraints=tool_interpreter_constraints,
extra_requirements=first_party_plugins.requirement_strings,
),
)

Expand Down
Loading

0 comments on commit 8f62a1c

Please sign in to comment.