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 Apr 23, 2021
1 parent 8852d70 commit b6827fb
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 19 deletions.
10 changes: 4 additions & 6 deletions analyzer/src/main/kotlin/Analyzer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import org.ossreviewtoolkit.model.Repository
import org.ossreviewtoolkit.model.VcsInfo
import org.ossreviewtoolkit.model.config.AnalyzerConfiguration
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.model.readValueOrNull
import org.ossreviewtoolkit.utils.Environment
import org.ossreviewtoolkit.utils.ORT_REPO_CONFIG_FILENAME
import org.ossreviewtoolkit.utils.log
Expand All @@ -57,13 +57,11 @@ class Analyzer(private val config: AnalyzerConfiguration) {
val actualRepositoryConfigurationFile = repositoryConfigurationFile
?: absoluteProjectPath.resolve(ORT_REPO_CONFIG_FILENAME)

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

actualRepositoryConfigurationFile.readValue()
} else {
RepositoryConfiguration()
}
actualRepositoryConfigurationFile.readValueOrNull()
} ?: RepositoryConfiguration()

log.debug { "Using the following configuration settings:\n$repositoryConfiguration" }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ 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>>()
curationFile.readValueOrDefault(emptyList())
}

override fun getCurationsFor(pkgId: Identifier) = packageCurations.filter { it.isApplicable(pkgId) }
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 @@ -45,16 +45,19 @@ import org.ossreviewtoolkit.GroupTypes.StringType
import org.ossreviewtoolkit.evaluator.Evaluator
import org.ossreviewtoolkit.model.FileFormat
import org.ossreviewtoolkit.model.OrtResult
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.model.writeValue
import org.ossreviewtoolkit.utils.ORT_COPYRIGHT_GARBAGE_FILENAME
Expand Down Expand Up @@ -239,11 +242,13 @@ class EvaluatorCommand : CliktCommand(name = "evaluate", help = "Evaluate ORT re
}

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 finalOrtResult = requireNotNull(ortResultInput) {
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 @@ -45,6 +45,7 @@ import org.ossreviewtoolkit.GlobalOptions
import org.ossreviewtoolkit.model.OrtResult
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 @@ -53,6 +54,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 @@ -207,7 +209,8 @@ class ReporterCommand : CliktCommand(
}

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 @@ -48,7 +48,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 @@ -95,7 +95,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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import java.io.IOException

import org.ossreviewtoolkit.helper.common.download
import org.ossreviewtoolkit.model.PackageCuration
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.model.readValueOrDefault
import org.ossreviewtoolkit.utils.collectMessagesAsString
import org.ossreviewtoolkit.utils.expandTilde

Expand All @@ -44,7 +44,7 @@ internal class VerifySourceArtifactCurationsCommand : CliktCommand(
.convert { it.absoluteFile.normalize() }

override fun run() {
val curations = packageCurationsFile.readValue<List<PackageCuration>>()
val curations = packageCurationsFile.readValueOrDefault(emptyList<PackageCuration>())

val failed = curations.filterNot { curation ->
curation.data.sourceArtifact?.let { sourceArtifact ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.Provenance
import org.ossreviewtoolkit.model.config.PackageConfiguration
import org.ossreviewtoolkit.model.config.VcsMatcher
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.model.readValueOrDefault
import org.ossreviewtoolkit.model.readValueOrNull

/**
* A provider for [PackageConfiguration]s providing exactly the packages of the given list.
Expand All @@ -48,9 +49,9 @@ class SimplePackageConfigurationProvider(
* configuration per [Identifier] and [Provenance].
*/
fun forDirectory(directory: File): SimplePackageConfigurationProvider {
val entries = findPackageConfigurationFiles(directory).mapTo(mutableListOf()) { file ->
val entries = findPackageConfigurationFiles(directory).mapNotNullTo(mutableListOf()) { file ->
try {
file.readValue<PackageConfiguration>()
file.readValueOrNull<PackageConfiguration>()
} catch (e: IOException) {
throw IOException("Error reading package configuration from '${file.absoluteFile}'.", e)
}
Expand All @@ -67,7 +68,7 @@ class SimplePackageConfigurationProvider(
* file. Throws an exception if there is more than one configuration per [Identifier] and [Provenance].
*/
fun forFile(file: File): SimplePackageConfigurationProvider {
val entries = file.readValue<List<PackageConfiguration>>()
val entries = file.readValueOrDefault(emptyList<PackageConfiguration>())

return SimplePackageConfigurationProvider(entries)
}
Expand Down

0 comments on commit b6827fb

Please sign in to comment.