Skip to content

Commit

Permalink
feat(spdx): Expose the validation of license text files' existence
Browse files Browse the repository at this point in the history
ORT has a complicate logic to search for a file containing the full license
text of a given license: several search paths are tried. Unfortunately,
the latter are not exposed, making it hard for an alternative
`LicenseTextProvider` to have the same logic. Additionally, the current
code assumes that the license files are on a local storage and the I/Os are
therefore cheap. To allow the current utility function to be used with a
`LicenseTextProvider` that works with license text files stored on a
network storage, this commit exposes the validation function.

Signed-off-by: Nicolas Nobelis <[email protected]>
  • Loading branch information
nnobelis committed Nov 28, 2024
1 parent a093540 commit 4537bc8
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions utils/spdx/src/main/kotlin/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,42 @@ fun getLicenseText(
licenseTextDirectories: List<File> = emptyList()
): String? = getLicenseTextReader(id, handleExceptions, addScanCodeLicenseTextsDir(licenseTextDirectories))?.invoke()

/**
* Get a lambda that allows to read the full text of the license with the provided SPDX [id]. Several locations are
* searched for the license text: [licenseTextDirectories], the ScanCode license texts directory, the ORT's list of SPDX
* licenses and the resources of this module. If [handleExceptions] is enabled, the [id] may also refer to a license
* with an SPDX exception.
*/
fun getLicenseTextReader(
id: String,
handleExceptions: Boolean = false,
licenseTextDirectories: List<File> = emptyList()
): (() -> String)? {
return getLicenseTextReader(id, handleExceptions, licenseTextDirectories) { dir, filename ->
dir.resolve(filename).takeIf { it.isFile }
}
}

/**
* Get a lambda that allows to read the full text of the license with the provided SPDX [id]. Several locations are
* searched for the license text: [licenseTextDirectories], the ScanCode license texts directory, the ORT's list of SPDX
* licenses and the resources of this module. If [handleExceptions] is enabled, the [id] may also refer to a license
* with an SPDX exception.
* [validateFile] is a lambda that is called with the directory and filename to look if the license file exists. It
* should return the file if it exists, null otherwise.
*/
fun getLicenseTextReader(
id: String,
handleExceptions: Boolean = false,
licenseTextDirectories: List<File> = emptyList(),
validateFile: (File, String) -> File?
): (() -> String)? {
return if (id.startsWith(LICENSE_REF_PREFIX)) {
getLicenseTextResource(id)?.let { { it.readText() } }
?: addScanCodeLicenseTextsDir(licenseTextDirectories).firstNotNullOfOrNull { dir ->
getLicenseTextFile(id, dir)?.let { file ->
getLicenseTextFile(id) { filename ->
validateFile(dir, filename)
}?.let { file ->
{
file.readText().removeYamlFrontMatter()
}
Expand All @@ -140,7 +167,7 @@ private fun getLicenseTextResource(id: String): URL? = object {}.javaClass.getRe

private val LICENSE_REF_FILENAME_REGEX by lazy { Regex("^$LICENSE_REF_PREFIX\\w+-") }

private fun getLicenseTextFile(id: String, dir: File): File? =
fun getLicenseTextFile(id: String, validateFile: (String) -> File?): File? =
id.replace(LICENSE_REF_FILENAME_REGEX, "").let { idWithoutLicenseRefNamespace ->
listOfNotNull(
id,
Expand All @@ -153,7 +180,7 @@ private fun getLicenseTextFile(id: String, dir: File): File? =
id == "LicenseRef-scancode-x11-xconsortium-veillard"
}
).firstNotNullOfOrNull { filename ->
dir.resolve(filename).takeIf { it.isFile }
validateFile(filename)
}
}

Expand Down

0 comments on commit 4537bc8

Please sign in to comment.