Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sschuberth committed Nov 3, 2022
1 parent 5788347 commit 44661f8
Show file tree
Hide file tree
Showing 22 changed files with 199 additions and 112 deletions.
1 change: 1 addition & 0 deletions cli/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ dependencies {
implementation(libs.clikt)
implementation(libs.hikari)
implementation(libs.jacksonModuleKotlin)
implementation(libs.koinCore)
implementation(libs.kotlinxCoroutines)
implementation(libs.kotlinxSerialization)
implementation(libs.log4jApiToSlf4j)
Expand Down
19 changes: 14 additions & 5 deletions cli/src/main/kotlin/OrtMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ import java.io.File

import kotlin.system.exitProcess

import org.koin.core.context.GlobalContext.startKoin
import org.koin.dsl.module

import org.ossreviewtoolkit.cli.commands.*
import org.ossreviewtoolkit.cli.utils.logger
import org.ossreviewtoolkit.model.config.LicenseFilenamePatterns
import org.ossreviewtoolkit.model.config.OrtConfiguration
import org.ossreviewtoolkit.utils.common.Os
import org.ossreviewtoolkit.utils.common.expandTilde
Expand All @@ -70,7 +72,6 @@ sealed interface GroupTypes {
* Helper class for collecting options that can be passed to subcommands.
*/
data class GlobalOptions(
val config: OrtConfiguration,
val forceOverwrite: Boolean
)

Expand Down Expand Up @@ -179,13 +180,21 @@ class OrtMain : CliktCommand(name = ORT_NAME, invokeWithoutSubcommand = true) {

logger.debug { "Used command line arguments: ${currentContext.originalArgv}" }

val ortConfigModule = module {
single {
OrtConfiguration.load(configArguments, configFile)
}
}

startKoin {
modules(ortConfigModule)
}

// Make the parameter globally available.
printStackTrace = stacktrace

// Make options available to subcommands and apply static configuration.
val ortConfiguration = OrtConfiguration.load(configArguments, configFile)
currentContext.findOrSetObject { GlobalOptions(ortConfiguration, forceOverwrite) }
LicenseFilenamePatterns.configure(ortConfiguration.licenseFilePatterns)
currentContext.findOrSetObject { GlobalOptions(forceOverwrite) }

if (helpAll) {
registeredSubcommands().forEach {
Expand Down
15 changes: 11 additions & 4 deletions cli/src/main/kotlin/commands/AdvisorCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ import com.github.ajalt.clikt.parameters.options.split
import com.github.ajalt.clikt.parameters.types.enum
import com.github.ajalt.clikt.parameters.types.file

import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

import org.ossreviewtoolkit.advisor.Advisor
import org.ossreviewtoolkit.cli.GlobalOptions
import org.ossreviewtoolkit.cli.utils.SeverityStats
Expand All @@ -42,14 +45,18 @@ import org.ossreviewtoolkit.cli.utils.outputGroup
import org.ossreviewtoolkit.cli.utils.readOrtResult
import org.ossreviewtoolkit.cli.utils.writeOrtResult
import org.ossreviewtoolkit.model.FileFormat
import org.ossreviewtoolkit.model.config.OrtConfiguration
import org.ossreviewtoolkit.model.utils.DefaultResolutionProvider
import org.ossreviewtoolkit.model.utils.mergeLabels
import org.ossreviewtoolkit.utils.common.expandTilde
import org.ossreviewtoolkit.utils.common.safeMkdirs
import org.ossreviewtoolkit.utils.ort.ORT_RESOLUTIONS_FILENAME
import org.ossreviewtoolkit.utils.ort.ortConfigDirectory

class AdvisorCommand : CliktCommand(name = "advise", help = "Check dependencies for security vulnerabilities.") {
class AdvisorCommand : KoinComponent, CliktCommand(
name = "advise",
help = "Check dependencies for security vulnerabilities."
) {
private val ortFile by option(
"--ort-file", "-i",
help = "An ORT result file with an analyzer result to use."
Expand Down Expand Up @@ -100,6 +107,7 @@ class AdvisorCommand : CliktCommand(name = "advise", help = "Check dependencies
).flag()

private val globalOptionsForSubcommands by requireObject<GlobalOptions>()
private val ortConfig by inject<OrtConfiguration>()

override fun run() {
val outputFiles = outputFormats.mapTo(mutableSetOf()) { format ->
Expand All @@ -117,8 +125,7 @@ class AdvisorCommand : CliktCommand(name = "advise", help = "Check dependencies
println("The following advisors are activated:")
println("\t" + distinctProviders.joinToString())

val config = globalOptionsForSubcommands.config
val advisor = Advisor(distinctProviders, config.advisor)
val advisor = Advisor(distinctProviders, ortConfig.advisor)

val ortResultInput = readOrtResult(ortFile)
val ortResultOutput = advisor.retrieveFindings(ortResultInput, skipExcluded).mergeLabels(labels)
Expand All @@ -138,6 +145,6 @@ class AdvisorCommand : CliktCommand(name = "advise", help = "Check dependencies
advisorResults.collectIssues().flatMap { it.value }.partition { resolutionProvider.isResolved(it) }
val severityStats = SeverityStats.createFromIssues(resolvedIssues, unresolvedIssues)

severityStats.print().conclude(config.severeIssueThreshold, 2)
severityStats.print().conclude(ortConfig.severeIssueThreshold, 2)
}
}
22 changes: 14 additions & 8 deletions cli/src/main/kotlin/commands/AnalyzerCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ import com.github.ajalt.clikt.parameters.options.split
import com.github.ajalt.clikt.parameters.types.enum
import com.github.ajalt.clikt.parameters.types.file

import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

import org.ossreviewtoolkit.analyzer.Analyzer
import org.ossreviewtoolkit.analyzer.PackageManager
import org.ossreviewtoolkit.analyzer.PackageManagerFactory
Expand All @@ -54,6 +57,7 @@ import org.ossreviewtoolkit.cli.utils.outputGroup
import org.ossreviewtoolkit.cli.utils.writeOrtResult
import org.ossreviewtoolkit.model.FileFormat
import org.ossreviewtoolkit.model.config.AnalyzerConfiguration
import org.ossreviewtoolkit.model.config.OrtConfiguration
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
import org.ossreviewtoolkit.model.readValueOrNull
import org.ossreviewtoolkit.model.utils.DefaultResolutionProvider
Expand All @@ -66,7 +70,10 @@ import org.ossreviewtoolkit.utils.ort.ORT_REPO_CONFIG_FILENAME
import org.ossreviewtoolkit.utils.ort.ORT_RESOLUTIONS_FILENAME
import org.ossreviewtoolkit.utils.ort.ortConfigDirectory

class AnalyzerCommand : CliktCommand(name = "analyze", help = "Determine dependencies of a software project.") {
class AnalyzerCommand : KoinComponent, CliktCommand(
name = "analyze",
help = "Determine dependencies of a software project."
) {
private val inputDir by option(
"--input-dir", "-i",
help = "The project directory to analyze. As a special case, if only one package manager is enabled, this " +
Expand Down Expand Up @@ -177,6 +184,7 @@ class AnalyzerCommand : CliktCommand(name = "analyze", help = "Determine depende
)

private val globalOptionsForSubcommands by requireObject<GlobalOptions>()
private val ortConfig by inject<OrtConfiguration>()

override fun run() {
val outputFiles = outputFormats.mapTo(mutableSetOf()) { format ->
Expand Down Expand Up @@ -209,12 +217,10 @@ class AnalyzerCommand : CliktCommand(name = "analyze", help = "Determine depende
println("Looking for analyzer-specific configuration in the following files and directories:")
println("\t" + configurationInfo)

val config = globalOptionsForSubcommands.config

val enabledPackageManagers = if (enabledPackageManagers != null || disabledPackageManagers != null) {
(enabledPackageManagers ?: PackageManager.ALL.values).toSet() - disabledPackageManagers.orEmpty().toSet()
} else {
config.analyzer.determineEnabledPackageManagers()
ortConfig.analyzer.determineEnabledPackageManagers()
}

println("The following package managers are enabled:")
Expand All @@ -226,7 +232,7 @@ class AnalyzerCommand : CliktCommand(name = "analyze", help = "Determine depende
?: RepositoryConfiguration()

val analyzerConfiguration =
repositoryConfiguration.analyzer?.let { config.analyzer.merge(it) } ?: config.analyzer
repositoryConfiguration.analyzer?.let { ortConfig.analyzer.merge(it) } ?: ortConfig.analyzer

val analyzer = Analyzer(analyzerConfiguration, labels)

Expand All @@ -237,7 +243,7 @@ class AnalyzerCommand : CliktCommand(name = "analyze", help = "Determine depende

val repositoryPackageCurations = repositoryConfiguration.curations.packages

if (config.enableRepositoryPackageCurations) {
if (ortConfig.enableRepositoryPackageCurations) {
add(SimplePackageCurationProvider(repositoryPackageCurations))
} else if (repositoryPackageCurations.isNotEmpty()) {
logger.warn {
Expand All @@ -249,7 +255,7 @@ class AnalyzerCommand : CliktCommand(name = "analyze", help = "Determine depende

val curationProviders = listOfNotNull(
CompositePackageCurationProvider(defaultCurationProviders),
config.analyzer.sw360Configuration?.let {
ortConfig.analyzer.sw360Configuration?.let {
Sw360PackageCurationProvider(it).takeIf { useSw360Curations }
},
ClearlyDefinedPackageCurationProvider().takeIf { useClearlyDefinedCurations }
Expand Down Expand Up @@ -301,7 +307,7 @@ class AnalyzerCommand : CliktCommand(name = "analyze", help = "Determine depende
analyzerResult.collectIssues().flatMap { it.value }.partition { resolutionProvider.isResolved(it) }
val severityStats = SeverityStats.createFromIssues(resolvedIssues, unresolvedIssues)

severityStats.print().conclude(config.severeIssueThreshold, 2)
severityStats.print().conclude(ortConfig.severeIssueThreshold, 2)
}
}

Expand Down
14 changes: 9 additions & 5 deletions cli/src/main/kotlin/commands/ConfigCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLMapper
import com.fasterxml.jackson.module.kotlin.registerKotlinModule

import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.requireObject
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option

import org.ossreviewtoolkit.cli.GlobalOptions
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

import org.ossreviewtoolkit.model.config.OrtConfiguration
import org.ossreviewtoolkit.model.config.OrtConfigurationWrapper
import org.ossreviewtoolkit.model.config.REFERENCE_CONFIG_FILENAME

class ConfigCommand : CliktCommand(name = "config", help = "Show different ORT configurations") {
class ConfigCommand : KoinComponent, CliktCommand(
name = "config",
help = "Show different ORT configurations"
) {
private val showDefault by option(
"--show-default",
help = "Show the default configuration used when no custom configuration is present."
Expand All @@ -49,7 +53,7 @@ class ConfigCommand : CliktCommand(name = "config", help = "Show different ORT c
"example entries for all supported configuration options."
).flag()

private val globalOptionsForSubcommands by requireObject<GlobalOptions>()
private val ortConfig by inject<OrtConfiguration>()

private val mapper = YAMLMapper().apply {
registerKotlinModule()
Expand All @@ -68,7 +72,7 @@ class ConfigCommand : CliktCommand(name = "config", help = "Show different ORT c
if (showActive) {
println("The active configuration is:")
println()
println(globalOptionsForSubcommands.config.renderYaml())
println(ortConfig.renderYaml())
}

if (showReference) {
Expand Down
19 changes: 12 additions & 7 deletions cli/src/main/kotlin/commands/DownloaderCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package org.ossreviewtoolkit.cli.commands

import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.ProgramResult
import com.github.ajalt.clikt.core.requireObject
import com.github.ajalt.clikt.parameters.groups.default
import com.github.ajalt.clikt.parameters.groups.mutuallyExclusiveOptions
import com.github.ajalt.clikt.parameters.groups.required
Expand All @@ -37,7 +36,9 @@ import com.github.ajalt.clikt.parameters.types.file

import java.io.File

import org.ossreviewtoolkit.cli.GlobalOptions
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

import org.ossreviewtoolkit.cli.GroupTypes.FileType
import org.ossreviewtoolkit.cli.GroupTypes.StringType
import org.ossreviewtoolkit.cli.utils.OPTION_GROUP_INPUT
Expand All @@ -56,6 +57,7 @@ import org.ossreviewtoolkit.model.PackageType
import org.ossreviewtoolkit.model.RemoteArtifact
import org.ossreviewtoolkit.model.VcsInfo
import org.ossreviewtoolkit.model.VcsType
import org.ossreviewtoolkit.model.config.OrtConfiguration
import org.ossreviewtoolkit.model.licenses.LicenseCategorization
import org.ossreviewtoolkit.model.licenses.LicenseClassifications
import org.ossreviewtoolkit.model.licenses.LicenseInfoResolver
Expand All @@ -75,7 +77,10 @@ import org.ossreviewtoolkit.utils.ort.ortConfigDirectory
import org.ossreviewtoolkit.utils.ort.showStackTrace
import org.ossreviewtoolkit.utils.spdx.model.SpdxLicenseChoice

class DownloaderCommand : CliktCommand(name = "download", help = "Fetch source code from a remote location.") {
class DownloaderCommand : KoinComponent, CliktCommand(
name = "download",
help = "Fetch source code from a remote location."
) {
private val input by mutuallyExclusiveOptions(
option(
"--ort-file", "-i",
Expand Down Expand Up @@ -176,7 +181,7 @@ class DownloaderCommand : CliktCommand(name = "download", help = "Fetch source c
"result to limit downloads to. If not specified, all packages are downloaded."
).split(",")

private val globalOptionsForSubcommands by requireObject<GlobalOptions>()
private val ortConfig by inject<OrtConfiguration>()

override fun run() {
val failureMessages = mutableListOf<String>()
Expand Down Expand Up @@ -237,7 +242,7 @@ class DownloaderCommand : CliktCommand(name = "download", help = "Fetch source c
}
}

val includedLicenseCategories = globalOptionsForSubcommands.config.downloader.includedLicenseCategories
val includedLicenseCategories = ortConfig.downloader.includedLicenseCategories
if (includedLicenseCategories.isNotEmpty() && licenseClassificationsFile.isFile) {
val originalCount = packages.size

Expand Down Expand Up @@ -268,7 +273,7 @@ class DownloaderCommand : CliktCommand(name = "download", help = "Fetch source c

packageDownloadDirs.forEach { (pkg, dir) ->
try {
Downloader(globalOptionsForSubcommands.config.downloader).download(pkg, dir)
Downloader(ortConfig.downloader).download(pkg, dir)

if (archiveMode == ArchiveMode.ENTITY) {
val zipFile = outputDir.resolve("${pkg.id.toPath("-")}.zip")
Expand Down Expand Up @@ -369,7 +374,7 @@ class DownloaderCommand : CliktCommand(name = "download", help = "Fetch source c
// Always allow moving revisions when directly downloading a single project only. This is for
// convenience as often the latest revision (referred to by some VCS-specific symbolic name) of a
// project needs to be downloaded.
val config = globalOptionsForSubcommands.config.downloader.copy(allowMovingRevisions = true)
val config = ortConfig.downloader.copy(allowMovingRevisions = true)
val provenance = Downloader(config).download(dummyPackage, outputDir)
println("Successfully downloaded $provenance.")
}.onFailure {
Expand Down
23 changes: 14 additions & 9 deletions cli/src/main/kotlin/commands/EvaluatorCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ import java.io.File

import kotlin.time.measureTimedValue

import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

import org.ossreviewtoolkit.analyzer.curation.FilePackageCurationProvider
import org.ossreviewtoolkit.cli.GlobalOptions
import org.ossreviewtoolkit.cli.GroupTypes.FileType
Expand All @@ -58,7 +61,7 @@ import org.ossreviewtoolkit.evaluator.Evaluator
import org.ossreviewtoolkit.model.FileFormat
import org.ossreviewtoolkit.model.RuleViolation
import org.ossreviewtoolkit.model.config.CopyrightGarbage
import org.ossreviewtoolkit.model.config.LicenseFilenamePatterns
import org.ossreviewtoolkit.model.config.OrtConfiguration
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
import org.ossreviewtoolkit.model.config.createFileArchiver
import org.ossreviewtoolkit.model.config.orEmpty
Expand All @@ -81,7 +84,10 @@ import org.ossreviewtoolkit.utils.ort.ORT_REPO_CONFIG_FILENAME
import org.ossreviewtoolkit.utils.ort.ORT_RESOLUTIONS_FILENAME
import org.ossreviewtoolkit.utils.ort.ortConfigDirectory

class EvaluatorCommand : CliktCommand(name = "evaluate", help = "Evaluate ORT result files against policy rules.") {
class EvaluatorCommand : KoinComponent, CliktCommand(
name = "evaluate",
help = "Evaluate ORT result files against policy rules."
) {
private val ortFile by option(
"--ort-file", "-i",
help = "The ORT result file to read as input."
Expand Down Expand Up @@ -204,6 +210,7 @@ class EvaluatorCommand : CliktCommand(name = "evaluate", help = "Evaluate ORT re
).flag()

private val globalOptionsForSubcommands by requireObject<GlobalOptions>()
private val ortConfig by inject<OrtConfiguration>()

override fun run() {
val configurationFiles = listOfNotNull(
Expand Down Expand Up @@ -272,9 +279,7 @@ class EvaluatorCommand : CliktCommand(name = "evaluate", help = "Evaluate ORT re
ortResultInput = ortResultInput.replacePackageCurations(curations)
}

val config = globalOptionsForSubcommands.config

val packageConfigurationProvider = if (config.enableRepositoryPackageConfigurations) {
val packageConfigurationProvider = if (ortConfig.enableRepositoryPackageConfigurations) {
CompositePackageConfigurationProvider(
SimplePackageConfigurationProvider(ortResultInput.repository.config.packageConfigurations),
packageConfigurationOption.createProvider()
Expand All @@ -292,9 +297,9 @@ class EvaluatorCommand : CliktCommand(name = "evaluate", help = "Evaluate ORT re
val licenseInfoResolver = LicenseInfoResolver(
provider = DefaultLicenseInfoProvider(ortResultInput, packageConfigurationProvider),
copyrightGarbage = copyrightGarbage,
addAuthorsToCopyrights = config.addAuthorsToCopyrights,
archiver = config.scanner.archive.createFileArchiver(),
licenseFilenamePatterns = LicenseFilenamePatterns.getInstance()
addAuthorsToCopyrights = ortConfig.addAuthorsToCopyrights,
archiver = ortConfig.scanner.archive.createFileArchiver(),
licenseFilenamePatterns = ortConfig.licenseFilePatterns
)

val resolutionProvider = DefaultResolutionProvider.create(ortResultInput, resolutionsFile)
Expand Down Expand Up @@ -322,7 +327,7 @@ class EvaluatorCommand : CliktCommand(name = "evaluate", help = "Evaluate ORT re
evaluatorRun.violations.partition { resolutionProvider.isResolved(it) }
val severityStats = SeverityStats.createFromRuleViolations(resolvedViolations, unresolvedViolations)

severityStats.print().conclude(config.severeRuleViolationThreshold, 2)
severityStats.print().conclude(ortConfig.severeRuleViolationThreshold, 2)
}
}

Expand Down
Loading

0 comments on commit 44661f8

Please sign in to comment.