Fix binascii hexlify under Python 3

Add wrapper functions for binascii hexlify/unhexlify to deal with the
conversion between bytes and str.

Partial-Bug: #1755413

Change-Id: I8351b30b62ba19290e05c30499c3588649f8f367
Signed-off-by: Zhao Chao <zhaochao1984@gmail.com>
This commit is contained in:
Zhao Chao 2018-03-14 17:15:14 +08:00
parent d8b0e423c0
commit a5cc40f488
7 changed files with 53 additions and 17 deletions

View File

@ -12,8 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import binascii
from django.core.urlresolvers import reverse
from django import http
from django.utils.translation import ugettext_lazy as _
@ -28,6 +26,7 @@ from troveclient import common
from trove_dashboard import api
from trove_dashboard.content.databases.workflows import create_instance
from trove_dashboard.test import helpers as test
from trove_dashboard.utils import common as common_utils
INDEX_URL = reverse('horizon:project:database_backups:index')
BACKUP_URL = reverse('horizon:project:database_backups:create')
@ -249,7 +248,7 @@ class DatabasesBackupsTests(test.TestCase):
self.assertTrue(len(fields['datastore'].choices), 1)
text = 'mysql - 5.6'
choice = fields['datastore'].choices[0]
self.assertTrue(choice[0], binascii.hexlify(text))
self.assertTrue(choice[0], common_utils.hexlify(text))
self.assertTrue(choice[1], text)
advanced_step = [step for step in res.context_data['workflow'].steps

View File

@ -13,7 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import binascii
import collections
import uuid
@ -36,6 +35,7 @@ from trove_dashboard.content.database_clusters \
from trove_dashboard.content.databases import db_capability
from trove_dashboard.content.databases.workflows \
import create_instance
from trove_dashboard.utils import common as common_utils
LOG = logging.getLogger(__name__)
@ -133,7 +133,7 @@ class LaunchForm(forms.SelfHandlingForm):
if datastore_field_value:
datastore, datastore_version = (
create_instance.parse_datastore_and_version_text(
binascii.unhexlify(datastore_field_value)))
common_utils.unhexlify(datastore_field_value)))
flavor_field_name = self._build_widget_field_name(
datastore, datastore_version)
@ -286,7 +286,7 @@ class LaunchForm(forms.SelfHandlingForm):
# Since the fieldnames cannot contain an uppercase character
# we generate a hex encoded string representation of the
# datastore and version as the fieldname
return binascii.hexlify(
return common_utils.hexlify(
self._build_datastore_display_text(datastore, datastore_version))
def _insert_datastore_version_fields(self, datastore_flavor_fields):
@ -330,7 +330,7 @@ class LaunchForm(forms.SelfHandlingForm):
try:
datastore, datastore_version = (
create_instance.parse_datastore_and_version_text(
binascii.unhexlify(data['datastore'])))
common_utils.unhexlify(data['datastore'])))
flavor_field_name = self._build_widget_field_name(
datastore, datastore_version)

View File

@ -14,7 +14,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import binascii
import logging
from django.core.urlresolvers import reverse
@ -30,6 +29,7 @@ from trove_dashboard.content.database_clusters \
import cluster_manager
from trove_dashboard.content.database_clusters import tables
from trove_dashboard.test import helpers as test
from trove_dashboard.utils import common as common_utils
INDEX_URL = reverse('horizon:project:database_clusters:index')
LAUNCH_URL = reverse('horizon:project:database_clusters:launch')
@ -662,5 +662,5 @@ class ClustersTests(test.TestCase):
return datastore + ' - ' + datastore_version
def _build_flavor_widget_name(self, datastore, datastore_version):
return binascii.hexlify(self._build_datastore_display_text(
return common_utils.hexlify(self._build_datastore_display_text(
datastore, datastore_version))

View File

@ -13,7 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import binascii
import logging
import django
@ -35,6 +34,7 @@ from trove_dashboard.content.databases import tables
from trove_dashboard.content.databases import views
from trove_dashboard.content.databases.workflows import create_instance
from trove_dashboard.test import helpers as test
from trove_dashboard.utils import common as common_utils
INDEX_URL = reverse('horizon:project:databases:index')
LAUNCH_URL = reverse('horizon:project:databases:launch')
@ -1254,7 +1254,7 @@ class DatabaseTests(test.TestCase):
return datastore + ' - ' + datastore_version
def _build_flavor_widget_name(self, datastore, datastore_version):
return binascii.hexlify(self._build_datastore_display_text(
return common_utils.hexlify(self._build_datastore_display_text(
datastore, datastore_version))
@test.create_stubs({

View File

@ -12,8 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import binascii
from django.conf import settings
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
@ -31,6 +29,7 @@ from oslo_log import log as logging
from trove_dashboard import api
from trove_dashboard.utils import common as common_utils
LOG = logging.getLogger(__name__)
@ -97,7 +96,7 @@ class SetInstanceDetailsAction(workflows.Action):
self._errors["datastore"] = self.error_class([msg])
else:
datastore, datastore_version = parse_datastore_and_version_text(
binascii.unhexlify(datastore_and_version))
common_utils.unhexlify(datastore_and_version))
field_name = self._build_flavor_field_name(datastore,
datastore_version)
flavor = self.data.get(field_name, None)
@ -114,7 +113,7 @@ class SetInstanceDetailsAction(workflows.Action):
datastore_and_version = context["datastore"]
if datastore_and_version:
datastore, datastore_version = parse_datastore_and_version_text(
binascii.unhexlify(context["datastore"]))
common_utils.unhexlify(context["datastore"]))
field_name = self._build_flavor_field_name(datastore,
datastore_version)
flavor = self.data[field_name]
@ -260,7 +259,7 @@ class SetInstanceDetailsAction(workflows.Action):
# Since the fieldnames cannot contain an uppercase character
# we generate a hex encoded string representation of the
# datastore and version as the fieldname
return binascii.hexlify(
return common_utils.hexlify(
self._build_datastore_display_text(datastore, datastore_version))
def _build_flavor_field_name(self, datastore, datastore_version):
@ -581,7 +580,7 @@ class LaunchInstance(workflows.Workflow):
def handle(self, request, context):
try:
datastore, datastore_version = parse_datastore_and_version_text(
binascii.unhexlify(self.context['datastore']))
common_utils.unhexlify(self.context['datastore']))
avail_zone = context.get('availability_zone', None)
LOG.info("Launching database instance with parameters "
"{name=%s, volume=%s, volume_type=%s, flavor=%s, "

View File

View File

@ -0,0 +1,38 @@
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import binascii
import six
def hexlify(text):
"""Hexlify raw text, return hexlified text."""
if six.PY3:
text = text.encode('utf-8')
hexlified = binascii.hexlify(text)
if six.PY3:
hexlified = hexlified.decode('utf-8')
return hexlified
def unhexlify(text):
"""Unhexlify raw text, return unhexlified text."""
unhexlified = binascii.unhexlify(text)
if six.PY3:
unhexlified = unhexlified.decode('utf-8')
return unhexlified