Make the ziploader wrapper work with PY3

This commit is contained in:
Toshio Kuratomi 2016-04-12 10:08:00 -07:00
parent 0031e08f7d
commit 7d174910c3

View file

@ -96,9 +96,10 @@ import subprocess
if sys.version_info < (3,): if sys.version_info < (3,):
bytes = str bytes = str
PY3 = False
else: else:
unicode = str unicode = str
PY3 = True
try: try:
# Python-2.6+ # Python-2.6+
from io import BytesIO as IOStream from io import BytesIO as IOStream
@ -122,9 +123,12 @@ def invoke_module(module, modlib_path, json_params):
stderr = stderr.read() stderr = stderr.read()
if not isinstance(stdout, (bytes, unicode)): if not isinstance(stdout, (bytes, unicode)):
stdout = stdout.read() stdout = stdout.read()
sys.stderr.write(stderr) if PY3:
sys.stdout.write(stdout) sys.stderr.buffer.write(stderr)
sys.stdout.buffer.write(stdout)
else:
sys.stderr.write(stderr)
sys.stdout.write(stdout)
return p.returncode return p.returncode
def debug(command, zipped_mod, json_params): def debug(command, zipped_mod, json_params):
@ -192,7 +196,8 @@ def debug(command, zipped_mod, json_params):
if __name__ == '__main__': if __name__ == '__main__':
ZIPLOADER_PARAMS = %(params)s ZIPLOADER_PARAMS = %(params)s
if PY3:
ZIPLOADER_PARAMS = ZIPLOADER_PARAMS.encode('utf-8')
try: try:
temp_path = tempfile.mkdtemp(prefix='ansible_') temp_path = tempfile.mkdtemp(prefix='ansible_')
zipped_mod = os.path.join(temp_path, 'ansible_modlib.zip') zipped_mod = os.path.join(temp_path, 'ansible_modlib.zip')