From 1007cd887f12a22ab9962ee01f5e709a993fd212 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Fri, 23 Apr 2021 08:14:47 +0200 Subject: [PATCH] Handle user-facing empty configuration files leniently Besides for repository configuration files (".ort.yml"), this is also done for package curation files as otherwise some code areas would look inconsistent. Fixes #3861. Signed-off-by: Sebastian Schuberth --- .../main/kotlin/curation/FilePackageCurationProvider.kt | 6 ++---- cli/src/main/kotlin/commands/AnalyzerCommand.kt | 4 ++-- cli/src/main/kotlin/commands/EvaluatorCommand.kt | 9 +++++++-- cli/src/main/kotlin/commands/ReporterCommand.kt | 5 ++++- cli/src/main/kotlin/commands/UploadCurationsCommand.kt | 4 ++-- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/analyzer/src/main/kotlin/curation/FilePackageCurationProvider.kt b/analyzer/src/main/kotlin/curation/FilePackageCurationProvider.kt index 31413d541b8cc..0637d9a271890 100644 --- a/analyzer/src/main/kotlin/curation/FilePackageCurationProvider.kt +++ b/analyzer/src/main/kotlin/curation/FilePackageCurationProvider.kt @@ -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 by lazy { - curationFile.readValue>() - } + internal val packageCurations by lazy { curationFile.readValueOrDefault(emptyList()) } override fun getCurationsFor(pkgId: Identifier) = packageCurations.filter { it.isApplicable(pkgId) } } diff --git a/cli/src/main/kotlin/commands/AnalyzerCommand.kt b/cli/src/main/kotlin/commands/AnalyzerCommand.kt index 66d6baf65aad7..e6992185dab28 100644 --- a/cli/src/main/kotlin/commands/AnalyzerCommand.kt +++ b/cli/src/main/kotlin/commands/AnalyzerCommand.kt @@ -50,7 +50,7 @@ import org.ossreviewtoolkit.cli.utils.writeOrtResult import org.ossreviewtoolkit.model.FileFormat import org.ossreviewtoolkit.model.Severity 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 @@ -172,7 +172,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( diff --git a/cli/src/main/kotlin/commands/EvaluatorCommand.kt b/cli/src/main/kotlin/commands/EvaluatorCommand.kt index 66e8d1f82d141..703cbb2f7a7be 100644 --- a/cli/src/main/kotlin/commands/EvaluatorCommand.kt +++ b/cli/src/main/kotlin/commands/EvaluatorCommand.kt @@ -53,9 +53,11 @@ import org.ossreviewtoolkit.cli.utils.readOrtResult import org.ossreviewtoolkit.cli.utils.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 @@ -63,6 +65,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.mergeLabels import org.ossreviewtoolkit.utils.ORT_COPYRIGHT_GARBAGE_FILENAME import org.ossreviewtoolkit.utils.ORT_LICENSE_CLASSIFICATIONS_FILENAME @@ -241,11 +244,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()) + ortResultInput = ortResultInput.replacePackageCurations(curations) } val packageConfigurationProvider = packageConfigurationOption.createProvider() diff --git a/cli/src/main/kotlin/commands/ReporterCommand.kt b/cli/src/main/kotlin/commands/ReporterCommand.kt index 089545fad847d..5b0d66b16f2d7 100644 --- a/cli/src/main/kotlin/commands/ReporterCommand.kt +++ b/cli/src/main/kotlin/commands/ReporterCommand.kt @@ -51,6 +51,7 @@ import org.ossreviewtoolkit.cli.utils.outputGroup import org.ossreviewtoolkit.cli.utils.readOrtResult 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 @@ -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 @@ -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() diff --git a/cli/src/main/kotlin/commands/UploadCurationsCommand.kt b/cli/src/main/kotlin/commands/UploadCurationsCommand.kt index e525d0f7cdf8e..f7fbbdcb5c450 100644 --- a/cli/src/main/kotlin/commands/UploadCurationsCommand.kt +++ b/cli/src/main/kotlin/commands/UploadCurationsCommand.kt @@ -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 @@ -96,7 +96,7 @@ class UploadCurationsCommand : CliktCommand( } override fun run() { - val curations = inputFile.readValue>() + val curations = inputFile.readValueOrDefault(emptyList()) val curationsToCoordinates = curations.mapNotNull { curation -> curation.id.toClearlyDefinedCoordinates()?.let { coordinates -> curation to coordinates