Преглед изворни кода

fix: proper handling of cleanup

Sam Jaffe пре 1 месец
родитељ
комит
00e0e44d30
2 измењених фајлова са 10 додато и 6 уклоњено
  1. 7 6
      src/cipy/action.py
  2. 3 0
      src/cipy/common.py

+ 7 - 6
src/cipy/action.py

@@ -107,12 +107,13 @@ class Composite(Action):
     name: str = ""
     steps: list[Action] = Field(frozen=True)
     _counter: int = PrivateAttr(default=0)
+    _results: Results = PrivateAttr(default_factory=Results)
 
     @final
     def run(self, context: Context) -> Status:
         status = Status.SKIPPED
 
-        with context.extend(steps=Results()) as outctx:
+        with context.extend(steps=self._results) as outctx:
             for step in self.steps:
                 self._counter += 1
 
@@ -131,8 +132,8 @@ class Composite(Action):
 
     @final
     def cleanup(self, context: Context) -> None:
-        if not hasattr(self, "counter"):
-            # We never ran, so we don't need to run the cleanup
-            return
-        for step in reversed(self.steps[0 : self._counter]):
-            step.cleanup(context)
+        for step in reversed(self.steps):
+            if step.id not in self._results:
+                continue
+            if self._results[step.id].conclusion == Status.SUCCESS:
+                step.cleanup(context)

+ 3 - 0
src/cipy/common.py

@@ -57,6 +57,9 @@ class Results(SimpleNamespace):
         conclusion: Status = Status.NOT_RUN
         outputs: Outputs = Outputs()
 
+    def __contains__(self, subscript: str) -> bool:
+        return hasattr(self, subscript)
+
     def __setitem__(self, subscript: str, value: Results.Item) -> None:
         if subscript:
             self.__setattr__(subscript, value)