[libvirt] [libvirt-test-API][PATCH] Fix utils.exec_cmd output problem

Jincheng Miao jmiao at redhat.com
Wed Nov 20 08:24:44 UTC 2013


----- Original Message -----
> The 'out' returned from exec_cmds is not 'None' type if stderr occurs, it is
> because utils pass 'subprocess.PIPE' to stdout to open this subprocess.
> 
> "Similarly, to get anything other than None in the result tuple, you need to
> give stdout=PIPE and/or stderr=PIPE too."
> (see http://docs.python.org/2/library/subprocess.html#module-subprocess)
> 
> Finally, make out equals to err when stderr happened.
> ---
>  utils/utils.py | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/utils/utils.py b/utils/utils.py
> index 147c1ef..2248ce1 100644
> --- a/utils/utils.py
> +++ b/utils/utils.py
> @@ -404,14 +404,12 @@ def exec_cmd(command, sudo=False, cwd=None,
> infile=None, outfile=None, shell=Fal
>              command = ["sudo"] + command
>      if infile == None:
>          infile = subprocess.PIPE
> -    if outfile == None:
> -        outfile = subprocess.PIPE

hmm, there is a little problem: if outfile is not subprocess.PIPE, exec_cmd 
would not capture the output of command. So I think the best choice is check
whether 'out' is a null string, rather than a None type.

>      p = subprocess.Popen(command, shell=shell, close_fds=True, cwd=cwd,
>                      stdin=infile, stdout=outfile, stderr=subprocess.PIPE)
>      (out, err) = p.communicate(data)
>      if out == None:
> -        # Prevent splitlines() from barfing later on
> -        out = ""
> +	# Because stderr is PIPE, err will not be None, and can be splitlines.
> +        out = err
>      return (p.returncode, out.splitlines())
>  
>  def remote_exec_pexpect(hostname, username, password, cmd):
> --
> 1.8.3.1
> 
> 




More information about the libvir-list mailing list