Merge pull request #918 from SEJeff/make-cmdrun-easier-in-states

Strip the trailing newline / whitespace from cmd.*
This commit is contained in:
Thomas S Hatch 2012-03-15 20:44:28 -07:00
commit 06e210f1c4

View File

@ -46,7 +46,8 @@ def _run(cmd,
runas=None, runas=None,
with_env=True, with_env=True,
shell=DEFAULT_SHELL, shell=DEFAULT_SHELL,
env=()): env=(),
rstrip=True):
''' '''
Do the DRY thing and only call subprocess.Popen() once Do the DRY thing and only call subprocess.Popen() once
''' '''
@ -105,6 +106,16 @@ def _run(cmd,
proc = subprocess.Popen(cmd, **kwargs) proc = subprocess.Popen(cmd, **kwargs)
out = proc.communicate() out = proc.communicate()
if rstrip:
# Cast out to a list as proc.communicate() returns a tuple
out = list(out)
if out[0]:
out[0] = out[0].rstrip()
# None lacks a rstrip() method
if out[1]:
out[1] = out[1].rstrip()
ret['stdout'] = out[0] ret['stdout'] = out[0]
ret['stderr'] = out[1] ret['stderr'] = out[1]
ret['pid'] = proc.pid ret['pid'] = proc.pid