mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-24 11:21:25 -07:00
Fixes to the controller text model (#17527)
* Fixes to the controller text model * Change command line args to text type * Make display replace undecodable bytes with replacement chars. This is only a problem on pyhton3 where surrogates can enter into the msg but sys.stdout doesn't know how to handle them. * Remove a deprecated playbook syntax in unicode.yml * Fix up run_cmd to change its parameters to byte string at appropriate times.
This commit is contained in:
parent
9868117d1f
commit
bf29961947
4 changed files with 27 additions and 9 deletions
|
@ -24,15 +24,26 @@ import shlex
|
|||
import subprocess
|
||||
import select
|
||||
|
||||
from ansible.compat.six import PY2
|
||||
from ansible.module_utils._text import to_bytes
|
||||
|
||||
def run_cmd(cmd, live=False, readsize=10):
|
||||
|
||||
#readsize = 10
|
||||
|
||||
# On python2, shlex needs byte strings
|
||||
if PY2:
|
||||
cmd = to_bytes(cmd, errors='surrogate_or_strict')
|
||||
cmdargs = shlex.split(cmd)
|
||||
|
||||
# subprocess should be passed byte strings. (on python2.6 it must be
|
||||
# passed byte strtings)
|
||||
cmdargs = [to_bytes(a, errors='surrogate_or_strict') for a in cmdargs]
|
||||
|
||||
p = subprocess.Popen(cmdargs, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
|
||||
stdout = ''
|
||||
stderr = ''
|
||||
stdout = b''
|
||||
stderr = b''
|
||||
rpipes = [p.stdout, p.stderr]
|
||||
while True:
|
||||
rfd, wfd, efd = select.select(rpipes, [], rpipes, 1)
|
||||
|
@ -42,14 +53,14 @@ def run_cmd(cmd, live=False, readsize=10):
|
|||
if live:
|
||||
sys.stdout.write(dat)
|
||||
stdout += dat
|
||||
if dat == '':
|
||||
if dat == b'':
|
||||
rpipes.remove(p.stdout)
|
||||
if p.stderr in rfd:
|
||||
dat = os.read(p.stderr.fileno(), readsize)
|
||||
stderr += dat
|
||||
if live:
|
||||
sys.stdout.write(dat)
|
||||
if dat == '':
|
||||
if dat == b'':
|
||||
rpipes.remove(p.stderr)
|
||||
# only break out if we've emptied the pipes, or there is nothing to
|
||||
# read from and the process has finished.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue