create_rpm_git_repo.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env python3
  2. from __future__ import print_function
  3. import argparse
  4. import os
  5. import subprocess
  6. from github import Github
  7. def main():
  8. parser = argparse.ArgumentParser(description='Creates a git repository in current directory for RPM spec file and sources')
  9. parser.add_argument('name', help='name of repository')
  10. parser.add_argument('--local',
  11. help='do not create github repository',
  12. action='store_true')
  13. parser.add_argument('token_file',
  14. help='file containing github authentication token',
  15. nargs='?',
  16. default=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'github.txt'))
  17. args = parser.parse_args()
  18. if not args.local:
  19. # authenticate to Github
  20. token = open(args.token_file).read().strip()
  21. g = Github(token)
  22. # create repository
  23. org = g.get_organization('xcp-ng-rpms')
  24. org.create_repo(args.name, "RPM sources for %s" % args.name)
  25. # initial commit to master
  26. gitignore = """BUILD
  27. BUILDROOT
  28. RPMS
  29. SRPMS
  30. """
  31. readme = """RPM sources for the %s package in XCP-ng (https://xcp-ng.org/).
  32. Make sure to have `git-lfs` installed before cloning. It is used for handling source archives separately.
  33. Branches:
  34. * `master` contains sources for the next `x.y` release of XCP-ng.
  35. * `x.y` (e.g. `7.5`) contains sources for the `x.y` release of XCP-ng, with their bugfix and/or security updates.
  36. * `XS-x.y` (e.g. `XS-7.5`), when they exist, contain sources from the `x.y` release of XenServer, with trademarked or copyrighted material stripped if needed.
  37. Built RPMs and source RPMs are available on https://updates.xcp-ng.org.
  38. """ % args.name
  39. if args.local:
  40. subprocess.check_call(['git', 'init', args.name])
  41. subprocess.check_call(['git', '-C', args.name,
  42. 'remote', 'add', 'origin',
  43. 'https://github.com/xcp-ng-rpms/%s.git' % args.name])
  44. else:
  45. subprocess.check_call(['git', 'clone', 'https://github.com/xcp-ng-rpms/%s.git' % args.name])
  46. os.chdir(args.name)
  47. if "git@github.com" not in subprocess.check_output(
  48. ['git', 'remote', 'get-url', '--push', 'origin'],
  49. universal_newlines=True):
  50. # only set if pushInsteadOf was not configured
  51. subprocess.check_call(['git', 'remote', 'set-url', '--push', 'origin',
  52. 'git@github.com:xcp-ng-rpms/%s.git' % args.name])
  53. open('.gitignore', 'w').write(gitignore)
  54. subprocess.check_call(['git', 'add', '.gitignore'])
  55. open('README.md', 'w').write(readme)
  56. subprocess.check_call(['git', 'add', 'README.md'])
  57. subprocess.check_call(['git', 'lfs', 'install'])
  58. subprocess.check_call(['git', 'lfs', 'track', '*.gz'])
  59. subprocess.check_call(['git', 'lfs', 'track', '*.bz2'])
  60. subprocess.check_call(['git', 'lfs', 'track', '*.xz'])
  61. subprocess.check_call(['git', 'lfs', 'track', '*.zip'])
  62. subprocess.check_call(['git', 'lfs', 'track', '*.tar'])
  63. subprocess.check_call(['git', 'lfs', 'track', '*.tgz'])
  64. subprocess.check_call(['git', 'lfs', 'track', '*.tbz'])
  65. subprocess.check_call(['git', 'add', '.gitattributes'])
  66. subprocess.check_call(['git', 'commit', '-s', '-m', 'Initial commit'])
  67. if not args.local:
  68. subprocess.check_call(['git', 'push'])
  69. if __name__ == "__main__":
  70. main()