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:
@@ -12,8 +12,6 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import binascii
|
|
||||||
|
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
from django import http
|
from django import http
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
@@ -28,6 +26,7 @@ from troveclient import common
|
|||||||
from trove_dashboard import api
|
from trove_dashboard import api
|
||||||
from trove_dashboard.content.databases.workflows import create_instance
|
from trove_dashboard.content.databases.workflows import create_instance
|
||||||
from trove_dashboard.test import helpers as test
|
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')
|
INDEX_URL = reverse('horizon:project:database_backups:index')
|
||||||
BACKUP_URL = reverse('horizon:project:database_backups:create')
|
BACKUP_URL = reverse('horizon:project:database_backups:create')
|
||||||
@@ -249,7 +248,7 @@ class DatabasesBackupsTests(test.TestCase):
|
|||||||
self.assertTrue(len(fields['datastore'].choices), 1)
|
self.assertTrue(len(fields['datastore'].choices), 1)
|
||||||
text = 'mysql - 5.6'
|
text = 'mysql - 5.6'
|
||||||
choice = fields['datastore'].choices[0]
|
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)
|
self.assertTrue(choice[1], text)
|
||||||
|
|
||||||
advanced_step = [step for step in res.context_data['workflow'].steps
|
advanced_step = [step for step in res.context_data['workflow'].steps
|
||||||
|
@@ -13,7 +13,6 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import binascii
|
|
||||||
import collections
|
import collections
|
||||||
import uuid
|
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 import db_capability
|
||||||
from trove_dashboard.content.databases.workflows \
|
from trove_dashboard.content.databases.workflows \
|
||||||
import create_instance
|
import create_instance
|
||||||
|
from trove_dashboard.utils import common as common_utils
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -133,7 +133,7 @@ class LaunchForm(forms.SelfHandlingForm):
|
|||||||
if datastore_field_value:
|
if datastore_field_value:
|
||||||
datastore, datastore_version = (
|
datastore, datastore_version = (
|
||||||
create_instance.parse_datastore_and_version_text(
|
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(
|
flavor_field_name = self._build_widget_field_name(
|
||||||
datastore, datastore_version)
|
datastore, datastore_version)
|
||||||
@@ -286,7 +286,7 @@ class LaunchForm(forms.SelfHandlingForm):
|
|||||||
# Since the fieldnames cannot contain an uppercase character
|
# Since the fieldnames cannot contain an uppercase character
|
||||||
# we generate a hex encoded string representation of the
|
# we generate a hex encoded string representation of the
|
||||||
# datastore and version as the fieldname
|
# datastore and version as the fieldname
|
||||||
return binascii.hexlify(
|
return common_utils.hexlify(
|
||||||
self._build_datastore_display_text(datastore, datastore_version))
|
self._build_datastore_display_text(datastore, datastore_version))
|
||||||
|
|
||||||
def _insert_datastore_version_fields(self, datastore_flavor_fields):
|
def _insert_datastore_version_fields(self, datastore_flavor_fields):
|
||||||
@@ -330,7 +330,7 @@ class LaunchForm(forms.SelfHandlingForm):
|
|||||||
try:
|
try:
|
||||||
datastore, datastore_version = (
|
datastore, datastore_version = (
|
||||||
create_instance.parse_datastore_and_version_text(
|
create_instance.parse_datastore_and_version_text(
|
||||||
binascii.unhexlify(data['datastore'])))
|
common_utils.unhexlify(data['datastore'])))
|
||||||
|
|
||||||
flavor_field_name = self._build_widget_field_name(
|
flavor_field_name = self._build_widget_field_name(
|
||||||
datastore, datastore_version)
|
datastore, datastore_version)
|
||||||
|
@@ -14,7 +14,6 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import binascii
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
@@ -30,6 +29,7 @@ from trove_dashboard.content.database_clusters \
|
|||||||
import cluster_manager
|
import cluster_manager
|
||||||
from trove_dashboard.content.database_clusters import tables
|
from trove_dashboard.content.database_clusters import tables
|
||||||
from trove_dashboard.test import helpers as test
|
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')
|
INDEX_URL = reverse('horizon:project:database_clusters:index')
|
||||||
LAUNCH_URL = reverse('horizon:project:database_clusters:launch')
|
LAUNCH_URL = reverse('horizon:project:database_clusters:launch')
|
||||||
@@ -662,5 +662,5 @@ class ClustersTests(test.TestCase):
|
|||||||
return datastore + ' - ' + datastore_version
|
return datastore + ' - ' + datastore_version
|
||||||
|
|
||||||
def _build_flavor_widget_name(self, 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))
|
datastore, datastore_version))
|
||||||
|
@@ -13,7 +13,6 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import binascii
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import django
|
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 import views
|
||||||
from trove_dashboard.content.databases.workflows import create_instance
|
from trove_dashboard.content.databases.workflows import create_instance
|
||||||
from trove_dashboard.test import helpers as test
|
from trove_dashboard.test import helpers as test
|
||||||
|
from trove_dashboard.utils import common as common_utils
|
||||||
|
|
||||||
INDEX_URL = reverse('horizon:project:databases:index')
|
INDEX_URL = reverse('horizon:project:databases:index')
|
||||||
LAUNCH_URL = reverse('horizon:project:databases:launch')
|
LAUNCH_URL = reverse('horizon:project:databases:launch')
|
||||||
@@ -1254,7 +1254,7 @@ class DatabaseTests(test.TestCase):
|
|||||||
return datastore + ' - ' + datastore_version
|
return datastore + ' - ' + datastore_version
|
||||||
|
|
||||||
def _build_flavor_widget_name(self, 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))
|
datastore, datastore_version))
|
||||||
|
|
||||||
@test.create_stubs({
|
@test.create_stubs({
|
||||||
|
@@ -12,8 +12,6 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import binascii
|
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
from django.utils.translation import ugettext_lazy as _
|
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 import api
|
||||||
|
from trove_dashboard.utils import common as common_utils
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -97,7 +96,7 @@ class SetInstanceDetailsAction(workflows.Action):
|
|||||||
self._errors["datastore"] = self.error_class([msg])
|
self._errors["datastore"] = self.error_class([msg])
|
||||||
else:
|
else:
|
||||||
datastore, datastore_version = parse_datastore_and_version_text(
|
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,
|
field_name = self._build_flavor_field_name(datastore,
|
||||||
datastore_version)
|
datastore_version)
|
||||||
flavor = self.data.get(field_name, None)
|
flavor = self.data.get(field_name, None)
|
||||||
@@ -114,7 +113,7 @@ class SetInstanceDetailsAction(workflows.Action):
|
|||||||
datastore_and_version = context["datastore"]
|
datastore_and_version = context["datastore"]
|
||||||
if datastore_and_version:
|
if datastore_and_version:
|
||||||
datastore, datastore_version = parse_datastore_and_version_text(
|
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,
|
field_name = self._build_flavor_field_name(datastore,
|
||||||
datastore_version)
|
datastore_version)
|
||||||
flavor = self.data[field_name]
|
flavor = self.data[field_name]
|
||||||
@@ -260,7 +259,7 @@ class SetInstanceDetailsAction(workflows.Action):
|
|||||||
# Since the fieldnames cannot contain an uppercase character
|
# Since the fieldnames cannot contain an uppercase character
|
||||||
# we generate a hex encoded string representation of the
|
# we generate a hex encoded string representation of the
|
||||||
# datastore and version as the fieldname
|
# datastore and version as the fieldname
|
||||||
return binascii.hexlify(
|
return common_utils.hexlify(
|
||||||
self._build_datastore_display_text(datastore, datastore_version))
|
self._build_datastore_display_text(datastore, datastore_version))
|
||||||
|
|
||||||
def _build_flavor_field_name(self, 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):
|
def handle(self, request, context):
|
||||||
try:
|
try:
|
||||||
datastore, datastore_version = parse_datastore_and_version_text(
|
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)
|
avail_zone = context.get('availability_zone', None)
|
||||||
LOG.info("Launching database instance with parameters "
|
LOG.info("Launching database instance with parameters "
|
||||||
"{name=%s, volume=%s, volume_type=%s, flavor=%s, "
|
"{name=%s, volume=%s, volume_type=%s, flavor=%s, "
|
||||||
|
0
trove_dashboard/utils/__init__.py
Normal file
0
trove_dashboard/utils/__init__.py
Normal file
38
trove_dashboard/utils/common.py
Normal file
38
trove_dashboard/utils/common.py
Normal 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
|
Reference in New Issue
Block a user