Skip to content

Commit

Permalink
Simplifies the class hierarchy so all EnvironmentAwares can define …
Browse files Browse the repository at this point in the history
…envvars

# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
  • Loading branch information
Christopher Neugebauer committed Sep 26, 2022
1 parent 236c7c1 commit 6324001
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Sequence

from pants.option.option_types import StrListOption
from pants.option.subsystem import DependsOnEnvVars, Subsystem
from pants.option.subsystem import Subsystem
from pants.util.strutil import safe_shlex_join, safe_shlex_split


Expand All @@ -15,8 +15,8 @@ class PythonNativeCodeSubsystem(Subsystem):
options_scope = "python-native-code"
help = "Options for building native code using Python, e.g. when resolving distributions."

class EnvironmentAware(DependsOnEnvVars):
env_var_names = ("CPPFLAGS", "LDFLAGS")
class EnvironmentAware(Subsystem.EnvironmentAware):
depends_on_env_vars = ("CPPFLAGS", "LDFLAGS")

# TODO(#7735): move the --cpp-flags and --ld-flags to a general subprocess support subsystem.
_cpp_flags = StrListOption(
Expand Down
6 changes: 3 additions & 3 deletions src/python/pants/backend/shell/shell_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os

from pants.option.option_types import BoolOption, StrListOption
from pants.option.subsystem import DependsOnEnvVars, Subsystem
from pants.option.subsystem import Subsystem
from pants.util.memo import memoized_property
from pants.util.ordered_set import OrderedSet
from pants.util.strutil import softwrap
Expand All @@ -31,8 +31,8 @@ class ShellSetup(Subsystem):
advanced=True,
)

class EnvironmentAware(DependsOnEnvVars):
env_var_names = ("PATH",)
class EnvironmentAware(Subsystem.EnvironmentAware):
depends_on_env_vars = ("PATH",)

_executable_search_path = StrListOption(
default=["<PATH>"],
Expand Down
15 changes: 5 additions & 10 deletions src/python/pants/option/subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ class EnvironmentAware(metaclass=ABCMeta):
"""

subsystem: ClassVar[type[Subsystem]]
depends_on_env_vars: ClassVar[tuple[str, ...]] = ()

options: OptionValueContainer
env_tgt: EnvironmentTarget
env_vars: EnvironmentVars

def __getattribute__(self, __name: str) -> Any:
from pants.core.util_rules.environments import resolve_environment_sensitive_option
Expand Down Expand Up @@ -255,14 +258,6 @@ def __eq__(self, other: Any) -> bool:
return bool(self.options == other.options)


class DependsOnEnvVars:
"""Indicates that an `EnvironmentAware` subclass depends environment variables, and makes them
available to instances at construction time."""

env_var_names: ClassVar[tuple[str, ...]]
env_vars: EnvironmentVars


async def _construct_subsytem(subsystem_typ: type[_SubsystemT]) -> _SubsystemT:
scoped_options = await Get(ScopedOptions, Scope(str(subsystem_typ.options_scope)))
return subsystem_typ(scoped_options.options)
Expand All @@ -281,7 +276,7 @@ async def _construct_env_aware(
t.options = subsystem_instance.options
t.env_tgt = env_tgt

if isinstance(t, DependsOnEnvVars):
t.env_vars = await Get(EnvironmentVars, EnvironmentVarsRequest(t.env_var_names))
if t.depends_on_env_vars:
t.env_vars = await Get(EnvironmentVars, EnvironmentVarsRequest(t.depends_on_env_vars))

return t

0 comments on commit 6324001

Please sign in to comment.