Skip to content

Commit

Permalink
ScanCode: Gracefully handle underscores in ScanCode license keys
Browse files Browse the repository at this point in the history
ScanCode version 3.2.1rc, which is currently in use in ORT, has one
license (overall) [1] with an underscore in its license key.
ScanCodeResultParser turns the license keys into SPDX 'LicenseRef-*'
identifiers strings, and when these get parse via toSpdx() in the
constructor of LicenseFinding ORT exits with an uncaught exception.
The exception is thrown because underscores are not allowed in SPDX
id strings. Fix this by turning underscores into dashes.

Note: This change could potentially be reverted in favor of an upstream
fix in ScanCode.

[1] https://github.com/nexB/scancode-toolkit/blob/v3.2.1rc2/src/licensedcode/data/licenses/x11-xconsortium_veillard.yml

Signed-off-by: Frank Viernau <[email protected]>
  • Loading branch information
fviernau committed Oct 8, 2020
1 parent 7e1a94b commit fb0370f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 1 addition & 1 deletion scanner/src/main/kotlin/scanners/ScanCodeResultParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private fun getLicenseId(license: JsonNode): String {
// [3] https://github.com/nexB/scancode-toolkit/issues/1336
// [4] https://github.com/nexB/scancode-toolkit/pull/2247
if (name.isEmpty() || name.startsWith("LicenseRef-")) {
val key = license["key"].textValue()
val key = license["key"].textValue().replace('_', '-')
name = if (key in UNKNOWN_LICENSE_KEYS) {
SpdxConstants.NOASSERTION
} else {
Expand Down
5 changes: 4 additions & 1 deletion spdx-utils/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,12 @@ val generateLicenseRefTextResources by tasks.registering {
val lines = licenseFile.readLines().map { it.trimEnd() }.asReversed().dropWhile { it.isEmpty() }
.asReversed().dropWhile { it.isEmpty() }

// Underscores are not allowed in SPDX 'LicenseRef-*' identifiers, so turn them into dashes.
val normalizedBaseName = baseName.replace('_', '-')

// Use a "namespaced" LicenseRef ID string as the file name, similar to ScanCode itself does for
// SPDX output formats, see https://github.com/nexB/scancode-toolkit/pull/1307.
val resourceFile = resourcesDir.resolve("LicenseRef-scancode-$baseName")
val resourceFile = resourcesDir.resolve("LicenseRef-scancode-$normalizedBaseName")
resourceFile.writeText(lines.joinToString("\n", postfix = "\n"))
} else {
logger.warn("No license text found for license '$baseName'.")
Expand Down

0 comments on commit fb0370f

Please sign in to comment.