import_srpm.py 4.7 KB

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