mirror of
https://github.com/moparisthebest/pacman
synced 2025-02-28 09:21:53 -05:00
* rankmirrors updates from Scott Horowitz <stonecrest@gmail.com>.
Formatted output is now the default. Added a TODO for later as well.
This commit is contained in:
parent
11fbc595d4
commit
4770ac784e
@ -23,17 +23,20 @@
|
|||||||
import os, sys, datetime, time, socket, urllib2
|
import os, sys, datetime, time, socket, urllib2
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
|
||||||
|
# TODO: handle KeyboardInterrupt better, print list of already timed
|
||||||
|
# servers and then exit. Easier if program is function-ized.
|
||||||
|
|
||||||
def createOptParser():
|
def createOptParser():
|
||||||
usage = "usage: %prog [options] MIRRORFILE | URL"
|
usage = "usage: %prog [options] MIRRORFILE | URL"
|
||||||
description = "Ranks pacman mirrors by their connection and opening " \
|
description = "Ranks pacman mirrors by their connection and opening " \
|
||||||
"speed. Pacman mirror files are located in /etc/pacman.d/. It " \
|
"speed. Pacman mirror files are located in /etc/pacman.d/. It " \
|
||||||
"can also rank one mirror if the URL is provided."
|
"can also rank one mirror if the URL is provided."
|
||||||
parser = OptionParser(usage = usage, description = description)
|
parser = OptionParser(usage = usage, description = description)
|
||||||
parser.add_option("-f", "--formatted", action = "store_true",
|
|
||||||
dest = "formatted", default = False,
|
|
||||||
help = "output in mirror file format")
|
|
||||||
parser.add_option("-n", type = "int", dest = "num", default = 0,
|
parser.add_option("-n", type = "int", dest = "num", default = 0,
|
||||||
help = "number of servers to output, 0 for all")
|
help = "number of servers to output, 0 for all")
|
||||||
|
parser.add_option("-t", "--times", action = "store_true",
|
||||||
|
dest = "times", default = False,
|
||||||
|
help = "only output mirrors and their response times")
|
||||||
parser.add_option("-u", "--url", action = "store_true", dest = "url",
|
parser.add_option("-u", "--url", action = "store_true", dest = "url",
|
||||||
default=False, help="test a specific url")
|
default=False, help="test a specific url")
|
||||||
parser.add_option("-v", "--verbose", action = "store_true",
|
parser.add_option("-v", "--verbose", action = "store_true",
|
||||||
@ -88,43 +91,49 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
fl = open(args[0], 'r')
|
fl = open(args[0], 'r')
|
||||||
serverToTime = {}
|
serverToTime = {}
|
||||||
if options.formatted:
|
if options.times:
|
||||||
|
print 'Querying servers, this may take some time...'
|
||||||
|
else:
|
||||||
print "# Server list generated by rankmirrors on",
|
print "# Server list generated by rankmirrors on",
|
||||||
print datetime.date.today()
|
print datetime.date.today()
|
||||||
else:
|
|
||||||
print 'Querying servers, this may take some time...'
|
|
||||||
for ln in fl.readlines():
|
for ln in fl.readlines():
|
||||||
splitted = ln.split('=')
|
splitted = ln.split('=')
|
||||||
if splitted[0].strip() != 'Server':
|
if splitted[0].strip() != 'Server':
|
||||||
if options.formatted:
|
if not options.times:
|
||||||
print ln,
|
print ln,
|
||||||
continue
|
continue
|
||||||
|
|
||||||
serverUrl = splitted[1].strip()
|
serverUrl = splitted[1].strip()
|
||||||
if serverUrl[-1] == '\n': serverUrl = serverUrl[0:-1]
|
if serverUrl[-1] == '\n': serverUrl = serverUrl[0:-1]
|
||||||
if options.verbose and options.formatted:
|
if options.verbose and options.times:
|
||||||
print '#',serverUrl,'...',
|
|
||||||
elif options.verbose:
|
|
||||||
print serverUrl,'...',
|
print serverUrl,'...',
|
||||||
elif not options.formatted:
|
elif options.verbose:
|
||||||
|
print '#',serverUrl,'...',
|
||||||
|
elif options.times:
|
||||||
print ' * ',
|
print ' * ',
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
serverToTime[serverUrl] = timeCmd(getFuncToTime(serverUrl))
|
serverToTime[serverUrl] = timeCmd(getFuncToTime(serverUrl))
|
||||||
if options.verbose:
|
if options.verbose:
|
||||||
print serverToTime[serverUrl]
|
try:
|
||||||
|
print "%.2f" % serverToTime[serverUrl]
|
||||||
|
except:
|
||||||
|
print serverToTime[serverUrl]
|
||||||
|
|
||||||
items = serverToTime.items()
|
items = serverToTime.items()
|
||||||
items.sort(cmpPairBySecond)
|
items.sort(cmpPairBySecond)
|
||||||
numToShow = int(options.num)
|
numToShow = int(options.num)
|
||||||
if numToShow == 0: numToShow = len(items)
|
if numToShow == 0: numToShow = len(items)
|
||||||
if len(items) > 0:
|
if len(items) > 0:
|
||||||
if options.formatted:
|
if options.times:
|
||||||
|
print
|
||||||
|
print ' Servers sorted by time (seconds):'
|
||||||
|
for i in items[0:numToShow]:
|
||||||
|
try:
|
||||||
|
print "%.2f" % i[1], ':', i[0]
|
||||||
|
except:
|
||||||
|
print i[1], ':', i[0]
|
||||||
|
else:
|
||||||
for i in items[0:numToShow]:
|
for i in items[0:numToShow]:
|
||||||
print 'Server =', i[0]
|
print 'Server =', i[0]
|
||||||
else:
|
|
||||||
print
|
|
||||||
print ' Servers sorted by time:'
|
|
||||||
for i in items[0:numToShow]:
|
|
||||||
print i[1], ':', i[0]
|
|
||||||
|
|
||||||
# vim: set ts=4 sw=4 et:
|
# vim: set ts=4 sw=4 et:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user