tavro-0.1.0.2: src/Tavro/Submodules.hs
{-# LANGUAGE MultiWayIf #-}
module Tavro.Submodules
( Submodule (..),
parseSubmoduleStatus,
listSubmodules,
submoduleLicenseFiles,
judgeSubmodule,
submodulePackage,
)
where
import Control.Exception (IOException, try)
import Control.Monad (filterM)
import Data.Containers.ListUtils (nubOrd)
import Data.Either (partitionEithers)
import Data.List (sort)
import Data.Maybe (mapMaybe)
import Data.Text (Text)
import Data.Text qualified as T
import System.Directory (doesDirectoryExist, doesFileExist, listDirectory)
import System.Exit (ExitCode (..))
import System.FilePath (takeFileName, (</>))
import System.Process (readProcessWithExitCode)
import Tavro.Cabal (Package (..), Verdict (..), licenseFilesIn)
import Tavro.Spdx (FromText (..), Ids, fromFiles)
data Submodule = Submodule
{ sPath :: FilePath,
sCommit :: Text
}
deriving stock (Eq, Show)
listSubmodules :: FilePath -> IO (Either String ([Submodule], [FilePath]))
listSubmodules dir = do
r <- try (readProcessWithExitCode "git" ["-C", dir, "submodule", "status", "--recursive"] "")
pure $ case r of
Left (e :: IOException) -> Left ("can't run git: " <> show e)
Right (ExitFailure _, _, err) -> Left ("git submodule status failed in " <> dir <> ": " <> T.unpack (T.strip (T.pack err)))
Right (ExitSuccess, out, _) -> Right (parseSubmoduleStatus (T.pack out))
parseSubmoduleStatus :: Text -> ([Submodule], [FilePath])
parseSubmoduleStatus = swap . partitionEithers . mapMaybe parseLine . T.lines
where
swap (missing, present) = (present, missing)
parseLine line = do
(state, rest) <- T.uncons line
let (commit, afterCommit) = T.breakOn " " rest
path <- T.stripPrefix " " afterCommit
if
| T.null commit || T.null path -> Nothing
| state == '-' -> Just (Left (T.unpack path))
| otherwise -> Just (Right (Submodule (T.unpack (dropDescription path)) commit))
dropDescription path = case T.breakOnEnd " (" path of
(before, described) | not (T.null before), ")" `T.isSuffixOf` described -> T.dropEnd 2 before
_ -> path
submoduleLicenseFiles :: FilePath -> IO [FilePath]
submoduleLicenseFiles root = do
topLevel <- licenseFilesIn [root]
reuse <- reuseLicenses (root </> "LICENSES")
pure (nubOrd (topLevel <> reuse))
where
reuseLicenses dir = do
exists <- doesDirectoryExist dir
if exists
then do
names <- sort <$> listDirectory dir
filterM doesFileExist (map (dir </>) names)
else pure []
submodulePackage :: Submodule -> Package
submodulePackage Submodule {..} =
Package
{ pName = T.pack sPath,
pVersion = sCommit,
pInProject = False,
pIds = [],
pSrcPath = Just sPath
}
judgeSubmodule :: Ids -> Submodule -> [(FilePath, [[Text]])] -> Verdict
judgeSubmodule ids sub identified = case fromFiles ids identified of
NoText
| null identified -> verdict "NOASSERTION" ["has no license file at its top"]
| otherwise -> verdict "NOASSERTION" [files <> " doesn't read as any license"]
Unclear why -> verdict "NOASSERTION" [files <> " " <> why <> ", so check the source headers"]
Reads pieces -> verdict (T.intercalate " AND " pieces) [dualLicenseNote pieces | length pieces > 1]
where
verdict expr = Verdict (submodulePackage sub) expr
files = T.intercalate ", " (map (T.pack . takeFileName . fst) identified)
dualLicenseNote pieces = files <> " read as " <> T.intercalate ", " pieces <> ". if it's dual licensed, that's OR, not AND"