Skip to content

Commit

Permalink
Use exec mode when passed a list of strings (#101)
Browse files Browse the repository at this point in the history
* use exec mode when passed a list of strings

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: KotlinIsland <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Sorin Sbarnea <[email protected]>
  • Loading branch information
4 people authored Jun 14, 2024
1 parent c6337b4 commit 2d3b1ad
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/subprocess_tee/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ async def _read_stream(stream: StreamReader, callback: Callable[..., Any]) -> No
break


async def _stream_subprocess(args: str, **kwargs: Any) -> CompletedProcess:
async def _stream_subprocess(
args: Union[str, List[str]], **kwargs: Any
) -> CompletedProcess:
platform_settings: Dict[str, Any] = {}
if platform.system() == "Windows":
platform_settings["env"] = os.environ
Expand Down Expand Up @@ -65,14 +67,24 @@ async def _stream_subprocess(args: str, **kwargs: Any) -> CompletedProcess:

# Some users are reporting that default (undocumented) limit 64k is too
# low
process = await asyncio.create_subprocess_shell(
args,
limit=STREAM_LIMIT,
stdin=kwargs.get("stdin", False),
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
**platform_settings,
)
if isinstance(args, str):
process = await asyncio.create_subprocess_shell(
args,
limit=STREAM_LIMIT,
stdin=kwargs.get("stdin", False),
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
**platform_settings,
)
else:
process = await asyncio.create_subprocess_exec(
*args,
limit=STREAM_LIMIT,
stdin=kwargs.get("stdin", False),
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
**platform_settings,
)
out: List[str] = []
err: List[str] = []

Expand Down Expand Up @@ -140,7 +152,7 @@ def run(args: Union[str, List[str]], **kwargs: Any) -> CompletedProcess:
if kwargs.get("echo", False):
print(f"COMMAND: {cmd}")

result = asyncio.run(_stream_subprocess(cmd, **kwargs))
result = asyncio.run(_stream_subprocess(args, **kwargs))
# we restore original args to mimic subproces.run()
result.args = args

Expand Down

0 comments on commit 2d3b1ad

Please sign in to comment.