Saturday, November 1, 2008

My new blog and a Pacman surprise

Hello there.

So now I've read many blogs out there. And I think it's time that I also own some blogs about what I'm doing with my spare time and whatnot :P So I came across and thought it would be a good idea to get my hands dirty and create one. So here we are.

First of all I want to tell you that pacman.d mirrorlist is sometimes really not ordered all that well :P So after waiting another round of minute for the VLC package to download completely, I figured it would be the best to just sit down and write this script:

trap "rm -f $tmpf; exit" INT TERM
gawk '
/#?Server =/ {
  host = gensub(/^[^:]+:\/\/([^/]+).*/, "\\1", 1, $3)
  print host, $0
}' /etc/pacman.d/mirrorlist |
while read host line; do
  echo "trying to ping '$host'" >/dev/stderr
  timed=$(ping -i 0.3 -c 1 -W 1 -n -q $host 2>/dev/null |
    tail -n1 |
    gawk '{ print $4 }' |
    cut -f2 -d/)
  [ x != x$timed ] && echo $timed ${line%#*};
done | LC_ALL=C sort -n | 
while read crap remains; do
  echo $remains \# $crap ms;
done > $tmpf

echo 'the following was created: '
cat $tmpf
echo -e '\nwant me to use it? (y|n)'
read reply
if [ x$reply = xy ]; then
  mv $tmpf /etc/pacman.d/mirrorlist
  rm -f $tmpf

What does it do? Well, it basicially finds out the currently best order for the mirrorlist file, so that the fastest mirror is at the top. After running it, I was really impressed about the speed of pacman. It rushed through like a tiger :P