Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update internal util module #67

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions foamlib/_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def run(
)
except CalledProcessError as e:
raise RuntimeError(
f"{e.cmd} failed with return code {e.returncode}\n{e.stderr.decode()}"
f"{e.cmd} failed with return code {e.returncode}\n{e.stderr}"
) from None

else:
Expand Down Expand Up @@ -533,7 +533,7 @@ async def run(
)
except CalledProcessError as e:
raise RuntimeError(
f"{e.cmd} failed with return code {e.returncode}\n{e.stderr.decode()}"
f"{e.cmd} failed with return code {e.returncode}\n{e.stderr}"
) from None

else:
Expand Down
20 changes: 14 additions & 6 deletions foamlib/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,19 @@ def is_sequence(

CalledProcessError = subprocess.CalledProcessError

if sys.version_info >= (3, 9):
CompletedProcess = subprocess.CompletedProcess[str]
else:
CompletedProcess = subprocess.CompletedProcess


def run_process(
cmd: Union[Sequence[Union[str, Path]], str, Path],
*,
check: bool = True,
cwd: Union[None, str, Path] = None,
env: Union[None, Mapping[str, str]] = None,
) -> "subprocess.CompletedProcess[bytes]":
) -> CompletedProcess:
shell = not is_sequence(cmd)

if sys.version_info < (3, 8):
Expand All @@ -55,7 +60,9 @@ def run_process(
cmd,
cwd=cwd,
env=env,
capture_output=True,
stdout=asyncio.subprocess.DEVNULL,
stderr=asyncio.subprocess.PIPE,
text=True,
shell=shell,
check=check,
)
Expand All @@ -69,13 +76,13 @@ async def run_process_async(
check: bool = True,
cwd: Union[None, str, Path] = None,
env: Union[None, Mapping[str, str]] = None,
) -> "subprocess.CompletedProcess[bytes]":
) -> CompletedProcess:
if not is_sequence(cmd):
proc = await asyncio.create_subprocess_shell(
str(cmd),
cwd=cwd,
env=env,
stdout=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.DEVNULL,
stderr=asyncio.subprocess.PIPE,
)

Expand All @@ -86,15 +93,16 @@ async def run_process_async(
*cmd,
cwd=cwd,
env=env,
stdout=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.DEVNULL,
stderr=asyncio.subprocess.PIPE,
)

stdout, stderr = await proc.communicate()

assert stdout is None
assert proc.returncode is not None

ret = subprocess.CompletedProcess(cmd, proc.returncode, stdout, stderr)
ret = CompletedProcess(cmd, proc.returncode, None, stderr.decode())

if check:
ret.check_returncode()
Expand Down
Loading