EvilZone

Programming and Scripting => Other => : Deque May 06, 2012, 09:58:27 AM

: [Haskell] mirrorrank
: Deque May 06, 2012, 09:58:27 AM
I made a little script for Arch that takes a newly updated mirrorlist, removes all comments from the german servers and calls rankmirrors with this new list afterwards to sort the mirrors by speed. It just automates the process of doing this (https://wiki.archlinux.org/index.php/Mirrors) (under "Sorting mirrors").

I know that this is not much, I just thought it might be interesting.

: (Haskell)
import Data.List
import System.Cmd

main = do
    x <- readFile "/etc/pacman.d/mirrorlist.pacnew"
    writeFile "/etc/pacman.d/mirrorlist.backup" $ unlines (prepareList (lines x))
    system "rankmirrors -n 6 /etc/pacman.d/mirrorlist.backup > /etc/pacman.d/mirrorlist"

prepareList :: [String] -> [String]
prepareList xss = _prepareList xss "German" "#" "Server"

_prepareList :: Eq a => [[a]] -> [a] -> [a] -> [a] -> [[a]]
_prepareList xss start comment server = case xss of
    [] -> []
    xs : xss' -> if contains xs start
            then [xs] ++ (removeComments xss' comment server)
            else [xs] ++ (_prepareList xss' start comment server)

removeComments :: Eq a => [[a]] -> [a] -> [a] -> [[a]]
removeComments xss comment server = case xss of
    [] -> []
    xs : xss' -> if contains xs server
            then [drop 1 xs] ++ (removeComments xss' comment server)
            else xs : xss'

contains :: Eq a => [a] -> [a] -> Bool
contains xs [] = True
contains xs (y : ys') = case xs of
    [] -> False
    x : xs' -> if x == y
            then contains xs' ys'
            else contains xs' (y : ys')

Edit: Syntax highlighting makes it pretty hard to read. Here is the code without

:
import Data.List
import System.Cmd

main = do
    x <- readFile "/etc/pacman.d/mirrorlist.pacnew"
    writeFile "/etc/pacman.d/mirrorlist.backup" $ unlines (prepareList (lines x))
    system "rankmirrors -n 6 /etc/pacman.d/mirrorlist.backup > /etc/pacman.d/mirrorlist"

prepareList :: [String] -> [String]
prepareList xss = _prepareList xss "German" "#" "Server"

_prepareList :: Eq a => [[a]] -> [a] -> [a] -> [a] -> [[a]]
_prepareList xss start comment server = case xss of
    [] -> []
    xs : xss' -> if contains xs start
            then [xs] ++ (removeComments xss' comment server)
            else [xs] ++ (_prepareList xss' start comment server)

removeComments :: Eq a => [[a]] -> [a] -> [a] -> [[a]]
removeComments xss comment server = case xss of
    [] -> []
    xs : xss' -> if contains xs server
            then [drop 1 xs] ++ (removeComments xss' comment server)
            else xs : xss'

contains :: Eq a => [a] -> [a] -> Bool
contains xs [] = True
contains xs (y : ys') = case xs of
    [] -> False
    x : xs' -> if x == y
            then contains xs' ys'
            else contains xs' (y : ys')