Skip to content

Commit b0ac800

Browse files
committed
fix: Remove any YAML front matter from ScanCode license files
ScanCode 32.0.0 started to prepend its `*.LICENSE` files with YAML-encoded metadata, see [1]. This is a hot fix to remove this header, if present, from the license files. A better solution will be implemented later as part of a larger refactoring of license providers. Different ScanCode versions also differ in whether license files come with a final newline or not. Align on not having a final newline to make tests pass either way. [1]: aboutcode-org/scancode-toolkit#3100 Signed-off-by: Sebastian Schuberth <[email protected]>
1 parent a9594cb commit b0ac800

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

plugins/reporters/spdx/src/funTest/assets/spdx-document-reporter-expected-output.spdx.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"dataLicense" : "CC0-1.0",
1212
"comment" : "some document comment",
1313
"hasExtractedLicensingInfos" : [ {
14-
"extractedText" : "ASMUS License\n\nDisclaimer and legal rights\n---------------------------\n\nThis file contains bugs. All representations to the contrary are void.\n\nSource code in this file and the accompanying headers and included \nfiles may be distributed free of charge by anyone, as long as full \ncredit is given and any and all liabilities are assumed by the \nrecipient.\n",
14+
"extractedText" : "ASMUS License\n\nDisclaimer and legal rights\n---------------------------\n\nThis file contains bugs. All representations to the contrary are void.\n\nSource code in this file and the accompanying headers and included \nfiles may be distributed free of charge by anyone, as long as full \ncredit is given and any and all liabilities are assumed by the \nrecipient.",
1515
"licenseId" : "LicenseRef-scancode-asmus"
1616
}, {
1717
"extractedText" : "To anyone who acknowledges that the file \"sRGB Color Space Profile.icm\" \nis provided \"AS IS\" WITH NO EXPRESS OR IMPLIED WARRANTY:\npermission to use, copy and distribute this file for any purpose is hereby \ngranted without fee, provided that the file is not changed including the HP \ncopyright notice tag, and that the name of Hewlett-Packard Company not be \nused in advertising or publicity pertaining to distribution of the software \nwithout specific, written prior permission. Hewlett-Packard Company makes \nno representations about the suitability of this software for any purpose.",

plugins/reporters/spdx/src/funTest/assets/spdx-document-reporter-expected-output.spdx.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ hasExtractedLicensingInfos:
1515
\nThis file contains bugs. All representations to the contrary are void.\n\nSource\
1616
\ code in this file and the accompanying headers and included \nfiles may be distributed\
1717
\ free of charge by anyone, as long as full \ncredit is given and any and all\
18-
\ liabilities are assumed by the \nrecipient.\n"
18+
\ liabilities are assumed by the \nrecipient."
1919
licenseId: "LicenseRef-scancode-asmus"
2020
- extractedText: "To anyone who acknowledges that the file \"sRGB Color Space Profile.icm\"\
2121
\ \nis provided \"AS IS\" WITH NO EXPRESS OR IMPLIED WARRANTY:\npermission to\

utils/spdx/src/main/kotlin/Utils.kt

+12-2
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,18 @@ fun getLicenseTextReader(
130130
): (() -> String)? {
131131
return if (id.startsWith(LICENSE_REF_PREFIX)) {
132132
getLicenseTextResource(id)?.let { { it.readText() } }
133-
?: addScanCodeLicenseTextsDir(licenseTextDirectories).firstNotNullOfOrNull {
134-
getLicenseTextFile(id, it)?.let { file -> { file.readText() } }
133+
?: addScanCodeLicenseTextsDir(licenseTextDirectories).firstNotNullOfOrNull { dir ->
134+
getLicenseTextFile(id, dir)?.let { file ->
135+
{
136+
val lines = file.readLines()
137+
138+
// Remove any YAML front matter enclosed by "---" from ScanCode license files.
139+
val licenseLines = lines.takeUnless { it.first() == "---" }
140+
?: lines.drop(1).dropWhile { it != "---" }.drop(1)
141+
142+
licenseLines.joinToString("\n").trim()
143+
}
144+
}
135145
}
136146
} else {
137147
SpdxLicense.forId(id.removeSuffix("+"))?.let { { it.text } }

utils/spdx/src/test/kotlin/UtilsTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ class UtilsTest : WordSpec() {
184184
"getLicenseText provided a custom dir" should {
185185
"return the custom license text for a license ID not known by ort but in custom dir" {
186186
val id = "LicenseRef-ort-abc"
187-
val text = "a\nb\nc\n"
187+
val text = "a\nb\nc"
188188

189189
setupTempFile(id, text)
190190

0 commit comments

Comments
 (0)