Skip to content

Commit

Permalink
Use the new OrtResult.load() function across the code base
Browse files Browse the repository at this point in the history
Signed-off-by: Oliver Heger <oliver.heger@bosch.io>
  • Loading branch information
oheger-bosch committed Apr 28, 2021
1 parent 775aaf5 commit e80d500
Show file tree
Hide file tree
Showing 28 changed files with 31 additions and 47 deletions.
3 changes: 1 addition & 2 deletions advisor/src/main/kotlin/Advisor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import org.ossreviewtoolkit.model.AdvisorRun
import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.OrtResult
import org.ossreviewtoolkit.model.config.AdvisorConfiguration
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.utils.Environment
import org.ossreviewtoolkit.utils.formatSizeInMib
import org.ossreviewtoolkit.utils.log
Expand All @@ -60,7 +59,7 @@ class Advisor(
fun retrieveVulnerabilityInformation(ortFile: File, skipExcluded: Boolean = false): OrtResult {
val startTime = Instant.now()

val (ortResult, duration) = measureTimedValue { ortFile.readValue<OrtResult>() }
val (ortResult, duration) = measureTimedValue { OrtResult.load(ortFile) }

log.perf {
"Read ORT result from '${ortFile.name}' (${ortFile.formatSizeInMib}) in ${duration.inMilliseconds}ms."
Expand Down
3 changes: 1 addition & 2 deletions advisor/src/test/kotlin/advisors/VulnerableCodeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import org.ossreviewtoolkit.model.Severity
import org.ossreviewtoolkit.model.Vulnerability
import org.ossreviewtoolkit.model.VulnerabilityReference
import org.ossreviewtoolkit.model.config.VulnerableCodeConfiguration
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.model.utils.toPurl
import org.ossreviewtoolkit.utils.test.shouldNotBeNull

Expand Down Expand Up @@ -268,7 +267,7 @@ private fun resultFile(): File = File(TEST_FILES_ROOT).resolve(TEST_RESULT_NAME)
* Return a list with [Package]s from the analyzer result file that serve as input for the [VulnerableCode] advisor.
*/
private fun inputPackages(): List<Package> =
resultFile().readValue<OrtResult>().getPackages(false).map { it.pkg }
OrtResult.load(resultFile()).getPackages(false).map { it.pkg }

/**
* Generate the JSON body of the request to query information about packages. It mainly consists of an array with the
Expand Down
2 changes: 1 addition & 1 deletion cli/src/funTest/kotlin/ExamplesFunTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class ExamplesFunTest : StringSpec() {
"rules.kts can be compiled and executed" {
val resultFile = File("src/funTest/assets/semver4j-analyzer-result.yml")
val licenseFile = File("../examples/license-classifications.yml")
val ortResult = resultFile.readValue<OrtResult>()
val ortResult = OrtResult.load(resultFile)
val evaluator = Evaluator(
ortResult = ortResult,
licenseInfoResolver = ortResult.createLicenseInfoResolver(),
Expand Down
5 changes: 2 additions & 3 deletions cli/src/funTest/kotlin/OrtMainFunTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import kotlin.io.path.createTempDirectory

import org.ossreviewtoolkit.downloader.VersionControlSystem
import org.ossreviewtoolkit.model.OrtResult
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.utils.ORT_NAME
import org.ossreviewtoolkit.utils.normalizeVcsUrl
import org.ossreviewtoolkit.utils.redirectStdout
Expand Down Expand Up @@ -131,7 +130,7 @@ class OrtMainFunTest : StringSpec() {
"-o", analyzerOutputDir.path
)

val analyzerResult = analyzerOutputDir.resolve("analyzer-result.yml").readValue<OrtResult>()
val analyzerResult = OrtResult.load(analyzerOutputDir.resolve("analyzer-result.yml"))
val resolvedResult = analyzerResult.withResolvedScopes()

patchActualResult(resolvedResult, patchStartAndEndTime = true) shouldBe expectedResult
Expand All @@ -154,7 +153,7 @@ class OrtMainFunTest : StringSpec() {
"--package-curations-file", projectDir.resolve("gradle/curations.yml").toString()
)

val analyzerResult = analyzerOutputDir.resolve("analyzer-result.yml").readValue<OrtResult>()
val analyzerResult = OrtResult.load(analyzerOutputDir.resolve("analyzer-result.yml"))
val resolvedResult = analyzerResult.withResolvedScopes()

patchActualResult(resolvedResult, patchStartAndEndTime = true) shouldBe expectedResult
Expand Down
3 changes: 1 addition & 2 deletions cli/src/main/kotlin/commands/DownloaderCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import org.ossreviewtoolkit.model.Package
import org.ossreviewtoolkit.model.RemoteArtifact
import org.ossreviewtoolkit.model.VcsInfo
import org.ossreviewtoolkit.model.VcsType
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.spdx.VCS_DIRECTORIES
import org.ossreviewtoolkit.utils.ArchiveType
import org.ossreviewtoolkit.utils.collectMessagesAsString
Expand Down Expand Up @@ -180,7 +179,7 @@ class DownloaderCommand : CliktCommand(name = "download", help = "Fetch source c
when (input) {
is FileType -> {
val ortFile = (input as FileType).file
val (ortResult, duration) = measureTimedValue { ortFile.readValue<OrtResult>() }
val (ortResult, duration) = measureTimedValue { OrtResult.load(ortFile) }

log.perf {
"Read ORT result from '${ortFile.name}' (${ortFile.formatSizeInMib}) in " +
Expand Down
2 changes: 1 addition & 1 deletion cli/src/main/kotlin/commands/EvaluatorCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class EvaluatorCommand : CliktCommand(name = "evaluate", help = "Evaluate ORT re
}
}

var (ortResultInput, readDuration) = measureTimedValue { ortFile?.readValue<OrtResult>() }
var (ortResultInput, readDuration) = measureTimedValue { ortFile?.let(OrtResult::load) }

ortFile?.let { file ->
log.perf {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/main/kotlin/commands/ReporterCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ class ReporterCommand : CliktCommand(
private val globalOptionsForSubcommands by requireObject<GlobalOptions>()

override fun run() {
var (ortResult, duration) = measureTimedValue { ortFile.readValue<OrtResult>() }
var (ortResult, duration) = measureTimedValue { OrtResult.load(ortFile) }

log.perf {
"Read ORT result from '${ortFile.name}' (${ortFile.formatSizeInMib}) in ${duration.inMilliseconds}ms."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import org.jetbrains.exposed.sql.transactions.transaction
import org.ossreviewtoolkit.GlobalOptions
import org.ossreviewtoolkit.model.OrtResult
import org.ossreviewtoolkit.model.config.PostgresStorageConfiguration
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.model.utils.DatabaseUtils
import org.ossreviewtoolkit.model.utils.DatabaseUtils.checkDatabaseEncoding
import org.ossreviewtoolkit.model.utils.DatabaseUtils.tableExists
Expand Down Expand Up @@ -87,7 +86,7 @@ class UploadResultToPostgresCommand : CliktCommand(
private val globalOptionsForSubcommands by requireObject<GlobalOptions>()

override fun run() {
val (ortResult, duration) = measureTimedValue { ortFile.readValue<OrtResult>() }
val (ortResult, duration) = measureTimedValue { OrtResult.load(ortFile) }

log.perf {
"Read ORT result from '${ortFile.name}' (${ortFile.formatSizeInMib}) in ${duration.inMilliseconds}ms."
Expand Down
3 changes: 1 addition & 2 deletions cli/src/main/kotlin/commands/UploadResultToSw360Command.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import org.ossreviewtoolkit.model.Package
import org.ossreviewtoolkit.model.Project
import org.ossreviewtoolkit.model.config.Sw360StorageConfiguration
import org.ossreviewtoolkit.model.jsonMapper
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.model.utils.toPurl
import org.ossreviewtoolkit.utils.collectMessagesAsString
import org.ossreviewtoolkit.utils.expandTilde
Expand All @@ -76,7 +75,7 @@ class UploadResultToSw360Command : CliktCommand(
private val globalOptionsForSubcommands by requireObject<GlobalOptions>()

override fun run() {
val (ortResult, duration) = measureTimedValue { ortFile.readValue<OrtResult>() }
val (ortResult, duration) = measureTimedValue { OrtResult.load(ortFile) }

log.perf {
"Read ORT result from '${ortFile.name}' (${ortFile.formatSizeInMib}) in ${duration.inMilliseconds}ms."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ internal class ExportLicenseFindingCurationsCommand : CliktCommand(
).flag()

override fun run() {
val ortResult = ortFile.readValue<OrtResult>().replaceConfig(repositoryConfigurationFile)
val ortResult = OrtResult.load(ortFile).replaceConfig(repositoryConfigurationFile)

val localLicenseFindingCurations = getLicenseFindingCurationsByRepository(
curations = ortResult.repository.config.curations.licenseFindings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ internal class ExportPathExcludesCommand : CliktCommand(
).flag()

override fun run() {
val localPathExcludes = ortFile
.readValue<OrtResult>()
val localPathExcludes = OrtResult.load(ortFile)
.replaceConfig(repositoryConfigurationFile)
.getRepositoryPathExcludes()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import com.github.ajalt.clikt.parameters.types.file

import org.ossreviewtoolkit.helper.common.write
import org.ossreviewtoolkit.model.OrtResult
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.utils.expandTilde

class ExtractRepositoryConfigurationCommand : CliktCommand(
Expand All @@ -50,8 +49,7 @@ class ExtractRepositoryConfigurationCommand : CliktCommand(
.required()

override fun run() {
ortFile
.readValue<OrtResult>()
OrtResult.load(ortFile)
.repository
.config.write(repositoryConfigurationFile)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ internal class GenerateProjectExcludesCommand : CliktCommand(
RepositoryConfiguration()
}

val ortResult = ortFile.readValue<OrtResult>()
val ortResult = OrtResult.load(ortFile)
.replaceConfig(repositoryConfiguration)

val generatedPathExcludes = ortResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ internal class GenerateRuleViolationResolutionsCommand : CliktCommand(

override fun run() {
val repositoryConfiguration = repositoryConfigurationFile.readValue<RepositoryConfiguration>()
val ortResult = ortFile.readValue<OrtResult>().replaceConfig(repositoryConfiguration)
val ortResult = OrtResult.load(ortFile).replaceConfig(repositoryConfiguration)

val generatedResolutions = ortResult
.getUnresolvedRuleViolations()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal class GenerateScopeExcludesCommand : CliktCommand(
.required()

override fun run() {
val ortResult = ortFile.readValue<OrtResult>()
val ortResult = OrtResult.load(ortFile)
val scopeExcludes = ortResult.generateScopeExcludes()

repositoryConfigurationFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ internal class GenerateTimeoutErrorResolutionsCommand : CliktCommand(
).flag()

override fun run() {
val ortResult = ortFile.readValue<OrtResult>().replaceConfig(repositoryConfigurationFile)
val ortResult = OrtResult.load(ortFile).replaceConfig(repositoryConfigurationFile)

val resolutionProvider = DefaultResolutionProvider().apply {
var resolutions = Resolutions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ internal class ImportLicenseFindingCurationsCommand : CliktCommand(
private val findingCurationMatcher = FindingCurationMatcher()

override fun run() {
val ortResult = ortFile.readValue<OrtResult>()
val ortResult = OrtResult.load(ortFile)
val repositoryConfiguration = if (repositoryConfigurationFile.isFile) {
repositoryConfigurationFile.readValue()
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import com.github.ajalt.clikt.parameters.options.required
import com.github.ajalt.clikt.parameters.types.file

import org.ossreviewtoolkit.model.OrtResult
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.scanner.storages.FileBasedStorage
import org.ossreviewtoolkit.utils.expandTilde
import org.ossreviewtoolkit.utils.storage.LocalFileStorage
Expand All @@ -51,7 +50,7 @@ internal class ImportScanResultsCommand : CliktCommand(
.required()

override fun run() {
val ortResult = ortFile.readValue<OrtResult>()
val ortResult = OrtResult.load(ortFile)
val scanResultsStorage = FileBasedStorage(LocalFileStorage(scanResultsStorageDir))
val ids = ortResult.getProjects().map { it.id } + ortResult.getPackages().map { it.pkg.id }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ internal class ListCopyrightsCommand : CliktCommand(
).single()

override fun run() {
val ortResult = ortFile.readValue<OrtResult>()
val ortResult = OrtResult.load(ortFile)
val copyrightGarbage = copyrightGarbageFile?.readValue<CopyrightGarbage>().orEmpty()
val packageConfigurationProvider = packageConfigurationOption.createProvider()

Expand Down
3 changes: 1 addition & 2 deletions helper-cli/src/main/kotlin/commands/ListLicensesCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import org.ossreviewtoolkit.model.Provenance
import org.ossreviewtoolkit.model.RepositoryProvenance
import org.ossreviewtoolkit.model.Severity
import org.ossreviewtoolkit.model.TextLocation
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.spdx.SpdxSingleLicenseExpression
import org.ossreviewtoolkit.utils.expandTilde

Expand Down Expand Up @@ -155,7 +154,7 @@ internal class ListLicensesCommand : CliktCommand(
}.default(emptyList())

override fun run() {
val ortResult = ortFile.readValue<OrtResult>().replaceConfig(repositoryConfigurationFile)
val ortResult = OrtResult.load(ortFile).replaceConfig(repositoryConfigurationFile)

if (ortResult.getPackageOrProject(packageId) == null) {
throw UsageError("Could not find the package for the given id '${packageId.toCoordinates()}'.")
Expand Down
3 changes: 1 addition & 2 deletions helper-cli/src/main/kotlin/commands/ListPackagesCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import com.github.ajalt.clikt.parameters.types.file
import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.OrtResult
import org.ossreviewtoolkit.model.licenses.LicenseView
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.model.utils.createLicenseInfoResolver
import org.ossreviewtoolkit.utils.expandTilde

Expand All @@ -51,7 +50,7 @@ class ListPackagesCommand : CliktCommand(
).split(",").default(emptyList())

override fun run() {
val ortResult = ortFile.readValue<OrtResult>()
val ortResult = OrtResult.load(ortFile)

val licenseInfoResolver = ortResult.createLicenseInfoResolver()

Expand Down
4 changes: 1 addition & 3 deletions helper-cli/src/main/kotlin/commands/MapCopyrightsCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import com.github.ajalt.clikt.parameters.types.file

import org.ossreviewtoolkit.helper.common.processAllCopyrightStatements
import org.ossreviewtoolkit.model.OrtResult
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.utils.expandTilde

internal class MapCopyrightsCommand : CliktCommand(
Expand Down Expand Up @@ -61,8 +60,7 @@ internal class MapCopyrightsCommand : CliktCommand(
override fun run() {
val processedCopyrightStatements = inputCopyrightGarbageFile.readLines().filterNot { it.isBlank() }

val unprocessedCopyrightStatements = ortFile
.readValue<OrtResult>()
val unprocessedCopyrightStatements = OrtResult.load(ortFile)
.getUnprocessedCopyrightStatements(processedCopyrightStatements)

outputCopyrightsFile.writeText(unprocessedCopyrightStatements.joinToString(separator = "\n"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ internal class RemoveConfigurationEntriesCommand : CliktCommand(

override fun run() {
val repositoryConfiguration = repositoryConfigurationFile.readValue<RepositoryConfiguration>()
val ortResult = ortFile.readValue<OrtResult>().replaceConfig(repositoryConfiguration)
val ortResult = OrtResult.load(ortFile).replaceConfig(repositoryConfiguration)

val pathExcludes = findFilesRecursive(sourceCodeDir).let { allFiles ->
ortResult.getExcludes().paths.filter { pathExclude ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import org.ossreviewtoolkit.model.Provenance
import org.ossreviewtoolkit.model.RepositoryProvenance
import org.ossreviewtoolkit.model.ScanSummary
import org.ossreviewtoolkit.model.VcsType
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.model.writeValue
import org.ossreviewtoolkit.utils.expandTilde

Expand Down Expand Up @@ -64,8 +63,8 @@ internal class SubtractScanResultsCommand : CliktCommand(
.required()

override fun run() {
val lhsOrtResult = lhsOrtFile.readValue<OrtResult>()
val rhsOrtResult = rhsOrtFile.readValue<OrtResult>()
val lhsOrtResult = OrtResult.load(lhsOrtFile)
val rhsOrtResult = OrtResult.load(rhsOrtFile)

val rhsScanSummaries = rhsOrtResult.scanner!!.results.scanResults.flatMap { it.value }.associateBy(
keySelector = { it.provenance.key() },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class ImportLicenseFindingCurationsCommand : CliktCommand(
private val findingCurationMatcher = FindingCurationMatcher()

override fun run() {
val ortResult = ortFile.readValue<OrtResult>()
val ortResult = OrtResult.load(ortFile)
val packageConfiguration = packageConfigurationFile.readValue<PackageConfiguration>()

val allLicenseFindings = ortResult.getScanResultFor(packageConfiguration)?.summary?.licenseFindings.orEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal class RemoveEntriesCommand : CliktCommand(

override fun run() {
val packageConfiguration = packageConfigurationFile.readValue<PackageConfiguration>()
val ortResult = ortFile.readValue<OrtResult>()
val ortResult = OrtResult.load(ortFile)
val scanResult = ortResult.getScanResultFor(packageConfiguration)

if (scanResult == null) {
Expand Down
4 changes: 2 additions & 2 deletions model/src/test/kotlin/AdvisorResultContainerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ class AdvisorResultContainerTest : WordSpec() {
"be deserialized with vulnerabilities in the initial format" {
val resultsFile = File("src/test/assets/advisor-result-initial.yml")

val result = resultsFile.readValue<OrtResult>()
val result = OrtResult.load(resultsFile)

result.advisor shouldNot beNull()
}

"be deserialized with vulnerabilities in format with references" {
val resultsFile = File("src/test/assets/advisor-result-vulnerability-refs.yml")

val result = resultsFile.readValue<OrtResult>()
val result = OrtResult.load(resultsFile)

result.advisor shouldNot beNull()
}
Expand Down
3 changes: 1 addition & 2 deletions scanner/src/main/kotlin/Scanner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import org.ossreviewtoolkit.model.ScannerRun
import org.ossreviewtoolkit.model.config.DownloaderConfiguration
import org.ossreviewtoolkit.model.config.ScannerConfiguration
import org.ossreviewtoolkit.model.config.ScannerOptions
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.model.utils.filterByProject
import org.ossreviewtoolkit.spdx.SpdxConstants
import org.ossreviewtoolkit.spdx.SpdxLicense
Expand Down Expand Up @@ -80,7 +79,7 @@ abstract class Scanner(
fun scanOrtResult(ortFile: File, outputDirectory: File, skipExcluded: Boolean = false): OrtResult {
val startTime = Instant.now()

val (ortResult, duration) = measureTimedValue { ortFile.readValue<OrtResult>() }
val (ortResult, duration) = measureTimedValue { OrtResult.load(ortFile) }

log.perf {
"Read ORT result from '${ortFile.name}' (${ortFile.formatSizeInMib}) in ${duration.inMilliseconds}ms."
Expand Down

0 comments on commit e80d500

Please sign in to comment.