Skip to content

Commit

Permalink
Merge d08f89c into 582f667
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Arellano committed Jul 28, 2020
2 parents 582f667 + d08f89c commit e353ba1
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 107 deletions.
3 changes: 1 addition & 2 deletions src/python/pants/backend/python/rules/pytest_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@
from pants.core.util_rules.determine_source_files import SourceFiles, SpecifiedSourceFilesRequest
from pants.engine.addresses import Addresses
from pants.engine.fs import AddPrefix, Digest, DigestSubset, MergeDigests, PathGlobs, Snapshot
from pants.engine.interactive_process import InteractiveProcess
from pants.engine.internals.uuid import UUIDRequest
from pants.engine.process import FallibleProcessResult, Process
from pants.engine.process import FallibleProcessResult, InteractiveProcess, Process
from pants.engine.rules import Get, MultiGet, collect_rules, rule
from pants.engine.target import TransitiveTargets
from pants.engine.unions import UnionRule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from pants.core.util_rules import determine_source_files, strip_source_roots
from pants.engine.addresses import Address
from pants.engine.fs import DigestContents, FileContent
from pants.engine.interactive_process import InteractiveRunner
from pants.engine.process import InteractiveRunner
from pants.engine.rules import RootRule
from pants.engine.target import TargetWithOrigin
from pants.python.python_requirement import PythonRequirement
Expand Down Expand Up @@ -177,7 +177,7 @@ def run_pytest(
)
test_result = self.request_single_product(TestResult, params)
debug_request = self.request_single_product(TestDebugRequest, params)
debug_result = InteractiveRunner(self.scheduler).run_process(debug_request.process)
debug_result = InteractiveRunner(self.scheduler).run(debug_request.process)
if test_result.status == Status.SUCCESS:
assert debug_result.exit_code == 0
else:
Expand Down
4 changes: 2 additions & 2 deletions src/python/pants/core/goals/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pants.engine.console import Console
from pants.engine.fs import Digest, Workspace
from pants.engine.goal import Goal, GoalSubsystem
from pants.engine.interactive_process import InteractiveProcess, InteractiveRunner
from pants.engine.process import InteractiveProcess, InteractiveRunner
from pants.engine.rules import Get, collect_rules, goal_rule
from pants.engine.target import Field, Target, Targets, TransitiveTargets
from pants.engine.unions import UnionMembership, union
Expand Down Expand Up @@ -112,7 +112,7 @@ async def run_repl(
tmpdir_relative_path = PurePath(tmpdir).relative_to(build_root.path).as_posix()
exe_path = PurePath(tmpdir, request.binary_name).as_posix()
workspace.write_digest(request.digest, path_prefix=tmpdir_relative_path)
result = interactive_runner.run_process(
result = interactive_runner.run(
InteractiveProcess(argv=(exe_path,), env=request.env, run_in_workspace=True)
)

Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/core/goals/repl_integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pants.core.goals.repl import Repl
from pants.core.goals.repl import rules as repl_rules
from pants.core.util_rules import archive, external_tool, strip_source_roots
from pants.engine.interactive_process import InteractiveRunner
from pants.engine.process import InteractiveRunner
from pants.engine.rules import RootRule
from pants.testutil.goal_rule_test_base import GoalRuleTestBase

Expand Down
4 changes: 2 additions & 2 deletions src/python/pants/core/goals/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pants.engine.console import Console
from pants.engine.fs import Digest, Workspace
from pants.engine.goal import Goal, GoalSubsystem
from pants.engine.interactive_process import InteractiveProcess, InteractiveRunner
from pants.engine.process import InteractiveProcess, InteractiveRunner
from pants.engine.rules import Get, collect_rules, goal_rule
from pants.engine.target import TargetsToValidFieldSets, TargetsToValidFieldSetsRequest
from pants.option.custom_types import shell_str
Expand Down Expand Up @@ -107,7 +107,7 @@ async def run(
run_in_workspace=True,
)
try:
result = interactive_runner.run_process(process)
result = interactive_runner.run(process)
exit_code = result.exit_code
except Exception as e:
console.print_stderr(f"Exception when attempting to run {field_set.address}: {e!r}")
Expand Down
4 changes: 2 additions & 2 deletions src/python/pants/core/goals/run_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pants.core.goals.run import Run, RunRequest, RunSubsystem, run
from pants.engine.addresses import Address
from pants.engine.fs import CreateDigest, Digest, FileContent, Workspace
from pants.engine.interactive_process import InteractiveProcess, InteractiveRunner
from pants.engine.process import InteractiveProcess, InteractiveRunner
from pants.engine.target import (
Target,
TargetsToValidFieldSets,
Expand Down Expand Up @@ -97,7 +97,7 @@ def test_materialize_input_files(self) -> None:
process = InteractiveProcess(
argv=("./program.py",), run_in_workspace=False, input_digest=binary.digest,
)
result = interactive_runner.run_process(process)
result = interactive_runner.run(process)
assert result.exit_code == 0

def test_failed_run(self) -> None:
Expand Down
5 changes: 2 additions & 3 deletions src/python/pants/core/goals/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
from pants.engine.engine_aware import EngineAware
from pants.engine.fs import Digest, MergeDigests, Workspace
from pants.engine.goal import Goal, GoalSubsystem
from pants.engine.interactive_process import InteractiveProcess, InteractiveRunner
from pants.engine.process import FallibleProcessResult
from pants.engine.process import FallibleProcessResult, InteractiveProcess, InteractiveRunner
from pants.engine.rules import Get, MultiGet, collect_rules, goal_rule, rule
from pants.engine.target import (
FieldSetWithOrigin,
Expand Down Expand Up @@ -304,7 +303,7 @@ async def run_tests(
)
field_set = targets_to_valid_field_sets.field_sets[0]
request = await Get(TestDebugRequest, TestFieldSet, field_set)
debug_result = interactive_runner.run_process(request.process)
debug_result = interactive_runner.run(request.process)
return Test(debug_result.exit_code)

targets_to_valid_field_sets = await Get(
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/core/goals/test_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
)
from pants.engine.addresses import Address
from pants.engine.fs import EMPTY_DIGEST, CreateDigest, Digest, FileContent, MergeDigests, Workspace
from pants.engine.interactive_process import InteractiveProcess, InteractiveRunner
from pants.engine.process import InteractiveProcess, InteractiveRunner
from pants.engine.target import (
Sources,
Target,
Expand Down
4 changes: 2 additions & 2 deletions src/python/pants/engine/desktop.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import ClassVar, Iterable, Iterator

from pants.engine.console import Console
from pants.engine.interactive_process import InteractiveProcess, InteractiveRunner
from pants.engine.process import InteractiveProcess, InteractiveRunner
from pants.util.osutil import get_os_name
from pants.util.strutil import safe_shlex_join

Expand All @@ -28,7 +28,7 @@ def open(self, files: Iterable[PurePath]) -> None:
for request in self._iter_openers(files):
open_command = safe_shlex_join(request.argv)
try:
result = self.runner.run_process(request)
result = self.runner.run(request)
if result.exit_code != 0:
self.console.print_stderr(
f"Failed to open files for viewing using `{open_command}` - received exit "
Expand Down
69 changes: 0 additions & 69 deletions src/python/pants/engine/interactive_process.py

This file was deleted.

13 changes: 0 additions & 13 deletions src/python/pants/engine/interactive_process_test.py

This file was deleted.

8 changes: 6 additions & 2 deletions src/python/pants/engine/internals/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@
RemovePrefix,
Snapshot,
)
from pants.engine.interactive_process import InteractiveProcess, InteractiveProcessResult
from pants.engine.internals.native_engine import PyTypes
from pants.engine.internals.nodes import Return, Throw
from pants.engine.internals.selectors import Params
from pants.engine.platform import Platform
from pants.engine.process import FallibleProcessResultWithPlatform, MultiPlatformProcess
from pants.engine.process import (
FallibleProcessResultWithPlatform,
InteractiveProcess,
InteractiveProcessResult,
MultiPlatformProcess,
)
from pants.engine.rules import Rule, RuleIndex, TaskRule
from pants.engine.unions import UnionMembership, union
from pants.option.global_options import ExecutionOptions
Expand Down
63 changes: 60 additions & 3 deletions src/python/pants/engine/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
import itertools
import logging
from dataclasses import dataclass
from typing import Dict, Iterable, Mapping, Optional, Tuple, Union
from typing import TYPE_CHECKING, Dict, Iterable, Mapping, Optional, Tuple, Union

from pants.base.exception_sink import ExceptionSink
from pants.engine.fs import EMPTY_DIGEST, Digest
from pants.engine.platform import Platform, PlatformConstraint
from pants.engine.rules import RootRule, collect_rules, rule
from pants.engine.rules import RootRule, collect_rules, rule, side_effecting
from pants.util.meta import frozen_after_init

if TYPE_CHECKING:
from pants.engine.internals.scheduler import SchedulerSession


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -230,10 +235,62 @@ def remove_platform_information(res: FallibleProcessResultWithPlatform,) -> Fall
)


@dataclass(frozen=True)
class InteractiveProcessResult:
exit_code: int


@frozen_after_init
@dataclass(unsafe_hash=True)
class InteractiveProcess:
argv: Tuple[str, ...]
env: Tuple[str, ...]
input_digest: Digest
run_in_workspace: bool

def __init__(
self,
argv: Iterable[str],
*,
env: Optional[Mapping[str, str]] = None,
input_digest: Digest = EMPTY_DIGEST,
run_in_workspace: bool = False,
) -> None:
"""Request to run a subprocess in the foreground, similar to subprocess.run().
Unlike `Process`, the result will not be cached.
To run the process, request an `InteractiveRunner` in a `@goal_rule`, then run
`interactive_runner.run_process()`.
"""
self.argv = tuple(argv)
self.env = tuple(itertools.chain.from_iterable((env or {}).items()))
self.input_digest = input_digest
self.run_in_workspace = run_in_workspace
self.__post_init__()

def __post_init__(self):
if self.input_digest != EMPTY_DIGEST and self.run_in_workspace:
raise ValueError(
"InteractiveProcessRequest should use the Workspace API to materialize any needed "
"files when it runs in the workspace"
)


@side_effecting
@dataclass(frozen=True)
class InteractiveRunner:
_scheduler: "SchedulerSession"

def run(self, request: InteractiveProcess) -> InteractiveProcessResult:
ExceptionSink.toggle_ignoring_sigint_v2_engine(True)
return self._scheduler.run_local_interactive_process(request)


def rules():
"""Creates rules that consume the intrinsic filesystem types."""
return [
*collect_rules(),
RootRule(Process),
RootRule(InteractiveRunner),
RootRule(MultiPlatformProcess),
]
9 changes: 9 additions & 0 deletions src/python/pants/engine/process_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
from dataclasses import dataclass
from typing import ClassVar, Tuple

import pytest

from pants.engine.fs import CreateDigest, Digest, DigestContents, FileContent, PathGlobs, Snapshot
from pants.engine.internals.scheduler import ExecutionError
from pants.engine.process import (
FallibleProcessResult,
InteractiveProcess,
Process,
ProcessExecutionFailure,
ProcessResult,
Expand Down Expand Up @@ -402,3 +405,9 @@ def test_non_fallible_failing_command_raises(self):
with self.assertRaises(ExecutionError) as cm:
self.request_single_product(ProcessResult, request)
assert "Process 'one-cat' failed with exit code 1." in str(cm.exception)


def test_running_interactive_process_in_workspace_cannot_have_input_files() -> None:
mock_digest = Digest("fake", 1)
with pytest.raises(ValueError):
InteractiveProcess(argv=["/bin/echo"], input_digest=mock_digest, run_in_workspace=True)
5 changes: 2 additions & 3 deletions src/python/pants/init/engine_initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
from pants.base.exiter import PANTS_SUCCEEDED_EXIT_CODE
from pants.base.specs import Specs
from pants.build_graph.build_configuration import BuildConfiguration
from pants.engine import fs, interactive_process, process
from pants.engine import fs, process
from pants.engine.console import Console
from pants.engine.fs import GlobMatchErrorBehavior, Workspace
from pants.engine.goal import Goal
from pants.engine.interactive_process import InteractiveRunner
from pants.engine.internals import graph, options_parsing, uuid
from pants.engine.internals.build_files import create_graph_rules
from pants.engine.internals.mapper import AddressMapper
Expand All @@ -25,6 +24,7 @@
from pants.engine.internals.selectors import Params
from pants.engine.internals.target_adaptor import TargetAdaptor
from pants.engine.platform import create_platform_rules
from pants.engine.process import InteractiveRunner
from pants.engine.rules import RootRule, collect_rules, rule
from pants.engine.target import RegisteredTargetTypes
from pants.engine.unions import UnionMembership
Expand Down Expand Up @@ -288,7 +288,6 @@ def build_root_singleton() -> BuildRoot:
*collect_rules(locals()),
RootRule(Console),
*fs.rules(),
*interactive_process.rules(),
*graph.rules(),
*uuid.rules(),
*options_parsing.rules(),
Expand Down

0 comments on commit e353ba1

Please sign in to comment.