Skip to content

Commit e9a4c64

Browse files
committed
refactor: Make license classifications work with old ScanCode versions
For any detected license ORT's ScanCode integration either uses the license ID from the SPDX license list if present (preferred) or "LicenseRef-scancode-$key" otherwise. It may happen that a particular license gets added to the SPDX license list. ORT's ScanCode integration would then return a `LicenseRef-*` license identifier if used with an older ScanCode version, and a license ID from the SPDX license list when used with a recent ScanCode version. So, adjust the logic to always categorize both license IDs for any particular license. This makes the most recent classifications (backwards) compatible with older ScanCode versions. Signed-off-by: Frank Viernau <[email protected]>
1 parent 3309b35 commit e9a4c64

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

tools/curations/buildSrc/src/main/kotlin/ScanCodeLicenseDbClassifications.kt

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,17 @@ private fun downloadLicenseDetailsBatched(
136136
return result.toMap()
137137
}
138138

139-
private fun LicenseDetails.getLicenseId(): SpdxSingleLicenseExpression {
140-
val expression = spdxLicenseKey ?: "LicenseRef-scancode-$key"
141-
return SpdxSingleLicenseExpression.parse(expression)
142-
}
139+
private fun LicenseDetails.getLicenseIds(): Set<SpdxSingleLicenseExpression> =
140+
listOfNotNull(
141+
spdxLicenseKey,
142+
"LicenseRef-scancode-$key"
143+
).mapTo(mutableSetOf()) { SpdxSingleLicenseExpression.parse(it) }
143144

144145
private fun LicenseDetails.getCategories(): Set<String> {
145-
val overrideLicenseCategory = OVERRIDE_LICENSE_CATEGORIES[getLicenseId()]
146+
val overrideLicenseCategory = getLicenseIds().firstNotNullOfOrNull { licenseId ->
147+
OVERRIDE_LICENSE_CATEGORIES[licenseId]
148+
}
149+
146150
val mappedCategory = when {
147151
isUnknown -> CATEGORY_UNKNOWN
148152
isGeneric -> CATEGORY_GENERIC
@@ -169,11 +173,13 @@ private fun getLicenseClassifications(licenseDetails: Collection<LicenseDetails>
169173
LicenseCategory(category)
170174
}
171175

172-
val categorizations = licenseDetails.map { details ->
173-
LicenseCategorization(
174-
id = details.getLicenseId(),
175-
categories = details.getCategories()
176-
)
176+
val categorizations = licenseDetails.flatMap { details ->
177+
details.getLicenseIds().map { licenseId ->
178+
LicenseCategorization(
179+
id = licenseId,
180+
categories = details.getCategories()
181+
)
182+
}
177183
}
178184

179185
return LicenseClassifications(

0 commit comments

Comments
 (0)