add_devel_repo.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. import argparse
  4. import sys
  5. import yaml
  6. from sort_yaml import sort_yaml_data
  7. def add_devel_repository(yaml_file, name, vcs_type, url, version=None):
  8. data = yaml.safe_load(open(yaml_file, 'r'))
  9. if data['type'] == 'gbp':
  10. add_devel_repository_fuerte(yaml_file, data, name, vcs_type, url, version)
  11. return
  12. if data['type'] != 'source':
  13. raise RuntimeError('The passed .yaml file is neither of type "source" nor "gbp"')
  14. if name in data['repositories']:
  15. raise RuntimeError('Repository with name "%s" is already in the .yaml file' % name)
  16. data['repositories'][name] = {
  17. 'type': vcs_type,
  18. 'url': url,
  19. 'version': version,
  20. }
  21. try:
  22. from rosdistro.verify import _to_yaml, _yaml_header_lines
  23. except ImportError as e:
  24. raise ImportError(str(e) + ' - you need to install the latest version of python-rosdistro.')
  25. data = _to_yaml(data)
  26. data = '\n'.join(_yaml_header_lines('source')) + '\n' + data
  27. with open(yaml_file, 'w') as f:
  28. f.write(data)
  29. def add_devel_repository_fuerte(yaml_file, data, name, vcs_type, url, version):
  30. if data['type'] != 'devel':
  31. raise RuntimeError('The passed .yaml file is not of type "devel"')
  32. if name in data['repositories']:
  33. raise RuntimeError('Repository with name "%s" is already in the .yaml file' % name)
  34. values = {
  35. 'type': vcs_type,
  36. 'url': url,
  37. }
  38. if version is None and vcs_type != 'svn':
  39. raise RuntimeError('All repository types except SVN require a version attribute')
  40. if version is not None:
  41. if vcs_type == 'svn':
  42. raise RuntimeError('SVN repository must not have a version attribute but must contain the version in the URL')
  43. values['version'] = version
  44. data['repositories'][name] = values
  45. sort_yaml_data(data)
  46. with open(yaml_file, 'w') as out_file:
  47. yaml.dump(data, out_file, default_flow_style=False)
  48. if __name__ == "__main__":
  49. parser = argparse.ArgumentParser(description='Insert a repository into the .yaml file.')
  50. parser.add_argument('yaml_file', help='The yaml file to update')
  51. parser.add_argument('name', help='The unique name of the repo')
  52. parser.add_argument('type', help='The type of the repository (i.e. "git", "hg", "svn")')
  53. parser.add_argument('url', help='The url of the repository')
  54. parser.add_argument('version', nargs='?', help='The version')
  55. args = parser.parse_args()
  56. try:
  57. add_devel_repository(args.yaml_file, args.name, args.type, args.url, args.version)
  58. except Exception as e:
  59. print(str(e), file=sys.stderr)
  60. exit(1)