import_srpm.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. print(" checking out parent ref...")
  36. subprocess.check_call(['git', 'fetch'])
  37. subprocess.check_call(['git', 'checkout', args.parent_branch])
  38. if args.push:
  39. subprocess.check_call(['git', 'pull'])
  40. print(" removing everything from SOURCES and SPECS...")
  41. if os.path.isdir('SOURCES') and len(os.listdir('SOURCES')) > 0:
  42. subprocess.check_call(['git', 'rm', 'SOURCES/*', '-r'])
  43. if os.path.isdir('SOURCES') and len(os.listdir('SOURCES')) > 0:
  44. parser.error("Files remaining in SOURCES/ after removing the tracked ones. ")
  45. parser.error("Delete them (including hidden files), reset --hard.")
  46. os.mkdir('SOURCES')
  47. if os.path.isdir('SPECS'):
  48. subprocess.check_call(['git', 'rm', 'SPECS/*', '-r'])
  49. os.mkdir('SPECS')
  50. print(" extracting SRPM...")
  51. os.chdir('SOURCES')
  52. os.system('rpm2cpio "%s" | cpio -idmv' % source_rpm_abs)
  53. os.chdir('..')
  54. os.system('mv SOURCES/*.spec SPECS/')
  55. print(" removing trademarked or copyrighted files...")
  56. sources = os.listdir('SOURCES')
  57. deletemsg = "File deleted from the original sources for trademark-related or copyright-related legal reasons.\n"
  58. deleted = []
  59. for f in ['Citrix_Logo_Black.png', 'COPYING.CitrixCommercial']:
  60. if f in sources:
  61. os.unlink(os.path.join('SOURCES', f))
  62. open(os.path.join('SOURCES', "%s.deleted-by-XCP-ng.txt" % f), 'w').write(deletemsg)
  63. deleted.append(f)
  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. print(" committing...")
  71. has_changes = False
  72. try:
  73. subprocess.check_call(['git', 'diff-index', '--quiet', 'HEAD', '--'])
  74. except:
  75. has_changes = True
  76. if not has_changes:
  77. print("\nWorking copy has no modifications. Nothing to commit. No changes from previous release?\n")
  78. else:
  79. msg = 'Import %s' % os.path.basename(args.source_rpm)
  80. if deleted:
  81. msg += "\n\nFiles deleted for legal reasons:\n - " + '\n - '.join(deleted)
  82. subprocess.check_call(['git', 'commit', '-s', '-m', msg])
  83. # tag
  84. if args.tag is not None:
  85. subprocess.check_call(['git', 'tag', args.tag])
  86. # push to remote
  87. if args.push:
  88. subprocess.check_call(['git', 'push', '--set-upstream', 'origin', args.branch])
  89. if args.tag is not None:
  90. subprocess.check_call(['git', 'push', 'origin', args.tag])
  91. print(" switching to master before leaving...")
  92. subprocess.check_call(['git', 'checkout', 'master'])
  93. # merge to master if needed
  94. if args.push and args.master:
  95. print(" merging to master...")
  96. subprocess.check_call(['git', 'push', 'origin', '%s:master' % args.branch])
  97. subprocess.check_call(['git', 'pull'])
  98. if __name__ == "__main__":
  99. main()