releases/openstack_releases/cmds/reformat_yaml.py
Joshua Harlow 9c1ac3dace Add a yamlutils file and a reformatting command
Do note this will lose comments, if pyyaml ever supports
not losing those then this can avoid losing those.

Change-Id: I4b26362f7cab42eb40804751c86d9145490c78d9
2016-05-16 21:45:40 -07:00

33 lines
1.1 KiB
Python

# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import argparse
from openstack_releases import yamlutils
def main():
parser = argparse.ArgumentParser()
parser.add_argument('file', nargs='*', help="Yaml file to reformat")
args = parser.parse_args()
for filename in args.file:
print("Reformatting %s" % filename)
with open(filename, 'rb') as fh:
contents = yamlutils.loads(fh.read())
contents = yamlutils.dumps(contents)
with open(filename, 'wb') as fh:
fh.write(contents)
if not contents.endswith("\n"):
fh.write("\n")