Fix int/size_t type in alpm_list_count() call
[pacman-ng.git] / scripts / rankmirrors.sh.in
blob875a14395c02b23619dc5942573574bffc6c64cc
1 #!/bin/bash
3 # rankmirrors - read a list of mirrors from a file and rank them by speed
4 # @configure_input@
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
22 trap finaloutput INT
24 usage() {
25 echo "Usage: rankmirrors [options] MIRRORFILE | URL"
26 echo
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"
29 echo "provided."
30 echo
31 echo "Options:"
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"
39 exit 0
42 version() {
43 echo "rankmirrors (pacman) @PACKAGE_VERSION@"
44 echo "Copyright (c) 2009 Matthew Bruenig <matthewbruenig@gmail.com>."
45 echo
46 echo "This is free software; see the source for copying conditions."
47 echo "There is NO WARRANTY, to the extent permitted by law."
48 exit 0
51 err() {
52 echo "$1" >&2
53 exit 1
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
58 gettime() {
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
62 echo "${output[0]}"
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)
69 ARCH="$(uname -m)"
70 getfetchurl() {
71 local strippedurl="${1%/}"
73 local replacedurl="${strippedurl//'$arch'/$ARCH}"
74 if [[ ! $TARGETREPO ]]; then
75 replacedurl="${replacedurl//'$repo'/core}"
76 local tmp="${replacedurl%/*}"
77 tmp="${tmp%/*}"
79 local reponame="${tmp##*/}"
80 else
81 replacedurl="${replacedurl//'$repo'/$TARGETREPO}"
82 local reponame="$TARGETREPO"
85 if [[ -z $reponame || $reponame = $replacedurl ]]; then
86 echo "fail"
87 else
88 local fetchurl="${replacedurl}/$reponame.db"
89 echo "$fetchurl"
93 # This exists to remove the need for a separate interrupt function
94 finaloutput() {
95 IFS=$'\n' read -r -d '' -a sortedarray < \
96 <(printf '%s\n' "${timesarray[@]}" | LC_COLLATE=C sort)
98 # Final output for mirrorfile
99 numiterator="0"
100 if [[ $TIMESONLY ]]; then
101 echo
102 echo " Servers sorted by time (seconds):"
103 for line in "${sortedarray[@]}"; do
104 echo "${line#* } : ${line% *}"
105 ((numiterator++))
106 (( NUM && numiterator >= NUM )) && break
107 done
108 else
109 for line in "${sortedarray[@]}"; do
110 echo "Server = ${line#* }"
111 ((numiterator++))
112 (( NUM && numiterator >= NUM )) && break
113 done
115 exit 0
119 # Argument parsing
120 [[ $1 ]] || usage
121 while [[ $1 ]]; do
122 if [[ ${1:0:2} = -- ]]; then
123 case "${1:2}" in
124 help) usage ;;
125 version) version ;;
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."
131 esac
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) )
137 STDIN=1
138 shift
139 else
140 snum=1
141 for ((i=1 ; i<${#1}; i++)); do
142 case ${1:$i:1} in
143 h) usage ;;
144 t) TIMESONLY=1 ;;
145 v) VERBOSE=1 ;;
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." ;;
150 esac
151 done
152 shift $snum
154 elif [[ -f $1 ]]; then
155 FILE="1"
156 IFS=$'\n' linearray=( $(<$1) )
157 [[ $linearray ]] || err "File is empty."
158 shift
159 else
160 err "\`$1' does not exist."
162 done
164 # Some sanity checks
165 [[ $NUM ]] || NUM=0
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")
175 echo "$URL : $time"
176 exit 0
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)"
186 timesarray=()
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
193 server="${line#*= }"
194 server="${server%%#*}"
195 url="$(getfetchurl "$server")"
196 [[ $url = fail ]] && err "url \`$URL' is malformed."
197 time=$(gettime "$url")
198 timesarray+=("$time $server")
200 # Output
201 if [[ $VERBOSE && $TIMESONLY ]]; then
202 echo "$server ... $time"
203 elif [[ $VERBOSE ]]; then
204 echo "# $server ... $time"
205 elif [[ $TIMESONLY ]]; then
206 echo -n " *"
209 done
210 finaloutput
212 # vim: set ts=2 sw=2 noet: