Added documenteation on code and cleaning some parts.

This commit is contained in:
Davi Vercillo C. Garcia 2009-07-21 23:09:29 -03:00
parent 086d01fda1
commit d72c7682d5

View file

@ -1,9 +1,6 @@
#! /usr/bin/env python
# coding: utf-8
#
# [Author]
# Davi Vercillo C. Garcia (davivercillo@gmail.com)
#
# [License]
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
@ -19,58 +16,80 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
__AUTHOR__ = "Davi Vercillo C. Garcia (davivercillo@gmail.com)"
__VERSION__ = "0.2"
__DATE__ = "21/07/2009"
import sys
import urllib
from signal import signal, SIG_DFL
signal(2, SIG_DFL)
if len(sys.argv) < 4:
print "Use: ./mirror_checker.py updates 11 x86_64"
sys.exit(-1)
main_mirror = "http://download.fedora.redhat.com/pub/fedora/linux/%s/%s/%s/repodata/"
mirror_list = "http://mirrors.fedoraproject.org/mirrorlist?path=/pub/fedora/linux/%s/%s/%s/repodata&country=global"
xml_file = "repomd.xml"
directory = sys.argv[1]
version = sys.argv[2]
architecture = sys.argv[3]
try:
mirrors = [ url for url in urllib.urlopen(mirror_list % (directory, version, architecture)).read().split("\n")
if url != "" and not "#" in url]
repomd = urllib.urlopen(main_mirror % (directory, version, architecture) + xml_file).read()
except Exception, err:
print "[ERROR] Cannot get info from URLs. Please check the parameters."
sys.exit(-1)
num_total = len(mirrors)
num_good = 0
num_bad = 0
num_error = 0
results = [[],[], []]
print "\nChecking the repositories repodata !\n\nUsing:", main_mirror % (directory, version, architecture)
for url in mirrors:
if "#" in url or not url:
continue
print "\rTesting: %d/%d" % (num_good, num_total),
sys.stdout.flush()
try:
if urllib.urlopen(url + xml_file).read() == repomd:
results[0].append(url)
num_good += 1
else:
results[1].append(url)
num_bad += 1
except Exception, err:
results[2].append(url)
num_error += 1
print """\n
class CheckMirrors:
"""Class that check repodata on the mirrors.
"""
def __init__(self, directory, version, architecture):
"""Class constructor.
directory -> (String) Ex: updates
version -> : (String) Ex: 11
architecture -> (String) Ex: x86_64
"""
if type(directory) != str or type(version) != str or type(architecture) != str:
raise TypeError, "Parameters need to be strings."
self.mirror_list_url = "http://mirrors.fedoraproject.org/mirrorlist?path=/pub/fedora/linux/%s/%s/%s/repodata&country=global"
self.main_mirror = "http://download.fedora.redhat.com/pub/fedora/linux/%s/%s/%s/repodata/"
self.xml_filename = "repomd.xml"
self.directory = directory
self.version = version
self.architecture = architecture
self.number_total_mirrors = 0
self.good_mirrors = [[], 0]
self.bad_mirrors = [[], 0]
self.error_mirrors = [[], 0]
def get_mirror_list(self):
"""Method that connect on fedoraproject.org, get the mirror list and the repomd.xml.
"""
temp = self.mirror_list_url % (self.directory, self.version, self.architecture)
try:
self.mirrors = [ url
for url in urllib.urlopen(temp).read().split("\n")
if url != "" and not "#" in url ]
except Exception, e:
print "[ERROR] Failed to get mirror list:", e
sys.exit(-1)
temp = self.main_mirror % (self.directory, self.version, self.architecture)
try:
self.repodata = urllib.urlopen(temp + self.xml_filename).read()
except Exception, e:
print "[ERROR] Failed to get XML repodata file:", e
sys.exit(-1)
self.number_total_mirrors = len(self.mirrors)
if self.number_total_mirrors == 0:
print "[ERROR] Did you specify the right options ?"
sys.exit(-1)
def check_mirrors(self):
"""Method that verify, for each mirror, if its repomd.xml is equal of that on main.
"""
print "\nChecking the repositories repodata !\n\nUsing:", self.main_mirror % (self.directory, self.version, self.architecture)
for url in self.mirrors:
print "\rTesting: %d/%d" % (self.good_mirrors[1] , self.number_total_mirrors),
sys.stdout.flush()
try:
if urllib.urlopen(url + self.xml_filename).read() == self.repodata:
self.good_mirrors[0].append(url)
self.good_mirrors[1] += 1
else:
self.bad_mirrors[0].append(url)
self.bad_mirrors[1] += 1
except Exception, err:
self.error_mirrors[0].append(url + "\n[" + err + "]")
self.error_mirrors[1] += 1
def print_results(self):
"""Method that put the results on STDOUT.
"""
print """\n
========================== Results ==========================
\tGood\tBad\tError\tTotal\tPerc.Good
\t%4d\t%3d\t%5d\t%5d\t%7.2f%%
@ -84,10 +103,32 @@ print """\n
[Errors]
%s
""" % (num_good, num_bad, num_error, num_total , float(num_good)*100/(num_good + num_bad),
"\n".join(results[0]),
"\n".join(results[1]),
"\n".join(results[2]),)
sys.exit(0)
""" % (self.good_mirrors[1],
self.bad_mirrors[1],
self.error_mirrors[1],
self.number_total_mirrors,
float(self.good_mirrors[1])*100/self.number_total_mirrors,
"\n".join(self.good_mirrors[0]),
"\n".join(self.bad_mirrors[0]),
"\n".join(self.error_mirrors[0]),)
def run(self):
"""Method that execute all method in the right order.
"""
self.get_mirror_list()
self.check_mirrors()
self.print_results()
if __name__ == "__main__":
"""Main Function.
If the programs was called as a script, this will be executed.
"""
signal(2, SIG_DFL)
if len(sys.argv) == 4:
CheckMirrors(sys.argv[1], sys.argv[2], sys.argv[3], ).run()
sys.exit(0)
else:
print "[ERROR] Use: ./mirror_checker.py <directory> <version> <architecture>"
sys.exit(-1)