2024-09-11 15:53:25 +00:00
|
|
|
{- sim files
|
|
|
|
-
|
|
|
|
- Copyright 2024 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Annex.Sim.File where
|
|
|
|
|
|
|
|
import Annex.Sim
|
2024-09-11 19:59:13 +00:00
|
|
|
import Annex.Common hiding (group)
|
2024-09-11 15:53:25 +00:00
|
|
|
import Utility.DataUnits
|
2024-09-11 19:59:13 +00:00
|
|
|
import Types.TrustLevel
|
|
|
|
import Types.Group
|
|
|
|
import Git.Config (isTrueFalse)
|
2024-09-11 15:53:25 +00:00
|
|
|
|
|
|
|
import Data.Char
|
|
|
|
import Text.Read
|
|
|
|
|
|
|
|
parseSimFile :: String -> Either String [SimCommand]
|
|
|
|
parseSimFile = go [] . lines
|
|
|
|
where
|
2024-09-12 01:00:51 +00:00
|
|
|
go cs [] = Right (reverse cs)
|
|
|
|
go cs (l:ls) = case parseSimFileLine l of
|
|
|
|
Right command -> go (command:cs) ls
|
2024-09-11 15:53:25 +00:00
|
|
|
Left err -> Left err
|
|
|
|
|
2024-09-12 01:00:51 +00:00
|
|
|
parseSimFileLine :: String -> Either String SimCommand
|
2024-09-11 15:53:25 +00:00
|
|
|
parseSimFileLine s
|
2024-09-12 01:00:51 +00:00
|
|
|
| "#" `isPrefixOf` s = Right (CommandComment s)
|
|
|
|
| all isSpace s = Right (CommandBlank)
|
2024-09-11 19:59:13 +00:00
|
|
|
| otherwise = parseSimCommand (words s)
|
|
|
|
|
|
|
|
generateSimFile :: [SimCommand] -> String
|
|
|
|
generateSimFile = unlines . map unwords . go
|
|
|
|
where
|
|
|
|
go [] = []
|
|
|
|
go (CommandInit (RepoName name) : rest) =
|
|
|
|
["init", name] : go rest
|
|
|
|
go (CommandInitRemote (RepoName name) : rest) =
|
|
|
|
["initremote", name] : go rest
|
|
|
|
go (CommandUse (RepoName name) what : rest) =
|
|
|
|
["use", name, what] : go rest
|
2024-09-12 01:00:51 +00:00
|
|
|
go (CommandConnect c : rest) =
|
|
|
|
("connect":formatConnections c) : go rest
|
|
|
|
go (CommandDisconnect c : rest) =
|
|
|
|
("disconnect":formatConnections c) : go rest
|
2024-09-11 19:59:13 +00:00
|
|
|
go (CommandAddTree (RepoName name) expr : rest) =
|
|
|
|
["addtree", name, expr] : go rest
|
|
|
|
go (CommandAdd f sz repos : rest) =
|
|
|
|
(["add", fromRawFilePath f, showsize sz] ++ map fromRepoName repos) : go rest
|
2024-09-17 15:19:59 +00:00
|
|
|
go (CommandAddMulti n suffix minsz maxsz repos : rest) =
|
|
|
|
(["addmulti", show n, suffix, showsize minsz, showsize maxsz] ++ map fromRepoName repos) : go rest
|
2024-09-11 19:59:13 +00:00
|
|
|
go (CommandStep n : rest) =
|
|
|
|
["step", show n] : go rest
|
2024-09-17 13:33:55 +00:00
|
|
|
go (CommandAction act : rest) = formatAction act : go rest
|
2024-09-11 19:59:13 +00:00
|
|
|
go (CommandSeed n : rest) =
|
|
|
|
["seed", show n] : go rest
|
|
|
|
go (CommandPresent (RepoName repo) f : rest) =
|
|
|
|
["present", repo, fromRawFilePath f] : go rest
|
|
|
|
go (CommandNotPresent (RepoName repo) f : rest) =
|
|
|
|
["notpresent", repo, fromRawFilePath f] : go rest
|
|
|
|
go (CommandNumCopies n : rest) =
|
|
|
|
["numcopies", show n] : go rest
|
|
|
|
go (CommandMinCopies n : rest) =
|
|
|
|
["mincopies", show n] : go rest
|
|
|
|
go (CommandTrustLevel (RepoName repo) trustlevel : rest) =
|
|
|
|
["trustlevel", repo, showTrustLevel trustlevel] : go rest
|
|
|
|
go (CommandGroup (RepoName repo) group : rest) =
|
|
|
|
["group", repo, fromGroup group] : go rest
|
|
|
|
go (CommandUngroup (RepoName repo) group : rest) =
|
|
|
|
["ungroup", repo, fromGroup group] : go rest
|
|
|
|
go (CommandWanted (RepoName repo) expr : rest) =
|
|
|
|
["wanted", repo, expr] : go rest
|
|
|
|
go (CommandRequired (RepoName repo) expr : rest) =
|
|
|
|
["required", repo, expr] : go rest
|
|
|
|
go (CommandGroupWanted group expr : rest) =
|
|
|
|
["groupwanted", fromGroup group, expr] : go rest
|
|
|
|
go (CommandMaxSize (RepoName repo) maxsize : rest) =
|
|
|
|
["maxsize", repo, showsize (fromMaxSize maxsize)] : go rest
|
|
|
|
go (CommandRebalance b : rest) =
|
|
|
|
["rebalance", if b then "on" else "off"] : go rest
|
|
|
|
go (CommandComment s : rest) =
|
|
|
|
[s] : go rest
|
|
|
|
go (CommandBlank : rest) =
|
|
|
|
[""] : go rest
|
|
|
|
|
|
|
|
showsize = filter (not . isSpace) . preciseSize storageUnits True
|
|
|
|
|
2024-09-17 13:33:55 +00:00
|
|
|
formatAction :: SimAction -> [String]
|
|
|
|
formatAction (ActionPull (RepoName repo) (RemoteName remote)) =
|
|
|
|
["action", repo, "pull", remote]
|
|
|
|
formatAction (ActionPush (RepoName repo) (RemoteName remote)) =
|
|
|
|
["action", repo, "push", remote]
|
|
|
|
formatAction (ActionSync (RepoName repo) (RemoteName remote)) =
|
|
|
|
["action", repo, "sync", remote]
|
|
|
|
formatAction (ActionGetWanted (RepoName repo) (RemoteName remote)) =
|
|
|
|
["action", repo, "getwanted", remote]
|
|
|
|
formatAction (ActionDropUnwanted (RepoName repo) (Just (RemoteName remote))) =
|
2024-09-17 17:35:27 +00:00
|
|
|
["action", repo, "dropunwantedfrom", remote]
|
2024-09-17 13:33:55 +00:00
|
|
|
formatAction (ActionDropUnwanted (RepoName repo) Nothing) =
|
|
|
|
["action", repo, "dropunwanted"]
|
|
|
|
formatAction (ActionSendWanted (RepoName repo) (RemoteName remote)) =
|
|
|
|
["action", repo, "sendwanted", remote]
|
|
|
|
formatAction (ActionGitPush (RepoName repo) (RemoteName remote)) =
|
|
|
|
["action", repo, "gitpush", remote]
|
|
|
|
formatAction (ActionGitPull (RepoName repo) (RemoteName remote)) =
|
|
|
|
["action", repo, "gitpull", remote]
|
|
|
|
formatAction (ActionWhile a b) =
|
|
|
|
formatAction a ++ ["while"] ++ formatAction b
|
|
|
|
|
2024-09-12 01:00:51 +00:00
|
|
|
parseSimCommand :: [String] -> Either String SimCommand
|
2024-09-11 19:59:13 +00:00
|
|
|
parseSimCommand ("init":name:[]) =
|
2024-09-12 01:00:51 +00:00
|
|
|
Right $ CommandInit (RepoName name)
|
2024-09-11 19:59:13 +00:00
|
|
|
parseSimCommand ("initremote":name:[]) =
|
2024-09-12 01:00:51 +00:00
|
|
|
Right $ CommandInitRemote (RepoName name)
|
2024-09-11 19:59:13 +00:00
|
|
|
parseSimCommand ("use":name:rest) =
|
2024-09-12 01:00:51 +00:00
|
|
|
Right $ CommandUse (RepoName name) (unwords rest)
|
|
|
|
parseSimCommand ("connect":rest) =
|
|
|
|
CommandConnect <$> parseConnections rest
|
2024-09-11 19:59:13 +00:00
|
|
|
parseSimCommand ("disconnect":rest) =
|
2024-09-12 01:00:51 +00:00
|
|
|
CommandDisconnect <$> parseConnections rest
|
2024-09-11 19:59:13 +00:00
|
|
|
parseSimCommand ("addtree":name:rest) =
|
2024-09-12 01:00:51 +00:00
|
|
|
Right $ CommandAddTree(RepoName name) (unwords rest)
|
2024-09-11 19:59:13 +00:00
|
|
|
parseSimCommand ("add":filename:size:repos) =
|
|
|
|
case readSize dataUnits size of
|
2024-09-12 01:00:51 +00:00
|
|
|
Just sz -> Right $ CommandAdd
|
|
|
|
(toRawFilePath filename)
|
|
|
|
sz
|
|
|
|
(map RepoName repos)
|
2024-09-11 19:59:13 +00:00
|
|
|
Nothing -> Left $ "Unable to parse file size \"" ++ size ++ "\""
|
2024-09-17 15:19:59 +00:00
|
|
|
parseSimCommand ("addmulti":num:suffix:minsize:maxsize:repos) =
|
|
|
|
case readSize dataUnits minsize of
|
|
|
|
Just minsz -> case readSize dataUnits maxsize of
|
|
|
|
Just maxsz -> case readMaybe num of
|
|
|
|
Just n -> Right $ CommandAddMulti
|
|
|
|
n suffix minsz maxsz
|
|
|
|
(map RepoName repos)
|
|
|
|
Nothing -> Left $ "Unable to parse number \"" ++ num ++ "\""
|
|
|
|
Nothing -> Left $ "Unable to parse file size \"" ++ maxsize ++ "\""
|
|
|
|
Nothing -> Left $ "Unable to parse file size \"" ++ minsize ++ "\""
|
2024-09-11 19:59:13 +00:00
|
|
|
parseSimCommand ("step":n:[]) =
|
|
|
|
case readMaybe n of
|
2024-09-12 01:00:51 +00:00
|
|
|
Just n' -> Right $ CommandStep n'
|
2024-09-11 19:59:13 +00:00
|
|
|
Nothing -> Left $ "Unable to parse step value \"" ++ n ++ "\""
|
2024-09-17 13:33:55 +00:00
|
|
|
parseSimCommand l@("action":_) = case parseSimAction l of
|
|
|
|
Right act -> Right $ CommandAction act
|
|
|
|
Left err -> Left err
|
2024-09-11 19:59:13 +00:00
|
|
|
parseSimCommand ("seed":n:[]) =
|
|
|
|
case readMaybe n of
|
2024-09-12 01:00:51 +00:00
|
|
|
Just n' -> Right $ CommandSeed n'
|
2024-09-11 19:59:13 +00:00
|
|
|
Nothing -> Left $ "Unable to parse seed value \"" ++ n ++ "\""
|
|
|
|
parseSimCommand ("present":repo:file:[]) =
|
2024-09-12 01:00:51 +00:00
|
|
|
Right $ CommandPresent (RepoName repo) (toRawFilePath file)
|
2024-09-11 19:59:13 +00:00
|
|
|
parseSimCommand ("notpresent":repo:file:[]) =
|
2024-09-12 01:00:51 +00:00
|
|
|
Right $ CommandNotPresent (RepoName repo) (toRawFilePath file)
|
2024-09-11 19:59:13 +00:00
|
|
|
parseSimCommand ("numcopies":n:[]) =
|
|
|
|
case readMaybe n of
|
2024-09-12 01:00:51 +00:00
|
|
|
Just n' -> Right $ CommandNumCopies n'
|
2024-09-11 19:59:13 +00:00
|
|
|
Nothing -> Left $ "Unable to parse numcopies value \"" ++ n ++ "\""
|
|
|
|
parseSimCommand ("mincopies":n:[]) =
|
|
|
|
case readMaybe n of
|
2024-09-12 01:00:51 +00:00
|
|
|
Just n' -> Right $ CommandMinCopies n'
|
2024-09-11 19:59:13 +00:00
|
|
|
Nothing -> Left $ "Unable to parse mincopies value \"" ++ n ++ "\""
|
|
|
|
parseSimCommand ("trustlevel":repo:s:[]) =
|
|
|
|
case readTrustLevel s of
|
2024-09-12 01:00:51 +00:00
|
|
|
Just trustlevel -> Right $
|
|
|
|
CommandTrustLevel (RepoName repo) trustlevel
|
2024-09-11 19:59:13 +00:00
|
|
|
Nothing -> Left $ "Unknown trust level \"" ++ s ++ "\"."
|
|
|
|
parseSimCommand ("group":repo:group:[]) =
|
2024-09-12 01:00:51 +00:00
|
|
|
Right $ CommandGroup (RepoName repo) (toGroup group)
|
2024-09-11 19:59:13 +00:00
|
|
|
parseSimCommand ("ungroup":repo:group:[]) =
|
2024-09-12 01:00:51 +00:00
|
|
|
Right $ CommandUngroup (RepoName repo) (toGroup group)
|
2024-09-11 19:59:13 +00:00
|
|
|
parseSimCommand ("wanted":repo:expr) =
|
2024-09-12 01:00:51 +00:00
|
|
|
Right $ CommandWanted (RepoName repo) (unwords expr)
|
2024-09-11 19:59:13 +00:00
|
|
|
parseSimCommand ("required":repo:expr) =
|
2024-09-12 01:00:51 +00:00
|
|
|
Right $ CommandRequired (RepoName repo) (unwords expr)
|
2024-09-11 19:59:13 +00:00
|
|
|
parseSimCommand ("groupwanted":group:expr) =
|
2024-09-12 01:00:51 +00:00
|
|
|
Right $ CommandGroupWanted (toGroup group) (unwords expr)
|
2024-09-11 19:59:13 +00:00
|
|
|
parseSimCommand ("maxsize":repo:size:[]) =
|
|
|
|
case readSize dataUnits size of
|
2024-09-12 01:00:51 +00:00
|
|
|
Just sz -> Right $ CommandMaxSize (RepoName repo) (MaxSize sz)
|
2024-09-11 19:59:13 +00:00
|
|
|
Nothing -> Left $ "Unable to parse maxsize \"" ++ size ++ "\""
|
|
|
|
parseSimCommand ("rebalance":onoff:[]) = case isTrueFalse onoff of
|
2024-09-12 01:00:51 +00:00
|
|
|
Just b -> Right $ CommandRebalance b
|
2024-09-11 19:59:13 +00:00
|
|
|
Nothing -> Left $ "Unable to parse rebalance value \"" ++ onoff ++ "\""
|
2024-09-17 13:33:55 +00:00
|
|
|
parseSimCommand ws = parseError ws
|
|
|
|
|
|
|
|
parseSimAction :: [String] -> Either String SimAction
|
|
|
|
parseSimAction ("action":repo:"pull":remote:rest) =
|
|
|
|
mkAction rest $ ActionPull (RepoName repo) (RemoteName remote)
|
|
|
|
parseSimAction ("action":repo:"push":remote:rest) =
|
|
|
|
mkAction rest $ ActionPush (RepoName repo) (RemoteName remote)
|
|
|
|
parseSimAction ("action":repo:"sync":remote:rest) =
|
|
|
|
mkAction rest $ ActionSync (RepoName repo) (RemoteName remote)
|
|
|
|
parseSimAction ("action":repo:"getwanted":remote:rest) =
|
|
|
|
mkAction rest $ ActionGetWanted (RepoName repo) (RemoteName remote)
|
2024-09-17 15:40:40 +00:00
|
|
|
parseSimAction ("action":repo:"sendwanted":remote:rest) =
|
|
|
|
mkAction rest $ ActionSendWanted (RepoName repo) (RemoteName remote)
|
2024-09-17 17:35:27 +00:00
|
|
|
parseSimAction ("action":repo:"dropunwantedfrom":remote:rest) =
|
2024-09-17 13:33:55 +00:00
|
|
|
mkAction rest $ ActionDropUnwanted (RepoName repo)
|
|
|
|
(Just (RemoteName remote))
|
2024-09-17 17:35:27 +00:00
|
|
|
parseSimAction ("action":repo:"dropunwanted":rest) =
|
|
|
|
mkAction rest $ ActionDropUnwanted (RepoName repo) Nothing
|
2024-09-17 13:33:55 +00:00
|
|
|
parseSimAction ("action":repo:"gitpush":remote:rest) =
|
|
|
|
mkAction rest $ ActionGitPush (RepoName repo) (RemoteName remote)
|
|
|
|
parseSimAction ("action":repo:"gitpull":remote:rest) =
|
|
|
|
mkAction rest $ ActionGitPull (RepoName repo) (RemoteName remote)
|
|
|
|
parseSimAction ws = parseError ws
|
|
|
|
|
|
|
|
mkAction :: [String] -> SimAction -> Either String SimAction
|
|
|
|
mkAction [] a = Right a
|
|
|
|
mkAction ("while":rest) a = case parseSimAction rest of
|
|
|
|
Right b -> Right (ActionWhile a b)
|
|
|
|
Left err -> Left err
|
|
|
|
mkAction ws _ = parseError ws
|
|
|
|
|
|
|
|
parseError :: [String] -> Either String a
|
|
|
|
parseError ws = Left $ "Unable to parse sim command: \"" ++ unwords ws ++ "\""
|
2024-09-11 15:53:25 +00:00
|
|
|
|
2024-09-12 01:00:51 +00:00
|
|
|
parseConnections :: [String] -> Either String Connections
|
|
|
|
parseConnections = go . reverse
|
2024-09-11 15:53:25 +00:00
|
|
|
where
|
2024-09-12 01:00:51 +00:00
|
|
|
go (r2:"->":r1:rest) =
|
|
|
|
chain (RepoName r1 :-> RemoteName r2) rest
|
|
|
|
go (r2:"<-":r1:rest) =
|
|
|
|
chain (RemoteName r1 :<- RepoName r2) rest
|
|
|
|
go (r2:"<->":r1:rest) =
|
|
|
|
chain (RepoName r1 :<-> RepoName r2) rest
|
|
|
|
go rest = bad rest
|
|
|
|
|
|
|
|
chain c [] = Right c
|
|
|
|
chain c ("->":r:rest) = chain (RepoName r :=> c) rest
|
|
|
|
chain c ("<-":r:rest) = chain (RemoteName r :<= c) rest
|
|
|
|
chain c ("<->":r:rest) = chain (RepoName r :<=> c) rest
|
|
|
|
chain _ rest = bad rest
|
|
|
|
|
|
|
|
bad rest = Left $ "Bad connect syntax near \"" ++ unwords rest ++ "\""
|
2024-09-11 15:53:25 +00:00
|
|
|
|
2024-09-12 01:00:51 +00:00
|
|
|
formatConnections :: Connections -> [String]
|
|
|
|
formatConnections (RepoName repo :-> RemoteName remote) =
|
|
|
|
[repo, "->", remote]
|
|
|
|
formatConnections (RemoteName remote :<- RepoName repo) =
|
|
|
|
[remote, "<-", repo]
|
|
|
|
formatConnections (RepoName repo1 :<-> RepoName repo2) =
|
|
|
|
[repo1, "<->", repo2]
|
|
|
|
formatConnections (RepoName repo :=> c) =
|
|
|
|
repo : "->" : formatConnections c
|
|
|
|
formatConnections (RemoteName remote :<= c) =
|
|
|
|
remote : "<-" : formatConnections c
|
|
|
|
formatConnections (RepoName repo :<=> c) =
|
|
|
|
repo : "<->" : formatConnections c
|
2024-09-11 15:53:25 +00:00
|
|
|
|