Author Topic: [Haskell] mirrorrank  (Read 4511 times)

0 Members and 1 Guest are viewing this topic.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
[Haskell] mirrorrank
« on: 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 (under "Sorting mirrors").

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

Code: (Haskell) [Select]
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

Code: [Select]
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')
« Last Edit: May 06, 2012, 10:01:20 am by Deque »