Advertisement
count0ru

Untitled

May 4th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.52 KB | None | 0 0
  1. import urllib.request
  2. import requests
  3. import re
  4. import subprocess
  5.  
  6. repos = dict()
  7. repos['rpmforge'] = 'http://mirror1.hs-esslingen.de/repoforge/redhat/el6/en/x86_64/rpmforge/RPMS/'
  8. repos['bareos'] = 'http://download.bareos.org/bareos/release/15.2/CentOS_6/x86_64/'
  9. repos['epel'] = 'http://fedora-mirror01.rbc.ru/pub/epel/6/x86_64/' #http://mirror.yandex.ru/epel/6Server/x86_64/
  10. repos['extras'] = 'http://mirror.centos.org/centos/6.7/extras/x86_64/Packages/'
  11. repos['ius'] = 'https://dl.iuscommunity.org/pub/ius/stable/CentOS/6/x86_64/'
  12. repos['base'] = 'http://mirror.centos.org/centos/6.7/os/x86_64/Packages/'
  13. repos['updates'] = 'http://mirror.centos.org/centos/6.7/updates/x86_64/Packages/'
  14. repos['mono'] = 'http://download.opensuse.org/repositories/Mono:/EL6/RHEL6/x86_64/'
  15. repos['nginx'] = 'http://nginx.org/packages/centos/6/x86_64/RPMS/'
  16. repos['percona'] = 'http://repo.percona.com/release/6Server/RPMS/x86_64/'
  17. repos['percona-release-noarch'] = 'http://repo.percona.com/release/6Server/RPMS/noarch/'
  18. repos['rpmforge'] = 'http://apt.sw.be/redhat/el6/en/x86_64/rpmforge/RPMS/'
  19.  
  20. rpm_list_path = '/home/dna/all_rpms.txt'
  21. local_repo_path = '/home/dna/rpms/'
  22. notfound_rpm_list = '/home/dna/nf_rpms.txt'
  23.  
  24.  
  25. rpm_list = []
  26.  
  27. def run_command(cmd):
  28.  
  29.     result = {}
  30.  
  31.     raw_result = subprocess.Popen(cmd.split(' '),
  32.                             stdout=subprocess.PIPE,
  33.                             stderr=subprocess.PIPE,
  34.                             stdin=subprocess.PIPE).communicate()
  35.  
  36.     result['stdout'] = (raw_result[0].decode('UTF-8')).rstrip('\n')
  37.  
  38.     return result
  39.  
  40.  
  41. rpm_list_file = open(rpm_list_path, "r")
  42.  
  43. stats = dict()
  44. stats['Founded'] = 0
  45. stats['Not found'] = 0
  46. stats['All'] = 0
  47. stats['Missed'] = 0
  48.  
  49. for line in rpm_list_file:
  50.     rpm_list.append(line.rstrip())
  51.     stats['All'] = stats['All'] + 1
  52.  
  53. for rpm_file in rpm_list:
  54.     #gathering info from yum
  55.     command = "/usr/bin/yum info " + rpm_file
  56.     raw_package_info = run_command(command)
  57.     package_info = (raw_package_info['stdout']).split("\n")
  58.     reponames = re.compile("^\S\+*")
  59.     iparams = dict()
  60.     for line in package_info:
  61.         if not (('Loaded plugins' in line) \
  62.             or ('Loading mirror speeds from cached hostfile' in line) \
  63.             or ('Installed Packages' in line) \
  64.             or (not reponames.match(line))):
  65.                 key, value = line.split(':', 1)
  66.                 key = key.strip()
  67.                 value = value.strip()
  68.                 iparams[key] = value
  69.     try:
  70.         #search package in repo
  71.         print("Search package " + rpm_file + " in " + iparams['From repo'])
  72.         repo = iparams['From repo']
  73.         repo_url = repos[repo]
  74.         rpm_url = repo_url + rpm_file + '.rpm'
  75.         try:
  76.             ret_code = requests.head(rpm_url).status_code
  77.             if ret_code == 200:
  78.                 stats['Founded'] = stats['Founded'] + 1
  79.             elif ret_code == 404:
  80.                 stats['Not found'] = 0
  81.                 print(rpm_url + " not found!")
  82.                 with open(notfound_rpm_list, "a") as nf_rpms_file:
  83.                     nf_rpms_file.write(rpm_file)
  84.                     nf_rpms_file.close()
  85.         except Exception as error:
  86.             print(error)
  87.     except:
  88.         print(rpm_file + " is missed!")
  89.         continue
  90. stats['Missed'] = stats['All'] - stats['Founded'] - stats['Not found']
  91. print('Founded:' + str(stats['Founded']))
  92. print('Not found:' + str(stats['Not found']))
  93. print('Missed' + str(stats['Missed']))
  94. print('All:' + str(stats['All']))
  95. rpm_list_file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement