3 # rankmirrors - read a list of mirrors from a file and rank them by speed
6 # Copyright (c) 2009 Matthew Bruenig <matthewbruenig@gmail.com>
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 # traps interrupt key to spit out pre-interrupt info
25 echo "Usage: rankmirrors [options] MIRRORFILE | URL"
27 echo "Ranks pacman mirrors by their connection and opening speed. Pacman mirror"
28 echo "files are located in @sysconfdir@/pacman.d/. It can also rank one mirror if the URL is"
32 echo " --version show program's version number and exit"
33 echo " -h, --help show this help message and exit"
34 echo " -n NUM number of servers to output, 0 for all"
35 echo " -t, --times only output mirrors and their response times"
36 echo " -u, --url test a specific url"
37 echo " -v, --verbose be verbose in ouptut"
38 echo " -r, --repo specify a specific repo name instead of guessing"
43 echo "rankmirrors (pacman) @PACKAGE_VERSION@"
44 echo "Copyright (c) 2009 Matthew Bruenig <matthewbruenig@gmail.com>."
46 echo "This is free software; see the source for copying conditions."
47 echo "There is NO WARRANTY, to the extent permitted by law."
56 # gettime fetchurl (e.g gettime http://foo.com/core/os/i686/core.db.tar.gz)
57 # returns the fetching time, or timeout, or unreachable
59 IFS
=' ' output
=( $
(curl
-s -m 10 -w "%{time_total} %{http_code}" "$1" -o/dev
/null
) )
60 (( $?
== 28 )) && echo timeout
&& return
61 (( ${output[1]} >= 400 ||
! ${output[1]} )) && echo unreachable
&& return
65 # getfetchurl serverurl (e.g. getturl http://foo.com/core/os/i686)
66 # if $repo is in the line, then assumes core
67 # if $arch is in the line, then assumes $(uname -m)
68 # returns a fetchurl (e.g. http://foo.com/core/os/i686/core.db.tar.gz)
71 local strippedurl
="${1%/}"
73 local replacedurl
="${strippedurl//'$arch'/$ARCH}"
74 if [[ ! $TARGETREPO ]]; then
75 replacedurl
="${replacedurl//'$repo'/core}"
76 local tmp
="${replacedurl%/*}"
79 local reponame
="${tmp##*/}"
81 replacedurl
="${replacedurl//'$repo'/$TARGETREPO}"
82 local reponame
="$TARGETREPO"
85 if [[ -z $reponame ||
$reponame = $replacedurl ]]; then
88 local fetchurl
="${replacedurl}/$reponame.db"
93 # This exists to remove the need for a separate interrupt function
95 IFS
=$
'\n' read -r -d '' -a sortedarray
< \
96 <(printf '%s\n' "${timesarray[@]}" | LC_COLLATE
=C
sort)
98 # Final output for mirrorfile
100 if [[ $TIMESONLY ]]; then
102 echo " Servers sorted by time (seconds):"
103 for line
in "${sortedarray[@]}"; do
104 echo "${line#* } : ${line% *}"
106 (( NUM
&& numiterator
>= NUM
)) && break
109 for line
in "${sortedarray[@]}"; do
110 echo "Server = ${line#* }"
112 (( NUM
&& numiterator
>= NUM
)) && break
122 if [[ ${1:0:2} = -- ]]; then
126 times) TIMESONLY
=1 ; shift ;;
127 verbose
) VERBOSE
=1 ; shift ;;
128 url
) CHECKURL
=1; [[ $2 ]] || err
"Must specify url."; URL
="$2"; shift 2;;
129 repo
) [[ $2 ]] || err
"Must specify repo name."; TARGETREPO
="$2"; shift 2;;
130 *) err
"\`$1' is an invalid argument."
132 elif [[ ${1:0:1} = - ]]; then
134 if [[ ! ${1:1:1} ]]; then
135 [[ -t 0 ]] && err
"Stdin is empty."
136 IFS
=$
'\n' linearray
=( $
(</dev
/stdin
) )
141 for ((i
=1 ; i
<${#1}; i
++)); do
146 u
) CHECKURL
=1; [[ $2 ]] || err
"Must specify url."; URL
="$2"; snum
=2;;
147 r
) [[ $2 ]] || err
"Must specify repo name."; TARGETREPO
="$2"; snum
=2;;
148 n
) [[ $2 ]] || err
"Must specify number." ; NUM
="$2" ; snum
=2;;
149 *) err
"\`-$1' is an invald argument." ;;
154 elif [[ -f $1 ]]; then
156 IFS
=$
'\n' linearray
=( $
(<$1) )
157 [[ $linearray ]] || err
"File is empty."
160 err
"\`$1' does not exist."
166 [[ $FILE && $CHECKURL ]] && err
"Cannot specify a url and mirrorfile."
167 [[ $FILE ||
$CHECKURL ||
$STDIN ]] || err
"Must specify url, mirrorfile, or stdin."
169 # Single url handling
170 if [[ $CHECKURL ]]; then
171 url
="$(getfetchurl "$URL")"
172 [[ $url = fail
]] && err
"url \`$URL' is malformed."
173 [[ $VERBOSE ]] && echo "Testing $url..."
174 time=$
(gettime
"$url")
179 # Get url results from mirrorfile, fill up the array, and so on
180 if [[ $TIMESONLY ]]; then
181 echo "Querying servers, this may take some time..."
182 elif [[ $FILE ]]; then
183 echo "# Server list generated by rankmirrors on $(date +%Y-%m-%d)"
187 for line
in "${linearray[@]}"; do
188 if [[ $line =~ ^
[[:space
:]]*# ]]; then
189 [[ $TIMESONLY ]] ||
echo $line
190 elif [[ $line =~ ^
[[:space
:]]*Server
]]; then
192 # Getting values and times and such
194 server
="${server%%#*}"
195 url
="$(getfetchurl "$server")"
196 [[ $url = fail
]] && err
"url \`$URL' is malformed."
197 time=$
(gettime
"$url")
198 timesarray
+=("$time $server")
201 if [[ $VERBOSE && $TIMESONLY ]]; then
202 echo "$server ... $time"
203 elif [[ $VERBOSE ]]; then
204 echo "# $server ... $time"
205 elif [[ $TIMESONLY ]]; then
212 # vim: set ts=2 sw=2 noet: