koji_import_rpms.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/bin/env python2
  2. from __future__ import print_function
  3. import argparse
  4. import os
  5. import subprocess
  6. import glob
  7. def get_srpm_info(srpmpath):
  8. return subprocess.check_output(['rpm', '-qp', srpmpath, '--qf', '%{name};;%{nvr}']).split(';;')
  9. def check_dir(dirpath):
  10. if not os.path.isdir(dirpath):
  11. raise Exception("Directory %s doesn't exist" % dirpath)
  12. return dirpath
  13. def main():
  14. parser = argparse.ArgumentParser(description='Import source RPM(s) and RPM(s) to koji and tag the builds')
  15. parser.add_argument('srpm_directory', help='path to a directory containing the source RPM(s) we want to import')
  16. parser.add_argument('rpm_directory', help='path to a directory containing the RPM(s) we want to import')
  17. parser.add_argument('package_tags', help='comma-separated list of tags for the package(s)')
  18. parser.add_argument('build_tags', help='comma-separated list of tags for imported build(s)')
  19. parser.add_argument('--owner', help='owner for the package(s)', default='kojiadmin')
  20. parser.add_argument('--create-build', help='create the build even if there\'s no SRPM', action='store_true', default=False)
  21. args = parser.parse_args()
  22. DEVNULL = open(os.devnull, 'w')
  23. srpm_directory = os.path.abspath(check_dir(args.srpm_directory))
  24. rpm_directory = os.path.abspath(check_dir(args.rpm_directory))
  25. srpms = []
  26. packages = []
  27. builds = []
  28. rpms = []
  29. for f in glob.glob(os.path.join(srpm_directory, '*.rpm')):
  30. if f.endswith('.src.rpm'):
  31. srpms.append(f)
  32. package, build = get_srpm_info(f)
  33. packages.append(package)
  34. builds.append(build)
  35. for f in glob.glob(os.path.join(rpm_directory, '*.rpm')):
  36. if not f.endswith('.src.rpm'):
  37. rpms.append(f)
  38. create_build = ['--create-build'] if args.create_build else []
  39. if not srpms and not create_build:
  40. raise Exception("No source RPMs to import. Use --create-build to force import.")
  41. if srpms:
  42. # import the SRPMs
  43. print('*** Importing SRPMs ***')
  44. subprocess.check_call(['koji', 'import'] + srpms)
  45. print()
  46. # import the RPMS
  47. print('*** Importing RPMs ***')
  48. if rpms:
  49. subprocess.check_call(['koji', 'import'] + create_build + rpms)
  50. else:
  51. print("No RPMs to import.")
  52. print()
  53. # tag packages
  54. pkg_tags = args.package_tags.split(',')
  55. if packages:
  56. for tag in pkg_tags:
  57. print('*** Tagging packages %s with tag %s' % (' '.join(packages), tag))
  58. subprocess.check_call(['koji', 'add-pkg', tag, '--owner', args.owner] + packages)
  59. print()
  60. # tag builds
  61. build_tags = args.build_tags.split(',')
  62. if builds:
  63. for tag in build_tags:
  64. print('*** Tagging builds %s with tag %s' % (' '.join(builds), tag))
  65. subprocess.check_call(['koji', 'tag-build', tag, '--nowait'] + builds)
  66. if create_build:
  67. print("NOTE: You used the --create-build option.")
  68. print("Packages without SRPMs have not been tagged with tags %s" % pkg_tags)
  69. print("Builds without SRPMs have not been tagged with tags %s" % build_tags)
  70. print("You will need to tag them manually.")
  71. if __name__ == "__main__":
  72. main()