Skip to content
This repository has been archived by the owner on Apr 1, 2022. It is now read-only.

AOSP notice files: accept paths as arguments #217

Merged
merged 7 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/App/Fossa/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import qualified App.Fossa.VPS.Report as VPSReport
import App.Fossa.VPS.Scan (LicenseOnlyScan (..), SkipIPRScan (..), scanMain)
import App.Fossa.VPS.AOSPNotice (aospNoticeMain)
import qualified App.Fossa.VPS.Test as VPSTest
import App.Fossa.VPS.Types (FilterExpressions (..), NinjaScanID (..))
import App.Fossa.VPS.Types (FilterExpressions (..), NinjaScanID (..), NinjaFilePaths (..))
import App.OptionExtensions
import App.Types
import App.Util (validateDir)
import App.Util (validateDir, validateFile)
import App.Version (fullVersionDescription)
import Control.Monad (unless, when)
import Data.Bifunctor (first)
Expand All @@ -42,6 +42,7 @@ import qualified System.Info as SysInfo
import Text.Megaparsec (errorBundlePretty, runParser)
import Text.URI (URI)
import Text.URI.QQ (uri)
import Path

windowsOsName :: String
windowsOsName = "mingw32"
Expand Down Expand Up @@ -118,7 +119,8 @@ appMain = do
VPSAOSPNoticeCommand VPSAOSPNoticeOptions {..} -> do
dieOnWindows "Vendored Package Scanning (VPS)"
baseDir <- validateDir vpsAOSPNoticeBaseDir
aospNoticeMain baseDir logSeverity override (NinjaScanID vpsNinjaScanID) apiOpts
ninjaPaths <- parseCommaSeparatedFileArg vpsNinjaFileList
aospNoticeMain baseDir logSeverity override (NinjaScanID vpsNinjaScanID) (NinjaFilePaths ninjaPaths) apiOpts

--
ContainerCommand ContainerOptions {..} -> do
Expand Down Expand Up @@ -149,6 +151,10 @@ appMain = do
dieOnWindows :: String -> IO ()
dieOnWindows op = when (SysInfo.os == windowsOsName) $ die $ "Operation is not supported on Windows: " <> op


parseCommaSeparatedFileArg :: Text -> IO [Path Abs File]
parseCommaSeparatedFileArg arg = sequence $ validateFile . T.unpack <$> T.splitOn arg ","
jssblck marked this conversation as resolved.
Show resolved Hide resolved

requireKey :: Maybe ApiKey -> IO ApiKey
requireKey (Just key) = pure key
requireKey Nothing = die "A FOSSA API key is required to run this command"
Expand Down Expand Up @@ -313,6 +319,7 @@ vpsAospNoticeOpts =
VPSAOSPNoticeOptions
<$> baseDirArg
<*> strOption (long "scan-id" <> help "ID of the scan to which notice content should be added. Reported by `analyze` upon completion.")
<*> strOption (long "ninja-files" <> help "A comma-separated list of ninja files to parse for build graph information.")
<*> metadataOpts

-- FIXME: make report type a positional argument, rather than a subcommand
Expand Down Expand Up @@ -500,6 +507,7 @@ data VPSAnalyzeOptions = VPSAnalyzeOptions
data VPSAOSPNoticeOptions = VPSAOSPNoticeOptions
{ vpsAOSPNoticeBaseDir :: FilePath,
vpsNinjaScanID :: Text,
vpsNinjaFileList :: Text,
vpsNinjaScanMeta :: ProjectMetadata
}

Expand Down
20 changes: 8 additions & 12 deletions src/App/Fossa/VPS/AOSPNotice.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ import App.Fossa.VPS.Scan.RunWiggins
import App.Fossa.VPS.Types
import App.Types (BaseDir (..), OverrideProject)
import Effect.Logger
import Data.Text (Text, isSuffixOf)
import Data.Text (Text)
import App.Fossa.ProjectInference
import Fossa.API.Types (ApiOpts(..))
import Path (Path, Abs, Dir, toFilePath)
import qualified Data.Text as T
import Path.IO (listDirRecurRel)
import Path (Path, Abs, Dir)

aospNoticeMain :: BaseDir -> Severity -> OverrideProject -> NinjaScanID -> ApiOpts -> IO ()
aospNoticeMain (BaseDir basedir) logSeverity overrideProject ninjaScanId apiOpts = withLogger logSeverity $ do
result <- runDiagnostics $ withWigginsBinary $ aospNoticeGenerate basedir logSeverity overrideProject ninjaScanId apiOpts
aospNoticeMain :: BaseDir -> Severity -> OverrideProject -> NinjaScanID -> NinjaFilePaths -> ApiOpts -> IO ()
aospNoticeMain (BaseDir basedir) logSeverity overrideProject ninjaScanId ninjaFilePaths apiOpts = withLogger logSeverity $ do
result <- runDiagnostics $ withWigginsBinary $ aospNoticeGenerate basedir logSeverity overrideProject ninjaScanId ninjaFilePaths apiOpts
case result of
Left failure -> do
logError $ renderFailureBundle failure
Expand All @@ -34,13 +32,11 @@ aospNoticeGenerate ::
( Has Diagnostics sig m
, Has Logger sig m
, Has (Lift IO) sig m
) => Path Abs Dir -> Severity -> OverrideProject -> NinjaScanID -> ApiOpts -> BinaryPaths -> m ()
aospNoticeGenerate basedir logSeverity overrideProject ninjaScanId apiOpts binaryPaths = do
) => Path Abs Dir -> Severity -> OverrideProject -> NinjaScanID -> NinjaFilePaths -> ApiOpts -> BinaryPaths -> m ()
aospNoticeGenerate basedir logSeverity overrideProject ninjaScanId ninjaFilePaths apiOpts binaryPaths = do
projectRevision <- mergeOverride overrideProject <$> (inferProjectFromVCS basedir <||> inferProjectDefault basedir)

(_, files) <- sendIO $ listDirRecurRel basedir
let ninjaInputFiles = NinjaInputFiles $ filter (".ninja" `isSuffixOf`) $ map (T.pack . toFilePath) files
let wigginsOpts = generateWigginsAOSPNoticeOpts basedir logSeverity apiOpts projectRevision ninjaScanId ninjaInputFiles
let wigginsOpts = generateWigginsAOSPNoticeOpts basedir logSeverity apiOpts projectRevision ninjaScanId ninjaFilePaths

logInfo "Running VPS plugin: generating AOSP notice files"
stdout <- runExecIO $ runWiggins binaryPaths wigginsOpts
Expand Down
15 changes: 8 additions & 7 deletions src/App/Fossa/VPS/Scan/RunWiggins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ module App.Fossa.VPS.Scan.RunWiggins
, generateWigginsAOSPNoticeOpts
, WigginsOpts(..)
, ScanType(..)
, NinjaInputFiles(..)
)
where

import App.Fossa.VPS.Types
( FilterExpressions (FilterExpressions),
NinjaFilePaths (unNinjaFilePaths),
NinjaScanID (unNinjaScanID),
encodeFilterExpressions )
import App.Fossa.EmbeddedBinary
import Control.Carrier.Error.Either
import Control.Effect.Diagnostics
Expand All @@ -23,9 +26,7 @@ import Fossa.API.Types
import App.Types
import Text.URI
import qualified Data.ByteString.Lazy as BL
import Data.Text.Encoding

newtype NinjaInputFiles = NinjaInputFiles { unNinjaInputFiles :: [Text] }
import Data.Text.Encoding ( decodeUtf8 )

data ScanType = ScanType
{ scanSkipIpr :: Bool
Expand All @@ -41,19 +42,19 @@ generateWigginsScanOpts :: Path Abs Dir -> Severity -> ProjectRevision -> ScanTy
generateWigginsScanOpts scanDir logSeverity projectRevision scanType fileFilters apiOpts metadata =
WigginsOpts scanDir $ generateSpectrometerScanArgs logSeverity projectRevision scanType fileFilters apiOpts metadata

generateWigginsAOSPNoticeOpts :: Path Abs Dir -> Severity -> ApiOpts -> ProjectRevision -> NinjaScanID -> NinjaInputFiles -> WigginsOpts
generateWigginsAOSPNoticeOpts :: Path Abs Dir -> Severity -> ApiOpts -> ProjectRevision -> NinjaScanID -> NinjaFilePaths -> WigginsOpts
generateWigginsAOSPNoticeOpts scanDir logSeverity apiOpts projectRevision ninjaScanId ninjaInputFiles =
WigginsOpts scanDir $ generateSpectrometerAOSPNoticeArgs logSeverity apiOpts projectRevision ninjaScanId ninjaInputFiles

generateSpectrometerAOSPNoticeArgs :: Severity -> ApiOpts -> ProjectRevision -> NinjaScanID -> NinjaInputFiles -> [Text]
generateSpectrometerAOSPNoticeArgs :: Severity -> ApiOpts -> ProjectRevision -> NinjaScanID -> NinjaFilePaths -> [Text]
generateSpectrometerAOSPNoticeArgs logSeverity ApiOpts{..} ProjectRevision{..} ninjaScanId ninjaInputFiles =
["aosp-notice-files"]
++ optBool "-debug" (logSeverity == SevDebug)
++ ["-endpoint", render apiOptsUri, "-fossa-api-key", unApiKey apiOptsApiKey]
++ ["-scan-id", unNinjaScanID ninjaScanId]
++ ["-name", projectName]
++ ["."]
++ unNinjaInputFiles ninjaInputFiles
++ (T.pack . show <$> unNinjaFilePaths ninjaInputFiles)
jssblck marked this conversation as resolved.
Show resolved Hide resolved

generateSpectrometerScanArgs :: Severity -> ProjectRevision -> ScanType -> FilterExpressions -> ApiOpts -> ProjectMetadata -> [Text]
generateSpectrometerScanArgs logSeverity ProjectRevision{..} ScanType{..} fileFilters ApiOpts{..} ProjectMetadata{..} =
Expand Down
4 changes: 4 additions & 0 deletions src/App/Fossa/VPS/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module App.Fossa.VPS.Types
, VPSOpts (..)
, NinjaGraphOpts (..)
, NinjaScanID (..)
, NinjaFilePaths (..)
) where

import Control.Monad.IO.Class (MonadIO(..))
Expand All @@ -25,9 +26,12 @@ import Network.HTTP.Req
import Data.Text.Prettyprint.Doc (viaShow)
import qualified Data.ByteString.Lazy as BSL
import Data.Text.Encoding (decodeUtf8)
import Path

newtype NinjaScanID = NinjaScanID { unNinjaScanID :: Text }

newtype NinjaFilePaths = NinjaFilePaths { unNinjaFilePaths :: [Path Abs File] }

newtype FilterExpressions = FilterExpressions { unFilterExpressions :: [Text] }

encodeFilterExpressions :: FilterExpressions -> Text
Expand Down
12 changes: 11 additions & 1 deletion src/App/Util.hs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{-# LANGUAGE DataKinds #-}

module App.Util
( validateDir,
( validateDir
, validateFile
)
where

import App.Types
import Control.Monad (unless)
import qualified Path.IO as P
import System.Exit (die)
import Path ( Path, Abs, File )

-- | Validate that a filepath points to a directory and the directory exists
validateDir :: FilePath -> IO BaseDir
Expand All @@ -18,3 +20,11 @@ validateDir dir = do

unless exists (die $ "ERROR: Directory " <> show absolute <> " does not exist")
pure $ BaseDir absolute

-- | Validate that a filepath points to a file and the file exists
validateFile :: FilePath -> IO (Path Abs File)
validateFile file = do
absolute <- P.resolveFile' file
exists <- P.doesFileExist absolute
unless exists (die $ "ERROR: File " <> show absolute <> " does not exist")
pure absolute