Skip to content

Commit

Permalink
Annotate executeOrRunSubProcess module
Browse files Browse the repository at this point in the history
  • Loading branch information
replaceafill committed Sep 11, 2024
1 parent 39fb35d commit f279191
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ warn_unused_configs = true

[[tool.mypy.overrides]]
module = [
"src.archivematicaCommon.lib.executeOrRunSubProcess",
"src.MCPClient.lib.client.*",
"src.MCPClient.lib.clientScripts.characterize_file",
"src.MCPClient.lib.clientScripts.has_packages",
Expand Down
66 changes: 45 additions & 21 deletions src/archivematicaCommon/lib/executeOrRunSubProcess.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,29 @@
import subprocess
import sys
import tempfile
from typing import Dict
from typing import List
from typing import Optional
from typing import Tuple
from typing import Union

from archivematicaFunctions import escape

Arguments = List[str]
Input = Union[str, bytes, io.IOBase]
Environment = Dict[str, str]
Command = Union[str, List[str]]
Result = Tuple[int, str, str]


def launchSubProcess(
command,
stdIn="",
printing=True,
arguments=None,
env_updates=None,
capture_output=False,
):
command: Command,
stdIn: Input = "",
printing: bool = True,
arguments: Optional[Arguments] = None,
env_updates: Optional[Environment] = None,
capture_output: bool = False,
) -> Result:
"""
Launches a subprocess using ``command``, where ``command`` is either:
a) a single string containing a commandline statement, or
Expand Down Expand Up @@ -91,7 +102,7 @@ def launchSubProcess(
stdin_pipe = subprocess.PIPE
communicate_input = stdIn
elif isinstance(stdIn, io.IOBase):
stdin_pipe = stdIn
stdin_pipe = stdIn.fileno()

Check warning on line 105 in src/archivematicaCommon/lib/executeOrRunSubProcess.py

View check run for this annotation

Codecov / codecov/patch

src/archivematicaCommon/lib/executeOrRunSubProcess.py#L105

Added line #L105 was not covered by tests
communicate_input = None
else:
raise Exception("stdIn must be a string or a file object")
Expand Down Expand Up @@ -141,8 +152,13 @@ def launchSubProcess(


def createAndRunScript(
text, stdIn="", printing=True, arguments=None, env_updates=None, capture_output=True
):
text: Command,
stdIn: Input = "",
printing: bool = True,
arguments: Optional[Arguments] = None,
env_updates: Optional[Environment] = None,
capture_output: bool = True,
) -> Result:
if arguments is None:
arguments = []
if env_updates is None:
Expand All @@ -152,7 +168,10 @@ def createAndRunScript(
encoding="utf-8", mode="wt", delete=False
) as tmpfile:
os.chmod(tmpfile.name, 0o770)
tmpfile.write(text)
if isinstance(text, str):
tmpfile.write(text)
else:
tmpfile.write(" ".join(text))

Check warning on line 174 in src/archivematicaCommon/lib/executeOrRunSubProcess.py

View check run for this annotation

Codecov / codecov/patch

src/archivematicaCommon/lib/executeOrRunSubProcess.py#L174

Added line #L174 was not covered by tests
tmpfile.close()
cmd = [tmpfile.name]
cmd.extend(arguments)
Expand All @@ -170,14 +189,14 @@ def createAndRunScript(


def executeOrRun(
type,
text,
stdIn="",
printing=True,
arguments=None,
env_updates=None,
capture_output=True,
):
type: str,
text: Command,
stdIn: Input = "",
printing: bool = True,
arguments: Optional[Arguments] = None,
env_updates: Optional[Environment] = None,
capture_output: bool = True,
) -> Result:
"""
Attempts to run the provided command on the shell, with the text of
"stdIn" passed as standard input if provided. The type parameter
Expand Down Expand Up @@ -222,7 +241,9 @@ def executeOrRun(
capture_output=capture_output,
)
if type == "bashScript":
text = "#!/bin/bash\n" + text
if not isinstance(text, str):
raise ValueError("command must be a str")
text = f"#!/bin/bash\n{text}"

Check warning on line 246 in src/archivematicaCommon/lib/executeOrRunSubProcess.py

View check run for this annotation

Codecov / codecov/patch

src/archivematicaCommon/lib/executeOrRunSubProcess.py#L245-L246

Added lines #L245 - L246 were not covered by tests
return createAndRunScript(
text,
stdIn=stdIn,
Expand All @@ -232,7 +253,9 @@ def executeOrRun(
capture_output=capture_output,
)
if type == "pythonScript":
text = "#!/usr/bin/env python\n" + text
if not isinstance(text, str):
raise ValueError("command must be a str")
text = f"#!/usr/bin/env python\n{text}"

Check warning on line 258 in src/archivematicaCommon/lib/executeOrRunSubProcess.py

View check run for this annotation

Codecov / codecov/patch

src/archivematicaCommon/lib/executeOrRunSubProcess.py#L257-L258

Added lines #L257 - L258 were not covered by tests
return createAndRunScript(
text,
stdIn=stdIn,
Expand All @@ -250,3 +273,4 @@ def executeOrRun(
env_updates=env_updates,
capture_output=capture_output,
)
raise ValueError(f"unknown type {type}")

0 comments on commit f279191

Please sign in to comment.