Просмотр исходного кода

refactor: move Shell to its own file, move related functions as well

Sam Jaffe 1 месяц назад
Родитель
Сommit
c80e6f4ad1
3 измененных файлов с 63 добавлено и 43 удалено
  1. 4 2
      src/cipy/__init__.py
  2. 4 41
      src/cipy/action.py
  3. 55 0
      src/cipy/shell.py

+ 4 - 2
src/cipy/__init__.py

@@ -1,16 +1,17 @@
 """
 """
 Entry point for cipy library, re-exporting all of the default items
 Entry point for cipy library, re-exporting all of the default items
 """
 """
+
 import typing
 import typing
 
 
 import pydantic
 import pydantic
 
 
 from cipy.action import Composite, NodeScript, Script
 from cipy.action import Composite, NodeScript, Script
-from cipy.common import Action, Context, Factory, Inputs, Outputs, Ref, Status
+from cipy.common import Context, Factory, Inputs, Outputs, Ref, Status
+from cipy.shell import Shell
 from cipy.workflow import Job, Workflow
 from cipy.workflow import Job, Workflow
 
 
 __all__ = [
 __all__ = [
-    "Action",
     "Composite",
     "Composite",
     "Context",
     "Context",
     "Inputs",
     "Inputs",
@@ -18,6 +19,7 @@ __all__ = [
     "NodeScript",
     "NodeScript",
     "Outputs",
     "Outputs",
     "Script",
     "Script",
+    "Shell",
     "Status",
     "Status",
     "Workflow",
     "Workflow",
     "compute",
     "compute",

+ 4 - 41
src/cipy/action.py

@@ -1,25 +1,16 @@
 """Module containing basic Action definitions, which perform linear operations"""
 """Module containing basic Action definitions, which perform linear operations"""
 
 
 import pathlib
 import pathlib
-import shutil
 import subprocess
 import subprocess
 import tempfile
 import tempfile
 
 
-from enum import StrEnum, auto
 from typing import final
 from typing import final
 
 
 from pydantic import Field, PrivateAttr
 from pydantic import Field, PrivateAttr
 
 
 import cipy.runner
 import cipy.runner
 from cipy.common import Action, Context, Results, Status, _validate
 from cipy.common import Action, Context, Results, Status, _validate
-
-
-class Shell(StrEnum):
-    """Enumeration of shells that this tool knows how to run natively"""
-
-    SH = auto()
-    BASH = auto()
-    PYTHON = auto()
+from cipy.shell import Shell
 
 
 
 
 class NodeScript(Action):
 class NodeScript(Action):
@@ -54,44 +45,16 @@ class NodeScript(Action):
 class Script(Action):
 class Script(Action):
     """Action descriptor for a generic shell runner"""
     """Action descriptor for a generic shell runner"""
 
 
-    shell: Shell | None = None
+    shell: Shell = Shell.DEFAULT
     script: str
     script: str
 
 
-    def command(self, script: str) -> list[str]:
-        """
-        Helper function to transform the shell into a command line.
-        Requires the script file as an argument because some shells may wrap the
-        script instead of simply passing it as a raw argument (Powershell).
-        """
-        if self.shell is None:
-            return [
-                "bash" if shutil.which("bash") else "sh",
-                "-e",
-                str(script),
-            ]
-
-        match self.shell:
-            case Shell.BASH:
-                return [
-                    "bash",
-                    "--noprofile",
-                    "--norc",
-                    "-eo",
-                    "pipefail",
-                    str(script),
-                ]
-            case Shell.PYTHON:
-                return ["python", str(script)]
-            case Shell.SH:
-                return ["sh", "-e", str(script)]
-
     @final
     @final
     @cipy.runner.ipc
     @cipy.runner.ipc
     def run(self, context: Context) -> Status:
     def run(self, context: Context) -> Status:
-        with tempfile.TemporaryFile(mode="w+") as script:
+        with tempfile.TemporaryFile(mode="w+", suffix=self.shell.extension()) as script:
             script.write(self.script)
             script.write(self.script)
             try:
             try:
-                subprocess.run(self.command(script.name), check=True)
+                subprocess.run(self.shell.command(script.name), check=True)
                 return Status.SUCCESS
                 return Status.SUCCESS
             except subprocess.CalledProcessError:
             except subprocess.CalledProcessError:
                 return Status.FAILURE
                 return Status.FAILURE

+ 55 - 0
src/cipy/shell.py

@@ -0,0 +1,55 @@
+"""Enumeration of shells"""
+
+import shutil
+
+from enum import StrEnum, auto
+from typing import Any
+
+
+class Shell(StrEnum):
+    """Enumeration of shells that this tool knows how to run natively"""
+
+    DEFAULT = auto()
+    SH = auto()
+    BASH = auto()
+    PYTHON = auto()
+
+    def extension(self) -> str:
+        """
+        Get the default extension for a script type.
+        Some languages are picky like that
+        """
+        match self:
+            case Shell.DEFAULT:
+                return ".sh"  # os check
+            case Shell.SH | Shell.BASH:
+                return ".sh"
+            case Shell.PYTHON:
+                return ".py"
+
+    def command(self, script: Any) -> list[str]:
+        """
+        Helper function to transform the shell into a command line.
+        Requires the script file as an argument because some shells may wrap the
+        script instead of simply passing it as a raw argument (Powershell).
+        """
+        match self:
+            case Shell.DEFAULT:
+                return [
+                    "bash" if shutil.which("bash") else "sh",
+                    "-e",
+                    str(script),
+                ]
+            case Shell.SH:
+                return ["sh", "-e", str(script)]
+            case Shell.BASH:
+                return [
+                    "bash",
+                    "--noprofile",
+                    "--norc",
+                    "-eo",
+                    "pipefail",
+                    str(script),
+                ]
+            case Shell.PYTHON:
+                return ["python", str(script)]