poethepoet.context

  1import re
  2from pathlib import Path
  3from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional, Tuple, Union
  4
  5if TYPE_CHECKING:
  6    from .config import PoeConfig
  7    from .env.manager import EnvVarsManager
  8    from .executor import PoeExecutor
  9    from .ui import PoeUi
 10
 11
 12class RunContext:
 13    config: "PoeConfig"
 14    ui: "PoeUi"
 15    env: "EnvVarsManager"
 16    dry: bool
 17    poe_active: Optional[str]
 18    project_dir: Path
 19    multistage: bool = False
 20    exec_cache: Dict[str, Any]
 21    captured_stdout: Dict[Tuple[str, ...], str]
 22
 23    def __init__(
 24        self,
 25        config: "PoeConfig",
 26        ui: "PoeUi",
 27        env: Mapping[str, str],
 28        dry: bool,
 29        poe_active: Optional[str],
 30        multistage: bool = False,
 31        cwd: Optional[Union[Path, str]] = None,
 32    ):
 33        from .env.manager import EnvVarsManager
 34
 35        self.config = config
 36        self.ui = ui
 37        self.project_dir = Path(config.project_dir)
 38        self.dry = dry
 39        self.poe_active = poe_active
 40        self.multistage = multistage
 41        self.exec_cache = {}
 42        self.captured_stdout = {}
 43
 44        # Init root EnvVarsManager
 45        self.env = EnvVarsManager(self.config, self.ui, base_env=env, cwd=cwd)
 46        for config_part in self.config.partitions():
 47            self.env.apply_env_config(
 48                envfile=config_part.get("envfile", None),
 49                config_env=config_part.get("env", None),
 50                config_dir=config_part.config_dir,
 51                config_working_dir=config_part.cwd,
 52            )
 53
 54    def _get_dep_values(
 55        self, used_task_invocations: Mapping[str, Tuple[str, ...]]
 56    ) -> Dict[str, str]:
 57        """
 58        Get env vars from upstream tasks declared via the uses option.
 59        """
 60        return {
 61            var_name: self.get_task_output(invocation)
 62            for var_name, invocation in used_task_invocations.items()
 63        }
 64
 65    def save_task_output(self, invocation: Tuple[str, ...], captured_stdout: bytes):
 66        """
 67        Store the stdout data from a task so that it can be reused by other tasks
 68        """
 69        try:
 70            self.captured_stdout[invocation] = captured_stdout.decode()
 71        except UnicodeDecodeError:
 72            # Attempt to recover in case a specific encoding is configured
 73            io_encoding = self.env.get("PYTHONIOENCODING")
 74            if io_encoding:
 75                self.captured_stdout[invocation] = captured_stdout.decode(io_encoding)
 76            else:
 77                raise
 78
 79    def get_task_output(self, invocation: Tuple[str, ...]):
 80        """
 81        Get the stored stdout data from a task so that it can be reused by other tasks
 82
 83        New lines are replaced with whitespace similar to how unquoted command
 84        interpolation works in bash.
 85        """
 86        return re.sub(r"\s+", " ", self.captured_stdout[invocation].strip("\r\n"))
 87
 88    def get_executor(
 89        self,
 90        invocation: Tuple[str, ...],
 91        env: "EnvVarsManager",
 92        working_dir: Path,
 93        *,
 94        executor_config: Optional[Mapping[str, str]] = None,
 95        capture_stdout: Union[str, bool] = False,
 96        delegate_dry_run: bool = False,
 97    ) -> "PoeExecutor":
 98        """
 99        Get an Executor object for use with this invocation.
100
101        if delegate_dry_run is set then the task will always be executed and be
102        entrusted to not have any side effects when the dry-run flag is set.
103        """
104
105        from .executor import PoeExecutor
106
107        if not executor_config:
108            if self.ui["executor"]:
109                executor_config = {"type": self.ui["executor"]}
110            else:
111                executor_config = self.config.executor
112
113        return PoeExecutor.get(
114            invocation=invocation,
115            context=self,
116            executor_config=executor_config,
117            env=env,
118            working_dir=working_dir,
119            capture_stdout=capture_stdout,
120            dry=False if delegate_dry_run else self.dry,
121        )
class RunContext:
 13class RunContext:
 14    config: "PoeConfig"
 15    ui: "PoeUi"
 16    env: "EnvVarsManager"
 17    dry: bool
 18    poe_active: Optional[str]
 19    project_dir: Path
 20    multistage: bool = False
 21    exec_cache: Dict[str, Any]
 22    captured_stdout: Dict[Tuple[str, ...], str]
 23
 24    def __init__(
 25        self,
 26        config: "PoeConfig",
 27        ui: "PoeUi",
 28        env: Mapping[str, str],
 29        dry: bool,
 30        poe_active: Optional[str],
 31        multistage: bool = False,
 32        cwd: Optional[Union[Path, str]] = None,
 33    ):
 34        from .env.manager import EnvVarsManager
 35
 36        self.config = config
 37        self.ui = ui
 38        self.project_dir = Path(config.project_dir)
 39        self.dry = dry
 40        self.poe_active = poe_active
 41        self.multistage = multistage
 42        self.exec_cache = {}
 43        self.captured_stdout = {}
 44
 45        # Init root EnvVarsManager
 46        self.env = EnvVarsManager(self.config, self.ui, base_env=env, cwd=cwd)
 47        for config_part in self.config.partitions():
 48            self.env.apply_env_config(
 49                envfile=config_part.get("envfile", None),
 50                config_env=config_part.get("env", None),
 51                config_dir=config_part.config_dir,
 52                config_working_dir=config_part.cwd,
 53            )
 54
 55    def _get_dep_values(
 56        self, used_task_invocations: Mapping[str, Tuple[str, ...]]
 57    ) -> Dict[str, str]:
 58        """
 59        Get env vars from upstream tasks declared via the uses option.
 60        """
 61        return {
 62            var_name: self.get_task_output(invocation)
 63            for var_name, invocation in used_task_invocations.items()
 64        }
 65
 66    def save_task_output(self, invocation: Tuple[str, ...], captured_stdout: bytes):
 67        """
 68        Store the stdout data from a task so that it can be reused by other tasks
 69        """
 70        try:
 71            self.captured_stdout[invocation] = captured_stdout.decode()
 72        except UnicodeDecodeError:
 73            # Attempt to recover in case a specific encoding is configured
 74            io_encoding = self.env.get("PYTHONIOENCODING")
 75            if io_encoding:
 76                self.captured_stdout[invocation] = captured_stdout.decode(io_encoding)
 77            else:
 78                raise
 79
 80    def get_task_output(self, invocation: Tuple[str, ...]):
 81        """
 82        Get the stored stdout data from a task so that it can be reused by other tasks
 83
 84        New lines are replaced with whitespace similar to how unquoted command
 85        interpolation works in bash.
 86        """
 87        return re.sub(r"\s+", " ", self.captured_stdout[invocation].strip("\r\n"))
 88
 89    def get_executor(
 90        self,
 91        invocation: Tuple[str, ...],
 92        env: "EnvVarsManager",
 93        working_dir: Path,
 94        *,
 95        executor_config: Optional[Mapping[str, str]] = None,
 96        capture_stdout: Union[str, bool] = False,
 97        delegate_dry_run: bool = False,
 98    ) -> "PoeExecutor":
 99        """
100        Get an Executor object for use with this invocation.
101
102        if delegate_dry_run is set then the task will always be executed and be
103        entrusted to not have any side effects when the dry-run flag is set.
104        """
105
106        from .executor import PoeExecutor
107
108        if not executor_config:
109            if self.ui["executor"]:
110                executor_config = {"type": self.ui["executor"]}
111            else:
112                executor_config = self.config.executor
113
114        return PoeExecutor.get(
115            invocation=invocation,
116            context=self,
117            executor_config=executor_config,
118            env=env,
119            working_dir=working_dir,
120            capture_stdout=capture_stdout,
121            dry=False if delegate_dry_run else self.dry,
122        )
RunContext( config: poethepoet.config.PoeConfig, ui: poethepoet.ui.PoeUi, env: Mapping[str, str], dry: bool, poe_active: Optional[str], multistage: bool = False, cwd: Union[pathlib.Path, str, NoneType] = None)
24    def __init__(
25        self,
26        config: "PoeConfig",
27        ui: "PoeUi",
28        env: Mapping[str, str],
29        dry: bool,
30        poe_active: Optional[str],
31        multistage: bool = False,
32        cwd: Optional[Union[Path, str]] = None,
33    ):
34        from .env.manager import EnvVarsManager
35
36        self.config = config
37        self.ui = ui
38        self.project_dir = Path(config.project_dir)
39        self.dry = dry
40        self.poe_active = poe_active
41        self.multistage = multistage
42        self.exec_cache = {}
43        self.captured_stdout = {}
44
45        # Init root EnvVarsManager
46        self.env = EnvVarsManager(self.config, self.ui, base_env=env, cwd=cwd)
47        for config_part in self.config.partitions():
48            self.env.apply_env_config(
49                envfile=config_part.get("envfile", None),
50                config_env=config_part.get("env", None),
51                config_dir=config_part.config_dir,
52                config_working_dir=config_part.cwd,
53            )
env: poethepoet.env.manager.EnvVarsManager
dry: bool
poe_active: Optional[str]
project_dir: pathlib.Path
multistage: bool = False
exec_cache: Dict[str, Any]
captured_stdout: Dict[Tuple[str, ...], str]
def save_task_output(self, invocation: Tuple[str, ...], captured_stdout: bytes):
66    def save_task_output(self, invocation: Tuple[str, ...], captured_stdout: bytes):
67        """
68        Store the stdout data from a task so that it can be reused by other tasks
69        """
70        try:
71            self.captured_stdout[invocation] = captured_stdout.decode()
72        except UnicodeDecodeError:
73            # Attempt to recover in case a specific encoding is configured
74            io_encoding = self.env.get("PYTHONIOENCODING")
75            if io_encoding:
76                self.captured_stdout[invocation] = captured_stdout.decode(io_encoding)
77            else:
78                raise

Store the stdout data from a task so that it can be reused by other tasks

def get_task_output(self, invocation: Tuple[str, ...]):
80    def get_task_output(self, invocation: Tuple[str, ...]):
81        """
82        Get the stored stdout data from a task so that it can be reused by other tasks
83
84        New lines are replaced with whitespace similar to how unquoted command
85        interpolation works in bash.
86        """
87        return re.sub(r"\s+", " ", self.captured_stdout[invocation].strip("\r\n"))

Get the stored stdout data from a task so that it can be reused by other tasks

New lines are replaced with whitespace similar to how unquoted command interpolation works in bash.

def get_executor( self, invocation: Tuple[str, ...], env: poethepoet.env.manager.EnvVarsManager, working_dir: pathlib.Path, *, executor_config: Optional[Mapping[str, str]] = None, capture_stdout: Union[str, bool] = False, delegate_dry_run: bool = False) -> poethepoet.executor.base.PoeExecutor:
 89    def get_executor(
 90        self,
 91        invocation: Tuple[str, ...],
 92        env: "EnvVarsManager",
 93        working_dir: Path,
 94        *,
 95        executor_config: Optional[Mapping[str, str]] = None,
 96        capture_stdout: Union[str, bool] = False,
 97        delegate_dry_run: bool = False,
 98    ) -> "PoeExecutor":
 99        """
100        Get an Executor object for use with this invocation.
101
102        if delegate_dry_run is set then the task will always be executed and be
103        entrusted to not have any side effects when the dry-run flag is set.
104        """
105
106        from .executor import PoeExecutor
107
108        if not executor_config:
109            if self.ui["executor"]:
110                executor_config = {"type": self.ui["executor"]}
111            else:
112                executor_config = self.config.executor
113
114        return PoeExecutor.get(
115            invocation=invocation,
116            context=self,
117            executor_config=executor_config,
118            env=env,
119            working_dir=working_dir,
120            capture_stdout=capture_stdout,
121            dry=False if delegate_dry_run else self.dry,
122        )

Get an Executor object for use with this invocation.

if delegate_dry_run is set then the task will always be executed and be entrusted to not have any side effects when the dry-run flag is set.