From 9ce538b6ffc1d49c4ea1eaa6220364262ea8ff1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Andr=C3=A9?= Date: Fri, 13 Jan 2017 11:30:27 +0100 Subject: [PATCH] Prevent trailing whitespace in rendered JSON On python <3.4 the separator for json.dump() defaulted to (', ', ': ') which may cause trailing whitespaces in rendered JSON files when used with indent. This change sets the separator to (',', ': ') to prevent trailing whitespaces with older versions of python. See https://docs.python.org/2/library/json.html#basic-usage Change-Id: I81fabd54659bdb28da43fb4900f19dd94a70336c --- heat-config-chef/install.d/hook-chef.py | 2 +- .../os-refresh-config/configure.d/50-heat-config-docker-cmd | 2 +- heat-config-hiera/install.d/hook-hiera.py | 3 ++- heat-config-json-file/install.d/hook-json-file.py | 3 ++- .../os-refresh-config/configure.d/50-heat-config-kubelet | 2 +- heat-config/os-refresh-config/configure.d/55-heat-config | 4 ++-- tests/test_hook_chef.py | 6 ++++-- 7 files changed, 13 insertions(+), 9 deletions(-) diff --git a/heat-config-chef/install.d/hook-chef.py b/heat-config-chef/install.d/hook-chef.py index 3a4d810..61743a8 100755 --- a/heat-config-chef/install.d/hook-chef.py +++ b/heat-config-chef/install.d/hook-chef.py @@ -128,7 +128,7 @@ def main(argv=sys.argv): node_file = os.path.join(node_path, "%s.json" % fqdn) with os.fdopen(os.open(node_file, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f: - f.write(json.dumps(node_config, indent=4)) + f.write(json.dumps(node_config, indent=4, separators=(',', ': '))) client_config += "\nnode_path '%s'" % node_path # write out the completed client config diff --git a/heat-config-docker-cmd/os-refresh-config/configure.d/50-heat-config-docker-cmd b/heat-config-docker-cmd/os-refresh-config/configure.d/50-heat-config-docker-cmd index 6db4b27..809e8cb 100755 --- a/heat-config-docker-cmd/os-refresh-config/configure.d/50-heat-config-docker-cmd +++ b/heat-config-docker-cmd/os-refresh-config/configure.d/50-heat-config-docker-cmd @@ -138,7 +138,7 @@ def write_project(c): with os.fdopen(os.open( proj_file, os.O_CREAT | os.O_WRONLY | os.O_TRUNC, 0o600), 'w') as f: - json.dump(proj_data, f, indent=2) + json.dump(proj_data, f, indent=2, separators=(',', ': ')) if __name__ == '__main__': diff --git a/heat-config-hiera/install.d/hook-hiera.py b/heat-config-hiera/install.d/hook-hiera.py index d8b9059..077559b 100755 --- a/heat-config-hiera/install.d/hook-hiera.py +++ b/heat-config-hiera/install.d/hook-hiera.py @@ -69,7 +69,8 @@ def main(argv=sys.argv): os.O_CREAT | os.O_TRUNC | os.O_WRONLY, 0o600), 'w') as hiera_data_file: - json.dump(data, hiera_data_file, indent=4, sort_keys=True) + json.dump(data, hiera_data_file, indent=4, sort_keys=True, + separators=(',', ': ')) response = { 'deploy_stdout': '', diff --git a/heat-config-json-file/install.d/hook-json-file.py b/heat-config-json-file/install.d/hook-json-file.py index 57c9a79..84c13f8 100755 --- a/heat-config-json-file/install.d/hook-json-file.py +++ b/heat-config-json-file/install.d/hook-json-file.py @@ -38,7 +38,8 @@ def main(argv=sys.argv): prepare_dir(os.path.dirname(fname)) data = c.get(fname) with open(fname, 'w') as json_data_file: - json.dump(data, json_data_file, indent=4, sort_keys=True) + json.dump(data, json_data_file, indent=4, sort_keys=True, + separators=(',', ': ')) response = { 'deploy_stdout': '', diff --git a/heat-config-kubelet/os-refresh-config/configure.d/50-heat-config-kubelet b/heat-config-kubelet/os-refresh-config/configure.d/50-heat-config-kubelet index 701a18d..ed9bfdf 100755 --- a/heat-config-kubelet/os-refresh-config/configure.d/50-heat-config-kubelet +++ b/heat-config-kubelet/os-refresh-config/configure.d/50-heat-config-kubelet @@ -66,7 +66,7 @@ def write_manifest(c): fn = os.path.join(MANIFESTS_DIR, '%s.json' % c['id']) with os.fdopen(os.open(fn, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f: - json.dump(c['config'], f, indent=2) + json.dump(c['config'], f, indent=2, separators=(',', ': ')) if __name__ == '__main__': sys.exit(main(sys.argv)) diff --git a/heat-config/os-refresh-config/configure.d/55-heat-config b/heat-config/os-refresh-config/configure.d/55-heat-config index 9cc0df1..86c3bf8 100755 --- a/heat-config/os-refresh-config/configure.d/55-heat-config +++ b/heat-config/os-refresh-config/configure.d/55-heat-config @@ -138,7 +138,7 @@ def invoke_hook(c, log): # subsequent hook success with os.fdopen(os.open( deployed_path, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f: - json.dump(c, f, indent=2) + json.dump(c, f, indent=2, separators=(',', ': ')) log.debug('Running %s < %s' % (hook_path, deployed_path)) subproc = subprocess.Popen([hook_path], @@ -170,7 +170,7 @@ def invoke_hook(c, log): # write out notify data for debugging with os.fdopen(os.open( signal_data_path, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f: - json.dump(signal_data, f, indent=2) + json.dump(signal_data, f, indent=2, separators=(',', ': ')) log.debug('Running %s %s < %s' % ( HEAT_CONFIG_NOTIFY, deployed_path, signal_data_path)) diff --git a/tests/test_hook_chef.py b/tests/test_hook_chef.py index b0a4865..023c737 100644 --- a/tests/test_hook_chef.py +++ b/tests/test_hook_chef.py @@ -91,7 +91,8 @@ class HookChefTest(common.RunScriptTest): 'fooval': {u'bar': u'baz'}, 'run_list': [u'recipe[apache]'] } - exp_node = json.dumps(exp_node, indent=4) + exp_node = json.dumps(exp_node, indent=4, + separators=(',', ': ')) exp_cfg = ("log_level :debug\n" "log_location STDOUT\n" "local_mode true\n" @@ -188,7 +189,8 @@ class HookChefTest(common.RunScriptTest): 'fooval': {u'bar': u'baz'}, 'run_list': [u'recipe[apache]'] } - exp_node = json.dumps(exp_node, indent=4) + exp_node = json.dumps(exp_node, indent=4, + separators=(',', ': ')) exp_cfg = ("log_level :debug\n" "log_location STDOUT\n" "local_mode true\n"