Only run json.dump once

Rather than dump twice, once for each file, dump once to a variable.
Then, while we've got it in memory, strip trailing spaces. It's not
required that json output have no trailing spaces, but gerrit shows them
as does vim when opening files to look at them. It's easy to rememdy,
and we're rolling out some file updates in the next patch anyway.

Change-Id: Id3d04655264480b202a58b93a813360003707508
This commit is contained in:
Monty Taylor 2017-08-12 15:39:15 -05:00
parent 4088c19d86
commit 288127acb0
No known key found for this signature in database
GPG Key ID: 7BAE94BC7141A594
1 changed files with 6 additions and 3 deletions

View File

@ -79,10 +79,13 @@ def main():
valid = validate.validate_all(schema, mapping, resolver=resolver)
if valid and '-n' not in sys.argv:
json.dump(mapping, open('service-types.json', 'w'), indent=2)
versioned_file_name = 'service-types.json.{version}'.format(
output = json.dumps(mapping, indent=2)
output.replace(' \n', '\n')
unversioned_filename = 'service-types.json'
versioned_filename = 'service-types.json.{version}'.format(
version=mapping['version'])
json.dump(mapping, open(versioned_file_name, 'w'), indent=2)
for filename in (unversioned_filename, versioned_filename):
open(filename, 'w').write(output)
return int(not valid)