alpm.h: document transaction flags
[pacman-ng.git] / scripts / rankmirrors.sh.in
blob64d5a73c79459f8f83630fd8979c975518ff147a
1 #!@BASH_SHELL@
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' sortedarray=( $(LC_COLLATE=C printf "%s\n" "${timesarray[@]}" | sort) )
97 # Final output for mirrorfile
98 numiterator="0"
99 if [[ $TIMESONLY ]]; then
100 echo
101 echo " Servers sorted by time (seconds):"
102 for line in "${sortedarray[@]}"; do
103 echo "${line#* } : ${line% *}"
104 ((numiterator++))
105 (( NUM && numiterator >= NUM )) && break
106 done
107 else
108 for line in "${sortedarray[@]}"; do
109 echo "Server = ${line#* }"
110 ((numiterator++))
111 (( NUM && numiterator >= NUM )) && break
112 done
114 exit 0
118 # Argument parsing
119 [[ $1 ]] || usage
120 while [[ $1 ]]; do
121 if [[ ${1:0:2} = -- ]]; then
122 case "${1:2}" in
123 help) usage ;;
124 version) version ;;
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."
130 esac
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) )
136 STDIN=1
137 shift
138 else
139 snum=1
140 for ((i=1 ; i<${#1}; i++)); do
141 case ${1:$i:1} in
142 h) usage ;;
143 t) TIMESONLY=1 ;;
144 v) VERBOSE=1 ;;
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." ;;
149 esac
150 done
151 shift $snum
153 elif [[ -f $1 ]]; then
154 FILE="1"
155 IFS=$'\n' linearray=( $(<$1) )
156 [[ $linearray ]] || err "File is empty."
157 shift
158 else
159 err "\`$1' does not exist."
161 done
163 # Some sanity checks
164 [[ $NUM ]] || NUM=0
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")
174 echo "$URL : $time"
175 exit 0
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)"
185 timesarray=()
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
192 server="${line#*= }"
193 server="${server%%#*}"
194 url="$(getfetchurl "$server")"
195 [[ $url = fail ]] && err "url \`$URL' is malformed."
196 time=$(gettime "$url")
197 timesarray+=("$time $server")
199 # Output
200 if [[ $VERBOSE && $TIMESONLY ]]; then
201 echo "$server ... $time"
202 elif [[ $VERBOSE ]]; then
203 echo "# $server ... $time"
204 elif [[ $TIMESONLY ]]; then
205 echo -n " *"
208 done
209 finaloutput
211 # vim: set ts=2 sw=2 noet: