koji_import_rpms.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/bin/env python
  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. if not srpms:
  39. raise Exception("No source RPMs to import")
  40. # import the SRPMs
  41. print('*** Importing SRPMs ***')
  42. subprocess.check_call(['koji', 'import'] + srpms)
  43. print()
  44. # import the RPMS
  45. print('*** Importing RPMs ***')
  46. if rpms:
  47. create_build = ['--create-build'] if args.create_build else []
  48. subprocess.check_call(['koji', 'import'] + create_build + rpms)
  49. else:
  50. print("No RPMs to import.")
  51. print()
  52. # tag packages
  53. pkg_tags = args.package_tags.split(',')
  54. for tag in pkg_tags:
  55. print('*** Tagging packages %s with tag %s' % (' '.join(packages), tag))
  56. subprocess.check_call(['koji', 'add-pkg', tag, '--owner', args.owner] + packages)
  57. print()
  58. # tag builds
  59. build_tags = args.build_tags.split(',')
  60. for tag in build_tags:
  61. print('*** Tagging builds %s with tag %s' % (' '.join(builds), tag))
  62. subprocess.check_call(['koji', 'tag-build', tag, '--nowait'] + builds)
  63. if __name__ == "__main__":
  64. main()