handleStep :: Bool -> Int -> Int -> SimState SimRepo -> Annex (SimState SimRepo)
handleStep muststabilize startn n st
- | n > 0 = do
+ | n >= 0 = do
let (st', actions) = getactions unsyncactions st
(st'', restactions) <- runoneaction actions st'
if null restactions
| otherwise = checkstabalized st
where
runrest actions st' n'
- | n' > 0 = do
+ | n' >= 0 = do
(st'', restactions) <- runoneaction actions st'
if null restactions
then handleStep muststabilize startn n' st'
paramCommand (withParams seek)
seek :: CmdParams -> CommandSeek
-seek ("start":[]) = start Nothing
-seek ("start":simfile:[]) = start (Just simfile)
-seek ("end":[]) = do
- simdir <- fromRawFilePath <$> fromRepo gitAnnexSimDir
- whenM (liftIO $ doesDirectoryExist simdir) $ do
- liftIO $ removeDirectoryRecursive simdir
- showLongNote $ UnquotedString "Sim ended."
+seek ("start":[]) = startsim Nothing
+seek ("start":simfile:[]) = startsim (Just simfile)
+seek ("end":[]) = endsim
seek ("show":[]) = do
simdir <- fromRepo gitAnnexSimDir
liftIO (restoreSim simdir) >>= \case
Left err -> giveup err
- Right st -> liftIO $ putStr $ generateSimFile $
- reverse $ simHistory st
+ Right st -> showsim st
+seek ("run":simfile:[]) = startsim' (Just simfile) >>= cleanup
+ where
+ cleanup st = do
+ endsim
+ when (simFailed st) $ do
+ showsim st
+ giveup "Simulation shown above had errors."
seek ps = case parseSimCommand ps of
Left err -> giveup err
Right simcmd -> do
when (simFailed st' && not (simFailed st)) $
giveup "Simulation had errors."
-start :: Maybe FilePath -> CommandSeek
-start simfile = do
+startsim :: Maybe FilePath -> CommandSeek
+startsim simfile = startsim' simfile >>= cleanup
+ where
+ cleanup st = do
+ liftIO $ suspendSim st
+ when (simFailed st) $
+ giveup "Simulation had errors."
+
+startsim' :: Maybe FilePath -> Annex (SimState SimRepo)
+startsim' simfile = do
simdir <- fromRawFilePath <$> fromRepo gitAnnexSimDir
whenM (liftIO $ doesDirectoryExist simdir) $
giveup "A sim was previously started. Use `git-annex sim end` to stop it before starting a new one."
repobyname <- mkGetExistingRepoByName
createAnnexDirectory (toRawFilePath simdir)
let st' = recordSeed st cs
- st'' <- go st' repobyname cs
- liftIO $ suspendSim st''
- when (simFailed st'') $
- giveup "Simulation had errors."
+ go st' repobyname cs
go st _ [] = return st
go st repobyname (c:cs) = do
st' <- runSimCommand c repobyname st
go st' repobyname cs
+
+endsim :: CommandSeek
+endsim = do
+ simdir <- fromRawFilePath <$> fromRepo gitAnnexSimDir
+ whenM (liftIO $ doesDirectoryExist simdir) $ do
+ liftIO $ removeDirectoryRecursive simdir
+ showLongNote $ UnquotedString "Sim ended."
+
+showsim :: SimState SimRepo -> Annex ()
+showsim = liftIO . putStr . generateSimFile . reverse . simHistory
git annex sim end
+git annex sim run my.sim
+
# DESCRIPTION
This command simulates the behavior of git-annex in a network of
Use "git annex sim end" to finish the simulation, and clean up.
+As a convenience, to run a sim from a file, and then stop it, use
+"git-annex sim run". If there is a problem running the sim, it will be
+shown before it is stopped.
+
# THE SIM FILE
This text file is used to configure the simulation and also to report on
--- /dev/null
+# This is a simulation of random preferred content expressions.
+# git-annex sim run this in a loop to explore if an expression can fail to
+# stabilize
+init foo
+init bar
+connect foo <-> bar
+addmulti 10 .x 100.0kB 10.0MB foo
+addmulti 10 .y 100.0kB 10.0MB foo
+randomwanted bar largerthan=1MB include=*.x anything present
+randomwanted foo largerthan=1MB include=*.x anything present
+# 40 is the maximum possible steps, in case bar wants to get all 20 files,
+# and foo wants to drop them all
+stepstable 40