Skip to content

Commit

Permalink
analyzer: Base FilePackageCurationProvider on `SimplePackageCuratio…
Browse files Browse the repository at this point in the history
…nProvider`

This reduces a bit of code duplication in `getCurationsFor()`.

Signed-off-by: Sebastian Schuberth <sebastian.schuberth@bosch.io>
  • Loading branch information
sschuberth committed Jan 4, 2022
1 parent bb4077d commit 2836f18
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
35 changes: 17 additions & 18 deletions analyzer/src/main/kotlin/curation/FilePackageCurationProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import java.io.File

import org.ossreviewtoolkit.analyzer.PackageCurationProvider
import org.ossreviewtoolkit.model.FileFormat
import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.PackageCuration
import org.ossreviewtoolkit.model.readValueOrDefault
import org.ossreviewtoolkit.utils.common.getDuplicates
Expand All @@ -34,7 +33,9 @@ import org.ossreviewtoolkit.utils.core.log
* A [PackageCurationProvider] that loads [PackageCuration]s from all given curation files. Supports all file formats
* specified in [FileFormat].
*/
class FilePackageCurationProvider(curationFiles: Collection<File>) : PackageCurationProvider {
class FilePackageCurationProvider(
curationFiles: Collection<File>
) : SimplePackageCurationProvider(readCurationFiles(curationFiles)) {
constructor(curationFile: File) : this(listOf(curationFile))

companion object {
Expand All @@ -45,27 +46,25 @@ class FilePackageCurationProvider(curationFiles: Collection<File>) : PackageCura

return FilePackageCurationProvider(curationFiles)
}
}

val packageCurations: Set<PackageCuration> = run {
val allCurations = curationFiles.map { curationFile ->
runCatching {
curationFile.readValueOrDefault(emptyList<PackageCuration>())
}.onFailure {
log.warn { "Failed parsing package curation from '${curationFile.absoluteFile}'." }
}.getOrThrow()
}.flatten()
fun readCurationFiles(curationFiles: Collection<File>): List<PackageCuration> {
val allCurations = curationFiles.map { curationFile ->
runCatching {
curationFile.readValueOrDefault(emptyList<PackageCuration>())
}.onFailure {
log.warn { "Failed parsing package curation from '${curationFile.absoluteFile}'." }
}.getOrThrow()
}.flatten()

val duplicates = allCurations.getDuplicates()
val duplicates = allCurations.getDuplicates()

if (duplicates.isNotEmpty()) {
throw DuplicatedCurationException("Duplicate curations found: $duplicates")
}
if (duplicates.isNotEmpty()) {
throw DuplicatedCurationException("Duplicate curations found: $duplicates")
}

allCurations.toSet()
return allCurations
}
}

override fun getCurationsFor(pkgId: Identifier) = packageCurations.filter { it.isApplicable(pkgId) }
}

private class DuplicatedCurationException(message: String?) : Exception(message)
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import org.ossreviewtoolkit.model.PackageCuration
/**
* A [PackageCurationProvider] that provides the specified [packageCurations].
*/
class SimplePackageCurationProvider(
private val packageCurations: Collection<PackageCuration>
open class SimplePackageCurationProvider(
val packageCurations: Collection<PackageCuration>
) : PackageCurationProvider {
override fun getCurationsFor(pkgId: Identifier) = packageCurations.filter { it.isApplicable(pkgId) }
}

0 comments on commit 2836f18

Please sign in to comment.