Skip to content

Commit

Permalink
feat(scancode): Run major versions >= 32 with an additional option
Browse files Browse the repository at this point in the history
License reference information has been excluded by default from the
result [1]. As that information is required to map ScanCode license keys
to SPDX IDs [2], explicitly enable that option.

[1]: aboutcode-org/scancode-toolkit@84eda21
[2]: aboutcode-org/scancode-toolkit#3458

Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com>
  • Loading branch information
sschuberth committed Sep 1, 2023
1 parent 34f8e5c commit e35f94d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
12 changes: 10 additions & 2 deletions plugins/scanners/scancode/src/main/kotlin/ScanCode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import org.ossreviewtoolkit.utils.ort.createOrtTempDir

import org.semver4j.RangesList
import org.semver4j.RangesListFactory
import org.semver4j.Semver

/**
* A wrapper for [ScanCode](https://github.com/nexB/scancode-toolkit).
Expand All @@ -63,6 +64,7 @@ class ScanCode internal constructor(
companion object : Logging {
const val SCANNER_NAME = "ScanCode"

private const val LICENSE_REFERENCES_OPTION_VERSION = "32.0.0"
private const val OUTPUT_FORMAT = "json-pp"
internal const val TIMEOUT = 300

Expand Down Expand Up @@ -113,12 +115,18 @@ class ScanCode internal constructor(
private val nonConfigurationOptions = scanCodeConfiguration["commandLineNonConfig"]?.splitOnWhitespace()
?: DEFAULT_NON_CONFIGURATION_OPTIONS

val commandLineOptions by lazy {
internal fun getCommandLineOptions(version: String) =
buildList {
addAll(configurationOptions)
addAll(nonConfigurationOptions)

if (Semver(version).isGreaterThanOrEqualTo(LICENSE_REFERENCES_OPTION_VERSION)) {
// Required to be able to map ScanCode license keys to SPDX IDs.
add("--license-references")
}
}
}

val commandLineOptions by lazy { getCommandLineOptions(version) }

override fun command(workingDir: File?) =
listOfNotNull(workingDir, if (Os.isWindows) "scancode.bat" else "scancode").joinToString(File.separator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import kotlinx.serialization.json.JsonTransformingSerializer
@Serializable
data class ScanCodeResult(
val headers: List<HeaderEntry>,
val files: List<FileEntry>
val files: List<FileEntry>,
val licenseReferences: List<LicenseReference>? = null // Available only with "--license-references".
)

@Serializable
Expand Down Expand Up @@ -96,6 +97,12 @@ sealed interface CopyrightEntry {
}
}

@Serializable
data class LicenseReference(
val key: String,
val spdxLicenseKey: String
)

/**
* A serializer that wraps an old primitive input option from ScanCode 3 into an array like it is for recent ScanCode
* versions. Note that the input option format changed before the output format version property was introduced.
Expand Down
10 changes: 6 additions & 4 deletions plugins/scanners/scancode/src/test/kotlin/ScanCodeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ class ScanCodeTest : WordSpec({
}
}

"commandLineOptions" should {
"getCommandLineOptions()" should {
"contain the default values if the scanner configuration is empty" {
scanner.commandLineOptions.joinToString(" ") shouldMatch
scanner.getCommandLineOptions("31.2.4").joinToString(" ") shouldMatch
"--copyright --license --info --strip-root --timeout 300 --processes \\d+"
scanner.getCommandLineOptions("32.0.0").joinToString(" ") shouldMatch
"--copyright --license --info --strip-root --timeout 300 --processes \\d+ --license-references"
}

"contain the values from the scanner configuration" {
Expand All @@ -82,7 +84,7 @@ class ScanCodeTest : WordSpec({
)
)

scannerWithConfig.commandLineOptions.joinToString(" ") shouldBe
scannerWithConfig.getCommandLineOptions("31.2.4").joinToString(" ") shouldBe
"--command --line --commandLineNonConfig"
}

Expand All @@ -99,7 +101,7 @@ class ScanCodeTest : WordSpec({
)
)

scannerWithConfig.commandLineOptions shouldBe listOf("--command", "--line", "-n", "-c")
scannerWithConfig.getCommandLineOptions("31.2.4") shouldBe listOf("--command", "--line", "-n", "-c")
}
}

Expand Down

0 comments on commit e35f94d

Please sign in to comment.