sort_yaml.py 913 B

12345678910111213141516171819202122232425262728293031323334
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. import argparse
  4. import sys
  5. import yaml
  6. def sort_yaml(yaml_file):
  7. data = yaml.safe_load(open(yaml_file, 'r'))
  8. if 'version' in data:
  9. print('This script does not support the new rosdistro yaml files', file=sys.stderr)
  10. sys.exit(1)
  11. sort_yaml_data(data)
  12. with open(yaml_file, 'w') as out_file:
  13. yaml.dump(data, out_file, default_flow_style=False)
  14. def sort_yaml_data(data):
  15. # sort lists
  16. if isinstance(data, list):
  17. data.sort()
  18. # recurse into each value of a dict
  19. elif isinstance(data, dict):
  20. for k in data:
  21. sort_yaml_data(data[k])
  22. if __name__ == "__main__":
  23. parser = argparse.ArgumentParser(description='Sort the .yaml file in place.')
  24. parser.add_argument('yaml_file', help='The .yaml file to update')
  25. args = parser.parse_args()
  26. sort_yaml(args.yaml_file)