Support for Python 3

Fix up various things to support Python 3. This also included removing
the pretty lines in README.rst, because tox with Python 3 just could
not cope.

Change-Id: I8e8a40ebc54ab781ccf0961a88fd1f8526aaeba0
This commit is contained in:
Steve Kowalik 2014-08-21 16:42:48 +10:00
parent 3685add131
commit 117e7c0f77
5 changed files with 18 additions and 13 deletions

View File

@ -64,11 +64,11 @@ example::
~/my_templates$ tree ~/my_templates$ tree
. .
└── etc +-- etc
├── keystone +-- keystone
│ └── keystone.conf | +-- keystone.conf
└── mysql +-- mysql
└── mysql.conf +-- mysql.conf
An example tree can be found `here <http://git.openstack.org/cgit/openstack/tripleo-image-elements/tree/elements/keystone/os-apply-config>`_. An example tree can be found `here <http://git.openstack.org/cgit/openstack/tripleo-image-elements/tree/elements/keystone/os-apply-config>`_.

View File

@ -103,6 +103,8 @@ def write_file(path, contents):
d = os.path.dirname(path) d = os.path.dirname(path)
os.path.exists(d) or os.makedirs(d) os.path.exists(d) or os.makedirs(d)
with tempfile.NamedTemporaryFile(dir=d, delete=False) as newfile: with tempfile.NamedTemporaryFile(dir=d, delete=False) as newfile:
if type(contents) == str:
contents = contents.encode('utf-8')
newfile.write(contents) newfile.write(contents)
os.chmod(newfile.name, mode) os.chmod(newfile.name, mode)
os.chown(newfile.name, uid, gid) os.chown(newfile.name, uid, gid)
@ -148,13 +150,13 @@ def render_executable(path, config):
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
stdout, stderr = p.communicate(json.dumps(config)) stdout, stderr = p.communicate(json.dumps(config).encode('utf-8'))
p.wait() p.wait()
if p.returncode != 0: if p.returncode != 0:
raise exc.ConfigException( raise exc.ConfigException(
"config script failed: %s\n\nwith output:\n\n%s" % "config script failed: %s\n\nwith output:\n\n%s" %
(path, stdout + stderr)) (path, stdout + stderr))
return stdout return stdout.decode('utf-8')
def template_paths(root): def template_paths(root):

View File

@ -1,7 +1,8 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import json import json
import sys import sys
params = json.loads(sys.stdin.read()) params = json.loads(sys.stdin.read())
x = params["x"] x = params["x"]
if x is None: raise Exception("undefined: x") if x is None: raise Exception("undefined: x")
print x print(x)

View File

@ -132,9 +132,10 @@ class TestRunOSConfigApplier(testtools.TestCase):
def test_os_config_files(self): def test_os_config_files(self):
with tempfile.NamedTemporaryFile() as fake_os_config_files: with tempfile.NamedTemporaryFile() as fake_os_config_files:
with tempfile.NamedTemporaryFile() as fake_config: with tempfile.NamedTemporaryFile() as fake_config:
fake_config.write(json.dumps(CONFIG)) fake_config.write(json.dumps(CONFIG).encode('utf-8'))
fake_config.flush() fake_config.flush()
fake_os_config_files.write(json.dumps([fake_config.name])) fake_os_config_files.write(
json.dumps([fake_config.name]).encode('utf-8'))
fake_os_config_files.flush() fake_os_config_files.flush()
apply_config.main(['os-apply-config', apply_config.main(['os-apply-config',
'--key', 'database.url', '--key', 'database.url',
@ -242,7 +243,7 @@ class OSConfigApplierTestCase(testtools.TestCase):
template("/etc/glance/script.conf"), {}) template("/etc/glance/script.conf"), {})
def test_template_paths(self): def test_template_paths(self):
expected = map(lambda p: (template(p), p), TEMPLATE_PATHS) expected = list(map(lambda p: (template(p), p), TEMPLATE_PATHS))
actual = apply_config.template_paths(TEMPLATES) actual = apply_config.template_paths(TEMPLATES)
expected.sort(key=lambda tup: tup[1]) expected.sort(key=lambda tup: tup[1])
actual.sort(key=lambda tup: tup[1]) actual.sort(key=lambda tup: tup[1])

View File

@ -15,7 +15,7 @@
import re import re
from config_exception import ConfigException from os_apply_config import config_exception
TYPES = { TYPES = {
"int": "^[0-9]+$", "int": "^[0-9]+$",
@ -38,6 +38,7 @@ def ensure_type(string_value, type_name='default'):
raise ValueError( raise ValueError(
"requested validation of unknown type: %s" % type_name) "requested validation of unknown type: %s" % type_name)
if not re.match(TYPES[type_name], string_value): if not re.match(TYPES[type_name], string_value):
raise ConfigException("cannot interpret value '%s' as type %s" % ( exception = config_exception.ConfigException
raise exception("cannot interpret value '%s' as type %s" % (
string_value, type_name)) string_value, type_name))
return string_value return string_value