From 6611e2c90bd9a9f9704329da00ab39d8f9bab37b Mon Sep 17 00:00:00 2001 From: goFrendiAsgard Date: Wed, 8 Nov 2023 17:56:42 +0700 Subject: [PATCH] use _get_all_envs --- docs/concepts/tasks/README.md | 8 ++++---- docs/tutorials/copy-and-reuse-task.md | 4 ++-- src/zrb/task/any_task.py | 8 ++++---- src/zrb/task/base_task.py | 11 ++++++----- src/zrb/task/base_task_composite.py | 18 +++++++++--------- src/zrb/task/cmd_task.py | 20 ++++++++++++-------- src/zrb/task/docker_compose_task.py | 6 +++--- src/zrb/task/flow_task.py | 8 ++++---- zrb_init.py | 6 +++--- 9 files changed, 47 insertions(+), 42 deletions(-) diff --git a/docs/concepts/tasks/README.md b/docs/concepts/tasks/README.md index 87a21cd3..7829f6f6 100644 --- a/docs/concepts/tasks/README.md +++ b/docs/concepts/tasks/README.md @@ -400,13 +400,13 @@ Every task share some common methods like `run`, `check`, and `to_function`. Deep copy current task -## `add_envs` +## `add_env` -## `add_env_files` +## `add_env_file` -## `add_inputs` +## `add_input` -## `add_upstreams` +## `add_upstream` ## `set_name` diff --git a/docs/tutorials/copy-and-reuse-task.md b/docs/tutorials/copy-and-reuse-task.md index f50c1032..8c7a64f3 100644 --- a/docs/tutorials/copy-and-reuse-task.md +++ b/docs/tutorials/copy-and-reuse-task.md @@ -20,10 +20,10 @@ local_hello = hello.copy() # Update name, input, and env local_hello.set_name('hello-local') -local_hello.add_inputs( +local_hello.add_input( StrInput(name='name', description='Name', default='dunia') ) -local_hello.add_envs( +local_hello.add_env( Env(name='GREETINGS', os_name='', default='Halo') ) diff --git a/src/zrb/task/any_task.py b/src/zrb/task/any_task.py index 78bb19a6..36869dd6 100644 --- a/src/zrb/task/any_task.py +++ b/src/zrb/task/any_task.py @@ -65,19 +65,19 @@ def to_function( pass @abstractmethod - def add_upstreams(self, *upstreams: TAnyTask): + def add_upstream(self, *upstreams: TAnyTask): pass @abstractmethod - def add_inputs(self, *inputs: AnyInput): + def add_input(self, *inputs: AnyInput): pass @abstractmethod - def add_envs(self, *envs: Env): + def add_env(self, *envs: Env): pass @abstractmethod - def add_env_files(self, *env_files: EnvFile): + def add_env_file(self, *env_files: EnvFile): pass @abstractmethod diff --git a/src/zrb/task/base_task.py b/src/zrb/task/base_task.py index 169d9c39..2871a911 100644 --- a/src/zrb/task/base_task.py +++ b/src/zrb/task/base_task.py @@ -126,14 +126,15 @@ def get_all_inputs(self) -> Iterable[AnyInput]: self._all_inputs: List[AnyInput] = [] existing_input_names: Mapping[str, bool] = {} # Add task inputs - for input_index, first_occurence_task_input in enumerate(self._inputs): + inputs = self.get_inputs() + for input_index, first_occurence_task_input in enumerate(inputs): input_name = first_occurence_task_input.get_name() if input_name in existing_input_names: continue # Look for all input with the same name in the current task task_inputs = [ candidate - for candidate in self._inputs[input_index:] + for candidate in inputs[input_index:] if candidate.get_name() == input_name ] # Get the last input, and add it to _all_inputs @@ -167,7 +168,7 @@ def function(*args: Any, **kwargs: Any) -> Any: )) return function - def add_upstreams(self, *upstreams: AnyTask): + def add_upstream(self, *upstreams: AnyTask): if not self._allow_add_upstreams: raise Exception(f'Cannot add upstreams on `{self._name}`') self._upstreams += upstreams @@ -288,10 +289,10 @@ def _get_all_envs(self) -> Mapping[str, Env]: all_envs[env_name] = Env( name=env_name, os_name=env_name, renderable=False ) - for env_file in self._env_files: + for env_file in self.get_env_files(): for env in env_file.get_envs(): all_envs[env.name] = env - for env in self._envs: + for env in self.get_envs(): all_envs[env.name] = env return all_envs diff --git a/src/zrb/task/base_task_composite.py b/src/zrb/task/base_task_composite.py index a5510dd2..3211f097 100644 --- a/src/zrb/task/base_task_composite.py +++ b/src/zrb/task/base_task_composite.py @@ -99,17 +99,17 @@ def set_retry_interval(self, new_retry_interval: Union[float, int]): def set_checking_interval(self, new_checking_interval: Union[float, int]): self._checking_interval = new_checking_interval - def add_inputs(self, *inputs: AnyInput): + def add_input(self, *inputs: AnyInput): if not self._allow_add_inputs: raise Exception(f'Cannot add inputs on `{self._name}`') self._inputs += inputs - def add_envs(self, *envs: Env): + def add_env(self, *envs: Env): if not self._allow_add_envs: raise Exception(f'Cannot add envs on `{self._name}`') self._envs += envs - def add_env_files(self, *env_files: EnvFile): + def add_env_file(self, *env_files: EnvFile): if not self._allow_add_env_files: raise Exception(f'Cannot add env_files on `{self._name}`') self._env_files += env_files @@ -127,16 +127,16 @@ def get_env_files(self) -> List[EnvFile]: return self._env_files def get_envs(self) -> List[Env]: - return self._envs + return list(self._envs) def get_inputs(self) -> List[AnyInput]: - return self._inputs + return list(self._inputs) - def get_checkers(self) -> Iterable[AnyTask]: - return self._checkers + def get_checkers(self) -> List[AnyTask]: + return list(self._checkers) - def get_upstreams(self) -> Iterable[AnyTask]: - return self._upstreams + def get_upstreams(self) -> List[AnyTask]: + return list(self._upstreams) def get_description(self) -> str: return self._description diff --git a/src/zrb/task/cmd_task.py b/src/zrb/task/cmd_task.py index 756e6dd2..dbd4a62e 100644 --- a/src/zrb/task/cmd_task.py +++ b/src/zrb/task/cmd_task.py @@ -160,6 +160,7 @@ def __init__( self._executable = executable self._process: Optional[asyncio.subprocess.Process] self._preexec_fn = preexec_fn + self._is_cmd_aditional_env_added = False def copy(self) -> TCmdTask: return super().copy() @@ -182,18 +183,21 @@ def print_result(self, result: CmdResult): return print(result.output) - def _get_shell_env_map(self) -> Mapping[str, Any]: - env_map = self.get_env_map() + def _get_all_envs(self) -> Mapping[str, Env]: + if self._is_cmd_aditional_env_added: + return super()._get_all_envs() input_map = self.get_input_map() + additional_envs: List[Env] = [] for input_name, input_value in input_map.items(): - upper_input_name = '_INPUT_' + input_name.upper() - if upper_input_name not in env_map: - env_map[upper_input_name] = f'{input_value}' - return env_map + env_name = '_INPUT_' + input_name.upper() + additional_envs.append( + Env(name=env_name, os_name='', default=str(input_value)) + ) + self._envs += additional_envs + return super()._get_all_envs() async def run(self, *args: Any, **kwargs: Any) -> CmdResult: cmd = self._get_cmd_str(*args, **kwargs) - env_map = self._get_shell_env_map() self.print_out_dark('Run script: ' + self._get_multiline_repr(cmd)) self.print_out_dark('Working directory: ' + self._cwd) self._output_buffer = [] @@ -203,7 +207,7 @@ async def run(self, *args: Any, **kwargs: Any) -> CmdResult: cwd=self._cwd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, - env=env_map, + env=self.get_env_map(), shell=True, executable=self._executable, close_fds=True, diff --git a/src/zrb/task/docker_compose_task.py b/src/zrb/task/docker_compose_task.py index 117683df..99a47195 100644 --- a/src/zrb/task/docker_compose_task.py +++ b/src/zrb/task/docker_compose_task.py @@ -153,7 +153,7 @@ def __init__( ) # Flag to make mark whether service config and compose environments # has been added to this task's envs and env_files - self._is_additional_env_added = False + self._is_compose_additional_env_added = False def copy(self) -> TDockerComposeTask: return super().copy() @@ -173,9 +173,9 @@ def _get_all_envs(self) -> Mapping[str, Env]: - Service config's envs and env_files are included - Any environment defined in docker compose file is also included ''' - if self._is_additional_env_added: + if self._is_compose_additional_env_added: return super()._get_all_envs() - self._is_additional_env_added = True + self._is_compose_additional_env_added = True # define additional envs and additonal env_files additional_envs: List[Env] = [] additional_env_files: List[EnvFile] = [] diff --git a/src/zrb/task/flow_task.py b/src/zrb/task/flow_task.py index 5dd138c5..601f2e2b 100644 --- a/src/zrb/task/flow_task.py +++ b/src/zrb/task/flow_task.py @@ -107,10 +107,10 @@ def _get_embeded_tasks( embeded_tasks: List[AnyTask] = [] for task in tasks: embeded_task = task.copy() - embeded_task.add_upstreams(*upstreams) - embeded_task.add_envs(*envs) - embeded_task.add_env_files(*env_files) - embeded_task.add_inputs(*inputs) + embeded_task.add_upstream(*upstreams) + embeded_task.add_env(*envs) + embeded_task.add_env_file(*env_files) + embeded_task.add_input(*inputs) embeded_tasks.append(embeded_task) return embeded_tasks diff --git a/zrb_init.py b/zrb_init.py index 0c7d5b28..7b04e760 100644 --- a/zrb_init.py +++ b/zrb_init.py @@ -99,7 +99,7 @@ ], ) skippable_build: CmdTask = build.copy() -skippable_build.add_inputs(build_zrb_input) +skippable_build.add_input(build_zrb_input) skippable_build.set_should_execute('{{ input.build_zrb}}') runner.register(build) @@ -273,7 +273,7 @@ ] ) skippable_install_symlink: CmdTask = install_symlink.copy() -skippable_install_symlink.add_inputs( +skippable_install_symlink.add_input( build_zrb_input, install_symlink_input ) @@ -342,7 +342,7 @@ preexec_fn=None ) skippable_create_playground: CmdTask = create_playground.copy() -skippable_create_playground.add_inputs( +skippable_create_playground.add_input( build_zrb_input, install_symlink_input, create_playground_input