From 45d20a3f01233793ddeda2fda4695b7d4cb91b68 Mon Sep 17 00:00:00 2001 From: Frank Viernau Date: Tue, 25 Jan 2022 19:29:35 +0100 Subject: [PATCH] ScanCode: Work around an underscore in a SPDX license key 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] https://github.com/nexB/scancode-toolkit/issues/2813 [2] https://github.com/oss-review-toolkit/ort/commit/fb0370f28940eb5c0ace1aba09592a08d373eef9 [3] https://github.com/oss-review-toolkit/ort/pull/4523 Signed-off-by: Frank Viernau --- .../main/kotlin/scanners/scancode/ScanCodeResultParser.kt | 2 +- utils/spdx/src/main/kotlin/Utils.kt | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/scanner/src/main/kotlin/scanners/scancode/ScanCodeResultParser.kt b/scanner/src/main/kotlin/scanners/scancode/ScanCodeResultParser.kt index 4363ca3504fc8..1399b774978b5 100644 --- a/scanner/src/main/kotlin/scanners/scancode/ScanCodeResultParser.kt +++ b/scanner/src/main/kotlin/scanners/scancode/ScanCodeResultParser.kt @@ -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 diff --git a/utils/spdx/src/main/kotlin/Utils.kt b/utils/spdx/src/main/kotlin/Utils.kt index a7331422c1744..eb97c12c2a09c 100644 --- a/utils/spdx/src/main/kotlin/Utils.kt +++ b/utils/spdx/src/main/kotlin/Utils.kt @@ -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()