import_srpm.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/env python
  2. from __future__ import print_function
  3. import argparse
  4. import os
  5. import subprocess
  6. def main():
  7. parser = argparse.ArgumentParser(description='Imports the contents of a source RPM into a git repository')
  8. parser.add_argument('source_rpm', help='local path to source RPM')
  9. parser.add_argument('repository', help='local path to the repository')
  10. parser.add_argument('parent_branch', help='git parent branch from which to branch')
  11. parser.add_argument('branch', help='destination branch')
  12. parser.add_argument('tag', nargs='?', help='tag')
  13. parser.add_argument('-c', '--commit', action='store_true', help='commit the changes')
  14. parser.add_argument('-p', '--push', action='store_true', help='commit and push')
  15. parser.add_argument('-m', '--master', action='store_true', help='merge to master afterwards')
  16. args = parser.parse_args()
  17. # check that the source RPM file exists
  18. if not os.path.isfile(args.source_rpm):
  19. parser.error("File %s does not exist." % args.source_rpm)
  20. if not args.source_rpm.endswith('.src.rpm'):
  21. parser.error("File %s does not appear to be a source RPM." % args.source_rpm)
  22. source_rpm_abs = os.path.abspath(args.source_rpm)
  23. # enter repository directory
  24. if not os.path.isdir(args.repository):
  25. parser.error("Repository directory %s does not exist." % args.repository)
  26. os.chdir(args.repository)
  27. # check that the working copy is clean
  28. try:
  29. subprocess.check_call(['git', 'diff-index', '--quiet', 'HEAD', '--'])
  30. print("Working copy is clean.")
  31. except:
  32. parser.error("Git repository seems to have local modifications.")
  33. # checkout parent ref
  34. subprocess.check_call(['git', 'fetch'])
  35. subprocess.check_call(['git', 'checkout', args.parent_branch])
  36. subprocess.check_call(['git', 'pull'])
  37. # remove everything from SOURCES and SPECS
  38. if os.path.isdir('SOURCES'):
  39. subprocess.check_call(['git', 'rm', 'SOURCES/*', '-r'])
  40. os.mkdir('SOURCES')
  41. if os.path.isdir('SPECS'):
  42. subprocess.check_call(['git', 'rm', 'SPECS/*', '-r'])
  43. os.mkdir('SPECS')
  44. # extract SRPM
  45. os.chdir('SOURCES')
  46. os.system('rpm2cpio "%s" | cpio -idmv' % source_rpm_abs)
  47. os.chdir('..')
  48. os.system('mv SOURCES/*.spec SPECS/')
  49. # remove trademarked or copyrighted files
  50. sources = os.listdir('SOURCES')
  51. deletemsg = "File deleted from the original sources for trademark-related or copyright-related legal reasons.\n"
  52. deleted = []
  53. for f in ['Citrix_Logo_Black.png']:
  54. if f in sources:
  55. os.unlink(os.path.join('SOURCES', f))
  56. open(os.path.join('SOURCES', "%s.deleted-by-XCP-ng.txt" % f), 'w').write(deletemsg)
  57. deleted.append(f)
  58. # commit
  59. if subprocess.call(['git', 'rev-parse', '--quiet', '--verify', args.branch]) != 0:
  60. subprocess.check_call(['git', 'checkout', '-b', args.branch])
  61. else:
  62. subprocess.check_call(['git', 'checkout', args.branch])
  63. subprocess.check_call(['git', 'add', '--all'])
  64. if args.commit or args.push:
  65. has_changes = False
  66. try:
  67. subprocess.check_call(['git', 'diff-index', '--quiet', 'HEAD', '--'])
  68. except:
  69. has_changes = True
  70. if not has_changes:
  71. print("\nWorking copy has no modifications. Nothing to commit. No changes from previous release?\n")
  72. else:
  73. msg = 'Import %s' % os.path.basename(args.source_rpm)
  74. if deleted:
  75. msg += "\n\nFiles deleted for legal reasons:\n - " + '\n - '.join(deleted)
  76. subprocess.check_call(['git', 'commit', '-m', msg])
  77. # tag
  78. if args.tag is not None:
  79. subprocess.check_call(['git', 'tag', args.tag])
  80. # push to remote
  81. if args.push:
  82. subprocess.check_call(['git', 'push', '--set-upstream', 'origin', args.branch])
  83. if args.tag is not None:
  84. subprocess.check_call(['git', 'push', 'origin', args.tag])
  85. # switch to master before leaving
  86. subprocess.check_call(['git', 'checkout', 'master'])
  87. # merge to master if needed
  88. if args.push and args.master:
  89. subprocess.check_call(['git', 'push', 'origin', '%s:master' % args.branch])
  90. subprocess.check_call(['git', 'pull'])
  91. if __name__ == "__main__":
  92. main()