Skip to content

Commit

Permalink
Restore uncomplicated OperationMeta.did_change function
Browse files Browse the repository at this point in the history
The context manager was never meant to be released and is entirely
undocumented, hence treating this as a bug.
  • Loading branch information
Fizzadar committed Jul 24, 2024
1 parent 4295e88 commit 3143695
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
22 changes: 12 additions & 10 deletions pyinfra/api/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def __repr__(self) -> str:
if self._commands is not None:
return (
"OperationMeta(executed=True, "
f"success={self.did_succeed}, hash={self._hash}, commands={len(self._commands)})"
f"success={self.did_succeed()}, hash={self._hash}, commands={len(self._commands)})"
)
return (
"OperationMeta(executed=False, "
Expand All @@ -87,6 +87,12 @@ def _raise_if_not_complete(self) -> None:
if not self.is_complete():
raise RuntimeError("Cannot evaluate operation result before execution")

@property
def executed(self) -> bool:
if self._commands is None:
return False
return len(self._commands) > 0

@property
def will_change(self) -> bool:
if self._maybe_is_change is not None:
Expand All @@ -100,16 +106,12 @@ def will_change(self) -> bool:
self._maybe_is_change = False
return False

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

@property
def did_change(self):
return context.host.when(self._did_change)

@property
def did_not_change(self):
return context.host.when(lambda: not self._did_change())
def did_not_change(self) -> bool:
return not self.did_change()

def did_succeed(self, _raise_if_not_complete=True) -> bool:
if _raise_if_not_complete:
Expand All @@ -124,7 +126,7 @@ def did_error(self) -> bool:
@property
def changed(self) -> bool:
if self.is_complete():
return self._did_change()
return self.did_change()
return self.will_change

@property
Expand Down
2 changes: 1 addition & 1 deletion pyinfra_cli/prints.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def print_results(state: "State"):

op_meta = state.ops[host][op_hash].operation_meta
if op_meta.did_succeed(_raise_if_not_complete=False):
if op_meta._did_change():
if op_meta.did_change():
hosts_in_op_success.append(host.name)
else:
hosts_in_op_no_change.append(host.name)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli/deploy/runtime_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def check_host_roles():
server.shell(commands=["echo if sees second op"])

# Use the op.did_change context
with second_line_op.did_change:
with host.when(second_line_op.did_change):
# This will be executed if the second line operation made any changes, in
# this deploy that's never. This will appear as a conditional op in the CLI.
server.shell(commands=["echo with sees second op"])
Expand Down

0 comments on commit 3143695

Please sign in to comment.