Skip to content

Commit

Permalink
use _get_all_envs
Browse files Browse the repository at this point in the history
  • Loading branch information
goFrendiAsgard committed Nov 8, 2023
1 parent ad10f95 commit 6611e2c
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 42 deletions.
8 changes: 4 additions & 4 deletions docs/concepts/tasks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
4 changes: 2 additions & 2 deletions docs/tutorials/copy-and-reuse-task.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')
)

Expand Down
8 changes: 4 additions & 4 deletions src/zrb/task/any_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions src/zrb/task/base_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
18 changes: 9 additions & 9 deletions src/zrb/task/base_task_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
20 changes: 12 additions & 8 deletions src/zrb/task/cmd_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 = []
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/zrb/task/docker_compose_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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] = []
Expand Down
8 changes: 4 additions & 4 deletions src/zrb/task/flow_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions zrb_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6611e2c

Please sign in to comment.