2006-12-08 17:07:34 -05:00
|
|
|
#! /usr/bin/python
|
|
|
|
#
|
2007-05-30 11:04:49 -04:00
|
|
|
# rankmirrors - read a list of mirrors from a file and rank them by speed
|
|
|
|
# @configure_input@
|
2006-12-08 17:07:34 -05:00
|
|
|
#
|
2007-07-06 19:35:32 -04:00
|
|
|
# Copyright (c) 2002-2007 by Judd Vinet <jvinet@zeroflux.org>
|
2006-12-08 17:07:34 -05:00
|
|
|
#
|
|
|
|
# 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 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2007-12-10 23:55:22 -05:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2006-12-08 17:07:34 -05:00
|
|
|
#
|
2007-01-16 22:48:02 -05:00
|
|
|
import os, sys, datetime, time, socket, urllib2
|
2006-12-08 17:07:34 -05:00
|
|
|
from optparse import OptionParser
|
2007-09-17 13:04:54 -04:00
|
|
|
from string import Template
|
2006-12-08 17:07:34 -05:00
|
|
|
|
|
|
|
def createOptParser():
|
2007-01-16 22:48:02 -05:00
|
|
|
usage = "usage: %prog [options] MIRRORFILE | URL"
|
2007-07-06 19:35:32 -04:00
|
|
|
version = "%prog (pacman) @PACKAGE_VERSION@\n" \
|
|
|
|
"Copyright (C) 2002-2007 Judd Vinet <jvinet@zeroflux.org>.\n\n" \
|
|
|
|
"This is free software; see the source for copying conditions.\n" \
|
|
|
|
"There is NO WARRANTY, to the extent permitted by law."
|
2007-02-22 03:06:54 -05:00
|
|
|
description = "Ranks pacman mirrors by their connection and opening " \
|
|
|
|
"speed. Pacman mirror files are located in /etc/pacman.d/. It " \
|
|
|
|
"can also rank one mirror if the URL is provided."
|
2007-05-29 16:53:15 -04:00
|
|
|
parser = OptionParser(usage = usage, version = version,
|
|
|
|
description = description)
|
2007-02-22 03:06:54 -05:00
|
|
|
parser.add_option("-n", type = "int", dest = "num", default = 0,
|
2007-03-11 23:02:57 -04:00
|
|
|
help = "number of servers to output, 0 for all")
|
2007-03-04 21:32:14 -05:00
|
|
|
parser.add_option("-t", "--times", action = "store_true",
|
2007-03-11 23:02:57 -04:00
|
|
|
dest = "times", default = False,
|
|
|
|
help = "only output mirrors and their response times")
|
2007-02-22 03:06:54 -05:00
|
|
|
parser.add_option("-u", "--url", action = "store_true", dest = "url",
|
2007-03-11 23:02:57 -04:00
|
|
|
default = False, help = "test a specific url")
|
2007-02-22 03:06:54 -05:00
|
|
|
parser.add_option("-v", "--verbose", action = "store_true",
|
2007-03-11 23:02:57 -04:00
|
|
|
dest = "verbose", default = False,
|
|
|
|
help = "be verbose in ouptut")
|
2007-05-29 16:53:15 -04:00
|
|
|
# The following two options should be automatic
|
|
|
|
#parser.add_option("-h", "--help", action = "help")
|
|
|
|
#parser.add_option("-V", "--version", action = "version")
|
2006-12-20 20:53:40 -05:00
|
|
|
return parser
|
2006-12-08 17:07:34 -05:00
|
|
|
|
|
|
|
def timeCmd(cmd):
|
2007-03-11 23:02:57 -04:00
|
|
|
before = time.time()
|
2006-12-20 20:53:40 -05:00
|
|
|
try:
|
2007-03-11 23:02:57 -04:00
|
|
|
cmd()
|
2006-12-20 20:53:40 -05:00
|
|
|
except KeyboardInterrupt, ki:
|
|
|
|
raise ki
|
|
|
|
except socket.timeout, ioe:
|
|
|
|
return 'timeout'
|
|
|
|
except Exception, e:
|
|
|
|
return 'unreachable'
|
2007-03-11 23:02:57 -04:00
|
|
|
return time.time() - before
|
2006-12-08 17:07:34 -05:00
|
|
|
|
|
|
|
def talkToServer(serverUrl):
|
2006-12-20 20:53:40 -05:00
|
|
|
opener = urllib2.build_opener()
|
2007-04-23 00:07:50 -04:00
|
|
|
# retrieve first 50,000 bytes only
|
|
|
|
tmp = opener.open(serverUrl).read(50000)
|
2006-12-08 17:07:34 -05:00
|
|
|
|
|
|
|
def getFuncToTime(serverUrl):
|
2006-12-20 20:53:40 -05:00
|
|
|
return lambda : talkToServer(serverUrl)
|
2006-12-08 17:07:34 -05:00
|
|
|
|
|
|
|
def cmpPairBySecond(p1, p2):
|
2007-03-11 23:02:57 -04:00
|
|
|
if p1[1] == p2[1]:
|
|
|
|
return 0
|
|
|
|
if p1[1] < p2[1]:
|
|
|
|
return -1
|
2006-12-20 20:53:40 -05:00
|
|
|
return 1
|
2006-12-08 17:07:34 -05:00
|
|
|
|
2007-03-11 23:02:57 -04:00
|
|
|
def printResults(servers, time, verbose, num):
|
|
|
|
items = servers.items()
|
|
|
|
items.sort(cmpPairBySecond)
|
|
|
|
itemsLen = len(items)
|
|
|
|
numToShow = num
|
|
|
|
if numToShow > itemsLen or numToShow == 0:
|
|
|
|
numToShow = itemsLen
|
|
|
|
if itemsLen > 0:
|
|
|
|
if time:
|
|
|
|
print
|
|
|
|
print ' Servers sorted by time (seconds):'
|
|
|
|
for i in items[0:numToShow]:
|
|
|
|
if i[1] == 'timeout' or i[1] == 'unreachable':
|
|
|
|
print i[0], ':', i[1]
|
|
|
|
else:
|
|
|
|
print i[0], ':', "%.2f" % i[1]
|
|
|
|
else:
|
|
|
|
for i in items[0:numToShow]:
|
|
|
|
print 'Server =', i[0]
|
|
|
|
|
2006-12-08 17:07:34 -05:00
|
|
|
if __name__ == "__main__":
|
2006-12-20 20:53:40 -05:00
|
|
|
parser = createOptParser()
|
|
|
|
(options, args) = parser.parse_args()
|
2006-12-08 17:07:34 -05:00
|
|
|
|
2006-12-20 20:53:40 -05:00
|
|
|
if len(args) != 1:
|
|
|
|
parser.print_help(sys.stderr)
|
|
|
|
sys.exit(0)
|
2006-12-08 17:07:34 -05:00
|
|
|
|
2006-12-20 20:53:40 -05:00
|
|
|
# allows connections to time out if they take too long
|
|
|
|
socket.setdefaulttimeout(10)
|
2006-12-08 17:07:34 -05:00
|
|
|
|
2007-01-16 22:48:02 -05:00
|
|
|
if options.url:
|
|
|
|
if options.verbose:
|
|
|
|
print 'Testing', args[0] + '...'
|
2007-03-11 23:02:57 -04:00
|
|
|
try:
|
|
|
|
serverToTime = timeCmd(getFuncToTime(args[0]))
|
|
|
|
except KeyboardInterrupt, ki:
|
|
|
|
sys.exit(1)
|
|
|
|
if serverToTime == 'timeout' or serverToTime == 'unreachable':
|
|
|
|
print args[0], ':', serverToTime
|
|
|
|
else:
|
|
|
|
print args[0], ':', "%.2f" % serverToTime
|
2007-01-16 22:48:02 -05:00
|
|
|
sys.exit(0)
|
|
|
|
|
2007-09-17 20:48:57 -04:00
|
|
|
if not os.path.isfile(args[0]) and args[0] != "-":
|
2007-03-11 23:02:57 -04:00
|
|
|
print >>sys.stderr, 'rankmirrors: file', args[0], 'does not exist.'
|
2007-01-16 22:48:02 -05:00
|
|
|
sys.exit(1)
|
|
|
|
|
2007-09-17 20:48:57 -04:00
|
|
|
if args[0] == "-":
|
|
|
|
fl = sys.stdin
|
|
|
|
else:
|
|
|
|
fl = open(args[0], 'r')
|
|
|
|
|
2006-12-20 20:53:40 -05:00
|
|
|
serverToTime = {}
|
2007-03-04 21:32:14 -05:00
|
|
|
if options.times:
|
|
|
|
print 'Querying servers, this may take some time...'
|
|
|
|
else:
|
2007-02-22 03:06:54 -05:00
|
|
|
print "# Server list generated by rankmirrors on",
|
|
|
|
print datetime.date.today()
|
2006-12-20 20:53:40 -05:00
|
|
|
for ln in fl.readlines():
|
|
|
|
splitted = ln.split('=')
|
|
|
|
if splitted[0].strip() != 'Server':
|
2007-03-04 21:32:14 -05:00
|
|
|
if not options.times:
|
2006-12-20 20:53:40 -05:00
|
|
|
print ln,
|
|
|
|
continue
|
2006-12-08 17:07:34 -05:00
|
|
|
|
2006-12-20 20:53:40 -05:00
|
|
|
serverUrl = splitted[1].strip()
|
2007-03-11 23:02:57 -04:00
|
|
|
if serverUrl[-1] == '\n':
|
|
|
|
serverUrl = serverUrl[0:-1]
|
2007-03-04 21:32:14 -05:00
|
|
|
if options.verbose and options.times:
|
2007-03-11 23:02:57 -04:00
|
|
|
print serverUrl, '...',
|
2007-03-04 21:32:14 -05:00
|
|
|
elif options.verbose:
|
2007-03-11 23:02:57 -04:00
|
|
|
print '#', serverUrl, '...',
|
2007-03-04 21:32:14 -05:00
|
|
|
elif options.times:
|
2006-12-20 20:53:40 -05:00
|
|
|
print ' * ',
|
|
|
|
sys.stdout.flush()
|
2007-04-23 00:07:50 -04:00
|
|
|
|
2007-09-17 13:04:54 -04:00
|
|
|
# if the $repo var is used in the url, replace it by core
|
|
|
|
tempUrl = Template(serverUrl).safe_substitute(repo='core')
|
|
|
|
|
2008-01-05 23:00:21 -05:00
|
|
|
# add @DBEXT@ to server name. the repo name is parsed
|
2007-04-23 00:07:50 -04:00
|
|
|
# from the mirror url; it is the third (or fourth) dir
|
|
|
|
# from the end, where the url is http://foo/bar/REPO/os/arch
|
|
|
|
try:
|
2007-09-17 13:04:54 -04:00
|
|
|
splitted2 = tempUrl.split('/')
|
|
|
|
if tempUrl[-1] != '/':
|
2007-04-23 00:07:50 -04:00
|
|
|
repoName = splitted2[-3]
|
2008-01-05 23:00:21 -05:00
|
|
|
dbFileName = '/' + repoName + '@DBEXT@'
|
2007-04-23 00:07:50 -04:00
|
|
|
else:
|
|
|
|
repoName = splitted2[-4]
|
2008-01-05 23:00:21 -05:00
|
|
|
dbFileName = repoName + '@DBEXT@'
|
2007-04-23 00:07:50 -04:00
|
|
|
except:
|
|
|
|
dbFileName = ''
|
|
|
|
|
2007-03-11 23:02:57 -04:00
|
|
|
try:
|
2007-09-17 13:04:54 -04:00
|
|
|
serverToTime[serverUrl] = timeCmd(getFuncToTime(tempUrl + dbFileName))
|
2007-03-11 23:02:57 -04:00
|
|
|
if options.verbose:
|
2007-03-04 21:32:14 -05:00
|
|
|
try:
|
2007-03-11 23:02:57 -04:00
|
|
|
print "%.2f" % serverToTime[serverUrl]
|
2007-03-04 21:32:14 -05:00
|
|
|
except:
|
2007-03-11 23:02:57 -04:00
|
|
|
print serverToTime[serverUrl]
|
|
|
|
except:
|
|
|
|
print
|
|
|
|
printResults(serverToTime, options.times, options.verbose,
|
|
|
|
options.num)
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
printResults(serverToTime, options.times, options.verbose, options.num)
|
2006-12-08 17:07:34 -05:00
|
|
|
|
2006-12-20 20:53:40 -05:00
|
|
|
# vim: set ts=4 sw=4 et:
|