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

DM-16220: Use subprocess.run for all external commands #62

Merged
merged 3 commits into from
Oct 29, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 17 additions & 8 deletions python/lsst/sconsUtils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def libraryPathPassThrough():
def whichPython():
global _pythonPath
if _pythonPath is None:
output = subprocess.check_output(["python", "-c", "import sys; print(sys.executable)"])
_pythonPath = output.decode().strip()
_pythonPath = runExternal(["python", "-c", "import sys; print(sys.executable)"],
fatal=True, msg="Error getting python path")
return _pythonPath


Expand Down Expand Up @@ -136,6 +136,8 @@ def libraryLoaderEnvironment():
##
# @brief Safe wrapper for running external programs, reading stdout, and sanitizing error messages.
#
# Command can be given as a list/tuple of command with separate options.
#
# Note that the entire program output is returned, not just a single line.
# @returns Strings not bytes.
##
Expand All @@ -145,15 +147,22 @@ def runExternal(cmd, fatal=False, msg=None):
msg = "Error running %s" % cmd.split()[0]
except Exception:
msg = "Error running external command"
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
if process.returncode != 0:

# Run with shell unless given a list of options
shell = True
if isinstance(cmd, (list, tuple)):
shell = False

try:
retval = subprocess.run(cmd, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
check=True)
except subprocess.CalledProcessError as e:
if fatal:
raise RuntimeError("%s: %s" % (msg, stderr))
raise RuntimeError(f"{msg}: {e.stderr.decode()}") from e
else:
from . import state # can't import at module scope due to circular dependency
state.log.warn("%s: %s" % (msg, stderr))
return stdout.decode()
state.log.warn(f"{msg}: {e.stderr}")
return retval.stdout.decode().strip()


##
Expand Down