mirror of
https://gitlab.com/ita1024/waf.git
synced 2025-01-11 19:05:04 +01:00
Added new parameters input/timeout to cmd_and_log/exec_command
This commit is contained in:
parent
e9e8df28a4
commit
a08b9fe26a
@ -322,7 +322,7 @@ class Context(ctx):
|
|||||||
unlike :py:meth:`waflib.Context.Context.cmd_and_log`
|
unlike :py:meth:`waflib.Context.Context.cmd_and_log`
|
||||||
|
|
||||||
:param cmd: command argument for subprocess.Popen
|
:param cmd: command argument for subprocess.Popen
|
||||||
:param kw: keyword arguments for subprocess.Popen
|
:param kw: keyword arguments for subprocess.Popen. The parameters input/timeout will be passed to wait/communicate.
|
||||||
"""
|
"""
|
||||||
subprocess = Utils.subprocess
|
subprocess = Utils.subprocess
|
||||||
kw['shell'] = isinstance(cmd, str)
|
kw['shell'] = isinstance(cmd, str)
|
||||||
@ -340,14 +340,22 @@ class Context(ctx):
|
|||||||
if Logs.verbose and not kw['shell'] and not Utils.check_exe(cmd[0]):
|
if Logs.verbose and not kw['shell'] and not Utils.check_exe(cmd[0]):
|
||||||
raise Errors.WafError("Program %s not found!" % cmd[0])
|
raise Errors.WafError("Program %s not found!" % cmd[0])
|
||||||
|
|
||||||
|
wargs = {}
|
||||||
|
if 'timeout' in kw:
|
||||||
|
wargs['timeout'] = kw['timeout']
|
||||||
|
del kw['timeout']
|
||||||
|
if 'input' in kw:
|
||||||
|
wargs['input'] = kw['input']
|
||||||
|
del kw['input']
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if kw['stdout'] or kw['stderr']:
|
if kw['stdout'] or kw['stderr']:
|
||||||
p = subprocess.Popen(cmd, **kw)
|
p = subprocess.Popen(cmd, **kw)
|
||||||
(out, err) = p.communicate()
|
(out, err) = p.communicate(**wargs)
|
||||||
ret = p.returncode
|
ret = p.returncode
|
||||||
else:
|
else:
|
||||||
out, err = (None, None)
|
out, err = (None, None)
|
||||||
ret = subprocess.Popen(cmd, **kw).wait()
|
ret = subprocess.Popen(cmd, **kw).wait(**wargs)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise Errors.WafError('Execution failure: %s' % str(e), ex=e)
|
raise Errors.WafError('Execution failure: %s' % str(e), ex=e)
|
||||||
|
|
||||||
@ -370,20 +378,21 @@ class Context(ctx):
|
|||||||
|
|
||||||
def cmd_and_log(self, cmd, **kw):
|
def cmd_and_log(self, cmd, **kw):
|
||||||
"""
|
"""
|
||||||
Execute a command and return stdout if the execution is successful.
|
Execute a command and return stdout/stderr if the execution is successful.
|
||||||
An exception is thrown when the exit status is non-0. In that case, both stderr and stdout
|
An exception is thrown when the exit status is non-0. In that case, both stderr and stdout
|
||||||
will be bound to the WafError object::
|
will be bound to the WafError object::
|
||||||
|
|
||||||
def configure(conf):
|
def configure(conf):
|
||||||
out = conf.cmd_and_log(['echo', 'hello'], output=waflib.Context.STDOUT, quiet=waflib.Context.BOTH)
|
out = conf.cmd_and_log(['echo', 'hello'], output=waflib.Context.STDOUT, quiet=waflib.Context.BOTH)
|
||||||
(out, err) = conf.cmd_and_log(['echo', 'hello'], output=waflib.Context.BOTH)
|
(out, err) = conf.cmd_and_log(['echo', 'hello'], output=waflib.Context.BOTH)
|
||||||
|
(out, err) = conf.cmd_and_log(cmd, input='\n'.encode(), stdin=waflib.Utils.subprocess.PIPE, output=waflib.Context.STDOUT)
|
||||||
try:
|
try:
|
||||||
conf.cmd_and_log(['which', 'someapp'], output=waflib.Context.BOTH)
|
conf.cmd_and_log(['which', 'someapp'], output=waflib.Context.BOTH)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e.stdout, e.stderr)
|
print(e.stdout, e.stderr)
|
||||||
|
|
||||||
:param cmd: args for subprocess.Popen
|
:param cmd: args for subprocess.Popen
|
||||||
:param kw: keyword arguments for subprocess.Popen
|
:param kw: keyword arguments for subprocess.Popen. The parameters input/timeout will be passed to wait/communicate.
|
||||||
"""
|
"""
|
||||||
subprocess = Utils.subprocess
|
subprocess = Utils.subprocess
|
||||||
kw['shell'] = isinstance(cmd, str)
|
kw['shell'] = isinstance(cmd, str)
|
||||||
@ -407,9 +416,18 @@ class Context(ctx):
|
|||||||
kw['stdout'] = kw['stderr'] = subprocess.PIPE
|
kw['stdout'] = kw['stderr'] = subprocess.PIPE
|
||||||
if quiet is None:
|
if quiet is None:
|
||||||
self.to_log(cmd)
|
self.to_log(cmd)
|
||||||
|
|
||||||
|
wargs = {}
|
||||||
|
if 'timeout' in kw:
|
||||||
|
wargs['timeout'] = kw['timeout']
|
||||||
|
del kw['timeout']
|
||||||
|
if 'input' in kw:
|
||||||
|
wargs['input'] = kw['input']
|
||||||
|
del kw['input']
|
||||||
|
|
||||||
try:
|
try:
|
||||||
p = subprocess.Popen(cmd, **kw)
|
p = subprocess.Popen(cmd, **kw)
|
||||||
(out, err) = p.communicate()
|
(out, err) = p.communicate(**wargs)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise Errors.WafError('Execution failure: %s' % str(e), ex=e)
|
raise Errors.WafError('Execution failure: %s' % str(e), ex=e)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user