Remove six of dir cinder/volume/*

Replace the following items with Python 3 style code.

- six.add_metaclass
- six.PY3
- six.text_type
- six.string_types
- six.integer_types

Change-Id: Ifc3484ef0897d25c3a5ed3c10efe4312dbbbd384
Implements: blueprint six-removal
This commit is contained in:
xuanyandong 2020-10-08 09:02:07 +08:00
parent 6ad1ab0c72
commit e02d909439
10 changed files with 54 additions and 84 deletions

View File

@ -27,7 +27,6 @@ from oslo_utils import excutils
from oslo_utils import strutils
from oslo_utils import timeutils
from oslo_utils import versionutils
import six
from cinder.api import common
from cinder.common import constants
@ -619,7 +618,7 @@ class API(base.Base):
filters['no_migration_targets'] = True
if filters:
LOG.debug("Searching by: %s.", six.text_type(filters))
LOG.debug("Searching by: %s.", filters)
if context.is_admin and allTenants:
# Need to remove all_tenants to pass the filtering below.
@ -1407,7 +1406,7 @@ class API(base.Base):
"to extend, currently %(status)s.")
% {'vol_id': volume.id,
'status': volume.status,
'expected': six.text_type(expected)})
'expected': str(expected)})
raise exception.InvalidVolume(reason=msg)
rollback = True
@ -1626,7 +1625,7 @@ class API(base.Base):
'vol_status': volume['status']}
raise exception.InvalidVolume(reason=msg)
self.update_volume_admin_metadata(context.elevated(), volume,
{'readonly': six.text_type(flag)})
{'readonly': str(flag)})
LOG.info("Update readonly setting on volume "
"completed successfully.",
resource=volume)
@ -1934,7 +1933,7 @@ class API(base.Base):
def _replication_db_change(self, ctxt, field, expected_value, new_value,
host, cluster_name, check_up=False):
def _error_msg(service):
expected = utils.build_or_str(six.text_type(expected_value))
expected = utils.build_or_str(str(expected_value))
up_msg = 'and must be up ' if check_up else ''
msg = (_('%(field)s in %(service)s must be %(expected)s '
'%(up_msg)sto failover.')

View File

@ -24,7 +24,6 @@ from oslo_config import cfg
from oslo_config import types
from oslo_log import log as logging
from oslo_utils import excutils
import six
from cinder import exception
from cinder.i18n import _
@ -357,8 +356,7 @@ CONF.register_opts(fqdn_opts, group=configuration.SHARED_CONF_GROUP)
CONF.import_opt('backup_use_same_host', 'cinder.backup.api')
@six.add_metaclass(abc.ABCMeta)
class BaseVD(object):
class BaseVD(object, metaclass=abc.ABCMeta):
"""Executes commands relating to Volumes.
Base Driver for Cinder Volume Control Path,
@ -1046,15 +1044,13 @@ class BaseVD(object):
except Exception as err:
try:
err_msg = (_('Unable to fetch connection information from '
'backend: %(err)s') %
{'err': six.text_type(err)})
'backend: %(err)s') % {'err': err})
LOG.error(err_msg)
LOG.debug("Cleaning up failed connect initialization.")
self.remove_export(context, volume)
except Exception as ex:
ex_msg = (_('Error encountered during cleanup '
'of a failed attach: %(ex)s') %
{'ex': six.text_type(ex)})
'of a failed attach: %(ex)s') % {'ex': ex})
LOG.error(err_msg)
raise exception.VolumeBackendAPIException(data=ex_msg)
raise exception.VolumeBackendAPIException(data=err_msg)
@ -1112,15 +1108,13 @@ class BaseVD(object):
except Exception as err:
try:
err_msg = (_('Unable to fetch connection information from '
'backend: %(err)s') %
{'err': six.text_type(err)})
'backend: %(err)s') % {'err': err})
LOG.error(err_msg)
LOG.debug("Cleaning up failed connect initialization.")
self.remove_export_snapshot(ctxt, snapshot)
except Exception as ex:
ex_msg = (_('Error encountered during cleanup '
'of a failed attach: %(ex)s') %
{'ex': six.text_type(ex)})
'of a failed attach: %(ex)s') % {'ex': ex})
LOG.error(err_msg)
raise exception.VolumeBackendAPIException(data=ex_msg)
raise exception.VolumeBackendAPIException(data=err_msg)
@ -1953,8 +1947,7 @@ class BaseVD(object):
for cfg_name in cfg_names]
@six.add_metaclass(abc.ABCMeta)
class CloneableImageVD(object):
class CloneableImageVD(object, metaclass=abc.ABCMeta):
@abc.abstractmethod
def clone_image(self, volume, image_location,
image_id, image_meta, image_service):
@ -1983,8 +1976,7 @@ class CloneableImageVD(object):
return None, False
@six.add_metaclass(abc.ABCMeta)
class MigrateVD(object):
class MigrateVD(object, metaclass=abc.ABCMeta):
@abc.abstractmethod
def migrate_volume(self, context, volume, host):
"""Migrate the volume to the specified host.
@ -2001,8 +1993,7 @@ class MigrateVD(object):
return (False, None)
@six.add_metaclass(abc.ABCMeta)
class ManageableVD(object):
class ManageableVD(object, metaclass=abc.ABCMeta):
@abc.abstractmethod
def manage_existing(self, volume, existing_ref):
"""Brings an existing backend storage object under Cinder management.
@ -2098,8 +2089,7 @@ class ManageableVD(object):
pass
@six.add_metaclass(abc.ABCMeta)
class ManageableSnapshotsVD(object):
class ManageableSnapshotsVD(object, metaclass=abc.ABCMeta):
# NOTE: Can't use abstractmethod before all drivers implement it
def manage_existing_snapshot(self, snapshot, existing_ref):
"""Brings an existing backend storage object under Cinder management.

View File

@ -12,7 +12,6 @@
from oslo_config import cfg
from oslo_log import log as logging
import six
import taskflow.engines
from taskflow.patterns import linear_flow
from taskflow.types import failure as ft
@ -175,7 +174,7 @@ class ExtractVolumeRequestTask(flow_utils.CinderTask):
raise exception.InvalidInput(reason=msg)
def validate_int(size):
if not isinstance(size, six.integer_types) or size <= 0:
if not isinstance(size, int) or size <= 0:
msg = _("Volume size '%(size)s' must be an integer and"
" greater than 0") % {'size': size}
raise exception.InvalidInput(reason=msg)

View File

@ -17,7 +17,6 @@
# under the License.
from oslo_log import log as logging
import six
from cinder import exception
@ -66,7 +65,7 @@ def restore_source_status(context, db, volume_spec):
def _clean_reason(reason):
if reason is None:
return 'Unknown reason'
reason = six.text_type(reason)
reason = str(reason)
if len(reason) <= REASON_LENGTH:
return reason
else:

View File

@ -21,7 +21,6 @@ from oslo_log import log as logging
from oslo_utils import excutils
from oslo_utils import fileutils
from oslo_utils import timeutils
import six
import taskflow.engines
from taskflow.patterns import linear_flow
from taskflow.types import failure as ft
@ -531,7 +530,7 @@ class CreateVolumeFromSpecTask(flow_utils.CinderTask):
LOG.debug("attempting attach for rekey, attach_info: %s",
attach_info)
if (isinstance(attach_info['device']['path'], six.string_types)):
if (isinstance(attach_info['device']['path'], str)):
image_info = image_utils.qemu_img_info(
attach_info['device']['path'])
else:

View File

@ -18,7 +18,6 @@
from oslo_config import cfg
from oslo_db import exception as db_exc
from oslo_log import log as logging
import six
import webob
from cinder import context
@ -80,7 +79,7 @@ def destroy(context, id):
try:
db.group_type_destroy(elevated, id)
except exception.GroupTypeInUse as e:
msg = _('Target group type is still in use. %s') % six.text_type(e)
msg = _('Target group type is still in use. %s') % e
raise webob.exc.HTTPBadRequest(explanation=msg)

View File

@ -50,7 +50,6 @@ from oslo_utils import units
from oslo_utils import uuidutils
profiler = importutils.try_import('osprofiler.profiler')
import requests
import six
from taskflow import exceptions as tfe
from cinder.common import constants
@ -1071,7 +1070,7 @@ class VolumeManager(manager.CleanableManager,
'snapshot %(s_id)s failed with %(error)s.')
msg_args = {'v_id': volume.id,
's_id': snapshot.id,
'error': six.text_type(error)}
'error': error}
v_res = volume.update_single_status_where(
'error',
'reverting')
@ -1184,7 +1183,7 @@ class VolumeManager(manager.CleanableManager,
exception=ex,
detail=message_field.Detail.SNAPSHOT_UPDATE_METADATA_FAILED
)
raise exception.MetadataCopyFailure(reason=six.text_type(ex))
raise exception.MetadataCopyFailure(reason=str(ex))
snapshot.status = fields.SnapshotStatus.AVAILABLE
snapshot.progress = '100%'
@ -1467,7 +1466,7 @@ class VolumeManager(manager.CleanableManager,
"remove-export failure.",
resource=volume)
raise exception.RemoveExportException(volume=volume_id,
reason=six.text_type(ex))
reason=str(ex))
volume.finish_detach(attachment.id)
self._notify_about_volume_usage(context, volume, "detach.end")
@ -1688,7 +1687,7 @@ class VolumeManager(manager.CleanableManager,
# Deletes the image if it is in queued or saving state
self._delete_image(context, image_meta['id'], image_service)
with excutils.save_and_reraise_exception():
payload['message'] = six.text_type(error)
payload['message'] = str(error)
finally:
self.db.volume_update_status_based_on_attachment(context,
volume_id)
@ -1834,10 +1833,10 @@ class VolumeManager(manager.CleanableManager,
try:
self.driver.validate_connector(connector)
except exception.InvalidConnectorException as err:
raise exception.InvalidInput(reason=six.text_type(err))
raise exception.InvalidInput(reason=str(err))
except Exception as err:
err_msg = (_("Validate volume connection failed "
"(error: %(err)s).") % {'err': six.text_type(err)})
"(error: %(err)s).") % {'err': err})
LOG.exception(err_msg, resource=volume)
raise exception.VolumeBackendAPIException(data=err_msg)
@ -1859,13 +1858,13 @@ class VolumeManager(manager.CleanableManager,
self.driver.remove_export(context.elevated(), volume)
except Exception:
LOG.exception('Could not remove export after DB model failed.')
raise exception.ExportFailure(reason=six.text_type(ex))
raise exception.ExportFailure(reason=str(ex))
try:
conn_info = self.driver.initialize_connection(volume, connector)
except Exception as err:
err_msg = (_("Driver initialize connection failed "
"(error: %(err)s).") % {'err': six.text_type(err)})
"(error: %(err)s).") % {'err': err})
LOG.exception(err_msg, resource=volume)
self.driver.remove_export(context.elevated(), volume)
@ -1883,10 +1882,10 @@ class VolumeManager(manager.CleanableManager,
try:
self.driver.validate_connector(connector)
except exception.InvalidConnectorException as err:
raise exception.InvalidInput(reason=six.text_type(err))
raise exception.InvalidInput(reason=str(err))
except Exception as err:
err_msg = (_("Validate snapshot connection failed "
"(error: %(err)s).") % {'err': six.text_type(err)})
"(error: %(err)s).") % {'err': err})
LOG.exception(err_msg, resource=snapshot)
raise exception.VolumeBackendAPIException(data=err_msg)
@ -1912,7 +1911,7 @@ class VolumeManager(manager.CleanableManager,
snapshot.save()
except exception.CinderException as ex:
LOG.exception("Model update failed.", resource=snapshot)
raise exception.ExportFailure(reason=six.text_type(ex))
raise exception.ExportFailure(reason=str(ex))
try:
conn = self.driver.initialize_connection_snapshot(snapshot,
@ -1920,15 +1919,13 @@ class VolumeManager(manager.CleanableManager,
except Exception as err:
try:
err_msg = (_('Unable to fetch connection information from '
'backend: %(err)s') %
{'err': six.text_type(err)})
'backend: %(err)s') % {'err': err})
LOG.error(err_msg)
LOG.debug("Cleaning up failed connect initialization.")
self.driver.remove_export_snapshot(ctxt.elevated(), snapshot)
except Exception as ex:
ex_msg = (_('Error encountered during cleanup '
'of a failed attach: %(ex)s') %
{'ex': six.text_type(ex)})
'of a failed attach: %(ex)s') % {'ex': ex})
LOG.error(ex_msg)
raise exception.VolumeBackendAPIException(data=ex_msg)
raise exception.VolumeBackendAPIException(data=err_msg)
@ -1953,7 +1950,7 @@ class VolumeManager(manager.CleanableManager,
force=force)
except Exception as err:
err_msg = (_('Terminate volume connection failed: %(err)s')
% {'err': six.text_type(err)})
% {'err': err})
LOG.exception(err_msg, resource=volume_ref)
raise exception.VolumeBackendAPIException(data=err_msg)
LOG.info("Terminate volume connection completed successfully.",
@ -1969,7 +1966,7 @@ class VolumeManager(manager.CleanableManager,
force=force)
except Exception as err:
err_msg = (_('Terminate snapshot connection failed: %(err)s')
% {'err': six.text_type(err)})
% {'err': err})
LOG.exception(err_msg, resource=snapshot)
raise exception.VolumeBackendAPIException(data=err_msg)
LOG.info("Terminate snapshot connection completed successfully.",
@ -2053,7 +2050,7 @@ class VolumeManager(manager.CleanableManager,
root_access = True
if not connector.check_valid_device(vol_handle['path'], root_access):
if isinstance(vol_handle['path'], six.string_types):
if isinstance(vol_handle['path'], str):
raise exception.DeviceUnavailable(
path=vol_handle['path'],
reason=(_("Unable to access the backend storage via the "
@ -3467,7 +3464,7 @@ class VolumeManager(manager.CleanableManager,
if group:
group.status = fields.GroupStatus.ERROR
group.save()
raise exception.MetadataCopyFailure(reason=six.text_type(ex))
raise exception.MetadataCopyFailure(reason=str(ex))
self.db.volume_update(context, vol['id'], update)
@ -3935,7 +3932,7 @@ class VolumeManager(manager.CleanableManager,
snapshot.status = fields.SnapshotStatus.ERROR
snapshot.save()
raise exception.MetadataCopyFailure(
reason=six.text_type(ex))
reason=str(ex))
snapshot.status = fields.SnapshotStatus.AVAILABLE
snapshot.progress = '100%'
@ -4498,10 +4495,10 @@ class VolumeManager(manager.CleanableManager,
try:
self.driver.validate_connector(connector)
except exception.InvalidConnectorException as err:
raise exception.InvalidInput(reason=six.text_type(err))
raise exception.InvalidInput(reason=str(err))
except Exception as err:
err_msg = (_("Validate volume connection failed "
"(error: %(err)s).") % {'err': six.text_type(err)})
"(error: %(err)s).") % {'err': err})
LOG.error(err_msg, resource=volume)
raise exception.VolumeBackendAPIException(data=err_msg)
@ -4519,13 +4516,13 @@ class VolumeManager(manager.CleanableManager,
volume.save()
except exception.CinderException as ex:
LOG.exception("Model update failed.", resource=volume)
raise exception.ExportFailure(reason=six.text_type(ex))
raise exception.ExportFailure(reason=str(ex))
try:
conn_info = self.driver.initialize_connection(volume, connector)
except Exception as err:
err_msg = (_("Driver initialize connection failed "
"(error: %(err)s).") % {'err': six.text_type(err)})
"(error: %(err)s).") % {'err': err})
LOG.exception(err_msg, resource=volume)
self.driver.remove_export(ctxt.elevated(), volume)
raise exception.VolumeBackendAPIException(data=err_msg)
@ -4641,7 +4638,7 @@ class VolumeManager(manager.CleanableManager,
except Exception as err:
err_msg = (_('Terminate volume connection failed: %(err)s')
% {'err': six.text_type(err)})
% {'err': err})
LOG.exception(err_msg, resource=volume)
raise exception.VolumeBackendAPIException(data=err_msg)
LOG.info("Terminate volume connection completed successfully.",
@ -4770,8 +4767,7 @@ class VolumeManager(manager.CleanableManager,
vol.status = 'error'
vol.replication_status = fields.ReplicationStatus.ERROR
vol.save()
err_msg = _("Enable replication group failed: "
"%s.") % six.text_type(ex)
err_msg = _("Enable replication group failed: %s.") % ex
raise exception.ReplicationGroupError(reason=err_msg,
group_id=group.id)
@ -4855,8 +4851,7 @@ class VolumeManager(manager.CleanableManager,
vol.status = 'error'
vol.replication_status = fields.ReplicationStatus.ERROR
vol.save()
err_msg = _("Disable replication group failed: "
"%s.") % six.text_type(ex)
err_msg = _("Disable replication group failed: %s.") % ex
raise exception.ReplicationGroupError(reason=err_msg,
group_id=group.id)
@ -4947,8 +4942,7 @@ class VolumeManager(manager.CleanableManager,
vol.status = 'error'
vol.replication_status = fields.ReplicationStatus.ERROR
vol.save()
err_msg = _("Failover replication group failed: "
"%s.") % six.text_type(ex)
err_msg = _("Failover replication group failed: %s.") % ex
raise exception.ReplicationGroupError(reason=err_msg,
group_id=group.id)

View File

@ -13,13 +13,11 @@
import abc
from oslo_config import cfg
import six
CONF = cfg.CONF
@six.add_metaclass(abc.ABCMeta)
class Target(object):
class Target(object, metaclass=abc.ABCMeta):
"""Target object for block storage devices.
Base class for target object, where target

View File

@ -17,7 +17,6 @@ from oslo_log import log as logging
from oslo_serialization import jsonutils as json
from oslo_utils import excutils
from oslo_utils import uuidutils
import six
from cinder import exception
from cinder.privsep import nvmcli
@ -122,7 +121,7 @@ class NVMET(nvmeof.NVMeOF):
"namespaces": [
{
"device": {
"nguid": six.text_type(uuidutils.generate_uuid()),
"nguid": str(uuidutils.generate_uuid()),
"path": volume_path,
},
"enable": 1,

View File

@ -43,8 +43,6 @@ from oslo_utils import netutils
from oslo_utils import strutils
from oslo_utils import timeutils
from oslo_utils import units
import six
from six.moves import range
from cinder.brick.local_dev import lvm as brick_lvm
from cinder import context
@ -550,11 +548,11 @@ def _transfer_data(src, dest, length, chunk_size):
def _copy_volume_with_file(src, dest, size_in_m):
src_handle = src
if isinstance(src, six.string_types):
if isinstance(src, str):
src_handle = _open_volume_with_path(src, 'rb')
dest_handle = dest
if isinstance(dest, six.string_types):
if isinstance(dest, str):
dest_handle = _open_volume_with_path(dest, 'wb')
if not src_handle:
@ -571,9 +569,9 @@ def _copy_volume_with_file(src, dest, size_in_m):
duration = max(1, timeutils.delta_seconds(start_time, timeutils.utcnow()))
if isinstance(src, six.string_types):
if isinstance(src, str):
src_handle.close()
if isinstance(dest, six.string_types):
if isinstance(dest, str):
dest_handle.close()
mbps = (size_in_m / duration)
@ -598,8 +596,8 @@ def copy_volume(src, dest, size_in_m, blocksize, sync=False,
instead of file paths and, at present moment, throttling is unavailable.
"""
if (isinstance(src, six.string_types) and
isinstance(dest, six.string_types)):
if (isinstance(src, str) and
isinstance(dest, str)):
if not throttle:
throttle = throttling.Throttle.get_default()
with throttle.subcommand(src, dest) as throttle_cmd:
@ -823,7 +821,7 @@ def check_already_managed_volume(vol_id):
volume id exists, otherwise return False
"""
try:
return (vol_id and isinstance(vol_id, six.string_types) and
return (vol_id and isinstance(vol_id, str) and
uuid.UUID(vol_id, version=4) and
objects.Volume.exists(context.get_admin_context(), vol_id))
except ValueError:
@ -1215,12 +1213,8 @@ def sanitize_host(host):
def sanitize_hostname(hostname):
"""Return a hostname which conforms to RFC-952 and RFC-1123 specs."""
if six.PY3:
hostname = hostname.encode('latin-1', 'ignore')
hostname = hostname.decode('latin-1')
else:
if isinstance(hostname, six.text_type):
hostname = hostname.encode('latin-1', 'ignore')
hostname = hostname.encode('latin-1', 'ignore')
hostname = hostname.decode('latin-1')
hostname = re.sub(r'[ _]', '-', hostname)
hostname = re.sub(r'[^\w.-]+', '', hostname)