Skip to content

Commit

Permalink
Handle user-facing empty configuration files leniently
Browse files Browse the repository at this point in the history
Fixes #3861.

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Jun 3, 2021
1 parent ce264eb commit 010797b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@ import org.ossreviewtoolkit.analyzer.PackageCurationProvider
import org.ossreviewtoolkit.model.FileFormat
import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.PackageCuration
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.model.readValueOrDefault

/**
* A [PackageCurationProvider] that loads [PackageCuration]s from a single file. Supports all file formats specified
* in [FileFormat].
*/
class FilePackageCurationProvider(curationFile: File) : PackageCurationProvider {
internal val packageCurations: List<PackageCuration> by lazy {
curationFile.readValue<List<PackageCuration>>()
}
internal val packageCurations by lazy { curationFile.readValueOrDefault(emptyList<PackageCuration>()) }

override fun getCurationsFor(pkgId: Identifier) = packageCurations.filter { it.isApplicable(pkgId) }
}
4 changes: 2 additions & 2 deletions cli/src/main/kotlin/commands/AnalyzerCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import org.ossreviewtoolkit.cli.utils.outputGroup
import org.ossreviewtoolkit.cli.writeOrtResult
import org.ossreviewtoolkit.model.FileFormat
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.model.readValueOrNull
import org.ossreviewtoolkit.model.utils.mergeLabels
import org.ossreviewtoolkit.utils.ORT_CURATIONS_FILENAME
import org.ossreviewtoolkit.utils.ORT_REPO_CONFIG_FILENAME
Expand Down Expand Up @@ -170,7 +170,7 @@ class AnalyzerCommand : CliktCommand(name = "analyze", help = "Determine depende

val repositoryConfiguration = actualRepositoryConfigurationFile.takeIf { it.isFile }?.let {
log.info { "Using configuration file '${it.absolutePath}'." }
it.readValue()
it.readValueOrNull()
} ?: RepositoryConfiguration()

val ortResult = analyzer.analyze(
Expand Down
9 changes: 7 additions & 2 deletions cli/src/main/kotlin/commands/EvaluatorCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,19 @@ import org.ossreviewtoolkit.cli.utils.outputGroup
import org.ossreviewtoolkit.cli.writeOrtResult
import org.ossreviewtoolkit.evaluator.Evaluator
import org.ossreviewtoolkit.model.FileFormat
import org.ossreviewtoolkit.model.PackageCuration
import org.ossreviewtoolkit.model.Severity
import org.ossreviewtoolkit.model.config.CopyrightGarbage
import org.ossreviewtoolkit.model.config.LicenseFilenamePatterns
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
import org.ossreviewtoolkit.model.config.createFileArchiver
import org.ossreviewtoolkit.model.config.orEmpty
import org.ossreviewtoolkit.model.licenses.DefaultLicenseInfoProvider
import org.ossreviewtoolkit.model.licenses.LicenseClassifications
import org.ossreviewtoolkit.model.licenses.LicenseInfoResolver
import org.ossreviewtoolkit.model.licenses.orEmpty
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.model.readValueOrDefault
import org.ossreviewtoolkit.model.utils.mergeLabels
import org.ossreviewtoolkit.utils.ORT_COPYRIGHT_GARBAGE_FILENAME
import org.ossreviewtoolkit.utils.ORT_LICENSE_CLASSIFICATIONS_FILENAME
Expand Down Expand Up @@ -240,11 +243,13 @@ class EvaluatorCommand : CliktCommand(name = "evaluate", help = "Evaluate ORT re
var ortResultInput = readOrtResult(existingOrtFile)

repositoryConfigurationFile?.let {
ortResultInput = ortResultInput.replaceConfig(it.readValue())
val config = it.readValueOrDefault(RepositoryConfiguration())
ortResultInput = ortResultInput.replaceConfig(config)
}

packageCurationsFile?.let {
ortResultInput = ortResultInput.replacePackageCurations(it.readValue())
val curations = it.readValueOrDefault(emptyList<PackageCuration>())
ortResultInput = ortResultInput.replacePackageCurations(curations)
}

val packageConfigurationProvider = packageConfigurationOption.createProvider()
Expand Down
5 changes: 4 additions & 1 deletion cli/src/main/kotlin/commands/ReporterCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import org.ossreviewtoolkit.cli.utils.inputGroup
import org.ossreviewtoolkit.cli.utils.outputGroup
import org.ossreviewtoolkit.model.config.CopyrightGarbage
import org.ossreviewtoolkit.model.config.LicenseFilenamePatterns
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
import org.ossreviewtoolkit.model.config.Resolutions
import org.ossreviewtoolkit.model.config.createFileArchiver
import org.ossreviewtoolkit.model.config.orEmpty
Expand All @@ -59,6 +60,7 @@ import org.ossreviewtoolkit.model.licenses.LicenseClassifications
import org.ossreviewtoolkit.model.licenses.LicenseInfoResolver
import org.ossreviewtoolkit.model.licenses.orEmpty
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.model.readValueOrDefault
import org.ossreviewtoolkit.model.utils.DefaultResolutionProvider
import org.ossreviewtoolkit.reporter.DefaultLicenseTextProvider
import org.ossreviewtoolkit.reporter.HowToFixTextProvider
Expand Down Expand Up @@ -205,7 +207,8 @@ class ReporterCommand : CliktCommand(
var ortResult = readOrtResult(ortFile)

repositoryConfigurationFile?.let {
ortResult = ortResult.replaceConfig(it.readValue())
val config = it.readValueOrDefault(RepositoryConfiguration())
ortResult = ortResult.replaceConfig(config)
}

val resolutionProvider = DefaultResolutionProvider()
Expand Down
4 changes: 2 additions & 2 deletions cli/src/main/kotlin/commands/UploadCurationsCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import org.ossreviewtoolkit.clients.clearlydefined.ErrorResponse
import org.ossreviewtoolkit.clients.clearlydefined.HarvestStatus
import org.ossreviewtoolkit.model.PackageCuration
import org.ossreviewtoolkit.model.jsonMapper
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.model.readValueOrDefault
import org.ossreviewtoolkit.model.utils.toClearlyDefinedCoordinates
import org.ossreviewtoolkit.model.utils.toClearlyDefinedSourceLocation
import org.ossreviewtoolkit.utils.OkHttpClientHelper
Expand Down Expand Up @@ -96,7 +96,7 @@ class UploadCurationsCommand : CliktCommand(
}

override fun run() {
val curations = inputFile.readValue<List<PackageCuration>>()
val curations = inputFile.readValueOrDefault(emptyList<PackageCuration>())
val curationsToCoordinates = curations.mapNotNull { curation ->
curation.id.toClearlyDefinedCoordinates()?.let { coordinates ->
curation to coordinates
Expand Down

0 comments on commit 010797b

Please sign in to comment.