|
@@ -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
|