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

Deprecate python_binary in favor of pex_binary #10939

Merged
merged 3 commits into from
Oct 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 11 additions & 11 deletions build-support/bin/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ python_tests(
timeout=90,
)

python_binary(
pex_binary(
name = 'bootstrap_and_deploy_ci_pants_pex',
sources = ['bootstrap_and_deploy_ci_pants_pex.py'],
)

python_binary(
pex_binary(
name = 'check_banned_imports',
sources = ['check_banned_imports.py'],
)

python_binary(
pex_binary(
name = 'check_inits',
sources = ['check_inits.py'],
)

python_binary(
pex_binary(
name = 'ci',
sources = ['ci.py'],
)
Expand All @@ -37,17 +37,17 @@ python_library(
sources = ['common.py'],
)

python_binary(
pex_binary(
name = 'deploy_to_s3',
sources = ['deploy_to_s3.py'],
)

python_binary(
pex_binary(
name = 'generate_travis_yml',
sources = ['generate_travis_yml.py'],
)

python_binary(
pex_binary(
name = 'generate_docs',
sources = ['generate_docs.py'],
dependencies = [
Expand All @@ -60,23 +60,23 @@ resources(
sources = ['docs_templates/*.mustache'],
)

python_binary(
pex_binary(
name = 'get_rbe_token',
sources = ['get_rbe_token.py'],
)

python_binary(
pex_binary(
name='reversion',
sources=["reversion.py"],
)

python_binary(
pex_binary(
name = 'shellcheck',
sources = ['shellcheck.py'],
)

# TODO: rename this to `release.py` once done porting Bash to Python.
python_binary(
pex_binary(
name = "packages",
sources = ["packages.py"],
)
2 changes: 1 addition & 1 deletion examples/src/python/example/hello/main/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Licensed under the Apache License, Version 2.0 (see LICENSE).

# Like Hello world, but built with Pants.
python_binary(
pex_binary(
dependencies=[
'examples/src/python/example/hello/greet:greet',
':lib',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
from typing import Tuple, cast

from pants.backend.python.target_types import (
PexAlwaysWriteCache,
PexEmitWarnings,
PexIgnoreErrors,
PexInheritPath,
PexShebang,
PexZipSafe,
PythonBinaryDefaults,
PythonBinarySources,
PythonEntryPoint,
PexAlwaysWriteCacheField,
PexBinaryDefaults,
PexBinarySources,
PexEmitWarningsField,
PexEntryPointField,
PexIgnoreErrorsField,
PexInheritPathField,
)
from pants.backend.python.target_types import PythonPlatforms as PythonPlatformsField
from pants.backend.python.target_types import PexPlatformsField as PythonPlatformsField
from pants.backend.python.target_types import PexShebangField, PexZipSafeField
from pants.backend.python.util_rules.pex import PexPlatforms, TwoStepPex
from pants.backend.python.util_rules.pex_from_targets import (
PexFromTargetsRequest,
Expand All @@ -40,28 +39,26 @@


@dataclass(frozen=True)
class PythonBinaryFieldSet(PackageFieldSet, BinaryFieldSet, RunFieldSet):
required_fields = (PythonEntryPoint, PythonBinarySources)
class PexBinaryFieldSet(PackageFieldSet, BinaryFieldSet, RunFieldSet):
required_fields = (PexEntryPointField, PexBinarySources)

sources: PythonBinarySources
entry_point: PythonEntryPoint
sources: PexBinarySources
entry_point: PexEntryPointField

output_path: OutputPathField
always_write_cache: PexAlwaysWriteCache
emit_warnings: PexEmitWarnings
ignore_errors: PexIgnoreErrors
inherit_path: PexInheritPath
shebang: PexShebang
zip_safe: PexZipSafe
always_write_cache: PexAlwaysWriteCacheField
emit_warnings: PexEmitWarningsField
ignore_errors: PexIgnoreErrorsField
inherit_path: PexInheritPathField
shebang: PexShebangField
zip_safe: PexZipSafeField
platforms: PythonPlatformsField

def generate_additional_args(
self, python_binary_defaults: PythonBinaryDefaults
) -> Tuple[str, ...]:
def generate_additional_args(self, pex_binary_defaults: PexBinaryDefaults) -> Tuple[str, ...]:
args = []
if self.always_write_cache.value is True:
args.append("--always-write-cache")
if self.emit_warnings.value_or_global_default(python_binary_defaults) is False:
if self.emit_warnings.value_or_global_default(pex_binary_defaults) is False:
args.append("--no-emit-warnings")
if self.ignore_errors.value is True:
args.append("--ignore-errors")
Expand All @@ -75,9 +72,9 @@ def generate_additional_args(


@rule(level=LogLevel.DEBUG)
async def package_python_binary(
field_set: PythonBinaryFieldSet,
python_binary_defaults: PythonBinaryDefaults,
async def package_pex_binary(
field_set: PexBinaryFieldSet,
pex_binary_defaults: PexBinaryDefaults,
global_options: GlobalOptions,
) -> BuiltPackage:
entry_point = field_set.entry_point.value
Expand All @@ -97,7 +94,7 @@ async def package_python_binary(
SourceRootRequest,
SourceRootRequest.for_file(entry_point_path),
)
entry_point = PythonBinarySources.translate_source_file_to_entry_point(
entry_point = PexBinarySources.translate_source_file_to_entry_point(
os.path.relpath(entry_point_path, source_root.path)
)

Expand All @@ -115,22 +112,22 @@ async def package_python_binary(
entry_point=entry_point,
platforms=PexPlatforms.create_from_platforms_field(field_set.platforms),
output_filename=output_filename,
additional_args=field_set.generate_additional_args(python_binary_defaults),
additional_args=field_set.generate_additional_args(pex_binary_defaults),
)
),
)
return BuiltPackage(two_step_pex.pex.digest, (BuiltPackageArtifact(output_filename),))


@rule(level=LogLevel.DEBUG)
async def create_python_binary(field_set: PythonBinaryFieldSet) -> CreatedBinary:
async def create_pex_binary(field_set: PexBinaryFieldSet) -> CreatedBinary:
pex = await Get(BuiltPackage, PackageFieldSet, field_set)
return CreatedBinary(pex.digest, cast(str, pex.artifacts[0].relpath))


def rules():
return [
*collect_rules(),
UnionRule(PackageFieldSet, PythonBinaryFieldSet),
UnionRule(BinaryFieldSet, PythonBinaryFieldSet),
UnionRule(PackageFieldSet, PexBinaryFieldSet),
UnionRule(BinaryFieldSet, PexBinaryFieldSet),
]
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import pytest

from pants.backend.python.dependency_inference import rules as dependency_inference_rules
from pants.backend.python.goals import package_python_binary, pytest_runner
from pants.backend.python.goals import package_pex_binary, pytest_runner
from pants.backend.python.goals.coverage_py import create_coverage_config
from pants.backend.python.goals.pytest_runner import PythonTestFieldSet
from pants.backend.python.target_types import (
Expand Down Expand Up @@ -40,7 +40,7 @@ def rule_runner() -> RuleRunner:
*dependency_inference_rules.rules(), # For conftest detection.
*distdir.rules(),
*binary.rules(),
*package_python_binary.rules(),
*package_pex_binary.rules(),
get_filtered_environment,
QueryRule(TestResult, (PythonTestFieldSet,)),
QueryRule(TestDebugRequest, (PythonTestFieldSet,)),
Expand Down Expand Up @@ -110,13 +110,13 @@ def create_test_target(
return tgt


def create_python_binary_target(rule_runner: RuleRunner, source_file: FileContent) -> None:
def create_pex_binary_target(rule_runner: RuleRunner, source_file: FileContent) -> None:
rule_runner.create_file(source_file.path, source_file.content.decode())
rule_runner.add_to_build_file(
relpath=PACKAGE,
target=dedent(
f"""\
python_binary(
pex_binary(
name='bin',
sources=['{PurePath(source_file.path).name}'],
)
Expand Down Expand Up @@ -442,7 +442,7 @@ def test_args():


def test_runtime_package_dependency(rule_runner: RuleRunner) -> None:
create_python_binary_target(rule_runner, BINARY_SOURCE)
create_pex_binary_target(rule_runner, BINARY_SOURCE)
rule_runner.create_file(
f"{PACKAGE}/test_binary_call.py",
dedent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import os

from pants.backend.python.goals.package_python_binary import PythonBinaryFieldSet
from pants.backend.python.target_types import PythonBinaryDefaults, PythonBinarySources
from pants.backend.python.goals.package_pex_binary import PexBinaryFieldSet
from pants.backend.python.target_types import PexBinaryDefaults, PexBinarySources
from pants.backend.python.util_rules.pex import Pex, PexRequest
from pants.backend.python.util_rules.pex_environment import PexEnvironment
from pants.backend.python.util_rules.pex_from_targets import PexFromTargetsRequest
Expand All @@ -23,9 +23,9 @@


@rule(level=LogLevel.DEBUG)
async def create_python_binary_run_request(
field_set: PythonBinaryFieldSet,
python_binary_defaults: PythonBinaryDefaults,
async def create_pex_binary_run_request(
field_set: PexBinaryFieldSet,
pex_binary_defaults: PexBinaryDefaults,
pex_env: PexEnvironment,
) -> RunRequest:
entry_point = field_set.entry_point.value
Expand All @@ -45,7 +45,7 @@ async def create_python_binary_run_request(
SourceRootRequest,
SourceRootRequest.for_file(entry_point_path),
)
entry_point = PythonBinarySources.translate_source_file_to_entry_point(
entry_point = PexBinarySources.translate_source_file_to_entry_point(
os.path.relpath(entry_point_path, source_root.path)
)
transitive_targets = await Get(TransitiveTargets, TransitiveTargetsRequest([field_set.address]))
Expand All @@ -70,7 +70,7 @@ async def create_python_binary_run_request(
PexRequest(
output_filename=output_filename,
interpreter_constraints=requirements_pex_request.interpreter_constraints,
additional_args=field_set.generate_additional_args(python_binary_defaults),
additional_args=field_set.generate_additional_args(pex_binary_defaults),
internal_only=True,
# Note that the entry point file is not in the Pex itself, but on the
# PEX_PATH. This works fine!
Expand Down Expand Up @@ -105,4 +105,4 @@ def in_chroot(relpath: str) -> str:


def rules():
return [*collect_rules(), UnionRule(RunFieldSet, PythonBinaryFieldSet)]
return [*collect_rules(), UnionRule(RunFieldSet, PexBinaryFieldSet)]
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
@pytest.mark.parametrize(
"tgt_content",
[
"python_binary(sources=['app.py'])",
"python_binary(sources=['app.py'], entry_point='project.app')",
"python_binary(sources=['app.py'], entry_point='project.app:main')",
"pex_binary(sources=['app.py'])",
"pex_binary(sources=['app.py'], entry_point='project.app')",
"pex_binary(sources=['app.py'], entry_point='project.app:main')",
],
)
def test_run_sample_script(tgt_content: str) -> None:
"""Test that we properly run a `python_binary` target.
"""Test that we properly run a `pex_binary` target.

This checks a few things:
- We can handle source roots.
Expand Down
4 changes: 2 additions & 2 deletions src/python/pants/backend/python/goals/setup_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from pants.backend.python.macros.python_artifact import PythonArtifact
from pants.backend.python.subsystems.setuptools import Setuptools
from pants.backend.python.target_types import (
PythonEntryPoint,
PexEntryPointField,
PythonInterpreterCompatibility,
PythonProvidesField,
PythonRequirementsField,
Expand Down Expand Up @@ -620,7 +620,7 @@ async def generate_chroot(request: SetupPyChrootRequest) -> SetupPyChroot:
Targets, UnparsedAddressInputs(key_to_binary_spec.values(), owning_address=target.address)
)
for key, binary in zip(key_to_binary_spec.keys(), binaries):
binary_entry_point = binary.get(PythonEntryPoint).value
binary_entry_point = binary.get(PexEntryPointField).value
if not binary_entry_point:
raise InvalidEntryPoint(
f"The binary {key} exported by {target.address} is not a valid entry point."
Expand Down
4 changes: 2 additions & 2 deletions src/python/pants/backend/python/goals/setup_py_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def test_generate_chroot(chroot_rule_runner: RuleRunner) -> None:
"""
python_library()

python_binary(name="bin", entry_point="foo.qux.bin")
pex_binary(name="bin", entry_point="foo.qux.bin")
"""
),
)
Expand Down Expand Up @@ -226,7 +226,7 @@ def test_invalid_binary(chroot_rule_runner: RuleRunner) -> None:
textwrap.dedent(
"""
python_library(name='not_a_binary', sources=[])
python_binary(name='no_entrypoint')
pex_binary(name='no_entrypoint')
python_distribution(
name='invalid_bin1',
provides=setup_py(
Expand Down
4 changes: 2 additions & 2 deletions src/python/pants/backend/python/macros/python_artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ def with_binaries(self, *args, **kw):
my_command = ':my_library_bin'
)

This adds a console_script entry_point for the python_binary target
This adds a console_script entry_point for the pex_binary target
pointed at by :my_library_bin. Currently only supports
python_binaries that specify entry_point explicitly instead of source.
pex_binaries that specify entry_point explicitly instead of source.

Also can take a dictionary, e.g.
with_binaries({'my-command': ':my_library_bin'})
Expand Down
Loading