36
36
def _subp(args, data=None, rcs=None, env=None, capture=False, shell=False,
37
logstring=False, decode="replace"):
57
57
stderr=stderr, stdin=stdin,
58
58
env=env, shell=shell)
59
59
(out, err) = sp.communicate(data)
60
if isinstance(out, bytes):
61
out = out.decode('utf-8')
62
if isinstance(err, bytes):
63
err = err.decode('utf-8')
61
# Just ensure blank instead of none.
62
if not out and capture:
64
if not err and capture:
67
def ldecode(data, m='utf-8'):
68
if not isinstance(data, bytes):
70
return data.decode(m, errors=decode)
64
74
except OSError as e:
65
75
raise ProcessExecutionError(cmd=args, reason=e)
66
76
rc = sp.returncode # pylint: disable=E1101
68
78
raise ProcessExecutionError(stdout=out, stderr=err,
71
# Just ensure blank instead of none?? (if capturing)
72
if not out and capture:
74
if not err and capture:
79
84
def subp(*args, **kwargs):
87
:param args: command to run in a list. [cmd, arg1, arg2...]
88
:param data: input to the command, made available on its stdin.
90
a list of allowed return codes. If subprocess exits with a value not
91
in this list, a ProcessExecutionError will be raised. By default,
92
data is returned as a string. See 'decode' parameter.
93
:param env: a dictionary for the command's environment.
95
boolean indicating if output should be captured. If True, then stderr
96
and stdout will be returned. If False, they will not be redirected.
97
:param shell: boolean indicating if this should be run with a shell.
99
the command will be logged to DEBUG. If it contains info that should
100
not be logged, then logstring will be logged instead.
102
if False, no decoding will be done and returned stdout and stderr will
103
be bytes. Other allowed values are 'strict', 'ignore', and 'replace'.
104
These values are passed through to bytes().decode() as the 'errors'
105
parameter. There is no support for decoding to other than utf-8.
107
a list of times to sleep in between retries. After each failure
108
subp will sleep for N seconds and then try again. A value of [1, 3]
109
means to run, sleep 1, run, sleep 3, run and then return exit code.
81
112
if "retries" in kwargs:
82
113
retries = kwargs.pop("retries")