Reuse to_utf8() and jsonutils.dump_as_bytes()

Reactor code:

* Reuse oslo_utils.encodeutils.to_utf8() instead of existing
  isinstance(text, six.text_type) test
* Replace jsonutils.dumps(obj).encode('utf-8') with
  jsonutils.dump_as_bytes(obj).
* Other minor bytes/Unicode changes

Change-Id: I03b8eff0fd70ab65ac66d6f3221e8ced0a56db17
This commit is contained in:
Victor Stinner 2016-02-16 15:23:20 +01:00
parent 8582373d61
commit 6355173e4c
11 changed files with 32 additions and 37 deletions

View File

@ -29,8 +29,8 @@ from eventlet import greenthread
from oslo_config import cfg
from oslo_log import log as logging
from oslo_rootwrap import client
from oslo_utils import encodeutils
from oslo_utils import excutils
import six
from six.moves import http_client as httplib
from neutron._i18n import _, _LE
@ -104,11 +104,10 @@ def execute(cmd, process_input=None, addl_env=None,
check_exit_code=True, return_stderr=False, log_fail_as_error=True,
extra_ok_codes=None, run_as_root=False):
try:
if (process_input is None or
isinstance(process_input, six.binary_type)):
_process_input = process_input
if process_input is not None:
_process_input = encodeutils.to_utf8(process_input)
else:
_process_input = process_input.encode('utf-8')
_process_input = None
if run_as_root and cfg.CONF.AGENT.root_helper_daemon:
returncode, _stdout, _stderr = (
execute_rootwrap_daemon(cmd, process_input, addl_env))
@ -153,8 +152,7 @@ def get_interface_mac(interface):
MAC_END = 24
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
dev = interface[:constants.DEVICE_NAME_MAX_LEN]
if isinstance(dev, six.text_type):
dev = dev.encode('utf-8')
dev = encodeutils.to_utf8(dev)
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', dev))
return ''.join(['%02x:' % ord(char)
for char in info[MAC_START:MAC_END]])[:-1]

View File

@ -20,6 +20,7 @@ from oslo_config import cfg
from oslo_log import log as logging
import oslo_messaging
from oslo_service import loopingcall
from oslo_utils import encodeutils
import six
import six.moves.urllib.parse as urlparse
import webob
@ -223,10 +224,8 @@ class MetadataProxyHandler(object):
def _sign_instance_id(self, instance_id):
secret = self.conf.metadata_proxy_shared_secret
if isinstance(secret, six.text_type):
secret = secret.encode('utf-8')
if isinstance(instance_id, six.text_type):
instance_id = instance_id.encode('utf-8')
secret = encodeutils.to_utf8(secret)
instance_id = encodeutils.to_utf8(instance_id)
return hmac.new(secret, instance_id, hashlib.sha256).hexdigest()

View File

@ -18,7 +18,7 @@ import os
from eventlet.green import subprocess
from eventlet import greenthread
from oslo_log import log as logging
import six
from oslo_utils import encodeutils
from neutron._i18n import _
from neutron.common import utils
@ -50,11 +50,10 @@ def execute(cmd, process_input=None, addl_env=None,
extra_ok_codes=None, run_as_root=False, do_decode=True):
try:
if (process_input is None or
isinstance(process_input, six.binary_type)):
_process_input = process_input
if process_input is not None:
_process_input = encodeutils.to_utf8(process_input)
else:
_process_input = process_input.encode('utf-8')
_process_input = None
obj, cmd = create_process(cmd, addl_env=addl_env)
_stdout, _stderr = obj.communicate(_process_input)
obj.stdin.close()

View File

@ -20,7 +20,7 @@ import hashlib
from oslo_config import cfg
from oslo_log import log as logging
import six
from oslo_utils import encodeutils
import webob.exc
from neutron._i18n import _, _LI
@ -180,10 +180,7 @@ def get_interface_name(name, prefix='', max_len=n_const.DEVICE_NAME_MAX_LEN):
"given length for an interface name."))
namelen = max_len - len(prefix) - INTERFACE_HASH_LEN
if isinstance(name, six.text_type):
hashed_name = hashlib.sha1(name.encode('utf-8'))
else:
hashed_name = hashlib.sha1(name)
hashed_name = hashlib.sha1(encodeutils.to_utf8(name))
new_name = ('%(prefix)s%(truncated)s%(hash)s' %
{'prefix': prefix, 'truncated': name[0:namelen],
'hash': hashed_name.hexdigest()[0:INTERFACE_HASH_LEN]})

View File

@ -15,6 +15,7 @@
import hashlib
import sys
from oslo_utils import encodeutils
from neutron.cmd.eventlet.plugins.ovs_neutron_agent import main as _main
from neutron.common import constants as n_const
@ -30,10 +31,13 @@ def get_tunnel_name_full(cls, network_type, local_ip, remote_ip):
# Remove length of network_type and two dashes
hashlen = (n_const.DEVICE_NAME_MAX_LEN - len(network_type) - 2) // 2
remote_ip_hex = remote_ip_hex.encode('utf-8')
remote_ip_hash = hashlib.sha1(remote_ip_hex).hexdigest()[0:hashlen]
local_ip_hex = cls.get_ip_in_hex(local_ip).encode('utf-8')
source_ip_hash = hashlib.sha1(local_ip_hex).hexdigest()[0:hashlen]
remote_ip_hex = encodeutils.to_utf8(remote_ip_hex)
remote_ip_hash = hashlib.sha1(remote_ip_hex).hexdigest()[:hashlen]
local_ip_hex = cls.get_ip_in_hex(local_ip)
local_ip_hex = encodeutils.to_utf8(local_ip_hex)
source_ip_hash = hashlib.sha1(local_ip_hex).hexdigest()[:hashlen]
return '%s-%s-%s' % (network_type, source_ip_hash, remote_ip_hash)
OVSNeutronAgent.get_tunnel_name = get_tunnel_name_full

View File

@ -35,7 +35,7 @@ CONF = cfg.CONF
# This message will be written to temporary file each time
# start method is called.
FAKE_START_MSG = "start".encode("utf-8")
FAKE_START_MSG = b"start"
TARGET_PLUGIN = 'neutron.plugins.ml2.plugin.Ml2Plugin'

View File

@ -135,12 +135,11 @@ class AgentUtilsExecuteTest(base.BaseTestCase):
self.mock_popen.return_value = [bytes_odata, b'']
result = utils.execute(['cat'], process_input=str_idata)
self.mock_popen.assert_called_once_with(bytes_idata)
self.assertEqual(str_odata, result)
else:
self.mock_popen.return_value = [str_odata, '']
result = utils.execute(['cat'], process_input=str_idata)
self.mock_popen.assert_called_once_with(str_idata)
self.assertEqual(str_odata, result)
self.assertEqual(str_odata, result)
def test_return_str_data(self):
str_data = "%s\n" % self.test_file

View File

@ -529,7 +529,7 @@ class RequestExtensionTest(base.BaseTestCase):
def extend_response_data(req, res):
data = jsonutils.loads(res.body)
data['FOXNSOX:extended_key'] = req.GET.get('extended_key')
res.body = jsonutils.dumps(data).encode('utf-8')
res.body = jsonutils.dump_as_bytes(data)
return res
app = self._setup_app_with_request_handler(extend_response_data, 'GET')
@ -555,7 +555,7 @@ class RequestExtensionTest(base.BaseTestCase):
def _update_handler(req, res):
data = jsonutils.loads(res.body)
data['uneditable'] = req.params['uneditable']
res.body = jsonutils.dumps(data).encode('utf-8')
res.body = jsonutils.dump_as_bytes(data)
return res
base_app = webtest.TestApp(setup_base_app(self))

View File

@ -77,7 +77,7 @@ class Foxinsocks(extensions.ExtensionDescriptor):
# You can use content type header to test for XML.
data = jsonutils.loads(res.body)
data['FOXNSOX:googoose'] = req.GET.get('chewing')
res.body = jsonutils.dumps(data).encode('utf-8')
res.body = jsonutils.dump_as_bytes(data)
return res
req_ext1 = extensions.RequestExtension('GET', '/dummy_resources/:(id)',
@ -89,7 +89,7 @@ class Foxinsocks(extensions.ExtensionDescriptor):
# You can use content type header to test for XML.
data = jsonutils.loads(res.body)
data['FOXNSOX:big_bands'] = 'Pig Bands!'
res.body = jsonutils.dumps(data).encode('utf-8')
res.body = jsonutils.dump_as_bytes(data)
return res
req_ext2 = extensions.RequestExtension('GET', '/dummy_resources/:(id)',

View File

@ -156,7 +156,7 @@ class TestWSGIServer(base.BaseTestCase):
])
def test_app(self):
greetings = 'Hello, World!!!'
greetings = b'Hello, World!!!'
def hello_world(env, start_response):
if env['PATH_INFO'] != '/':
@ -171,7 +171,7 @@ class TestWSGIServer(base.BaseTestCase):
response = open_no_proxy('http://127.0.0.1:%d/' % server.port)
self.assertEqual(greetings.encode('utf-8'), response.read())
self.assertEqual(greetings, response.read())
server.stop()

View File

@ -32,6 +32,7 @@ from oslo_service import service as common_service
from oslo_service import sslutils
from oslo_service import systemd
from oslo_service import wsgi
from oslo_utils import encodeutils
from oslo_utils import excutils
import six
import webob.dec
@ -69,9 +70,7 @@ def encode_body(body):
WebOb requires to encode unicode body used to update response body.
"""
if isinstance(body, six.text_type):
return body.encode('utf-8')
return body
return encodeutils.to_utf8(body)
class WorkerService(worker.NeutronWorker):