Merge "Use six.moves to fix imports on Python 3"

This commit is contained in:
Jenkins 2015-05-28 20:28:32 +00:00 committed by Gerrit Code Review
commit f89e68a615
13 changed files with 84 additions and 87 deletions

View File

@ -13,10 +13,9 @@
# License for the specific language governing permissions and limitations
# under the License.
import httplib
from oslo_config import cfg
from oslo_serialization import jsonutils
from six.moves import http_client
import webob.dec
from glance.common import wsgi
@ -71,7 +70,7 @@ class Controller(object):
])
response = webob.Response(request=req,
status=httplib.MULTIPLE_CHOICES,
status=http_client.MULTIPLE_CHOICES,
content_type='application/json')
response.body = jsonutils.dumps(dict(versions=version_objs))
return response

View File

@ -18,13 +18,13 @@
from __future__ import print_function
import httplib
import os
import sys
from oslo_config import cfg
from oslo_log import log as logging
from oslo_serialization import jsonutils
from six.moves import http_client
import six.moves.urllib.parse as urlparse
from webob import exc
@ -115,7 +115,7 @@ class ImageService(object):
def __init__(self, conn, auth_token):
"""Initialize the ImageService.
conn: a httplib.HTTPConnection to the glance server
conn: a http_client.HTTPConnection to the glance server
auth_token: authentication token to pass in the x-auth-token header
"""
self.auth_token = auth_token
@ -131,7 +131,7 @@ class ImageService(object):
body: body to send with the request
ignore_result_body: the body of the result will be ignored
Returns: a httplib response object
Returns: a http_client response object
"""
if self.auth_token:
headers.setdefault('x-auth-token', self.auth_token)
@ -148,7 +148,7 @@ class ImageService(object):
response = self.conn.getresponse()
headers = self._header_list_to_dict(response.getheaders())
code = response.status
code_description = httplib.responses[code]
code_description = http_client.responses[code]
LOG.debug('Response: %(code)s %(status)s %(headers)s'
% {'code': code,
'status': code_description,
@ -176,7 +176,7 @@ class ImageService(object):
if ignore_result_body:
# NOTE: because we are pipelining requests through a single HTTP
# connection, httplib requires that we read the response body
# connection, http_client requires that we read the response body
# before we can make another request. If the caller knows they
# don't care about the body, they can ask us to do that for them.
response.read()
@ -209,7 +209,7 @@ class ImageService(object):
image_uuid: the id of an image
Returns: a httplib Response object where the body is the image.
Returns: a http_client Response object where the body is the image.
"""
url = '/v1/images/%s' % image_uuid
return self._http_request('GET', url, {}, '')
@ -332,7 +332,7 @@ def replication_size(options, args):
count = 0
imageservice = get_image_service()
client = imageservice(httplib.HTTPConnection(server, port),
client = imageservice(http_client.HTTPConnection(server, port),
options.slavetoken)
for image in client.get_images():
LOG.debug('Considering image: %(image)s' % {'image': image})
@ -362,7 +362,7 @@ def replication_dump(options, args):
server, port = utils.parse_valid_host_port(args.pop())
imageservice = get_image_service()
client = imageservice(httplib.HTTPConnection(server, port),
client = imageservice(http_client.HTTPConnection(server, port),
options.mastertoken)
for image in client.get_images():
LOG.debug('Considering: %s' % image['id'])
@ -434,7 +434,7 @@ def replication_load(options, args):
server, port = utils.parse_valid_host_port(args.pop())
imageservice = get_image_service()
client = imageservice(httplib.HTTPConnection(server, port),
client = imageservice(http_client.HTTPConnection(server, port),
options.slavetoken)
updated = []
@ -509,11 +509,11 @@ def replication_livecopy(options, args):
imageservice = get_image_service()
slave_server, slave_port = utils.parse_valid_host_port(args.pop())
slave_conn = httplib.HTTPConnection(slave_server, slave_port)
slave_conn = http_client.HTTPConnection(slave_server, slave_port)
slave_client = imageservice(slave_conn, options.slavetoken)
master_server, master_port = utils.parse_valid_host_port(args.pop())
master_conn = httplib.HTTPConnection(master_server, master_port)
master_conn = http_client.HTTPConnection(master_server, master_port)
master_client = imageservice(master_conn, options.mastertoken)
updated = []
@ -580,11 +580,11 @@ def replication_compare(options, args):
imageservice = get_image_service()
slave_server, slave_port = utils.parse_valid_host_port(args.pop())
slave_conn = httplib.HTTPConnection(slave_server, slave_port)
slave_conn = http_client.HTTPConnection(slave_server, slave_port)
slave_client = imageservice(slave_conn, options.slavetoken)
master_server, master_port = utils.parse_valid_host_port(args.pop())
master_conn = httplib.HTTPConnection(master_server, master_port)
master_conn = http_client.HTTPConnection(master_server, master_port)
master_client = imageservice(master_conn, options.mastertoken)
differences = {}

View File

@ -21,7 +21,6 @@ import collections
import copy
import errno
import functools
import httplib
import os
import re
@ -43,6 +42,7 @@ except ImportError:
from oslo_log import log as logging
from oslo_utils import encodeutils
import six
from six.moves import http_client
# NOTE(jokke): simplified transition to py3, behaves like py2 xrange
from six.moves import range
import six.moves.urllib.parse as urlparse
@ -94,7 +94,7 @@ def handle_redirects(func):
return wrapped
class HTTPSClientAuthConnection(httplib.HTTPSConnection):
class HTTPSClientAuthConnection(http_client.HTTPSConnection):
"""
Class to make a HTTPS connection, with support for
full client-based SSL Authentication
@ -105,8 +105,9 @@ class HTTPSClientAuthConnection(httplib.HTTPSConnection):
def __init__(self, host, port, key_file, cert_file,
ca_file, timeout=None, insecure=False):
httplib.HTTPSConnection.__init__(self, host, port, key_file=key_file,
cert_file=cert_file)
http_client.HTTPSConnection.__init__(self, host, port,
key_file=key_file,
cert_file=cert_file)
self.key_file = key_file
self.cert_file = cert_file
self.ca_file = ca_file
@ -151,18 +152,18 @@ class BaseClient(object):
'/etc/ssl/cert.pem')
OK_RESPONSE_CODES = (
httplib.OK,
httplib.CREATED,
httplib.ACCEPTED,
httplib.NO_CONTENT,
http_client.OK,
http_client.CREATED,
http_client.ACCEPTED,
http_client.NO_CONTENT,
)
REDIRECT_RESPONSE_CODES = (
httplib.MOVED_PERMANENTLY,
httplib.FOUND,
httplib.SEE_OTHER,
httplib.USE_PROXY,
httplib.TEMPORARY_REDIRECT,
http_client.MOVED_PERMANENTLY,
http_client.FOUND,
http_client.SEE_OTHER,
http_client.USE_PROXY,
http_client.TEMPORARY_REDIRECT,
)
def __init__(self, host, port=None, timeout=None, use_ssl=False,
@ -332,7 +333,7 @@ class BaseClient(object):
if self.use_ssl:
return HTTPSClientAuthConnection
else:
return httplib.HTTPConnection
return http_client.HTTPConnection
def _authenticate(self, force_reauth=False):
"""
@ -514,24 +515,24 @@ class BaseClient(object):
return res
elif status_code in self.REDIRECT_RESPONSE_CODES:
raise exception.RedirectException(res.getheader('Location'))
elif status_code == httplib.UNAUTHORIZED:
elif status_code == http_client.UNAUTHORIZED:
raise exception.NotAuthenticated(res.read())
elif status_code == httplib.FORBIDDEN:
elif status_code == http_client.FORBIDDEN:
raise exception.Forbidden(res.read())
elif status_code == httplib.NOT_FOUND:
elif status_code == http_client.NOT_FOUND:
raise exception.NotFound(res.read())
elif status_code == httplib.CONFLICT:
elif status_code == http_client.CONFLICT:
raise exception.Duplicate(res.read())
elif status_code == httplib.BAD_REQUEST:
elif status_code == http_client.BAD_REQUEST:
raise exception.Invalid(res.read())
elif status_code == httplib.MULTIPLE_CHOICES:
elif status_code == http_client.MULTIPLE_CHOICES:
raise exception.MultipleChoices(body=res.read())
elif status_code == httplib.REQUEST_ENTITY_TOO_LARGE:
elif status_code == http_client.REQUEST_ENTITY_TOO_LARGE:
raise exception.LimitExceeded(retry=_retry(res),
body=res.read())
elif status_code == httplib.INTERNAL_SERVER_ERROR:
elif status_code == http_client.INTERNAL_SERVER_ERROR:
raise exception.ServerError()
elif status_code == httplib.SERVICE_UNAVAILABLE:
elif status_code == http_client.SERVICE_UNAVAILABLE:
raise exception.ServiceUnavailable(retry=_retry(res))
else:
raise exception.UnexpectedStatus(status=status_code,

View File

@ -13,12 +13,12 @@
# under the License.
from collections import OrderedDict
import ConfigParser
import re
from oslo_config import cfg
from oslo_log import log as logging
from oslo_policy import policy
from six.moves import configparser
import glance.api.policy
from glance.common import exception
@ -26,7 +26,7 @@ from glance import i18n
# NOTE(bourke): The default dict_type is collections.OrderedDict in py27, but
# we must set manually for compatibility with py26
CONFIG = ConfigParser.SafeConfigParser(dict_type=OrderedDict)
CONFIG = configparser.SafeConfigParser(dict_type=OrderedDict)
LOG = logging.getLogger(__name__)
_ = i18n._
_LE = i18n._LE

View File

@ -22,9 +22,8 @@ __all__ = [
]
import urllib2
from oslo_log import log as logging
from six.moves import urllib
from glance.common import exception
from glance import i18n
@ -108,7 +107,7 @@ def validate_location_uri(location):
msg = _("The given uri is not valid. Please specify a "
"valid uri from the following list of supported uri "
"%(supported)s") % {'supported': supported}
raise urllib2.URLError(msg)
raise urllib.error.URLError(msg)
def get_image_data_iter(uri):
@ -134,4 +133,4 @@ def get_image_data_iter(uri):
# into memory. Some images may be quite heavy.
return open(uri, "r")
return urllib2.urlopen(uri)
return urllib.request.urlopen(uri)

View File

@ -13,10 +13,9 @@
# under the License.
from collections import OrderedDict
import ConfigParser
from oslo_config import cfg
from oslo_log import log as logging
from six.moves import configparser
from glance.common import exception
from glance import i18n
@ -45,7 +44,7 @@ swift_opts = [
# NOTE(bourke): The default dict_type is collections.OrderedDict in py27, but
# we must set manually for compatibility with py26
CONFIG = ConfigParser.SafeConfigParser(dict_type=OrderedDict)
CONFIG = configparser.SafeConfigParser(dict_type=OrderedDict)
LOG = logging.getLogger(__name__)
@ -94,7 +93,7 @@ class SwiftParams(object):
reference['user'] = CONFIG.get(ref, 'user')
reference['key'] = CONFIG.get(ref, 'key')
account_params[ref] = reference
except (ValueError, SyntaxError, ConfigParser.NoOptionError) as e:
except (ValueError, SyntaxError, configparser.NoOptionError) as e:
LOG.exception(_LE("Invalid format of swift store config "
"cfg"))
return account_params

View File

@ -20,10 +20,10 @@ Utility methods to set testcases up for Swift and/or S3 tests.
from __future__ import print_function
import BaseHTTPServer
import threading
from oslo_utils import units
from six.moves import BaseHTTPServer
FIVE_KB = 5 * units.Ki

View File

@ -15,11 +15,11 @@
import mock
import os
import urllib2
import glance_store
from oslo_config import cfg
from six.moves import cStringIO
from six.moves import urllib
from taskflow import task
import glance.async.flows.base_import as import_flow
@ -211,7 +211,7 @@ class TestImportTask(test_utils.BaseTestCase):
self.img_repo.get.return_value = self.image
img_factory.new_image.side_effect = create_image
with mock.patch.object(urllib2, 'urlopen') as umock:
with mock.patch.object(urllib.request, 'urlopen') as umock:
content = "TEST_IMAGE"
umock.return_value = cStringIO(content)

View File

@ -12,9 +12,9 @@
# 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 urllib2
import mock
from six.moves import urllib
from glance.common.scripts.image_import import main as image_import_script
import glance.tests.utils as test_utils
@ -89,5 +89,5 @@ class TestImageImport(test_utils.BaseTestCase):
def test_set_image_data_http_error(self):
uri = 'blahhttp://www.example.com'
image = mock.Mock()
self.assertRaises(urllib2.URLError,
self.assertRaises(urllib.error.URLError,
image_import_script.set_image_data, image, uri, None)

View File

@ -12,9 +12,9 @@
# 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 urllib2
import mock
from six.moves import urllib
from glance.common import exception
from glance.common.scripts import utils as script_utils
@ -96,61 +96,61 @@ class TestScriptsUtils(test_utils.BaseTestCase):
def test_validate_location_unsupported_error(self):
location = 'swift'
self.assertRaises(urllib2.URLError,
self.assertRaises(urllib.error.URLError,
script_utils.validate_location_uri, location)
location = 'swift+http'
self.assertRaises(urllib2.URLError,
self.assertRaises(urllib.error.URLError,
script_utils.validate_location_uri, location)
location = 'swift+https'
self.assertRaises(urllib2.URLError,
self.assertRaises(urllib.error.URLError,
script_utils.validate_location_uri, location)
location = 'swift+config'
self.assertRaises(urllib2.URLError,
self.assertRaises(urllib.error.URLError,
script_utils.validate_location_uri, location)
location = 'vsphere'
self.assertRaises(urllib2.URLError,
self.assertRaises(urllib.error.URLError,
script_utils.validate_location_uri, location)
location = 'sheepdog://'
self.assertRaises(urllib2.URLError,
self.assertRaises(urllib.error.URLError,
script_utils.validate_location_uri, location)
location = 's3+https://'
self.assertRaises(urllib2.URLError,
self.assertRaises(urllib.error.URLError,
script_utils.validate_location_uri, location)
location = 'rbd://'
self.assertRaises(urllib2.URLError,
self.assertRaises(urllib.error.URLError,
script_utils.validate_location_uri, location)
location = 'gridfs://'
self.assertRaises(urllib2.URLError,
self.assertRaises(urllib.error.URLError,
script_utils.validate_location_uri, location)
location = 'cinder://'
self.assertRaises(urllib2.URLError,
self.assertRaises(urllib.error.URLError,
script_utils.validate_location_uri, location)
def test_get_image_data_http(self):
uri = "http://example.com"
response = urllib2.urlopen(uri)
response = urllib.request.urlopen(uri)
expected = response.read()
self.assertEqual(expected,
script_utils.get_image_data_iter(uri).read())
def test_get_image_data_https(self):
uri = "https://example.com"
response = urllib2.urlopen(uri)
response = urllib.request.urlopen(uri)
expected = response.read()
self.assertEqual(expected,
script_utils.get_image_data_iter(uri).read())
def test_get_image_data_http_error(self):
uri = "http:/example.com"
self.assertRaises(urllib2.URLError,
self.assertRaises(urllib.error.URLError,
script_utils.get_image_data_iter,
uri)

View File

@ -13,9 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import httplib
from mox3 import mox
from six.moves import http_client
import testtools
from glance.common import auth
@ -28,8 +27,8 @@ class TestClient(testtools.TestCase):
def setUp(self):
super(TestClient, self).setUp()
self.mock = mox.Mox()
self.mock.StubOutWithMock(httplib.HTTPConnection, 'request')
self.mock.StubOutWithMock(httplib.HTTPConnection, 'getresponse')
self.mock.StubOutWithMock(http_client.HTTPConnection, 'request')
self.mock.StubOutWithMock(http_client.HTTPConnection, 'getresponse')
self.endpoint = 'example.com'
self.client = client.BaseClient(self.endpoint, port=9191,
@ -55,16 +54,16 @@ class TestClient(testtools.TestCase):
self.mock.VerifyAll()
def test_http_encoding_headers(self):
httplib.HTTPConnection.request(
http_client.HTTPConnection.request(
mox.IgnoreArg(),
mox.IgnoreArg(),
mox.IgnoreArg(),
mox.IgnoreArg())
# Lets fake the response
# returned by httplib
# returned by http_client
fake = utils.FakeHTTPResponse(data="Ok")
httplib.HTTPConnection.getresponse().AndReturn(fake)
http_client.HTTPConnection.getresponse().AndReturn(fake)
self.mock.ReplayAll()
headers = {"test": u'ni\xf1o'}
@ -73,16 +72,16 @@ class TestClient(testtools.TestCase):
self.assertEqual(fake, resp)
def test_http_encoding_params(self):
httplib.HTTPConnection.request(
http_client.HTTPConnection.request(
mox.IgnoreArg(),
mox.IgnoreArg(),
mox.IgnoreArg(),
mox.IgnoreArg())
# Lets fake the response
# returned by httplib
# returned by http_client
fake = utils.FakeHTTPResponse(data="Ok")
httplib.HTTPConnection.getresponse().AndReturn(fake)
http_client.HTTPConnection.getresponse().AndReturn(fake)
self.mock.ReplayAll()
params = {"test": u'ni\xf1o'}

View File

@ -15,12 +15,12 @@
import copy
import os
import sys
import UserDict
import uuid
import fixtures
from oslo_serialization import jsonutils
import six
from six import moves
import webob
from glance.cmd import replicator as glance_replicator
@ -302,7 +302,7 @@ def get_image_service():
def check_no_args(command, args):
options = UserDict.UserDict()
options = moves.UserDict()
no_args_error = False
orig_img_service = glance_replicator.get_image_service
@ -318,7 +318,7 @@ def check_no_args(command, args):
def check_bad_args(command, args):
options = UserDict.UserDict()
options = moves.UserDict()
bad_args_error = False
orig_img_service = glance_replicator.get_image_service
@ -335,7 +335,7 @@ def check_bad_args(command, args):
class ReplicationCommandsTestCase(test_utils.BaseTestCase):
def test_replication_size(self):
options = UserDict.UserDict()
options = moves.UserDict()
options.slavetoken = 'slavetoken'
args = ['localhost:9292']
@ -367,7 +367,7 @@ class ReplicationCommandsTestCase(test_utils.BaseTestCase):
def test_replication_dump(self):
tempdir = self.useFixture(fixtures.TempDir()).path
options = UserDict.UserDict()
options = moves.UserDict()
options.chunksize = 4096
options.mastertoken = 'mastertoken'
options.metaonly = False
@ -454,7 +454,7 @@ class ReplicationCommandsTestCase(test_utils.BaseTestCase):
f.write(jsonutils.dumps([1, 2, 3, 4, 5]))
# Finally, we're ready to test
options = UserDict.UserDict()
options = moves.UserDict()
options.dontreplicate = 'dontrepl dontreplabsent'
options.slavetoken = 'slavetoken'
args = ['localhost:9292', tempdir]
@ -482,7 +482,7 @@ class ReplicationCommandsTestCase(test_utils.BaseTestCase):
self.assertTrue(check_bad_args(command, args))
def test_replication_livecopy(self):
options = UserDict.UserDict()
options = moves.UserDict()
options.chunksize = 4096
options.dontreplicate = 'dontrepl dontreplabsent'
options.mastertoken = 'livemastertoken'
@ -510,7 +510,7 @@ class ReplicationCommandsTestCase(test_utils.BaseTestCase):
self.assertTrue(check_bad_args(command, args))
def test_replication_compare(self):
options = UserDict.UserDict()
options = moves.UserDict()
options.chunksize = 4096
options.dontreplicate = 'dontrepl dontreplabsent'
options.mastertoken = 'livemastertoken'

View File

@ -15,7 +15,6 @@
"""Common utilities used in testing"""
import BaseHTTPServer
import errno
import functools
import os
@ -31,6 +30,7 @@ from oslo_serialization import jsonutils
from oslo_utils import timeutils
from oslotest import moxstubout
import six
from six.moves import BaseHTTPServer
import testtools
import webob