2022-06-21 00:56:42 -04:00
|
|
|
{-# LANGUAGE GADTs #-}
|
|
|
|
{-# LANGUAGE TupleSections #-}
|
2021-11-21 22:47:43 -05:00
|
|
|
|
2021-11-07 13:35:08 -05:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Functions for handling dependencies
|
|
|
|
|
|
|
|
module XMonad.Internal.Dependency
|
2022-06-21 00:56:42 -04:00
|
|
|
( AlwaysX
|
|
|
|
, AlwaysIO
|
2022-06-22 01:28:46 -04:00
|
|
|
, Feature
|
2022-06-21 00:56:42 -04:00
|
|
|
, Always(..)
|
2022-06-22 01:28:46 -04:00
|
|
|
, TestedSometimes(..)
|
2022-06-21 00:56:42 -04:00
|
|
|
, SometimesX
|
|
|
|
, SometimesIO
|
|
|
|
, Sometimes
|
2022-06-22 01:28:46 -04:00
|
|
|
, ioSometimes
|
|
|
|
, ioAlways
|
|
|
|
, evalFeature
|
2022-06-21 00:56:42 -04:00
|
|
|
, executeSometimes
|
|
|
|
, executeAlways
|
|
|
|
, evalAlways
|
|
|
|
, evalSometimes
|
|
|
|
|
|
|
|
, Subfeature(..)
|
|
|
|
, LogLevel(..)
|
|
|
|
|
2021-11-21 23:32:10 -05:00
|
|
|
, Action(..)
|
2022-06-21 00:56:42 -04:00
|
|
|
|
|
|
|
-- feature construction
|
|
|
|
, sometimes1
|
|
|
|
, sometimesIO
|
|
|
|
, sometimesDBus
|
|
|
|
, sometimesExe
|
|
|
|
, sometimesExeArgs
|
|
|
|
, sometimesEndpoint
|
|
|
|
|
|
|
|
-- Dependency tree
|
|
|
|
, ActionTree(..)
|
|
|
|
, Tree(..)
|
|
|
|
, IODependency(..)
|
|
|
|
, DBusDependency(..)
|
2021-11-21 10:26:28 -05:00
|
|
|
, DBusMember(..)
|
2022-06-21 00:56:42 -04:00
|
|
|
, UnitType(..)
|
|
|
|
, listToAnds
|
|
|
|
, toAnd
|
2021-11-07 18:41:25 -05:00
|
|
|
, pathR
|
|
|
|
, pathRW
|
2022-06-21 00:56:42 -04:00
|
|
|
, pathW
|
2021-11-07 13:35:08 -05:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Control.Monad.IO.Class
|
2021-11-22 23:02:23 -05:00
|
|
|
import Control.Monad.Identity
|
2021-11-07 13:35:08 -05:00
|
|
|
|
2022-06-16 18:50:24 -04:00
|
|
|
-- import Data.Aeson
|
2022-06-21 00:56:42 -04:00
|
|
|
import Data.Bifunctor
|
2022-06-22 01:28:46 -04:00
|
|
|
-- import Data.Either
|
2021-11-27 01:02:22 -05:00
|
|
|
import Data.List (find)
|
2022-06-17 00:37:12 -04:00
|
|
|
import Data.Maybe
|
2022-06-16 18:50:24 -04:00
|
|
|
-- import qualified Data.Text as T
|
2021-11-07 20:16:53 -05:00
|
|
|
|
|
|
|
import DBus
|
|
|
|
import DBus.Client
|
2021-11-27 01:02:22 -05:00
|
|
|
import DBus.Internal
|
|
|
|
import qualified DBus.Introspection as I
|
2021-11-07 13:35:08 -05:00
|
|
|
|
2021-11-27 01:02:22 -05:00
|
|
|
import System.Directory (findExecutable, readable, writable)
|
2022-06-22 01:28:46 -04:00
|
|
|
-- import System.Environment
|
2021-11-07 13:35:08 -05:00
|
|
|
import System.Exit
|
|
|
|
|
2021-11-27 01:02:22 -05:00
|
|
|
import XMonad.Core (X, io)
|
2021-11-07 18:41:25 -05:00
|
|
|
import XMonad.Internal.IO
|
2021-11-07 13:35:08 -05:00
|
|
|
import XMonad.Internal.Process
|
|
|
|
import XMonad.Internal.Shell
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2021-11-21 10:26:28 -05:00
|
|
|
-- | Features
|
2022-06-21 00:56:42 -04:00
|
|
|
|
|
|
|
-- data AlwaysAny = AX AlwaysX | AIO AlwaysIO
|
|
|
|
|
|
|
|
type AlwaysX = Always (X ())
|
|
|
|
|
|
|
|
type AlwaysIO = Always (IO ())
|
|
|
|
|
|
|
|
type SometimesX = Sometimes (X ())
|
|
|
|
|
|
|
|
type SometimesIO = Sometimes (IO ())
|
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
type Feature a = Either (Sometimes a) (Always a)
|
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
data Always a = Option (Subfeature a Tree) (Always a) | Always a
|
|
|
|
|
|
|
|
type Sometimes a = [Subfeature a Tree]
|
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
ioSometimes :: MonadIO m => Sometimes (IO a) -> Sometimes (m a)
|
|
|
|
ioSometimes = fmap ioSubfeature
|
|
|
|
|
|
|
|
ioAlways :: MonadIO m => Always (IO a) -> Always (m a)
|
|
|
|
ioAlways (Always x) = Always $ io x
|
|
|
|
ioAlways (Option sf a) = Option (ioSubfeature sf) $ ioAlways a
|
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
data TestedAlways a p =
|
|
|
|
Primary (Finished a p) [FailedFeature a p] (Always a)
|
|
|
|
| Fallback a [FailedFeature a p]
|
|
|
|
|
|
|
|
data TestedSometimes a p = TestedSometimes
|
|
|
|
{ tsSuccess :: Maybe (Finished a p)
|
|
|
|
, tsFailed :: [FailedFeature a p]
|
|
|
|
, tsUntested :: [Subfeature a Tree]
|
2021-11-20 01:15:04 -05:00
|
|
|
}
|
2022-06-16 18:50:24 -04:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
type FailedFeature a p = Either (Subfeature a Tree, String)
|
|
|
|
(Subfeature a ResultTree, [String])
|
2021-11-21 22:47:43 -05:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
data Finished a p = Finished
|
|
|
|
{ finData :: Subfeature a ResultTree
|
|
|
|
, finAction :: a
|
|
|
|
, finWarnings :: [String]
|
|
|
|
}
|
2021-11-21 10:26:28 -05:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
data FeatureResult a p = Untestable (Subfeature a Tree) String |
|
|
|
|
FailedFtr (Subfeature a ResultTree) [String] |
|
|
|
|
SuccessfulFtr (Finished a p)
|
2021-11-11 00:11:15 -05:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
type ActionTreeMaybe a p = Either (ActionTree a Tree, String)
|
|
|
|
(ActionTree a ResultTree, Maybe a, [String])
|
2021-11-20 01:15:04 -05:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
sometimes1_ :: LogLevel -> String -> ActionTree a Tree -> Sometimes a
|
|
|
|
sometimes1_ l n t = [Subfeature{ sfTree = t, sfName = n, sfLevel = l }]
|
2022-06-17 00:37:12 -04:00
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
-- always1_ :: LogLevel -> String -> ActionTree a Tree -> a -> Always a
|
|
|
|
-- always1_ l n t x =
|
|
|
|
-- Option (Subfeature{ sfTree = t, sfName = n, sfLevel = l }) (Always x)
|
2021-11-21 18:18:09 -05:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
sometimes1 :: String -> ActionTree a Tree -> Sometimes a
|
|
|
|
sometimes1 = sometimes1_ Error
|
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
sometimesIO :: String -> Tree (IODependency p) p -> a -> Sometimes a
|
2022-06-21 00:56:42 -04:00
|
|
|
sometimesIO n t x = sometimes1 n $ IOTree (Standalone x) t
|
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
sometimesDBus :: Maybe Client -> String -> Tree (DBusDependency p) p
|
2022-06-21 00:56:42 -04:00
|
|
|
-> (Client -> a) -> Sometimes a
|
|
|
|
sometimesDBus c n t x = sometimes1 n $ DBusTree (Standalone x) c t
|
2021-11-22 23:46:51 -05:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Feature Data
|
2022-06-17 00:37:12 -04:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
data Subfeature a t = Subfeature
|
|
|
|
{ sfTree :: ActionTree a t
|
|
|
|
, sfName :: String
|
|
|
|
, sfLevel :: LogLevel
|
|
|
|
}
|
2022-06-17 00:37:12 -04:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
data LogLevel = Silent | Error | Warn | Debug deriving (Eq, Show, Ord)
|
2021-11-22 23:46:51 -05:00
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
ioSubfeature :: MonadIO m => Subfeature (IO a) t -> Subfeature (m a) t
|
|
|
|
ioSubfeature sf = sf { sfTree = ioActionTree $ sfTree sf }
|
|
|
|
|
|
|
|
-- data Msg = Msg LogLevel String String
|
2021-11-22 23:46:51 -05:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2022-06-21 00:56:42 -04:00
|
|
|
-- | Action Tree
|
2021-11-22 23:46:51 -05:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
data ActionTree a t =
|
2022-06-22 01:28:46 -04:00
|
|
|
forall p. IOTree (Action a p) (t (IODependency p) p)
|
|
|
|
| forall p. DBusTree (Action (Client -> a) p) (Maybe Client)
|
|
|
|
(t (DBusDependency p) p)
|
2021-11-22 23:46:51 -05:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
data Action a p = Standalone a | Consumer (p -> a)
|
2021-11-22 23:46:51 -05:00
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
ioActionTree :: MonadIO m => ActionTree (IO a) t -> ActionTree (m a) t
|
|
|
|
ioActionTree (IOTree (Standalone a) t) = IOTree (Standalone $ io a) t
|
|
|
|
ioActionTree (IOTree (Consumer a) t) = IOTree (Consumer $ io . a) t
|
|
|
|
ioActionTree (DBusTree (Standalone a) cl t) = DBusTree (Standalone $ io . a) cl t
|
|
|
|
ioActionTree (DBusTree (Consumer a) cl t) = DBusTree (Consumer (\p c -> io $ a p c)) cl t
|
|
|
|
|
|
|
|
-- --------------------------------------------------------------------------------
|
2022-06-21 00:56:42 -04:00
|
|
|
-- | Dependency Tree
|
2021-11-22 23:46:51 -05:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
data Tree d p =
|
|
|
|
And (p -> p -> p) (Tree d p) (Tree d p)
|
|
|
|
| Or (p -> p) (p -> p) (Tree d p) (Tree d p)
|
|
|
|
| Only d
|
2021-11-21 23:32:10 -05:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
listToAnds :: d -> [d] -> Tree d (Maybe x)
|
|
|
|
listToAnds i = foldr (And (const . const Nothing) . Only) (Only i)
|
|
|
|
|
|
|
|
toAnd :: d -> d -> Tree d (Maybe x)
|
|
|
|
toAnd a b = And (const . const Nothing) (Only a) (Only b)
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Result Tree
|
2021-11-22 23:02:23 -05:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
-- | how to interpret ResultTree combinations:
|
|
|
|
-- First (LeafSuccess a) (Tree a) -> Or that succeeded on left
|
|
|
|
-- First (LeafFail a) (Tree a) -> And that failed on left
|
|
|
|
-- Both (LeafFail a) (Fail a) -> Or that failed
|
|
|
|
-- Both (LeafSuccess a) (LeafSuccess a) -> And that succeeded
|
|
|
|
-- Both (LeafFail a) (LeafSuccess a) -> Or that failed first and succeeded second
|
|
|
|
-- Both (LeafSuccess a) (LeafFail a) -> And that failed on the right
|
2021-11-21 23:32:10 -05:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
data ResultTree d p =
|
|
|
|
First (ResultTree d p) (Tree d p)
|
|
|
|
| Both (ResultTree d p) (ResultTree d p)
|
|
|
|
| LeafSuccess d [String]
|
|
|
|
| LeafFail d [String]
|
2021-11-21 10:26:28 -05:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
type Payload p = (Maybe p, [String])
|
2021-11-21 10:26:28 -05:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
type Summary p = Either [String] (Payload p)
|
2021-11-21 10:26:28 -05:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
smryNil :: q -> Summary p
|
|
|
|
smryNil = const $ Right (Nothing, [])
|
2021-11-21 10:26:28 -05:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
smryFail :: String -> Either [String] a
|
|
|
|
smryFail msg = Left [msg]
|
2021-11-21 10:26:28 -05:00
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
-- smryInit :: Summary p
|
|
|
|
-- smryInit = Right (Nothing, [])
|
2022-06-21 00:56:42 -04:00
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
-- foldResultTreeMsgs :: ResultTree d p -> ([String], [String])
|
|
|
|
-- foldResultTreeMsgs = undefined
|
2021-11-21 10:26:28 -05:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2022-06-21 00:56:42 -04:00
|
|
|
-- | Result
|
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
-- type Result p = Either [String] (Maybe p)
|
2021-11-21 10:26:28 -05:00
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
-- resultNil :: p -> Result q
|
|
|
|
-- resultNil = const $ Right Nothing
|
2022-06-17 00:37:12 -04:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | IO Dependency
|
2022-06-17 00:37:12 -04:00
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
data IODependency p = Executable Bool FilePath
|
2021-11-21 10:26:28 -05:00
|
|
|
| AccessiblePath FilePath Bool Bool
|
2022-06-16 18:50:24 -04:00
|
|
|
| IOTest String (IO (Maybe String))
|
2022-06-21 00:56:42 -04:00
|
|
|
| IORead String (IO (Either String (Maybe p)))
|
2021-11-21 10:26:28 -05:00
|
|
|
| Systemd UnitType String
|
2022-06-22 01:28:46 -04:00
|
|
|
| forall a. NestedAlways (Always a) (a -> p)
|
|
|
|
| forall a. NestedSometimes (Sometimes a) (a -> p)
|
2021-11-21 10:26:28 -05:00
|
|
|
|
|
|
|
data UnitType = SystemUnit | UserUnit deriving (Eq, Show)
|
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
sometimesExe :: MonadIO m => String -> Bool -> FilePath -> Sometimes (m ())
|
|
|
|
sometimesExe n sys path = sometimesExeArgs n sys path []
|
2021-11-07 18:41:25 -05:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
sometimesExeArgs :: MonadIO m => String -> Bool -> FilePath -> [String] -> Sometimes (m ())
|
|
|
|
sometimesExeArgs n sys path args =
|
|
|
|
sometimesIO n (Only (Executable sys path)) $ spawnCmd path args
|
2021-11-07 18:41:25 -05:00
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
pathR :: String -> IODependency p
|
2022-06-21 00:56:42 -04:00
|
|
|
pathR n = AccessiblePath n True False
|
2021-11-07 18:41:25 -05:00
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
pathW :: String -> IODependency p
|
2022-06-21 00:56:42 -04:00
|
|
|
pathW n = AccessiblePath n False True
|
2021-11-07 13:35:08 -05:00
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
pathRW :: String -> IODependency p
|
2022-06-21 00:56:42 -04:00
|
|
|
pathRW n = AccessiblePath n True True
|
2021-11-07 13:35:08 -05:00
|
|
|
|
2021-11-21 10:26:28 -05:00
|
|
|
--------------------------------------------------------------------------------
|
2022-06-21 00:56:42 -04:00
|
|
|
-- | DBus Dependency Result
|
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
data DBusDependency p =
|
2022-06-21 00:56:42 -04:00
|
|
|
Bus BusName
|
|
|
|
| Endpoint BusName ObjectPath InterfaceName DBusMember
|
2022-06-22 01:28:46 -04:00
|
|
|
| DBusIO (IODependency p)
|
2021-11-22 23:46:51 -05:00
|
|
|
|
|
|
|
data DBusMember = Method_ MemberName
|
|
|
|
| Signal_ MemberName
|
|
|
|
| Property_ String
|
|
|
|
deriving (Eq, Show)
|
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
introspectInterface :: InterfaceName
|
|
|
|
introspectInterface = interfaceName_ "org.freedesktop.DBus.Introspectable"
|
|
|
|
|
|
|
|
introspectMethod :: MemberName
|
|
|
|
introspectMethod = memberName_ "Introspect"
|
|
|
|
|
|
|
|
sometimesEndpoint :: MonadIO m => String -> BusName -> ObjectPath -> InterfaceName
|
|
|
|
-> MemberName -> Maybe Client -> Sometimes (m ())
|
|
|
|
sometimesEndpoint name busname path iface mem client =
|
|
|
|
sometimesDBus client name deps cmd
|
|
|
|
where
|
|
|
|
deps = Only $ Endpoint busname path iface $ Method_ mem
|
|
|
|
cmd c = io $ void $ callMethod c busname path iface mem
|
2021-11-22 23:46:51 -05:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2022-06-21 00:56:42 -04:00
|
|
|
-- | Feature evaluation
|
2021-11-21 10:26:28 -05:00
|
|
|
--
|
2022-06-21 00:56:42 -04:00
|
|
|
-- Here we attempt to build and return the monadic actions encoded by each
|
|
|
|
-- feature.
|
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
executeAlways :: MonadIO m => Always (m a) -> m a
|
|
|
|
executeAlways = join . evalAlways
|
2022-06-21 00:56:42 -04:00
|
|
|
|
|
|
|
executeSometimes :: MonadIO m => Sometimes (m a) -> m (Maybe a)
|
|
|
|
executeSometimes a = maybe (return Nothing) (fmap Just) =<< evalSometimes a
|
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
evalFeature :: MonadIO m => Feature a -> m (Maybe a)
|
|
|
|
evalFeature (Right a) = Just <$> evalAlways a
|
|
|
|
evalFeature (Left s) = evalSometimes s
|
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
-- TODO actually print things
|
|
|
|
evalSometimes :: MonadIO m => Sometimes a -> m (Maybe a)
|
|
|
|
evalSometimes x = either (const Nothing) (Just . fst) <$> evalSometimesMsg x
|
|
|
|
|
|
|
|
-- TODO actually collect error messages here
|
|
|
|
-- TODO add feature name to errors
|
|
|
|
evalSometimesMsg :: MonadIO m => Sometimes a -> m (Either [String] (a, [String]))
|
|
|
|
evalSometimesMsg x = io $ do
|
|
|
|
TestedSometimes { tsSuccess = s, tsFailed = _ } <- testSometimes x
|
|
|
|
return $ maybe (Left []) (\Finished { finAction = a } -> Right (a, [])) s
|
|
|
|
|
|
|
|
|
|
|
|
-- TODO actually print things
|
|
|
|
evalAlways :: MonadIO m => Always a -> m a
|
|
|
|
evalAlways a = fst <$> evalAlwaysMsg a
|
|
|
|
|
|
|
|
evalAlwaysMsg :: MonadIO m => Always a -> m (a, [String])
|
|
|
|
evalAlwaysMsg a = io $ do
|
|
|
|
r <- testAlways a
|
2021-11-19 00:35:54 -05:00
|
|
|
return $ case r of
|
2022-06-21 00:56:42 -04:00
|
|
|
(Primary (Finished { finAction = act }) _ _) -> (act, [])
|
|
|
|
(Fallback act _) -> (act, [])
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Dependency Testing
|
|
|
|
--
|
|
|
|
-- Here we test all dependencies and keep the tree structure so we can print it
|
|
|
|
-- for diagnostic purposes. This obviously has overlap with feature evaluation
|
|
|
|
-- since we need to resolve dependencies to build each feature.
|
2021-11-07 13:35:08 -05:00
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
testAlways :: Always m -> IO (TestedAlways m p)
|
2022-06-21 00:56:42 -04:00
|
|
|
testAlways = go []
|
|
|
|
where
|
|
|
|
go failed (Option fd next) = do
|
|
|
|
r <- testSubfeature fd
|
|
|
|
case r of
|
|
|
|
(Untestable fd' err) -> go (Left (fd' ,err):failed) next
|
|
|
|
(FailedFtr fd' errs) -> go (Right (fd' ,errs):failed) next
|
|
|
|
(SuccessfulFtr s) -> return $ Primary s failed next
|
|
|
|
go failed (Always a) = return $ Fallback a failed
|
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
testSometimes :: Sometimes m -> IO (TestedSometimes m p)
|
2022-06-21 00:56:42 -04:00
|
|
|
testSometimes = go (TestedSometimes Nothing [] [])
|
|
|
|
where
|
|
|
|
go ts [] = return ts
|
|
|
|
go ts (x:xs) = do
|
|
|
|
r <- testSubfeature x
|
|
|
|
case r of
|
|
|
|
(Untestable fd' err) -> go (addFail ts (Left (fd' ,err))) xs
|
|
|
|
(FailedFtr fd' errs) -> go (addFail ts (Right (fd' ,errs))) xs
|
|
|
|
(SuccessfulFtr s) -> return $ ts { tsSuccess = Just s }
|
|
|
|
addFail ts@(TestedSometimes { tsFailed = f }) new
|
|
|
|
= ts { tsFailed = new:f }
|
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
testSubfeature :: Subfeature m Tree -> IO (FeatureResult m p)
|
2022-06-21 00:56:42 -04:00
|
|
|
testSubfeature fd@(Subfeature { sfTree = t }) = do
|
|
|
|
atm <- testActionTree t
|
|
|
|
return $ either untestable checkAction atm
|
|
|
|
where
|
|
|
|
untestable (t', err) = Untestable (fd { sfTree = t' }) err
|
|
|
|
checkAction (t', Just a, ms) = SuccessfulFtr
|
|
|
|
$ Finished { finData = fd { sfTree = t' }
|
|
|
|
, finAction = a
|
|
|
|
, finWarnings = ms
|
|
|
|
}
|
|
|
|
checkAction (t', Nothing, ms) = FailedFtr (fd { sfTree = t' }) ms
|
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
testActionTree :: ActionTree m Tree -> IO (ActionTreeMaybe m p)
|
2022-06-21 00:56:42 -04:00
|
|
|
testActionTree t = do
|
|
|
|
case t of
|
|
|
|
(IOTree a d) -> do
|
|
|
|
(t', a', msgs) <- doTest testIOTree d a
|
|
|
|
return $ Right (IOTree a t', a', msgs)
|
|
|
|
(DBusTree a (Just cl) d) -> do
|
|
|
|
(t', a', msgs) <- doTest (testDBusTree cl) d a
|
|
|
|
return $ Right (DBusTree a (Just cl) t', fmap (\f -> f cl) a', msgs)
|
|
|
|
_ -> return $ Left (t, "client not available")
|
|
|
|
where
|
|
|
|
doTest testFun d a = do
|
|
|
|
(t', r) <- testFun d
|
|
|
|
-- TODO actually recover the proper error messages
|
|
|
|
let (a', msgs) = maybe (Nothing, []) (\p -> (fmap (apply a) p, [])) r
|
|
|
|
return (t', a', msgs)
|
|
|
|
apply (Standalone a) _ = a
|
|
|
|
apply (Consumer a) p = a p
|
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
testIOTree :: Tree (IODependency p) p
|
|
|
|
-> IO (ResultTree (IODependency p) p, Maybe (Maybe p))
|
2022-06-21 00:56:42 -04:00
|
|
|
testIOTree = testTree testIODependency
|
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
testDBusTree :: Client -> Tree (DBusDependency p) p
|
|
|
|
-> IO (ResultTree (DBusDependency p) p, Maybe (Maybe p))
|
2022-06-21 00:56:42 -04:00
|
|
|
testDBusTree client = testTree (testDBusDependency client)
|
|
|
|
|
|
|
|
testTree :: Monad m => (d -> m (Summary p)) -> Tree d p
|
|
|
|
-> m (ResultTree d p, Maybe (Maybe p))
|
|
|
|
testTree test = go
|
|
|
|
where
|
|
|
|
go (And f a b) = do
|
|
|
|
(ra, pa) <- go a
|
|
|
|
let combine = maybe (const Nothing) (\pa' -> Just . f pa')
|
|
|
|
let pass p = test2nd (combine p) ra b
|
|
|
|
let fail_ = return (First ra b, Nothing)
|
|
|
|
maybe fail_ pass pa
|
|
|
|
go (Or fa fb a b) = do
|
|
|
|
(ra, pa) <- go a
|
|
|
|
let pass p = return (First ra b, Just $ fa <$> p)
|
|
|
|
let fail_ = test2nd (Just . fb) ra b
|
|
|
|
maybe fail_ pass pa
|
|
|
|
go (Only a) =
|
|
|
|
either (\es -> (LeafFail a es, Nothing)) (\(p, ws) -> (LeafSuccess a ws, Just p))
|
|
|
|
<$> test a
|
|
|
|
test2nd f ra b = do
|
|
|
|
(rb, pb) <- go b
|
|
|
|
return (Both ra rb, fmap (f =<<) pb)
|
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
testIODependency :: IODependency p -> IO (Summary p)
|
2022-06-21 00:56:42 -04:00
|
|
|
testIODependency (Executable _ bin) = maybe err smryNil <$> findExecutable bin
|
|
|
|
where
|
|
|
|
err = Left ["executable '" ++ bin ++ "' not found"]
|
|
|
|
|
|
|
|
testIODependency (IOTest _ t) = maybe (Right (Nothing, [])) (Left . (:[])) <$> t
|
|
|
|
|
|
|
|
testIODependency (IORead _ t) = bimap (:[]) (, []) <$> t
|
|
|
|
|
|
|
|
testIODependency (Systemd t n) = do
|
2021-11-07 13:35:08 -05:00
|
|
|
(rc, _, _) <- readCreateProcessWithExitCode' (shell cmd) ""
|
|
|
|
return $ case rc of
|
2022-06-21 00:56:42 -04:00
|
|
|
ExitSuccess -> Right (Nothing, [])
|
|
|
|
_ -> Left ["systemd " ++ unitType t ++ " unit '" ++ n ++ "' not found"]
|
2021-11-07 13:35:08 -05:00
|
|
|
where
|
2022-06-21 00:56:42 -04:00
|
|
|
cmd = fmtCmd "systemctl" $ ["--user" | t == UserUnit] ++ ["status", n]
|
2021-11-19 00:35:54 -05:00
|
|
|
unitType SystemUnit = "system"
|
|
|
|
unitType UserUnit = "user"
|
2021-11-07 13:35:08 -05:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
testIODependency (AccessiblePath p r w) = do
|
2021-11-07 18:41:25 -05:00
|
|
|
res <- getPermissionsSafe p
|
|
|
|
let msg = permMsg res
|
|
|
|
return msg
|
|
|
|
where
|
2022-06-21 00:56:42 -04:00
|
|
|
testPerm False _ _ = Nothing
|
|
|
|
testPerm True f res = Just $ f res
|
|
|
|
permMsg NotFoundError = smryFail "file not found"
|
|
|
|
permMsg PermError = smryFail "could not get permissions"
|
|
|
|
permMsg (PermResult res) =
|
|
|
|
case (testPerm r readable res, testPerm w writable res) of
|
|
|
|
(Just False, Just False) -> smryFail "file not readable or writable"
|
|
|
|
(Just False, _) -> smryFail "file not readable"
|
|
|
|
(_, Just False) -> smryFail "file not writable"
|
|
|
|
_ -> Right (Nothing, [])
|
|
|
|
|
|
|
|
-- TODO actually collect errors here
|
|
|
|
testIODependency (NestedAlways a f) = do
|
|
|
|
r <- testAlways a
|
|
|
|
return $ Right $ case r of
|
|
|
|
(Primary (Finished { finAction = act }) _ _) -> (Just $ f act, [])
|
|
|
|
(Fallback act _) -> (Just $ f act, [])
|
|
|
|
|
|
|
|
testIODependency (NestedSometimes x f) = do
|
|
|
|
TestedSometimes { tsSuccess = s, tsFailed = _ } <- testSometimes x
|
|
|
|
return $ maybe (Left []) (\Finished { finAction = a } -> Right (Just $ f a, [])) s
|
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
testDBusDependency :: Client -> DBusDependency p -> IO (Summary p)
|
2022-06-21 00:56:42 -04:00
|
|
|
testDBusDependency client (Bus bus) = do
|
2021-11-21 17:54:00 -05:00
|
|
|
ret <- callMethod client queryBus queryPath queryIface queryMem
|
2021-11-20 19:35:24 -05:00
|
|
|
return $ case ret of
|
2022-06-21 00:56:42 -04:00
|
|
|
Left e -> smryFail e
|
2021-11-20 19:35:24 -05:00
|
|
|
Right b -> let ns = bodyGetNames b in
|
2022-06-21 00:56:42 -04:00
|
|
|
if bus' `elem` ns then Right (Nothing, [])
|
|
|
|
else smryFail $ unwords ["name", singleQuote bus', "not found on dbus"]
|
2021-11-07 20:16:53 -05:00
|
|
|
where
|
2021-11-20 19:35:24 -05:00
|
|
|
bus' = formatBusName bus
|
|
|
|
queryBus = busName_ "org.freedesktop.DBus"
|
|
|
|
queryIface = interfaceName_ "org.freedesktop.DBus"
|
|
|
|
queryPath = objectPath_ "/"
|
|
|
|
queryMem = memberName_ "ListNames"
|
|
|
|
bodyGetNames [v] = fromMaybe [] $ fromVariant v :: [String]
|
|
|
|
bodyGetNames _ = []
|
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
testDBusDependency client (Endpoint busname objpath iface mem) = do
|
2021-11-21 22:47:43 -05:00
|
|
|
ret <- callMethod client busname objpath introspectInterface introspectMethod
|
2021-11-20 19:35:24 -05:00
|
|
|
return $ case ret of
|
2022-06-21 00:56:42 -04:00
|
|
|
Left e -> smryFail e
|
2021-11-20 19:35:24 -05:00
|
|
|
Right body -> procBody body
|
|
|
|
where
|
|
|
|
procBody body = let res = findMem =<< I.parseXML objpath =<< fromVariant
|
|
|
|
=<< listToMaybe body in
|
|
|
|
case res of
|
2022-06-21 00:56:42 -04:00
|
|
|
Just True -> Right (Nothing, [])
|
|
|
|
_ -> smryFail $ fmtMsg' mem
|
2021-11-20 19:35:24 -05:00
|
|
|
findMem = fmap (matchMem mem)
|
|
|
|
. find (\i -> I.interfaceName i == iface)
|
|
|
|
. I.objectInterfaces
|
|
|
|
matchMem (Method_ n) = elemMember n I.methodName I.interfaceMethods
|
|
|
|
matchMem (Signal_ n) = elemMember n I.signalName I.interfaceSignals
|
|
|
|
matchMem (Property_ n) = elemMember n I.propertyName I.interfaceProperties
|
|
|
|
elemMember n fname fmember = elem n . fmap fname . fmember
|
|
|
|
fmtMem (Method_ n) = "method " ++ singleQuote (formatMemberName n)
|
|
|
|
fmtMem (Signal_ n) = "signal " ++ singleQuote (formatMemberName n)
|
|
|
|
fmtMem (Property_ n) = "property " ++ singleQuote n
|
|
|
|
fmtMsg' m = unwords
|
|
|
|
[ "could not find"
|
|
|
|
, fmtMem m
|
|
|
|
, "on interface"
|
|
|
|
, singleQuote $ formatInterfaceName iface
|
|
|
|
, "on bus"
|
2021-11-21 22:47:43 -05:00
|
|
|
, formatBusName busname
|
2021-11-20 19:35:24 -05:00
|
|
|
]
|
2021-11-26 23:35:03 -05:00
|
|
|
|
2022-06-21 00:56:42 -04:00
|
|
|
testDBusDependency _ (DBusIO d) = testIODependency d
|
2022-06-17 00:37:12 -04:00
|
|
|
|
2022-06-16 18:50:24 -04:00
|
|
|
--------------------------------------------------------------------------------
|
2022-06-21 00:56:42 -04:00
|
|
|
-- | Printing
|
|
|
|
|
2022-06-22 01:28:46 -04:00
|
|
|
-- printMsgs :: LogLevel -> [Msg] -> IO ()
|
|
|
|
-- printMsgs lvl ms = do
|
|
|
|
-- pn <- getProgName
|
|
|
|
-- mapM_ (printMsg pn lvl) ms
|
|
|
|
|
|
|
|
-- printMsg :: String -> LogLevel -> Msg -> IO ()
|
|
|
|
-- printMsg pname lvl (Msg ml mn msg)
|
|
|
|
-- | lvl > ml = putStrLn $ unwords [bracket pname, bracket mn, msg]
|
|
|
|
-- | otherwise = skip
|
|
|
|
-- where
|
|
|
|
-- bracket s = "[" ++ s ++ "]"
|