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' sortedarray
=( $
(LC_COLLATE
=C
printf "%s\n" "${timesarray[@]}" |
sort) )
97 # Final output for mirrorfile
99 if [[ $TIMESONLY ]]; then
101 echo " Servers sorted by time (seconds):"
102 for line
in "${sortedarray[@]}"; do
103 echo "${line#* } : ${line% *}"
105 (( NUM
&& numiterator
>= NUM
)) && break
108 for line
in "${sortedarray[@]}"; do
109 echo "Server = ${line#* }"
111 (( NUM
&& numiterator
>= NUM
)) && break
121 if [[ ${1:0:2} = -- ]]; then
125 times) TIMESONLY
=1 ; shift ;;
126 verbose
) VERBOSE
=1 ; shift ;;
127 url
) CHECKURL
=1; [[ $2 ]] || err
"Must specify url."; URL
="$2"; shift 2;;
128 repo
) [[ $2 ]] || err
"Must specify repo name."; TARGETREPO
="$2"; shift 2;;
129 *) err
"\`$1' is an invalid argument."
131 elif [[ ${1:0:1} = - ]]; then
133 if [[ ! ${1:1:1} ]]; then
134 [[ -t 0 ]] && err
"Stdin is empty."
135 IFS
=$
'\n' linearray
=( $
(</dev
/stdin
) )
140 for ((i
=1 ; i
<${#1}; i
++)); do
145 u
) CHECKURL
=1; [[ $2 ]] || err
"Must specify url."; URL
="$2"; snum
=2;;
146 r
) [[ $2 ]] || err
"Must specify repo name."; TARGETREPO
="$2"; snum
=2;;
147 n
) [[ $2 ]] || err
"Must specify number." ; NUM
="$2" ; snum
=2;;
148 *) err
"\`-$1' is an invald argument." ;;
153 elif [[ -f $1 ]]; then
155 IFS
=$
'\n' linearray
=( $
(<$1) )
156 [[ $linearray ]] || err
"File is empty."
159 err
"\`$1' does not exist."
165 [[ $FILE && $CHECKURL ]] && err
"Cannot specify a url and mirrorfile."
166 [[ $FILE ||
$CHECKURL ||
$STDIN ]] || err
"Must specify url, mirrorfile, or stdin."
168 # Single url handling
169 if [[ $CHECKURL ]]; then
170 url
="$(getfetchurl "$URL")"
171 [[ $url = fail
]] && err
"url \`$URL' is malformed."
172 [[ $VERBOSE ]] && echo "Testing $url..."
173 time=$
(gettime
"$url")
178 # Get url results from mirrorfile, fill up the array, and so on
179 if [[ $TIMESONLY ]]; then
180 echo "Querying servers, this may take some time..."
181 elif [[ $FILE ]]; then
182 echo "# Server list generated by rankmirrors on $(date +%Y-%m-%d)"
186 for line
in "${linearray[@]}"; do
187 if [[ $line =~ ^
[[:space
:]]*# ]]; then
188 [[ $TIMESONLY ]] ||
echo $line
189 elif [[ $line =~ ^
[[:space
:]]*Server
]]; then
191 # Getting values and times and such
193 server
="${server%%#*}"
194 url
="$(getfetchurl "$server")"
195 [[ $url = fail
]] && err
"url \`$URL' is malformed."
196 time=$
(gettime
"$url")
197 timesarray
+=("$time $server")
200 if [[ $VERBOSE && $TIMESONLY ]]; then
201 echo "$server ... $time"
202 elif [[ $VERBOSE ]]; then
203 echo "# $server ... $time"
204 elif [[ $TIMESONLY ]]; then
211 # vim: set ts=2 sw=2 noet: