import_srpm.py 5.0 KB

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