Skip to content

Commit

Permalink
ScanCode: Work around an underscore in a SPDX license key
Browse files Browse the repository at this point in the history
ScanCode has one SPDX license key containing an underscore characters
which is not allwed, see [1]. This results in ORT's scanner crashing
due to an SpdxException when it tries to parse the SPDX license key.

This issue has first occured in 2020 and been fixed by [2]. It got
re-introduced recently by [3].

Deliberatly don't fix the general problem with underscores in
`getLicenseTextFile()` in favor of a license ID specific work around,
because this can be implemented efficiently without doing a refactoring
first.

[1] aboutcode-org/scancode-toolkit#2813
[2] fb0370f
[3] #4523

Signed-off-by: Frank Viernau <[email protected]>
  • Loading branch information
fviernau committed Jan 25, 2022
1 parent def4677 commit 45d20a3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ private fun getLicenseFindings(result: JsonNode, parseExpressions: Boolean): Lis
*/
private fun getSpdxLicenseId(license: JsonNode): String {
// There is a bug in ScanCode 3.0.2 that returns an empty string instead of null for licenses unknown to SPDX.
val id = license["spdx_license_key"].textValueOrEmpty()
val id = license["spdx_license_key"].textValueOrEmpty().replace('_', '-')

// For regular SPDX IDs, return early here.
if (id.isNotEmpty() && !id.startsWith(LICENSE_REF_PREFIX)) return id
Expand Down
8 changes: 6 additions & 2 deletions utils/spdx/src/main/kotlin/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,15 @@ private val LICENSE_REF_FILENAME_REGEX by lazy { Regex("^LicenseRef-\\w+-") }

private fun getLicenseTextFile(id: String, dir: File): File? =
id.replace(LICENSE_REF_FILENAME_REGEX, "").let { idWithoutLicenseRefNamespace ->
sequenceOf(
listOfNotNull(
id,
id.removePrefix("LicenseRef-"),
idWithoutLicenseRefNamespace,
"$idWithoutLicenseRefNamespace.LICENSE"
"$idWithoutLicenseRefNamespace.LICENSE",
"x11-xconsortium_veillard.LICENSE".takeIf {
// Work around for https://github.com/nexB/scancode-toolkit/issues/2813.
id == "LicenseRef-scancode-x11-xconsortium-veillard"
}
).mapNotNull { filename ->
dir.resolve(filename).takeIf { it.isFile }
}.firstOrNull()
Expand Down

0 comments on commit 45d20a3

Please sign in to comment.