yaml2rosinstall.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. import argparse
  4. import os
  5. import sys
  6. import yaml
  7. def convert_yaml_to_rosinstall(yaml_file, rosinstall_file):
  8. data = yaml.safe_load(open(yaml_file, 'r'))
  9. data = convert_yaml_data_to_rosinstall_data(data)
  10. with open(rosinstall_file, 'w') as out_file:
  11. yaml.dump(data, out_file, default_flow_style=False)
  12. def convert_yaml_data_to_rosinstall_data(data):
  13. rosinstall_data = []
  14. for name in sorted(data['repositories'].keys()):
  15. values = data['repositories'][name]
  16. repo = {}
  17. repo['local-name'] = name
  18. repo['uri'] = values['url']
  19. if 'version' in values:
  20. repo['version'] = values['version']
  21. # fallback type is git for gbp repositories
  22. vcs_type = values['type'] if 'type' in values else 'git'
  23. rosinstall_data.append({vcs_type: repo})
  24. return rosinstall_data
  25. if __name__ == '__main__':
  26. parser = argparse.ArgumentParser(description='Convert a .yaml file into a .rosinstall file.')
  27. parser.add_argument('yaml_file', help='The .yaml file to convert')
  28. parser.add_argument('rosinstall_file', nargs='?', help='The generated .rosinstall file (default: same name as .yaml file except extension)')
  29. args = parser.parse_args()
  30. if args.rosinstall_file is None:
  31. path_without_ext, _ = os.path.splitext(args.yaml_file)
  32. args.rosinstall_file = path_without_ext + '.rosinstall'
  33. try:
  34. convert_yaml_to_rosinstall(args.yaml_file, args.rosinstall_file)
  35. except Exception as e:
  36. print(str(e), file=sys.stderr)
  37. exit(1)