def sh(cmd, **kwds):
"""run cmd in a subprocess and return its output.
raises RuntimeError on error.
"""
shell = True if isinstance(cmd, (str, unicode)) else False
# Prevents subprocess to open error dialogs in case of error.
flags = 0x8000000 if WINDOWS and shell else 0
kwds.setdefault("shell", shell)
kwds.setdefault("stdout", subprocess.PIPE)
kwds.setdefault("stderr", subprocess.PIPE)
kwds.setdefault("universal_newlines", True)
kwds.setdefault("creationflags", flags)
p = subprocess.Popen(cmd, **kwds)
_subprocesses_started.add(p)
stdout, stderr = p.communicate()
if p.returncode != 0:
raise RuntimeError(stderr)
if stderr:
warn(stderr)
if stdout.endswith('\n'):
stdout = stdout[:-1]
return stdout