Skip to content

Commit

Permalink
Add OperationMeta.will_change attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Fizzadar committed May 12, 2024
1 parent 35ab0a1 commit 9c4b772
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions pyinfra/api/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class OperationMeta:

_combined_output: Optional["CommandOutput"] = None
_commands: Optional[list[Any]] = None
_maybe_is_change: Optional[bool] = False
_maybe_is_change: Optional[bool] = None
_success: Optional[bool] = None

def __init__(self, hash, is_change: Optional[bool]):
Expand Down Expand Up @@ -87,6 +87,19 @@ def _raise_if_not_complete(self) -> None:
if not self.is_complete():
raise RuntimeError("Cannot evaluate operation result before execution")

@property
def will_change(self) -> bool:
if self._maybe_is_change is not None:
return self._maybe_is_change

op_data = context.state.get_op_data_for_host(context.host, self._hash)
cmd_gen = op_data.command_generator
for _ in cmd_gen():
self._maybe_is_change = True
return True
self._maybe_is_change = False
return False

def _did_change(self) -> bool:
return bool(self._success and len(self._commands or []) > 0)

Expand All @@ -112,15 +125,7 @@ def did_error(self) -> bool:
def changed(self) -> bool:
if self.is_complete():
return self._did_change()

if self._maybe_is_change is not None:
return self._maybe_is_change

op_data = context.state.get_op_data_for_host(context.host, self._hash)
cmd_gen = op_data.command_generator
for _ in cmd_gen():
return True
return False
return self.will_change

@property
def stdout_lines(self) -> list[str]:
Expand Down

0 comments on commit 9c4b772

Please sign in to comment.