download_centos.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env python3
  2. import argparse
  3. import os
  4. import sys
  5. import glob
  6. import subprocess
  7. import csv
  8. import shutil
  9. def build_url(path):
  10. if path.startswith('7/'):
  11. return "http://mirror.centos.org/centos/" + path
  12. else:
  13. return "http://vault.centos.org/" + path
  14. def download_rpm(rpm, downloaddir, vendor, local_centos, local_epel):
  15. if vendor == "CentOS":
  16. if local_centos is not None:
  17. shutil.copy2(os.path.join(local_centos, rpm), downloaddir)
  18. print("%s: copied from %s" % (rpm, local_centos))
  19. return
  20. else:
  21. for version in ['7.2.1511', '7.3.1611', '7.4.1708', '7.5.1804', '7']:
  22. for repodir in ['os', 'updates']:
  23. url = build_url('%s/%s/x86_64/Packages/%s' % (version, repodir, rpm))
  24. try:
  25. subprocess.check_call(['wget', '-q', '-O', os.path.join(downloaddir, rpm), url])
  26. print("%s: fetched from %s" % (rpm, url))
  27. return
  28. except:
  29. pass
  30. if vendor == "Fedora Project":
  31. if local_epel is not None:
  32. shutil.copy2(os.path.join(local_epel, rpm), downloaddir)
  33. print("%s: copied from %s" % (rpm, local_epel))
  34. return
  35. else:
  36. url = 'http://mirror.in2p3.fr/pub/epel/7/x86_64/Packages/%s/%s' % (rpm[0], rpm)
  37. try:
  38. subprocess.check_call(['wget', '-q', '-O', os.path.join(downloaddir, rpm), url])
  39. print("%s: fetched from %s" % (rpm, url))
  40. return
  41. except:
  42. pass
  43. print("%s: NOT FOUND" % rpm)
  44. sys.exit(1)
  45. def main():
  46. parser = argparse.ArgumentParser(description='Looks for missing RPMs based on their SRPMs and downloads if it can')
  47. parser.add_argument('rpmdir', help='directory that contains the RPMs')
  48. parser.add_argument('srpmdir', help='directory that contains the source RPMs')
  49. parser.add_argument('centos_rpms_and_srpms',
  50. help='file that contains a list of x86_64 and noarch packages with their source RPM')
  51. parser.add_argument('downloaddir', help='where to download the missing RPMs to')
  52. parser.add_argument('--local-centos', help='path to a local directory containing all the CentOS RPMs')
  53. parser.add_argument('--local-epel', help='path to a local directory containing all the EPEL RPMs')
  54. args = parser.parse_args()
  55. DEVNULL = open(os.devnull, 'w')
  56. rpmdir = os.path.abspath(args.rpmdir)
  57. srpmdir = os.path.abspath(args.srpmdir)
  58. downloaddir = os.path.abspath(args.downloaddir)
  59. if not os.path.isdir(downloaddir):
  60. os.makedirs(downloaddir)
  61. local_centos = None
  62. local_epel = None
  63. if args.local_centos:
  64. local_centos=os.path.abspath(args.local_centos)
  65. if args.local_epel:
  66. local_epel=os.path.abspath(args.local_epel)
  67. srpms_to_rpms = {}
  68. with open(args.centos_rpms_and_srpms, 'r') as csvfile:
  69. for row in csv.reader(csvfile, delimiter=','):
  70. if row[1] not in srpms_to_rpms:
  71. srpms_to_rpms[row[1]] = []
  72. srpms_to_rpms[row[1]].append(row[0])
  73. srpms = {}
  74. for filepath in sorted(glob.glob(os.path.join(srpmdir, '*.src.rpm'))):
  75. filename = os.path.basename(filepath)
  76. vendor = subprocess.check_output(['rpm', '-qp', filepath, '--qf', '%{vendor}'], stderr=DEVNULL).decode('utf-8')
  77. if vendor not in ("CentOS", "Fedora Project"):
  78. print("Skipping %s due to unknown vendor %s" % (filename, vendor))
  79. continue
  80. print("\n*** %s" % filename)
  81. if filename not in srpms_to_rpms:
  82. print("Warning: SRPM not found in reference file")
  83. else:
  84. for rpm in srpms_to_rpms[filename]:
  85. if os.path.exists(os.path.join(rpmdir, rpm)):
  86. print("%s: OK" % rpm)
  87. else:
  88. if os.path.exists(os.path.join(downloaddir, rpm)):
  89. print("%s: already downloaded" % rpm)
  90. else:
  91. download_rpm(rpm, downloaddir, vendor, local_centos, local_epel)
  92. if __name__ == "__main__":
  93. main()