2012-10-04 19:48:59 +00:00
|
|
|
{- git-annex preferred content matcher configuration
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Logs.PreferredContent (
|
add ConfigMonitor thread
Monitors git-annex branch for changes, which are noticed by the Merger
thread whenever the branch ref is changed (either due to an incoming push,
or a local change), and refreshes cached config values for modified config
files.
Rate limited to run no more often than once per minute. This is important
because frequent git-annex branch changes happen when files are being
added, or transferred, etc.
A primary use case is that, when preferred content changes are made,
and get pushed to remotes, the remotes start honoring those settings.
Other use cases include propigating repository description and trust
changes to remotes, and learning when a remote has added a new special
remote, so the webapp can present the GUI to enable that special remote
locally.
Also added a uuid.log cache. All other config files already had caches.
2012-10-20 20:37:06 +00:00
|
|
|
preferredContentLog,
|
2012-10-04 19:48:59 +00:00
|
|
|
preferredContentSet,
|
2012-10-08 17:16:53 +00:00
|
|
|
isPreferredContent,
|
2012-10-04 19:48:59 +00:00
|
|
|
preferredContentMap,
|
add ConfigMonitor thread
Monitors git-annex branch for changes, which are noticed by the Merger
thread whenever the branch ref is changed (either due to an incoming push,
or a local change), and refreshes cached config values for modified config
files.
Rate limited to run no more often than once per minute. This is important
because frequent git-annex branch changes happen when files are being
added, or transferred, etc.
A primary use case is that, when preferred content changes are made,
and get pushed to remotes, the remotes start honoring those settings.
Other use cases include propigating repository description and trust
changes to remotes, and learning when a remote has added a new special
remote, so the webapp can present the GUI to enable that special remote
locally.
Also added a uuid.log cache. All other config files already had caches.
2012-10-20 20:37:06 +00:00
|
|
|
preferredContentMapLoad,
|
2012-10-04 19:48:59 +00:00
|
|
|
preferredContentMapRaw,
|
|
|
|
checkPreferredContentExpression,
|
2012-10-10 19:35:10 +00:00
|
|
|
setStandardGroup,
|
2012-10-04 19:48:59 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import qualified Data.Map as M
|
2012-10-10 17:52:24 +00:00
|
|
|
import qualified Data.Set as S
|
2012-10-04 19:48:59 +00:00
|
|
|
import Data.Either
|
|
|
|
import Data.Time.Clock.POSIX
|
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import qualified Annex.Branch
|
|
|
|
import qualified Annex
|
|
|
|
import Logs.UUIDBased
|
2012-10-08 17:39:18 +00:00
|
|
|
import Limit
|
2012-10-04 19:48:59 +00:00
|
|
|
import qualified Utility.Matcher
|
2012-10-08 17:16:53 +00:00
|
|
|
import Annex.UUID
|
|
|
|
import Git.FilePath
|
2012-10-08 19:18:58 +00:00
|
|
|
import Types.Group
|
|
|
|
import Logs.Group
|
2012-10-10 20:04:28 +00:00
|
|
|
import Types.StandardGroups
|
2012-10-04 19:48:59 +00:00
|
|
|
|
|
|
|
{- Filename of preferred-content.log. -}
|
|
|
|
preferredContentLog :: FilePath
|
|
|
|
preferredContentLog = "preferred-content.log"
|
|
|
|
|
|
|
|
{- Changes the preferred content configuration of a remote. -}
|
|
|
|
preferredContentSet :: UUID -> String -> Annex ()
|
|
|
|
preferredContentSet uuid@(UUID _) val = do
|
|
|
|
ts <- liftIO getPOSIXTime
|
|
|
|
Annex.Branch.change preferredContentLog $
|
|
|
|
showLog id . changeLog ts uuid val . parseLog Just
|
|
|
|
Annex.changeState $ \s -> s { Annex.groupmap = Nothing }
|
|
|
|
preferredContentSet NoUUID _ = error "unknown UUID; cannot modify"
|
|
|
|
|
2012-10-08 17:16:53 +00:00
|
|
|
{- Checks if a file is preferred content for the specified repository
|
|
|
|
- (or the current repository if none is specified). -}
|
2012-10-17 20:01:09 +00:00
|
|
|
isPreferredContent :: Maybe UUID -> AssumeNotPresent -> FilePath -> Annex Bool
|
2012-10-08 17:16:53 +00:00
|
|
|
isPreferredContent mu notpresent file = do
|
2012-10-17 20:01:09 +00:00
|
|
|
matchfile <- getTopFilePath <$> inRepo (toTopFilePath file)
|
|
|
|
let fi = Annex.FileInfo
|
|
|
|
{ Annex.matchFile = matchfile
|
|
|
|
, Annex.relFile = file
|
|
|
|
}
|
2012-10-08 17:16:53 +00:00
|
|
|
u <- maybe getUUID return mu
|
|
|
|
m <- preferredContentMap
|
|
|
|
case M.lookup u m of
|
|
|
|
Nothing -> return True
|
2012-10-17 20:01:09 +00:00
|
|
|
Just matcher -> Utility.Matcher.matchMrun matcher $ \a ->
|
|
|
|
a notpresent fi
|
2012-10-08 17:16:53 +00:00
|
|
|
|
add ConfigMonitor thread
Monitors git-annex branch for changes, which are noticed by the Merger
thread whenever the branch ref is changed (either due to an incoming push,
or a local change), and refreshes cached config values for modified config
files.
Rate limited to run no more often than once per minute. This is important
because frequent git-annex branch changes happen when files are being
added, or transferred, etc.
A primary use case is that, when preferred content changes are made,
and get pushed to remotes, the remotes start honoring those settings.
Other use cases include propigating repository description and trust
changes to remotes, and learning when a remote has added a new special
remote, so the webapp can present the GUI to enable that special remote
locally.
Also added a uuid.log cache. All other config files already had caches.
2012-10-20 20:37:06 +00:00
|
|
|
{- The map is cached for speed. -}
|
2012-10-04 19:48:59 +00:00
|
|
|
preferredContentMap :: Annex Annex.PreferredContentMap
|
add ConfigMonitor thread
Monitors git-annex branch for changes, which are noticed by the Merger
thread whenever the branch ref is changed (either due to an incoming push,
or a local change), and refreshes cached config values for modified config
files.
Rate limited to run no more often than once per minute. This is important
because frequent git-annex branch changes happen when files are being
added, or transferred, etc.
A primary use case is that, when preferred content changes are made,
and get pushed to remotes, the remotes start honoring those settings.
Other use cases include propigating repository description and trust
changes to remotes, and learning when a remote has added a new special
remote, so the webapp can present the GUI to enable that special remote
locally.
Also added a uuid.log cache. All other config files already had caches.
2012-10-20 20:37:06 +00:00
|
|
|
preferredContentMap = maybe preferredContentMapLoad return
|
|
|
|
=<< Annex.getState Annex.preferredcontentmap
|
|
|
|
|
|
|
|
{- Loads the map, updating the cache. -}
|
|
|
|
preferredContentMapLoad :: Annex Annex.PreferredContentMap
|
|
|
|
preferredContentMapLoad = do
|
2012-10-08 19:18:58 +00:00
|
|
|
groupmap <- groupMap
|
add ConfigMonitor thread
Monitors git-annex branch for changes, which are noticed by the Merger
thread whenever the branch ref is changed (either due to an incoming push,
or a local change), and refreshes cached config values for modified config
files.
Rate limited to run no more often than once per minute. This is important
because frequent git-annex branch changes happen when files are being
added, or transferred, etc.
A primary use case is that, when preferred content changes are made,
and get pushed to remotes, the remotes start honoring those settings.
Other use cases include propigating repository description and trust
changes to remotes, and learning when a remote has added a new special
remote, so the webapp can present the GUI to enable that special remote
locally.
Also added a uuid.log cache. All other config files already had caches.
2012-10-20 20:37:06 +00:00
|
|
|
m <- simpleMap
|
|
|
|
. parseLogWithUUID ((Just .) . makeMatcher groupmap)
|
|
|
|
<$> Annex.Branch.get preferredContentLog
|
|
|
|
Annex.changeState $ \s -> s { Annex.preferredcontentmap = Just m }
|
|
|
|
return m
|
2012-10-04 19:48:59 +00:00
|
|
|
|
|
|
|
preferredContentMapRaw :: Annex (M.Map UUID String)
|
|
|
|
preferredContentMapRaw = simpleMap . parseLog Just
|
|
|
|
<$> Annex.Branch.get preferredContentLog
|
|
|
|
|
|
|
|
{- This intentionally never fails, even on unparsable expressions,
|
|
|
|
- because the configuration is shared amoung repositories and newer
|
|
|
|
- versions of git-annex may add new features. Instead, parse errors
|
|
|
|
- result in a Matcher that will always succeed. -}
|
2012-10-10 17:52:24 +00:00
|
|
|
makeMatcher :: GroupMap -> UUID -> String -> Utility.Matcher.Matcher MatchFiles
|
|
|
|
makeMatcher groupmap u s
|
|
|
|
| s == "standard" = standardMatcher groupmap u
|
|
|
|
| null (lefts tokens) = Utility.Matcher.generate $ rights tokens
|
|
|
|
| otherwise = matchAll
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
|
|
|
tokens = map (parseToken (Just u) groupmap) (tokenizeMatcher s)
|
2012-10-04 19:48:59 +00:00
|
|
|
|
2012-10-10 17:52:24 +00:00
|
|
|
{- Standard matchers are pre-defined for some groups. If none is defined,
|
|
|
|
- or a repository is in multiple groups with standard matchers, match all. -}
|
|
|
|
standardMatcher :: GroupMap -> UUID -> Utility.Matcher.Matcher MatchFiles
|
2012-10-10 20:23:41 +00:00
|
|
|
standardMatcher m u = maybe matchAll (makeMatcher m u . preferredContent) $
|
|
|
|
getStandardGroup =<< u `M.lookup` groupsByUUID m
|
2012-10-10 19:15:56 +00:00
|
|
|
|
|
|
|
matchAll :: Utility.Matcher.Matcher MatchFiles
|
|
|
|
matchAll = Utility.Matcher.generate []
|
2012-10-10 17:52:24 +00:00
|
|
|
|
2012-10-04 19:48:59 +00:00
|
|
|
{- Checks if an expression can be parsed, if not returns Just error -}
|
|
|
|
checkPreferredContentExpression :: String -> Maybe String
|
2012-10-10 17:52:24 +00:00
|
|
|
checkPreferredContentExpression s
|
|
|
|
| s == "standard" = Nothing
|
2012-10-19 20:09:21 +00:00
|
|
|
| otherwise = case lefts $ map (parseToken Nothing emptyGroupMap) (tokenizeMatcher s) of
|
2012-10-08 19:18:58 +00:00
|
|
|
[] -> Nothing
|
|
|
|
l -> Just $ unwords $ map ("Parse failure: " ++) l
|
2012-10-04 19:48:59 +00:00
|
|
|
|
2012-10-19 20:09:21 +00:00
|
|
|
parseToken :: (Maybe UUID) -> GroupMap -> String -> Either String (Utility.Matcher.Token MatchFiles)
|
|
|
|
parseToken mu groupmap t
|
2012-10-04 19:48:59 +00:00
|
|
|
| any (== t) Utility.Matcher.tokens = Right $ Utility.Matcher.token t
|
2012-10-19 20:09:21 +00:00
|
|
|
| t == "present" = use $ limitPresent mu
|
|
|
|
| otherwise = maybe (Left $ "near " ++ show t) use $ M.lookup k $
|
|
|
|
M.fromList
|
2012-10-04 19:48:59 +00:00
|
|
|
[ ("include", limitInclude)
|
|
|
|
, ("exclude", limitExclude)
|
|
|
|
, ("copies", limitCopies)
|
2012-10-08 19:18:58 +00:00
|
|
|
, ("inbackend", limitInBackend)
|
2012-10-08 17:39:18 +00:00
|
|
|
, ("largerthan", limitSize (>))
|
|
|
|
, ("smallerthan", limitSize (<))
|
2012-10-10 16:59:45 +00:00
|
|
|
, ("inallgroup", limitInAllGroup groupmap)
|
2012-10-04 19:48:59 +00:00
|
|
|
]
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
|
|
|
(k, v) = separate (== '=') t
|
|
|
|
use a = Utility.Matcher.Operation <$> a v
|
2012-10-04 19:48:59 +00:00
|
|
|
|
|
|
|
{- This is really dumb tokenization; there's no support for quoted values.
|
|
|
|
- Open and close parens are always treated as standalone tokens;
|
|
|
|
- otherwise tokens must be separated by whitespace. -}
|
|
|
|
tokenizeMatcher :: String -> [String]
|
|
|
|
tokenizeMatcher = filter (not . null ) . concatMap splitparens . words
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
|
|
|
splitparens = segmentDelim (`elem` "()")
|
2012-10-10 19:35:10 +00:00
|
|
|
|
|
|
|
{- Puts a UUID in a standard group, and sets its preferred content to use
|
|
|
|
- the standard expression for that group, unless something is already set. -}
|
|
|
|
setStandardGroup :: UUID -> StandardGroup -> Annex ()
|
|
|
|
setStandardGroup u g = do
|
|
|
|
groupSet u $ S.singleton $ fromStandardGroup g
|
|
|
|
m <- preferredContentMap
|
|
|
|
unless (isJust $ M.lookup u m) $
|
|
|
|
preferredContentSet u "standard"
|