From a3c1159ed49b8a97068fbe526444b154bc94effb Mon Sep 17 00:00:00 2001 From: Steve Kowalik Date: Tue, 2 Sep 2014 15:24:41 +1000 Subject: [PATCH] Add support for Python 3 Make changes pretty much all over the code base with respect to encoding strings and fixing imports to support Python 3. Change-Id: Id1920129001b8e223474c1b2faf8bd9d527fe7e7 --- os_collect_config/cache.py | 4 ++-- os_collect_config/cfn.py | 4 ++-- os_collect_config/collect.py | 4 ++-- os_collect_config/ec2.py | 2 +- os_collect_config/heat.py | 2 +- os_collect_config/heat_local.py | 2 +- os_collect_config/keystone.py | 8 ++++---- os_collect_config/local.py | 4 ++-- os_collect_config/tests/test_cfn.py | 4 ++-- os_collect_config/tests/test_collect.py | 2 +- os_collect_config/tests/test_ec2.py | 2 +- os_collect_config/tests/test_heat_local.py | 2 +- 12 files changed, 20 insertions(+), 20 deletions(-) diff --git a/os_collect_config/cache.py b/os_collect_config/cache.py index 9a287f0..c07e94c 100644 --- a/os_collect_config/cache.py +++ b/os_collect_config/cache.py @@ -49,7 +49,7 @@ def store(name, content): with tempfile.NamedTemporaryFile( dir=cfg.CONF.cachedir, delete=False) as new: - new.write(json.dumps(content, indent=1)) + new.write(json.dumps(content, indent=1).encode('utf-8')) new.flush() if not os.path.exists(orig_path): shutil.copy(new.name, orig_path) @@ -80,6 +80,6 @@ def store_meta_list(name, data_keys): with tempfile.NamedTemporaryFile(prefix='tmp_meta_list.', dir=os.path.dirname(dest), delete=False) as out: - out.write(json.dumps(final_list)) + out.write(json.dumps(final_list).encode('utf-8')) os.rename(out.name, dest) return dest diff --git a/os_collect_config/cfn.py b/os_collect_config/cfn.py index b6e9d86..53bf1b6 100644 --- a/os_collect_config/cfn.py +++ b/os_collect_config/cfn.py @@ -19,11 +19,11 @@ import os from keystoneclient.contrib.ec2 import utils as ec2_utils from lxml import etree from oslo.config import cfg -import urlparse +import six.moves.urllib.parse as urlparse -from openstack.common import log from os_collect_config import common from os_collect_config import exc +from os_collect_config.openstack.common import log CONF = cfg.CONF logger = log.getLogger(__name__) diff --git a/os_collect_config/collect.py b/os_collect_config/collect.py index 6ec773c..4d36e47 100644 --- a/os_collect_config/collect.py +++ b/os_collect_config/collect.py @@ -22,7 +22,6 @@ import subprocess import sys import time -from openstack.common import log from os_collect_config import cache from os_collect_config import cfn from os_collect_config import ec2 @@ -31,6 +30,7 @@ from os_collect_config import heat from os_collect_config import heat_local from os_collect_config import keystone from os_collect_config import local +from os_collect_config.openstack.common import log from os_collect_config import version from oslo.config import cfg @@ -193,7 +193,7 @@ def getfilehash(files): try: with open(filename) as fp: data = fp.read() - m.update(data) + m.update(data.encode('utf-8')) except IOError: pass return m.hexdigest() diff --git a/os_collect_config/ec2.py b/os_collect_config/ec2.py index af5b06c..eeefcc2 100644 --- a/os_collect_config/ec2.py +++ b/os_collect_config/ec2.py @@ -15,9 +15,9 @@ from oslo.config import cfg -from openstack.common import log from os_collect_config import common from os_collect_config import exc +from os_collect_config.openstack.common import log EC2_METADATA_URL = 'http://169.254.169.254/latest/meta-data' CONF = cfg.CONF diff --git a/os_collect_config/heat.py b/os_collect_config/heat.py index 4f3325a..f003104 100644 --- a/os_collect_config/heat.py +++ b/os_collect_config/heat.py @@ -16,9 +16,9 @@ from heatclient import client as heatclient from keystoneclient.v3 import client as keystoneclient from oslo.config import cfg -from openstack.common import log from os_collect_config import exc from os_collect_config import keystone +from os_collect_config.openstack.common import log CONF = cfg.CONF logger = log.getLogger(__name__) diff --git a/os_collect_config/heat_local.py b/os_collect_config/heat_local.py index 95b5790..1cbbf3b 100644 --- a/os_collect_config/heat_local.py +++ b/os_collect_config/heat_local.py @@ -17,8 +17,8 @@ import json import os from oslo.config import cfg -from openstack.common import log from os_collect_config import exc +from os_collect_config.openstack.common import log HEAT_METADATA_PATH = ['/var/lib/heat-cfntools/cfn-init-data'] CONF = cfg.CONF diff --git a/os_collect_config/keystone.py b/os_collect_config/keystone.py index 4dc7811..d79c3c2 100644 --- a/os_collect_config/keystone.py +++ b/os_collect_config/keystone.py @@ -69,10 +69,10 @@ class Keystone(object): def _make_key(self, key): m = hashlib.sha256() - m.update(self.auth_url) - m.update(self.user_id) - m.update(self.project_id) - m.update(key) + m.update(self.auth_url.encode('utf-8')) + m.update(self.user_id.encode('utf-8')) + m.update(self.project_id.encode('utf-8')) + m.update(key.encode('utf-8')) return m.hexdigest() @property diff --git a/os_collect_config/local.py b/os_collect_config/local.py index 1fc8d50..8da1a26 100644 --- a/os_collect_config/local.py +++ b/os_collect_config/local.py @@ -19,8 +19,8 @@ import os from oslo.config import cfg import stat -from openstack.common import log from os_collect_config import exc +from os_collect_config.openstack.common import log LOCAL_DEFAULT_PATHS = ['/var/lib/os-collect-config/local-data'] CONF = cfg.CONF @@ -89,7 +89,7 @@ class Collector(object): # Now sort specifically by C locale def locale_aware_by_first_item(data): return locale.strxfrm(data[0]) - save_locale = locale.getlocale(locale.LC_ALL) + save_locale = locale.getdefaultlocale() locale.setlocale(locale.LC_ALL, 'C') sorted_content = sorted(final_content, key=locale_aware_by_first_item) locale.setlocale(locale.LC_ALL, save_locale) diff --git a/os_collect_config/tests/test_cfn.py b/os_collect_config/tests/test_cfn.py index ed20e6a..7daeb72 100644 --- a/os_collect_config/tests/test_cfn.py +++ b/os_collect_config/tests/test_cfn.py @@ -15,12 +15,12 @@ import json import tempfile -import urlparse import fixtures from lxml import etree from oslo.config import cfg import requests +import six.moves.urllib.parse as urlparse import testtools from testtools import content as test_content from testtools import matchers @@ -196,7 +196,7 @@ class TestCfnBase(testtools.TestCase): self.log = self.useFixture(fixtures.FakeLogger()) self.useFixture(fixtures.NestedTempfile()) self.hint_file = tempfile.NamedTemporaryFile() - self.hint_file.write('http://127.0.0.1:8000') + self.hint_file.write(u'http://127.0.0.1:8000'.encode('utf-8')) self.hint_file.flush() self.addCleanup(self.hint_file.close) collect.setup_conf() diff --git a/os_collect_config/tests/test_collect.py b/os_collect_config/tests/test_collect.py index 1b76af9..d1c6625 100644 --- a/os_collect_config/tests/test_collect.py +++ b/os_collect_config/tests/test_collect.py @@ -38,7 +38,7 @@ from os_collect_config.tests import test_heat_local def _setup_local_metadata(test_case): test_case.useFixture(fixtures.NestedTempfile()) local_md = tempfile.NamedTemporaryFile(delete=False) - local_md.write(json.dumps(test_heat_local.META_DATA)) + local_md.write(json.dumps(test_heat_local.META_DATA).encode('utf-8')) local_md.flush() return local_md.name diff --git a/os_collect_config/tests/test_ec2.py b/os_collect_config/tests/test_ec2.py index 38ff201..db76477 100644 --- a/os_collect_config/tests/test_ec2.py +++ b/os_collect_config/tests/test_ec2.py @@ -15,9 +15,9 @@ import fixtures import requests +import six.moves.urllib.parse as urlparse import testtools from testtools import matchers -import urlparse import uuid from os_collect_config import collect diff --git a/os_collect_config/tests/test_heat_local.py b/os_collect_config/tests/test_heat_local.py index 83f496a..2347681 100644 --- a/os_collect_config/tests/test_heat_local.py +++ b/os_collect_config/tests/test_heat_local.py @@ -54,7 +54,7 @@ class TestHeatLocal(testtools.TestCase): def test_collect_heat_local(self): with tempfile.NamedTemporaryFile() as md: - md.write(json.dumps(META_DATA)) + md.write(json.dumps(META_DATA).encode('utf-8')) md.flush() local_md = self._call_collect(md.name)