2011-06-30 11:07:54 -04:00
|
|
|
#!/bin/bash
|
2009-08-09 14:27:40 -04:00
|
|
|
#
|
|
|
|
# rankmirrors - read a list of mirrors from a file and rank them by speed
|
|
|
|
# @configure_input@
|
|
|
|
#
|
|
|
|
# Copyright (c) 2009 Matthew Bruenig <matthewbruenig@gmail.com>
|
|
|
|
#
|
|
|
|
# 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 3 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
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# traps interrupt key to spit out pre-interrupt info
|
|
|
|
trap finaloutput INT
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
echo "Usage: rankmirrors [options] MIRRORFILE | URL"
|
|
|
|
echo
|
|
|
|
echo "Ranks pacman mirrors by their connection and opening speed. Pacman mirror"
|
2010-10-11 19:23:16 -04:00
|
|
|
echo "files are located in @sysconfdir@/pacman.d/. It can also rank one mirror if the URL is"
|
2009-08-09 14:27:40 -04:00
|
|
|
echo "provided."
|
|
|
|
echo
|
|
|
|
echo "Options:"
|
|
|
|
echo " --version show program's version number and exit"
|
|
|
|
echo " -h, --help show this help message and exit"
|
|
|
|
echo " -n NUM number of servers to output, 0 for all"
|
|
|
|
echo " -t, --times only output mirrors and their response times"
|
|
|
|
echo " -u, --url test a specific url"
|
|
|
|
echo " -v, --verbose be verbose in ouptut"
|
2010-03-20 15:03:45 -04:00
|
|
|
echo " -r, --repo specify a specific repo name instead of guessing"
|
2009-08-09 14:27:40 -04:00
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
|
|
|
version() {
|
|
|
|
echo "rankmirrors (pacman) @PACKAGE_VERSION@"
|
|
|
|
echo "Copyright (c) 2009 Matthew Bruenig <matthewbruenig@gmail.com>."
|
|
|
|
echo
|
|
|
|
echo "This is free software; see the source for copying conditions."
|
|
|
|
echo "There is NO WARRANTY, to the extent permitted by law."
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
|
|
|
err() {
|
2010-06-22 22:42:49 -04:00
|
|
|
echo "$1" >&2
|
2009-08-09 14:27:40 -04:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
# gettime fetchurl (e.g gettime http://foo.com/core/os/i686/core.db.tar.gz)
|
|
|
|
# returns the fetching time, or timeout, or unreachable
|
|
|
|
gettime() {
|
|
|
|
IFS=' ' output=( $(curl -s -m 10 -w "%{time_total} %{http_code}" "$1" -o/dev/null) )
|
2009-11-05 10:55:48 -05:00
|
|
|
(( $? == 28 )) && echo timeout && return
|
|
|
|
(( ${output[1]} >= 400 || ! ${output[1]} )) && echo unreachable && return
|
2009-08-09 14:27:40 -04:00
|
|
|
echo "${output[0]}"
|
|
|
|
}
|
|
|
|
|
|
|
|
# getfetchurl serverurl (e.g. getturl http://foo.com/core/os/i686)
|
|
|
|
# if $repo is in the line, then assumes core
|
|
|
|
# if $arch is in the line, then assumes $(uname -m)
|
|
|
|
# returns a fetchurl (e.g. http://foo.com/core/os/i686/core.db.tar.gz)
|
|
|
|
ARCH="$(uname -m)"
|
|
|
|
getfetchurl() {
|
|
|
|
local strippedurl="${1%/}"
|
|
|
|
|
2010-06-22 22:42:51 -04:00
|
|
|
local replacedurl="${strippedurl//'$arch'/$ARCH}"
|
2010-03-20 15:03:45 -04:00
|
|
|
if [[ ! $TARGETREPO ]]; then
|
2010-06-22 22:42:51 -04:00
|
|
|
replacedurl="${replacedurl//'$repo'/core}"
|
2010-03-20 15:03:45 -04:00
|
|
|
local tmp="${replacedurl%/*}"
|
|
|
|
tmp="${tmp%/*}"
|
2009-08-09 14:27:40 -04:00
|
|
|
|
2010-03-20 15:03:45 -04:00
|
|
|
local reponame="${tmp##*/}"
|
|
|
|
else
|
2010-06-22 22:42:51 -04:00
|
|
|
replacedurl="${replacedurl//'$repo'/$TARGETREPO}"
|
2010-03-20 15:03:45 -04:00
|
|
|
local reponame="$TARGETREPO"
|
|
|
|
fi
|
2009-08-09 14:27:40 -04:00
|
|
|
|
|
|
|
if [[ -z $reponame || $reponame = $replacedurl ]]; then
|
|
|
|
echo "fail"
|
|
|
|
else
|
2010-06-03 10:10:50 -04:00
|
|
|
local fetchurl="${replacedurl}/$reponame.db"
|
2009-08-09 14:27:40 -04:00
|
|
|
echo "$fetchurl"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# This exists to remove the need for a separate interrupt function
|
|
|
|
finaloutput() {
|
2011-09-03 22:23:21 -04:00
|
|
|
IFS=$'\n' read -r -d '' -a sortedarray < \
|
|
|
|
<(printf '%s\n' "${timesarray[@]}" | LC_COLLATE=C sort)
|
2009-08-09 14:27:40 -04:00
|
|
|
|
|
|
|
# Final output for mirrorfile
|
|
|
|
numiterator="0"
|
|
|
|
if [[ $TIMESONLY ]]; then
|
|
|
|
echo
|
|
|
|
echo " Servers sorted by time (seconds):"
|
|
|
|
for line in "${sortedarray[@]}"; do
|
|
|
|
echo "${line#* } : ${line% *}"
|
|
|
|
((numiterator++))
|
2009-11-05 10:55:48 -05:00
|
|
|
(( NUM && numiterator >= NUM )) && break
|
2009-08-09 14:27:40 -04:00
|
|
|
done
|
|
|
|
else
|
|
|
|
for line in "${sortedarray[@]}"; do
|
|
|
|
echo "Server = ${line#* }"
|
|
|
|
((numiterator++))
|
2009-11-05 10:55:48 -05:00
|
|
|
(( NUM && numiterator >= NUM )) && break
|
2009-08-09 14:27:40 -04:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Argument parsing
|
|
|
|
[[ $1 ]] || usage
|
|
|
|
while [[ $1 ]]; do
|
2009-11-05 10:55:48 -05:00
|
|
|
if [[ ${1:0:2} = -- ]]; then
|
2009-08-09 14:27:40 -04:00
|
|
|
case "${1:2}" in
|
|
|
|
help) usage ;;
|
|
|
|
version) version ;;
|
|
|
|
times) TIMESONLY=1 ; shift ;;
|
|
|
|
verbose) VERBOSE=1 ; shift ;;
|
|
|
|
url) CHECKURL=1; [[ $2 ]] || err "Must specify url."; URL="$2"; shift 2;;
|
2010-03-20 15:03:45 -04:00
|
|
|
repo) [[ $2 ]] || err "Must specify repo name."; TARGETREPO="$2"; shift 2;;
|
2013-09-05 14:46:29 -04:00
|
|
|
*) err "'$1' is an invalid argument."
|
2009-08-09 14:27:40 -04:00
|
|
|
esac
|
|
|
|
elif [[ ${1:0:1} = - ]]; then
|
|
|
|
|
|
|
|
if [[ ! ${1:1:1} ]]; then
|
|
|
|
[[ -t 0 ]] && err "Stdin is empty."
|
|
|
|
IFS=$'\n' linearray=( $(</dev/stdin) )
|
|
|
|
STDIN=1
|
|
|
|
shift
|
|
|
|
else
|
|
|
|
snum=1
|
|
|
|
for ((i=1 ; i<${#1}; i++)); do
|
|
|
|
case ${1:$i:1} in
|
|
|
|
h) usage ;;
|
|
|
|
t) TIMESONLY=1 ;;
|
|
|
|
v) VERBOSE=1 ;;
|
|
|
|
u) CHECKURL=1; [[ $2 ]] || err "Must specify url."; URL="$2"; snum=2;;
|
2010-03-20 15:03:45 -04:00
|
|
|
r) [[ $2 ]] || err "Must specify repo name."; TARGETREPO="$2"; snum=2;;
|
2009-08-09 14:27:40 -04:00
|
|
|
n) [[ $2 ]] || err "Must specify number." ; NUM="$2" ; snum=2;;
|
2013-09-05 14:46:29 -04:00
|
|
|
*) err "'$1' is an invalid argument." ;;
|
2009-08-09 14:27:40 -04:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $snum
|
|
|
|
fi
|
2009-11-05 10:55:48 -05:00
|
|
|
elif [[ -f $1 ]]; then
|
2009-08-09 14:27:40 -04:00
|
|
|
FILE="1"
|
|
|
|
IFS=$'\n' linearray=( $(<$1) )
|
|
|
|
[[ $linearray ]] || err "File is empty."
|
|
|
|
shift
|
|
|
|
else
|
2013-09-05 14:46:29 -04:00
|
|
|
err "'$1' does not exist."
|
2009-08-09 14:27:40 -04:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
# Some sanity checks
|
|
|
|
[[ $NUM ]] || NUM=0
|
|
|
|
[[ $FILE && $CHECKURL ]] && err "Cannot specify a url and mirrorfile."
|
|
|
|
[[ $FILE || $CHECKURL || $STDIN ]] || err "Must specify url, mirrorfile, or stdin."
|
|
|
|
|
|
|
|
# Single url handling
|
|
|
|
if [[ $CHECKURL ]]; then
|
|
|
|
url="$(getfetchurl "$URL")"
|
2013-09-05 14:46:29 -04:00
|
|
|
[[ $url = fail ]] && err "url '$URL' is malformed."
|
2009-08-09 14:27:40 -04:00
|
|
|
[[ $VERBOSE ]] && echo "Testing $url..."
|
|
|
|
time=$(gettime "$url")
|
|
|
|
echo "$URL : $time"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Get url results from mirrorfile, fill up the array, and so on
|
|
|
|
if [[ $TIMESONLY ]]; then
|
|
|
|
echo "Querying servers, this may take some time..."
|
|
|
|
elif [[ $FILE ]]; then
|
|
|
|
echo "# Server list generated by rankmirrors on $(date +%Y-%m-%d)"
|
|
|
|
fi
|
|
|
|
|
|
|
|
timesarray=()
|
|
|
|
for line in "${linearray[@]}"; do
|
2010-06-22 22:42:50 -04:00
|
|
|
if [[ $line =~ ^[[:space:]]*# ]]; then
|
2009-08-09 14:27:40 -04:00
|
|
|
[[ $TIMESONLY ]] || echo $line
|
2010-06-22 22:42:50 -04:00
|
|
|
elif [[ $line =~ ^[[:space:]]*Server ]]; then
|
2009-08-09 14:27:40 -04:00
|
|
|
|
|
|
|
# Getting values and times and such
|
|
|
|
server="${line#*= }"
|
2010-03-20 15:03:45 -04:00
|
|
|
server="${server%%#*}"
|
2009-08-09 14:27:40 -04:00
|
|
|
url="$(getfetchurl "$server")"
|
2013-09-05 14:46:29 -04:00
|
|
|
[[ $url = fail ]] && err "url '$URL' is malformed."
|
2009-08-09 14:27:40 -04:00
|
|
|
time=$(gettime "$url")
|
|
|
|
timesarray+=("$time $server")
|
|
|
|
|
|
|
|
# Output
|
|
|
|
if [[ $VERBOSE && $TIMESONLY ]]; then
|
|
|
|
echo "$server ... $time"
|
|
|
|
elif [[ $VERBOSE ]]; then
|
|
|
|
echo "# $server ... $time"
|
|
|
|
elif [[ $TIMESONLY ]]; then
|
|
|
|
echo -n " *"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
finaloutput
|
|
|
|
|
|
|
|
# vim: set ts=2 sw=2 noet:
|