Skip to content

Commit

Permalink
Parse stdout/stderr separately in runProgram
Browse files Browse the repository at this point in the history
  • Loading branch information
jmeyers314 committed Oct 30, 2023
1 parent 123e869 commit 4ae2b5d
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions python/lsst/ts/wep/utils/taskUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
import os
import shlex
import subprocess
from contextlib import ExitStack

import lsst.obs.lsst as obs_lsst


def runProgram(command, binDir=None, argstring=None):
def runProgram(command, binDir=None, argstring=None, stdout=None, stderr=None):
"""Run the program w/o arguments.
Parameters
Expand All @@ -44,6 +45,8 @@ def runProgram(command, binDir=None, argstring=None):
Directory of binary application. (the default is None.)
argstring : str, optional
Arguments of program. (the default is None.)
stdout, stderr : str or _io.TextIOWrapper, optional
Buffered text output/error streams or filenames
Raises
------
Expand All @@ -59,9 +62,26 @@ def runProgram(command, binDir=None, argstring=None):
if argstring is not None:
command += " " + argstring

# Call the program w/o arguments
if subprocess.call(shlex.split(command), shell=False) != 0:
raise RuntimeError("Error running: %s" % command)
with ExitStack() as stack:
if isinstance(stdout, str):
stdout = stack.enter_context(open(stdout, "w"))
# Don't open competing filehandles on the same file
if isinstance(stderr, str) and (stderr == stdout.name):
stderr = stdout
if isinstance(stderr, str):
stderr = stack.enter_context(open(stderr, "w"))

# Call the program w/o arguments
if (
subprocess.run(
shlex.split(command),
shell=False,
stdout=stdout,
stderr=stderr,
).returncode
!= 0
):
raise RuntimeError("Error running: %s" % command)


def writePipetaskCmd(
Expand Down

0 comments on commit 4ae2b5d

Please sign in to comment.