Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
37 changes: 23 additions & 14 deletions cli/src/funTest/kotlin/ExamplesFunTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ import java.io.File
import java.io.IOException
import java.time.Instant

import org.koin.core.context.loadKoinModules
import org.koin.dsl.module

import org.ossreviewtoolkit.evaluator.Evaluator
import org.ossreviewtoolkit.model.AnalyzerResult
import org.ossreviewtoolkit.model.AnalyzerRun
Expand All @@ -49,6 +52,7 @@ import org.ossreviewtoolkit.model.PackageCuration
import org.ossreviewtoolkit.model.Severity
import org.ossreviewtoolkit.model.config.CopyrightGarbage
import org.ossreviewtoolkit.model.config.NotifierConfiguration
import org.ossreviewtoolkit.model.config.OrtConfiguration
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
import org.ossreviewtoolkit.model.config.Resolutions
import org.ossreviewtoolkit.model.config.SendMailConfiguration
Expand All @@ -60,6 +64,7 @@ import org.ossreviewtoolkit.reporter.ReporterInput
import org.ossreviewtoolkit.reporter.reporters.freemarker.asciidoc.PdfTemplateReporter
import org.ossreviewtoolkit.utils.ort.ORT_REPO_CONFIG_FILENAME
import org.ossreviewtoolkit.utils.spdx.toSpdx
import org.ossreviewtoolkit.utils.test.ProjectConfig
import org.ossreviewtoolkit.utils.test.createSpecTempDir
import org.ossreviewtoolkit.utils.test.shouldNotBeNull

Expand Down Expand Up @@ -122,10 +127,10 @@ class ExamplesFunTest : StringSpec() {
}

"asciidoctor-pdf-theme.yml is a valid asciidoctor-pdf theme" {
loadKoinModules(ProjectConfig.defaultConfigModule)
val outputDir = createSpecTempDir()

takeExampleFile("asciidoctor-pdf-theme.yml")

val report = PdfTemplateReporter().generateReport(
ReporterInput(OrtResult.EMPTY),
outputDir,
Expand All @@ -136,6 +141,8 @@ class ExamplesFunTest : StringSpec() {
}

"example.rules.kts can be compiled and executed" {
loadKoinModules(ProjectConfig.defaultConfigModule)

val resultFile = File("src/funTest/assets/semver4j-ort-result.yml")
val licenseFile = File("../examples/license-classifications.yml")
val ortResult = resultFile.readValue<OrtResult>()
Expand Down Expand Up @@ -164,21 +171,23 @@ class ExamplesFunTest : StringSpec() {
greenMail.setUser("no-reply@oss-review-toolkit.org", "no-reply@oss-review-toolkit.org", "pwd")
greenMail.start()

val ortResult = createOrtResultWithIssue()
val notifier = Notifier(
ortResult,
NotifierConfiguration(
SendMailConfiguration(
hostName = "localhost",
port = greenMail.smtp.serverSetup.port,
username = "no-reply@oss-review-toolkit.org",
password = "pwd",
useSsl = false,
fromAddress = "no-reply@oss-review-toolkit.org"
)
)
val sendMailConfig = SendMailConfiguration(
hostName = "localhost",
port = greenMail.smtp.serverSetup.port,
username = "no-reply@oss-review-toolkit.org",
password = "pwd",
useSsl = false,
fromAddress = "no-reply@oss-review-toolkit.org"
)

val sendMailConfigModule = module {
single { OrtConfiguration(notifier = NotifierConfiguration(mail = sendMailConfig)) }
}

loadKoinModules(sendMailConfigModule)
val ortResult = createOrtResultWithIssue()
val notifier = Notifier(ortResult)

val script = examplesDir.resolve("notifications/src/main/resources/example.notifications.kts").readText()

notifier.run(script)
Expand Down
17 changes: 12 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,19 @@ class OrtMain : CliktCommand(name = ORT_NAME, invokeWithoutSubcommand = true) {

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

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

startKoin {
modules(configModule)
}

// 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
Loading