Pārlūkot izejas kodu

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

Sam Jaffe 1 mēnesi atpakaļ
vecāks
revīzija
c80e6f4ad1
3 mainītis faili ar 63 papildinājumiem un 43 dzēšanām
  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
 """
+
 import typing
 
 import pydantic
 
 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
 
 __all__ = [
-    "Action",
     "Composite",
     "Context",
     "Inputs",
@@ -18,6 +19,7 @@ __all__ = [
     "NodeScript",
     "Outputs",
     "Script",
+    "Shell",
     "Status",
     "Workflow",
     "compute",

+ 4 - 41
src/cipy/action.py

@@ -1,25 +1,16 @@
 """Module containing basic Action definitions, which perform linear operations"""
 
 import pathlib
-import shutil
 import subprocess
 import tempfile
 
-from enum import StrEnum, auto
 from typing import final
 
 from pydantic import Field, PrivateAttr
 
 import cipy.runner
 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):
@@ -54,44 +45,16 @@ class NodeScript(Action):
 class Script(Action):
     """Action descriptor for a generic shell runner"""
 
-    shell: Shell | None = None
+    shell: Shell = Shell.DEFAULT
     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
     @cipy.runner.ipc
     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)
             try:
-                subprocess.run(self.command(script.name), check=True)
+                subprocess.run(self.shell.command(script.name), check=True)
                 return Status.SUCCESS
             except subprocess.CalledProcessError:
                 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)]