2010-11-18 17:48:28 +00:00
|
|
|
{- Checks system configuration and generates SysConfig.hs. -}
|
2010-11-18 17:30:42 +00:00
|
|
|
|
|
|
|
import System.IO
|
|
|
|
import System.Cmd
|
|
|
|
import System.Exit
|
|
|
|
import System.Directory
|
|
|
|
|
2011-01-19 22:08:50 +00:00
|
|
|
type ConfigKey = String
|
|
|
|
data ConfigValue = BoolConfig Bool | StringConfig String
|
|
|
|
data Config = Config ConfigKey ConfigValue
|
|
|
|
|
|
|
|
type Test = IO Config
|
|
|
|
type TestName = String
|
|
|
|
data TestCase = TestCase TestName Test
|
2010-11-18 17:30:42 +00:00
|
|
|
|
2010-11-18 18:07:22 +00:00
|
|
|
instance Show Config where
|
2010-11-22 19:46:57 +00:00
|
|
|
show (Config key value) = unlines [
|
2011-01-19 22:08:50 +00:00
|
|
|
key ++ " :: " ++ valuetype value
|
|
|
|
, key ++ " = " ++ showvalue value
|
2010-11-18 18:07:22 +00:00
|
|
|
]
|
2011-01-19 22:08:50 +00:00
|
|
|
where
|
|
|
|
valuetype (BoolConfig _) = "Bool"
|
|
|
|
valuetype (StringConfig _) = "String"
|
|
|
|
showvalue (BoolConfig b) = show b
|
|
|
|
showvalue (StringConfig s) = show s
|
2010-11-18 18:07:22 +00:00
|
|
|
|
2010-11-27 21:31:20 +00:00
|
|
|
tests :: [TestCase]
|
2010-11-18 17:30:42 +00:00
|
|
|
tests = [
|
2011-01-19 22:08:50 +00:00
|
|
|
TestCase "cp -a" $ testCp "cp_a" "-a"
|
|
|
|
, TestCase "cp -p" $ testCp "cp_p" "-p"
|
|
|
|
, TestCase "cp --reflink=auto" $ testCp "cp_reflink_auto" "--reflink=auto"
|
|
|
|
, TestCase "uuid" $ selectCmd "uuid" ["uuid", "uuidgen"]
|
|
|
|
, TestCase "xargs -0" $ requireCmd "xargs_0" "xargs -0" "xargs -0 </dev/null"
|
|
|
|
, TestCase "rsync" $ requireCmd "rsync" "rsync" "rsync --version >/dev/null"
|
2010-11-18 17:30:42 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
tmpDir :: String
|
|
|
|
tmpDir = "tmp"
|
|
|
|
|
|
|
|
testFile :: String
|
|
|
|
testFile = tmpDir ++ "/testfile"
|
|
|
|
|
2011-01-19 22:08:50 +00:00
|
|
|
requireCmd :: ConfigKey -> String -> String -> Test
|
|
|
|
requireCmd k c cmdline = do
|
|
|
|
ret <- testCmd k cmdline
|
|
|
|
handle ret
|
|
|
|
where
|
|
|
|
handle r@(Config _ (BoolConfig True)) = return r
|
|
|
|
handle r = do
|
|
|
|
testEnd r
|
2010-12-02 20:55:21 +00:00
|
|
|
error $ "** the " ++ c ++ " command is required to use git-annex"
|
2010-11-18 17:30:42 +00:00
|
|
|
|
2011-01-19 22:08:50 +00:00
|
|
|
testCp :: ConfigKey -> String -> Test
|
|
|
|
testCp k option = testCmd k $
|
|
|
|
"cp " ++ option ++ " " ++ testFile ++ " " ++ testFile ++ ".new"
|
2010-11-18 17:30:42 +00:00
|
|
|
|
2011-01-19 22:08:50 +00:00
|
|
|
testCmd :: ConfigKey -> String -> Test
|
|
|
|
testCmd k c = do
|
|
|
|
ret <- system $ quiet c
|
|
|
|
return $ Config k (BoolConfig $ ret == ExitSuccess)
|
|
|
|
|
|
|
|
selectCmd :: ConfigKey -> [String] -> Test
|
|
|
|
selectCmd k cmds = search cmds
|
|
|
|
where
|
|
|
|
search [] = do
|
|
|
|
testEnd $ Config k (BoolConfig False)
|
|
|
|
error $ "* need one of these commands, but none are available: " ++ show cmds
|
|
|
|
search (c:cs) = do
|
|
|
|
ret <- system $ quiet c
|
|
|
|
if (ret == ExitSuccess)
|
|
|
|
then return $ Config k (StringConfig c)
|
|
|
|
else search cs
|
|
|
|
|
|
|
|
quiet :: String -> String
|
|
|
|
quiet s = s ++ " >/dev/null 2>&1"
|
2010-11-18 17:30:42 +00:00
|
|
|
|
2011-01-19 22:08:50 +00:00
|
|
|
testStart :: TestName -> IO ()
|
2010-11-18 17:30:42 +00:00
|
|
|
testStart s = do
|
|
|
|
putStr $ " checking " ++ s ++ "..."
|
|
|
|
hFlush stdout
|
|
|
|
|
2011-01-19 22:08:50 +00:00
|
|
|
testEnd :: Config -> IO ()
|
|
|
|
testEnd (Config _ (BoolConfig True)) = putStrLn $ " yes"
|
|
|
|
testEnd (Config _ (BoolConfig False)) = putStrLn $ " no"
|
|
|
|
testEnd (Config _ (StringConfig s)) = putStrLn $ " " ++ s
|
2010-11-18 17:30:42 +00:00
|
|
|
|
|
|
|
writeSysConfig :: [Config] -> IO ()
|
2010-11-18 18:11:18 +00:00
|
|
|
writeSysConfig config = writeFile "SysConfig.hs" body
|
2010-11-18 17:30:42 +00:00
|
|
|
where
|
2010-11-18 18:11:18 +00:00
|
|
|
body = unlines $ header ++ map show config ++ footer
|
2010-11-18 17:30:42 +00:00
|
|
|
header = [
|
|
|
|
"{- Automatically generated by configure. -}"
|
|
|
|
, "module SysConfig where"
|
2010-11-18 17:48:28 +00:00
|
|
|
, ""
|
2010-11-18 17:30:42 +00:00
|
|
|
]
|
|
|
|
footer = []
|
|
|
|
|
2010-11-27 21:31:20 +00:00
|
|
|
runTests :: [TestCase] -> IO [Config]
|
2010-11-18 17:30:42 +00:00
|
|
|
runTests [] = return []
|
2011-01-19 22:08:50 +00:00
|
|
|
runTests ((TestCase tname t):ts) = do
|
2010-11-18 17:30:42 +00:00
|
|
|
testStart tname
|
2011-01-19 22:08:50 +00:00
|
|
|
c <- t
|
|
|
|
testEnd c
|
2010-11-18 17:30:42 +00:00
|
|
|
rest <- runTests ts
|
2011-01-19 22:08:50 +00:00
|
|
|
return $ c:rest
|
2010-11-18 17:30:42 +00:00
|
|
|
|
2010-11-18 18:11:18 +00:00
|
|
|
setup :: IO ()
|
|
|
|
setup = do
|
2010-11-18 17:30:42 +00:00
|
|
|
createDirectoryIfMissing True tmpDir
|
|
|
|
writeFile testFile "test file contents"
|
2010-11-18 18:11:18 +00:00
|
|
|
|
|
|
|
cleanup :: IO ()
|
|
|
|
cleanup = do
|
2010-11-18 17:30:42 +00:00
|
|
|
removeDirectoryRecursive tmpDir
|
2010-11-18 18:11:18 +00:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
setup
|
|
|
|
config <- runTests tests
|
2010-11-18 17:30:42 +00:00
|
|
|
writeSysConfig config
|
2010-11-18 18:11:18 +00:00
|
|
|
cleanup
|