py3 (1) - update utils to py3 compatibilty

- use osli.utils for string decoding
- add get_ansible_command function so ansible can run on py3 default system
- use six to replace unicode function
This commit is contained in:
Steve Noyes 2015-11-06 16:18:45 -05:00
parent f044408e22
commit 538cfddf60
4 changed files with 50 additions and 8 deletions

View File

@ -43,6 +43,7 @@ Requires: python-cliff >= 1.13.0
Requires: python-cliff-tablib >= 1.1
Requires: python-jsonpickle >= 0.9.2
Requires: python-oslo-i18n >= 2.5.0
Requires: python-oslo-utils >= 2.7.0
Requires: python-paramiko >= 1.15.1
Requires: python-pbr >= 1.6.0
Requires: python-six >= 1.9.0
@ -154,6 +155,9 @@ esac
%changelog
* Fri Nov 06 2015 - Steve Noyes <steve.noyes@oracle.com>
- add python-oslo-utils requirement
* Mon Oct 26 2015 - Steve Noyes <steve.noyes@oracle.com>
- Remove obsolete json_generator

View File

@ -16,8 +16,13 @@ import logging
import os
import pexpect
import pwd
import six
import sys
import yaml
from kollacli.exceptions import CommandError
from oslo_utils.encodeutils import safe_decode
def get_kolla_home():
return os.environ.get("KOLLA_HOME", "/usr/share/kolla/")
@ -48,7 +53,14 @@ def get_admin_uids():
def get_kolla_log_file_size():
return os.environ.get('KOLLA_LOG_FILE_SIZE', 500000)
envvar = 'KOLLA_LOG_FILE_SIZE'
size_str = os.environ.get(envvar, '500000')
try:
size = int(size_str)
except Exception:
raise CommandError('Environmental variable ' +
'(%s) is not an integer' % (envvar, size_str))
return size
def get_admin_user():
@ -85,18 +97,41 @@ def save_etc_yaml(fileName, contents):
f.write(yaml.dump(contents))
def get_ansible_command(playbook=False):
"""get a python2 ansible command
Ansible cannot run yet with python3. If the current default
python is py3, prefix the ansible command with a py2
interpreter.
"""
cmd = 'ansible'
if playbook:
cmd = 'ansible-playbook'
if sys.version_info[0] >= 3:
# running with py3, find a py2 interpreter for ansible
py2_path = None
usr_bin = os.path.join('/', 'usr', 'bin')
for file in os.listdir(usr_bin):
if (file.startswith('python2.') and
os.path.isfile(os.path.join(usr_bin, file))):
suffix = file.split('.')[1]
if suffix.isdigit():
py2_path = os.path.join(usr_bin, file)
break
if py2_path is None:
raise Exception('ansible-playbook requires python2 and no '
'python2 interpreter found in %s' % usr_bin)
cmd = '%s %s' % (py2_path, os.path.join(usr_bin, cmd))
return cmd
def convert_to_unicode(the_string):
"""convert string to unicode.
This is used to fixup extended ascii chars in strings. these chars cause
errors in json pickle/unpickle.
"""
uni_string = ''
try:
uni_string = unicode(the_string)
except UnicodeDecodeError:
uni_string = the_string.decode('utf-8')
return uni_string
return six.u(the_string)
def run_cmd(cmd, print_output=True):
@ -118,6 +153,7 @@ def run_cmd(cmd, print_output=True):
try:
child = pexpect.spawn(cmd)
sniff = child.read(len(pwd_prompt))
sniff = safe_decode(sniff)
if sniff == pwd_prompt:
output = sniff + '\n'
raise Exception(
@ -125,6 +161,7 @@ def run_cmd(cmd, print_output=True):
child.maxsize = 1
child.timeout = 86400
for line in child:
line = safe_decode(line)
outline = sniff + line.rstrip()
sniff = ''
output = ''.join([output, outline, '\n'])

View File

@ -5,6 +5,7 @@ cliff-tablib>=1.1
docker-py>=1.3.1
jsonpickle>=0.9
oslo.i18n>=1.3.0 # Apache-2.0
oslo.utils>-2.7.0
paramiko>=1.15
pbr>=0.10
pexpect>=2.3,<3.3

View File

@ -14,7 +14,7 @@ deps = -r{toxinidir}/requirements.txt
[testenv:py27]
commands =
/usr/bin/find . -type f -name "*.pyc" -delete
{envpython} setup.py test -s tests
{envpython} -m unittest discover -s tests -p "*.*"
[testenv:pep8]
commands = flake8