2020-03-01 14:36:12 +01:00
|
|
|
import os
|
2020-02-08 13:39:35 +01:00
|
|
|
import shlex
|
|
|
|
import subprocess
|
|
|
|
import logging
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-05-14 20:36:01 +02:00
|
|
|
def execute(
|
|
|
|
cmd,
|
|
|
|
wait=True,
|
|
|
|
ignore_errors=False,
|
|
|
|
include_stderr=False,
|
|
|
|
env=None,
|
|
|
|
return_exitcode=False,
|
2020-05-16 11:41:34 +02:00
|
|
|
shell=False,
|
2020-05-14 20:36:01 +02:00
|
|
|
):
|
2020-05-05 19:55:37 +02:00
|
|
|
"""Executes a commandline utility and returns its output
|
|
|
|
|
|
|
|
:param cmd: the command (as string) to execute, returns the program's output
|
|
|
|
:param wait: set to True to wait for command completion, False to return immediately, defaults to True
|
|
|
|
:param ignore_errors: set to True to return a string when an exception is thrown, otherwise might throw, defaults to False
|
|
|
|
:param include_stderr: set to True to include stderr output in the return value, defaults to False
|
|
|
|
:param env: provide a dict here to specify a custom execution environment, defaults to None
|
2020-05-11 15:10:20 +02:00
|
|
|
:param return_exitcode: set to True to return a pair, where the first member is the exit code and the message the second, defaults to False
|
2020-05-16 11:41:34 +02:00
|
|
|
:param shell: set to True to run command in a separate shell, defaults to False
|
2020-05-05 19:55:37 +02:00
|
|
|
|
|
|
|
:raises RuntimeError: the command either didn't exist or didn't exit cleanly, and ignore_errors was set to False
|
|
|
|
|
2020-05-11 15:10:20 +02:00
|
|
|
:return: output of cmd, or stderr, if ignore_errors is True and the command failed; or a tuple of exitcode and the previous, if return_exitcode is set to True
|
|
|
|
:rtype: string or tuple (if return_exitcode is set to True)
|
2020-05-05 19:55:37 +02:00
|
|
|
"""
|
2020-05-16 11:41:34 +02:00
|
|
|
args = cmd if shell else shlex.split(cmd)
|
2020-02-08 13:39:35 +01:00
|
|
|
logging.debug(cmd)
|
2020-06-24 07:47:31 +02:00
|
|
|
|
|
|
|
if not env:
|
|
|
|
env = os.environ.copy()
|
|
|
|
env["LC_ALL"] = "C"
|
|
|
|
|
2020-02-08 13:39:35 +01:00
|
|
|
try:
|
2020-05-03 11:15:52 +02:00
|
|
|
proc = subprocess.Popen(
|
|
|
|
args,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT if include_stderr else subprocess.PIPE,
|
|
|
|
env=env,
|
2020-05-16 11:41:34 +02:00
|
|
|
shell=shell,
|
2020-05-03 11:15:52 +02:00
|
|
|
)
|
2020-02-08 13:39:35 +01:00
|
|
|
except FileNotFoundError as e:
|
2020-05-03 11:15:52 +02:00
|
|
|
raise RuntimeError("{} not found".format(cmd))
|
2020-02-08 13:39:35 +01:00
|
|
|
|
|
|
|
if wait:
|
|
|
|
out, _ = proc.communicate()
|
2020-03-30 21:09:09 +02:00
|
|
|
if proc.returncode != 0:
|
2020-05-03 11:15:52 +02:00
|
|
|
err = "{} exited with code {}".format(cmd, proc.returncode)
|
2020-05-16 15:16:23 +02:00
|
|
|
logging.warning(err)
|
2020-03-30 21:09:09 +02:00
|
|
|
if ignore_errors:
|
2020-05-11 15:10:20 +02:00
|
|
|
return (proc.returncode, err) if return_exitcode else err
|
2020-03-30 21:09:09 +02:00
|
|
|
raise RuntimeError(err)
|
2020-05-11 15:10:20 +02:00
|
|
|
res = out.decode("utf-8")
|
2020-05-16 15:16:23 +02:00
|
|
|
logging.debug(res)
|
2020-05-11 15:10:20 +02:00
|
|
|
return (proc.returncode, res) if return_exitcode else res
|
|
|
|
return (0, "") if return_exitcode else ""
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-02-08 13:39:35 +01:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|