Improve compatible with python3

Changes:
* use six.iteritems instead dict.iteritems
* use urllib in CDH API client
* filtering directories in HDP versionhandler because py3 creates
  __pycache__ directory

Unit tests result:

Ran: 750 tests in 13.4569 sec.
 - Passed: 669
 - Skipped: 5
 - Expected Fail: 0
 - Unexpected Success: 0
 - Failed: 76

Change-Id: Ib36b24f04a933c6e134bdace4994a5020ff4166b
This commit is contained in:
Sergey Reshetnyak 2015-05-10 22:48:41 +03:00
parent 1af382f7a4
commit 1f9d4089f9
12 changed files with 42 additions and 36 deletions

View File

@ -21,6 +21,7 @@ from __future__ import print_function
import sys import sys
from oslo_middleware import base from oslo_middleware import base
import six
import webob.dec import webob.dec
@ -47,7 +48,7 @@ class LogExchange(base.Middleware):
resp = req.get_response(self.application) resp = req.get_response(self.application)
print(("*" * 40) + " RESPONSE HEADERS") print(("*" * 40) + " RESPONSE HEADERS")
for (key, value) in resp.headers.iteritems(): for (key, value) in six.iteritems(resp.headers):
print(key, "=", value) print(key, "=", value)
print() print()

View File

@ -87,7 +87,7 @@ class Resource(types.FrozenDict):
def __init__(self, dct): def __init__(self, dct):
super(Resource, self).__setattr__('_initial_dict', dct) super(Resource, self).__setattr__('_initial_dict', dct)
newdct = dict() newdct = dict()
for refname, entity in dct.iteritems(): for refname, entity in six.iteritems(dct):
newdct[refname] = self._wrap_entity(refname, entity) newdct[refname] = self._wrap_entity(refname, entity)
super(Resource, self).__init__(newdct) super(Resource, self).__init__(newdct)
@ -143,7 +143,7 @@ class Resource(types.FrozenDict):
def _to_dict(self, backref): def _to_dict(self, backref):
dct = dict() dct = dict()
for refname, entity in self.iteritems(): for refname, entity in six.iteritems(self):
if refname != backref and refname not in self._filter_fields: if refname != backref and refname not in self._filter_fields:
childs_backref = None childs_backref = None
if refname in self._children: if refname in self._children:

View File

@ -22,15 +22,13 @@
# To satisfy the pep8 and python3 tests, we did some changes to the codes. # To satisfy the pep8 and python3 tests, we did some changes to the codes.
# We also change some importings to use Sahara inherited classes. # We also change some importings to use Sahara inherited classes.
import cookielib
import posixpath import posixpath
import types import types
import urllib
import urllib2
from oslo_log import log as logging from oslo_log import log as logging
from oslo_serialization import jsonutils as json from oslo_serialization import jsonutils as json
import six import six
from six.moves import urllib
from sahara.i18n import _LW from sahara.i18n import _LW
from sahara.plugins.cdh import exceptions as ex from sahara.plugins.cdh import exceptions as ex
@ -53,15 +51,15 @@ class HttpClient(object):
self._headers = {} self._headers = {}
# Make a basic auth handler that does nothing. Set credentials later. # Make a basic auth handler that does nothing. Set credentials later.
self._passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm() self._passmgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
authhandler = urllib2.HTTPBasicAuthHandler(self._passmgr) authhandler = urllib.request.HTTPBasicAuthHandler(self._passmgr)
# Make a cookie processor # Make a cookie processor
cookiejar = cookielib.CookieJar() cookiejar = six.moves.http_cookiejar.CookieJar()
self._opener = urllib2.build_opener( self._opener = urllib.request.build_opener(
urllib2.HTTPErrorProcessor(), urllib.request.HTTPErrorProcessor(),
urllib2.HTTPCookieProcessor(cookiejar), urllib.request.HTTPCookieProcessor(cookiejar),
authhandler) authhandler)
def set_basic_auth(self, username, password, realm): def set_basic_auth(self, username, password, realm):
@ -103,7 +101,7 @@ class HttpClient(object):
:param data: The data to attach to the body of the request. :param data: The data to attach to the body of the request.
:param headers: The headers to set for this request. :param headers: The headers to set for this request.
:return: The result of urllib2.urlopen() :return: The result of urllib.request.urlopen()
""" """
# Prepare URL and params # Prepare URL and params
url = self._make_url(path, params) url = self._make_url(path, params)
@ -115,7 +113,7 @@ class HttpClient(object):
data = None data = None
# Setup the request # Setup the request
request = urllib2.Request(url, data) request = urllib.request.Request(url, data)
# Hack/workaround because urllib2 only does GET and POST # Hack/workaround because urllib2 only does GET and POST
request.get_method = lambda: http_method request.get_method = lambda: http_method
@ -128,7 +126,7 @@ class HttpClient(object):
url=url)) url=url))
try: try:
return self._opener.open(request) return self._opener.open(request)
except urllib2.HTTPError as ex: except urllib.error.HTTPError as ex:
message = six.text_type(ex) message = six.text_type(ex)
try: try:
json_body = json.loads(message) json_body = json.loads(message)
@ -142,7 +140,7 @@ class HttpClient(object):
if path: if path:
res += posixpath.normpath('/' + path.lstrip('/')) res += posixpath.normpath('/' + path.lstrip('/'))
if params: if params:
param_str = urllib.urlencode(params, True) param_str = urllib.parse.urlencode(params, True)
res += '?' + param_str res += '?' + param_str
return iri_to_uri(res) return iri_to_uri(res)
@ -169,14 +167,14 @@ def iri_to_uri(iri):
# sub-delims = "!" / "$" / "&" / "'" / "(" / ")" # sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
# / "*" / "+" / "," / ";" / "=" # / "*" / "+" / "," / ";" / "="
# unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" # unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
# Of the unreserved characters, urllib.quote already considers all but # Of the unreserved characters, urllib.parse.quote already considers all
# the ~ safe. # but the ~ safe.
# The % character is also added to the list of safe characters here, as the # The % character is also added to the list of safe characters here, as the
# end of section 3.1 of RFC 3987 specifically mentions that % must not be # end of section 3.1 of RFC 3987 specifically mentions that % must not be
# converted. # converted.
if iri is None: if iri is None:
return iri return iri
return urllib.quote(smart_str(iri), safe="/#%[]=:;$&()+,!?*@'~") return urllib.parse.quote(smart_str(iri), safe="/#%[]=:;$&()+,!?*@'~")
# #

View File

@ -24,11 +24,11 @@
import posixpath import posixpath
import socket import socket
import urllib2
from oslo_log import log as logging from oslo_log import log as logging
from oslo_serialization import jsonutils as json from oslo_serialization import jsonutils as json
import six import six
from six.moves import urllib
from sahara import context from sahara import context
from sahara.i18n import _ from sahara.i18n import _
@ -111,7 +111,7 @@ class Resource(object):
context.sleep(self.retry_sleep) context.sleep(self.retry_sleep)
try: try:
return self.invoke("GET", relpath, params) return self.invoke("GET", relpath, params)
except (socket.error, urllib2.URLError) as e: except (socket.error, urllib.error.URLError) as e:
if "timed out" in six.text_type(e).lower(): if "timed out" in six.text_type(e).lower():
if retry < self.retries: if retry < self.retries:
LOG.warning(_LW("Timeout issuing GET request for " LOG.warning(_LW("Timeout issuing GET request for "

View File

@ -14,6 +14,7 @@
# limitations under the License. # limitations under the License.
from oslo_serialization import jsonutils as json from oslo_serialization import jsonutils as json
import six
from sahara.plugins.cdh.client import api_client from sahara.plugins.cdh.client import api_client
@ -58,7 +59,7 @@ def process_service(service, service_name):
def parse_config(config): def parse_config(config):
cfg = [] cfg = []
for name, value in config.iteritems(): for name, value in six.iteritems(config):
p = { p = {
'name': value.name, 'name': value.name,
'value': value.default, 'value': value.default,

View File

@ -14,6 +14,7 @@
# limitations under the License. # limitations under the License.
from oslo_serialization import jsonutils as json from oslo_serialization import jsonutils as json
import six
from sahara.plugins.cdh.client import api_client from sahara.plugins.cdh.client import api_client
@ -63,7 +64,7 @@ def process_service(service, service_name):
def parse_config(config): def parse_config(config):
cfg = [] cfg = []
for name, value in config.iteritems(): for name, value in six.iteritems(config):
p = { p = {
'name': value.name, 'name': value.name,
'value': value.default, 'value': value.default,

View File

@ -29,7 +29,8 @@ class VersionHandlerFactory(object):
src_dir = os.path.join(os.path.dirname(__file__), '') src_dir = os.path.join(os.path.dirname(__file__), '')
versions = [name[8:].replace('_', '.') versions = [name[8:].replace('_', '.')
for name in os.listdir(src_dir) for name in os.listdir(src_dir)
if os.path.isdir(os.path.join(src_dir, name))] if os.path.isdir(os.path.join(src_dir, name))
and name.startswith('version_')]
versions.sort(key=general.natural_sort_key) versions.sort(key=general.natural_sort_key)
VersionHandlerFactory.versions = versions VersionHandlerFactory.versions = versions

View File

@ -186,7 +186,7 @@ PRIORITY_1_CONFS += CLUSTER_WIDE_CONFS
def _initialise_configs(): def _initialise_configs():
configs = [] configs = []
for service, config_lists in XML_CONFS.iteritems(): for service, config_lists in six.iteritems(XML_CONFS):
for config_list in config_lists: for config_list in config_lists:
for config in config_list: for config in config_list:
if config['name'] not in HIDDEN_CONFS: if config['name'] not in HIDDEN_CONFS:
@ -206,13 +206,13 @@ def _initialise_configs():
cfg.priority = 1 cfg.priority = 1
configs.append(cfg) configs.append(cfg)
for service, config_items in ENV_CONFS.iteritems(): for service, config_items in six.iteritems(ENV_CONFS):
for name, param_format_str in config_items.iteritems(): for name, param_format_str in six.iteritems(config_items):
configs.append(p.Config(name, service, "node", configs.append(p.Config(name, service, "node",
default_value=1024, priority=1, default_value=1024, priority=1,
config_type="int")) config_type="int"))
for service, config_items in SPARK_CONFS.iteritems(): for service, config_items in six.iteritems(SPARK_CONFS):
for item in config_items['OPTIONS']: for item in config_items['OPTIONS']:
cfg = p.Config(name=item["name"], cfg = p.Config(name=item["name"],
description=item["description"], description=item["description"],

View File

@ -14,6 +14,7 @@
# limitations under the License. # limitations under the License.
from oslo_config import cfg from oslo_config import cfg
import six
from sahara import exceptions as ex from sahara import exceptions as ex
from sahara.i18n import _ from sahara.i18n import _
@ -90,7 +91,7 @@ PRIORITY_1_CONFS += CLUSTER_WIDE_CONFS
def init_xml_configs(xml_confs): def init_xml_configs(xml_confs):
configs = [] configs = []
for service, config_lists in xml_confs.iteritems(): for service, config_lists in six.iteritems(xml_confs):
for config_list in config_lists: for config_list in config_lists:
for config in config_list: for config in config_list:
if config['name'] not in HIDDEN_CONFS: if config['name'] not in HIDDEN_CONFS:
@ -150,8 +151,8 @@ DATANODES_STARTUP_TIMEOUT = p.Config(
def init_env_configs(env_confs): def init_env_configs(env_confs):
configs = [] configs = []
for service, config_items in env_confs.iteritems(): for service, config_items in six.iteritems(env_confs):
for name, value in config_items.iteritems(): for name, value in six.iteritems(config_items):
configs.append(p.Config(name, service, "node", configs.append(p.Config(name, service, "node",
default_value=value, priority=1, default_value=value, priority=1,
config_type="int")) config_type="int"))

View File

@ -15,6 +15,7 @@
from oslo_config import cfg from oslo_config import cfg
from oslo_log import log as logging from oslo_log import log as logging
import six
from sahara import conductor as c from sahara import conductor as c
from sahara import context from sahara import context
@ -135,7 +136,7 @@ PRIORITY_1_CONFS += CLUSTER_WIDE_CONFS
def _initialise_configs(): def _initialise_configs():
configs = [] configs = []
for service, config_lists in XML_CONFS.iteritems(): for service, config_lists in six.iteritems(XML_CONFS):
for config_list in config_lists: for config_list in config_lists:
for config in config_list: for config in config_list:
if config['name'] not in HIDDEN_CONFS: if config['name'] not in HIDDEN_CONFS:
@ -155,8 +156,8 @@ def _initialise_configs():
cfg.priority = 1 cfg.priority = 1
configs.append(cfg) configs.append(cfg)
for service, config_items in ENV_CONFS.iteritems(): for service, config_items in six.iteritems(ENV_CONFS):
for name, param_format_str in config_items.iteritems(): for name, param_format_str in six.iteritems(config_items):
configs.append(p.Config(name, service, "node", configs.append(p.Config(name, service, "node",
default_value=1024, priority=1, default_value=1024, priority=1,
config_type="int")) config_type="int"))

View File

@ -52,7 +52,7 @@ def _get_plugin_configs(plugin_name, hadoop_version, scope=None):
def _check_duplicates(lst, message): def _check_duplicates(lst, message):
invalid = [] invalid = []
lst = collections.Counter(lst) lst = collections.Counter(lst)
for (key, value) in lst.iteritems(): for key, value in six.iteritems(lst):
if value > 1: if value > 1:
invalid.append(key) invalid.append(key)

View File

@ -15,6 +15,8 @@
import inspect import inspect
import six
class BaseResource(object): class BaseResource(object):
__resource_name__ = 'base' __resource_name__ = 'base'
@ -43,7 +45,7 @@ class BaseResource(object):
def to_dict(self): def to_dict(self):
dictionary = self.__dict__.copy() dictionary = self.__dict__.copy()
return {k: v for k, v in dictionary.iteritems() return {k: v for k, v in six.iteritems(dictionary)
if not self._filter_field(k)} if not self._filter_field(k)}
def as_resource(self): def as_resource(self):