Skip to content

Commit

Permalink
refactor(model)!: Rename a class to ParentLicenseMatcher
Browse files Browse the repository at this point in the history
The name `RootLicenseMatcher` was confusing as not necessarily license
files from the root are matched: If no license files are in the root,
but a license file is in a parent directory on the path to the root
directory, that license would be applicable. Reflect that in several
renamings that avoid the "root" term if not appropriate.

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Oct 18, 2024
1 parent 58cbc16 commit 75a737c
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import org.ossreviewtoolkit.model.Package
import org.ossreviewtoolkit.model.ScanResult
import org.ossreviewtoolkit.model.config.OrtConfiguration
import org.ossreviewtoolkit.model.utils.FindingCurationMatcher
import org.ossreviewtoolkit.model.utils.RootLicenseMatcher
import org.ossreviewtoolkit.model.utils.ParentLicenseMatcher
import org.ossreviewtoolkit.model.yamlMapper
import org.ossreviewtoolkit.plugins.packageconfigurationproviders.dir.DirPackageConfigurationProvider
import org.ossreviewtoolkit.scanner.ScanStorages
Expand Down Expand Up @@ -94,7 +94,7 @@ internal class GetPackageLicensesCommand : OrtHelperCommand(

val detectedLicense = curatedFindings.toSpdxExpression()

val rootLicense = RootLicenseMatcher().getApplicableRootLicenseFindingsForDirectories(
val rootLicense = ParentLicenseMatcher().getApplicableLicenseFindingsForDirectories(

Check warning on line 97 in helper-cli/src/main/kotlin/commands/GetPackageLicensesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/GetPackageLicensesCommand.kt#L97

Added line #L97 was not covered by tests
licenseFindings = curatedFindings,
directories = listOf("") // TODO: use the proper VCS path.
).values.flatten().toSpdxExpression()
Expand Down
4 changes: 2 additions & 2 deletions model/src/main/kotlin/ScanResult.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package org.ossreviewtoolkit.model

import com.fasterxml.jackson.annotation.JsonInclude

import org.ossreviewtoolkit.model.utils.RootLicenseMatcher
import org.ossreviewtoolkit.model.utils.ParentLicenseMatcher

/**
* The result of a single scan of a single package.
Expand Down Expand Up @@ -51,7 +51,7 @@ data class ScanResult(
) {
/**
* Filter all detected licenses and copyrights from the [summary] which are underneath [path], and set the [path]
* for [provenance]. Findings which [RootLicenseMatcher] assigns as root license files for [path] are also kept.
* for [provenance]. Findings which [ParentLicenseMatcher] assigns as root license files for [path] are also kept.
*/
fun filterByPath(path: String): ScanResult =
when {
Expand Down
10 changes: 5 additions & 5 deletions model/src/main/kotlin/ScanSummary.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import java.time.Instant
import org.ossreviewtoolkit.model.config.LicenseFilePatterns
import org.ossreviewtoolkit.model.utils.CopyrightFindingSortedSetConverter
import org.ossreviewtoolkit.model.utils.LicenseFindingSortedSetConverter
import org.ossreviewtoolkit.model.utils.RootLicenseMatcher
import org.ossreviewtoolkit.model.utils.ParentLicenseMatcher
import org.ossreviewtoolkit.model.utils.SnippetFindingSortedSetConverter
import org.ossreviewtoolkit.utils.common.FileMatcher
import org.ossreviewtoolkit.utils.spdx.SpdxExpression
Expand Down Expand Up @@ -100,19 +100,19 @@ data class ScanSummary(

/**
* Filter all detected licenses and copyrights from this [ScanSummary] which are underneath [path]. Findings which
* [RootLicenseMatcher] assigns as root license files for [path] are also kept.
* [ParentLicenseMatcher] assigns as root license files for [path] are also kept.
*/
fun filterByPath(path: String): ScanSummary = filterByPaths(listOf(path))

/**
* Filter all detected licenses and copyrights from this [ScanSummary] which are underneath the given [paths].
* Findings which [RootLicenseMatcher] assigns as root license files for path in [paths] are also kept.
* Findings which [ParentLicenseMatcher] assigns as root license files for path in [paths] are also kept.
*/
fun filterByPaths(paths: Collection<String>): ScanSummary {
if (paths.any { it.isBlank() }) return this

val rootLicenseMatcher = RootLicenseMatcher(LicenseFilePatterns.getInstance())
val applicableLicenseFiles = rootLicenseMatcher.getApplicableRootLicenseFindingsForDirectories(
val parentLicenseMatcher = ParentLicenseMatcher(LicenseFilePatterns.getInstance())
val applicableLicenseFiles = parentLicenseMatcher.getApplicableLicenseFindingsForDirectories(
licenseFindings = licenseFindings,
directories = paths
).values.flatten().mapTo(mutableSetOf()) { it.location.path }
Expand Down
8 changes: 4 additions & 4 deletions model/src/main/kotlin/licenses/LicenseInfoResolver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import org.ossreviewtoolkit.model.config.PathExclude
import org.ossreviewtoolkit.model.utils.FileArchiver
import org.ossreviewtoolkit.model.utils.FindingCurationMatcher
import org.ossreviewtoolkit.model.utils.FindingsMatcher
import org.ossreviewtoolkit.model.utils.RootLicenseMatcher
import org.ossreviewtoolkit.model.utils.ParentLicenseMatcher
import org.ossreviewtoolkit.model.utils.prependedPath
import org.ossreviewtoolkit.utils.ort.createOrtTempDir
import org.ossreviewtoolkit.utils.spdx.SpdxSingleLicenseExpression
Expand All @@ -49,10 +49,10 @@ class LicenseInfoResolver(
) {
private val resolvedLicenseInfo = ConcurrentHashMap<Identifier, ResolvedLicenseInfo>()
private val resolvedLicenseFiles = ConcurrentHashMap<Identifier, ResolvedLicenseFileInfo>()
private val rootLicenseMatcher = RootLicenseMatcher(
private val parentLicenseMatcher = ParentLicenseMatcher(
licenseFilePatterns = licenseFilePatterns.copy(rootLicenseFilenames = emptyList())
)
private val findingsMatcher = FindingsMatcher(RootLicenseMatcher(licenseFilePatterns))
private val findingsMatcher = FindingsMatcher(ParentLicenseMatcher(licenseFilePatterns))

/**
* Get the [ResolvedLicenseInfo] for the project or package identified by [id].
Expand Down Expand Up @@ -268,7 +268,7 @@ class LicenseInfoResolver(
}

val directory = (provenance as? RepositoryProvenance)?.vcsInfo?.path.orEmpty()
val rootLicenseFiles = rootLicenseMatcher.getApplicableLicenseFilesForDirectories(
val rootLicenseFiles = parentLicenseMatcher.getApplicableLicenseFilesForDirectories(
relativeFilePaths = archiveDir.walk().filter { it.isFile }.mapTo(mutableSetOf()) {
it.toRelativeString(archiveDir)
},
Expand Down
8 changes: 4 additions & 4 deletions model/src/main/kotlin/utils/FindingsMatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ import org.ossreviewtoolkit.utils.spdx.toSpdx
/**
* A class for matching copyright findings to license findings. Copyright statements may be matched either to license
* findings located nearby in the same file or to a license found in a license file whereas the given
* [rootLicenseMatcher] determines whether a file is a license file.
* [parentLicenseMatcher] determines whether a file is a license file.
*/
class FindingsMatcher(
private val rootLicenseMatcher: RootLicenseMatcher = RootLicenseMatcher(),
private val parentLicenseMatcher: ParentLicenseMatcher = ParentLicenseMatcher(),
private val toleranceLines: Int = DEFAULT_TOLERANCE_LINES,
private val expandToleranceLines: Int = DEFAULT_EXPAND_TOLERANCE_LINES
) {
Expand Down Expand Up @@ -137,7 +137,7 @@ class FindingsMatcher(
* Associate the [copyrightFindings] to the [licenseFindings]. Copyright findings are matched to license findings
* located nearby in the same file. Copyright findings that are not located close to a license finding are
* associated to the root licenses instead. The root licenses are the licenses found in any of the license files
* defined by [rootLicenseMatcher].
* defined by [parentLicenseMatcher].
*/
fun match(licenseFindings: Set<LicenseFinding>, copyrightFindings: Set<CopyrightFinding>): FindingsMatcherResult {
val licenseFindingsByPath = licenseFindings.groupBy { it.location.path }
Expand Down Expand Up @@ -172,7 +172,7 @@ class FindingsMatcher(
licenseFindings: Set<LicenseFinding>,
copyrightFindings: Set<CopyrightFinding>
): Map<LicenseFinding, Set<CopyrightFinding>> {
val rootLicensesForDirectories = rootLicenseMatcher.getApplicableRootLicenseFindingsForDirectories(
val rootLicensesForDirectories = parentLicenseMatcher.getApplicableLicenseFindingsForDirectories(
licenseFindings = licenseFindings,
directories = copyrightFindings.map { it.location.directory() }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import org.ossreviewtoolkit.utils.common.getAllAncestorDirectories
*
* Patent files are assigned in an analog way, but without any fallback pattern.
*/
class RootLicenseMatcher(licenseFilePatterns: LicenseFilePatterns = LicenseFilePatterns.DEFAULT) {
class ParentLicenseMatcher(licenseFilePatterns: LicenseFilePatterns = LicenseFilePatterns.DEFAULT) {
private val licenseFileMatcher = createFileMatcher(licenseFilePatterns.licenseFilenames)
private val patentFileMatcher = createFileMatcher(licenseFilePatterns.patentFilenames)
private val rootLicenseFileMatcher = createFileMatcher(licenseFilePatterns.rootLicenseFilenames)
Expand All @@ -52,16 +52,16 @@ class RootLicenseMatcher(licenseFilePatterns: LicenseFilePatterns = LicenseFileP
* applicable to the respective directory. The values of the map entries are subsets of the given
* [licenseFindings].
*/
fun getApplicableRootLicenseFindingsForDirectories(
fun getApplicableLicenseFindingsForDirectories(
licenseFindings: Collection<LicenseFinding>,
directories: Collection<String>
): Map<String, Set<LicenseFinding>> {
val licenseFindingsByPath = licenseFindings.groupBy { it.location.path }

return getApplicableLicenseFilesForDirectories(
licenseFindingsByPath.keys, directories
).mapValues { (_, rootLicenseFilePath) ->
rootLicenseFilePath.flatMapTo(mutableSetOf()) { licenseFindingsByPath.getValue(it) }
).mapValues { (_, licenseFilePath) ->
licenseFilePath.flatMapTo(mutableSetOf()) { licenseFindingsByPath.getValue(it) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ private val COMMONLY_USED_LICENSE_FILE_NAMES = listOf(
"unlicense"
)

class RootLicenseMatcherTest : WordSpec({
class ParentLicenseMatcherTest : WordSpec({
"getApplicableLicenseFilesForDirectories" should {
"override license files of ancestors as expected" {
RootLicenseMatcher().getApplicableRootLicenseFindingsForDirectories(
ParentLicenseMatcher().getApplicableLicenseFindingsForDirectories(
licenseFindings = licenseFindings(
"README",
"PATENTS",
Expand All @@ -68,7 +68,7 @@ class RootLicenseMatcherTest : WordSpec({
}

"not use the readme if there is a license file" {
RootLicenseMatcher().getApplicableRootLicenseFindingsForDirectories(
ParentLicenseMatcher().getApplicableLicenseFindingsForDirectories(
licenseFindings = licenseFindings(
"README",
"LICENSE"
Expand All @@ -78,7 +78,7 @@ class RootLicenseMatcherTest : WordSpec({
}

"use the readme if there is no license but a patents file" {
RootLicenseMatcher().getApplicableRootLicenseFindingsForDirectories(
ParentLicenseMatcher().getApplicableLicenseFindingsForDirectories(
licenseFindings = licenseFindings(
"README",
"PATENTS"
Expand All @@ -89,7 +89,7 @@ class RootLicenseMatcherTest : WordSpec({

"match commonly used license file paths in lower-case" {
COMMONLY_USED_LICENSE_FILE_NAMES.map { it.lowercase() }.forAll {
RootLicenseMatcher().getApplicableRootLicenseFindingsForDirectories(
ParentLicenseMatcher().getApplicableLicenseFindingsForDirectories(
licenseFindings = licenseFindings(it),
directories = listOf("")
).paths() shouldBe mapOf("" to setOf(it))
Expand All @@ -98,7 +98,7 @@ class RootLicenseMatcherTest : WordSpec({

"match commonly used license file paths in upper-case" {
COMMONLY_USED_LICENSE_FILE_NAMES.map { it.uppercase() }.forAll {
RootLicenseMatcher().getApplicableRootLicenseFindingsForDirectories(
ParentLicenseMatcher().getApplicableLicenseFindingsForDirectories(
licenseFindings = licenseFindings(it),
directories = listOf("")
).paths() shouldBe mapOf("" to setOf(it))
Expand All @@ -107,7 +107,7 @@ class RootLicenseMatcherTest : WordSpec({

"match commonly used license file paths in capital-case" {
COMMONLY_USED_LICENSE_FILE_NAMES.map { it.uppercaseFirstChar() }.forAll {
RootLicenseMatcher().getApplicableRootLicenseFindingsForDirectories(
ParentLicenseMatcher().getApplicableLicenseFindingsForDirectories(
licenseFindings = licenseFindings(it),
directories = listOf("")
).paths() shouldBe mapOf("" to setOf(it))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import org.ossreviewtoolkit.model.licenses.LicenseView
import org.ossreviewtoolkit.model.toYaml
import org.ossreviewtoolkit.model.utils.FindingCurationMatcher
import org.ossreviewtoolkit.model.utils.FindingsMatcher
import org.ossreviewtoolkit.model.utils.RootLicenseMatcher
import org.ossreviewtoolkit.model.utils.ParentLicenseMatcher
import org.ossreviewtoolkit.model.utils.filterByVcsPath
import org.ossreviewtoolkit.model.vulnerabilities.Vulnerability
import org.ossreviewtoolkit.reporter.ReporterInput
Expand Down Expand Up @@ -78,7 +78,7 @@ internal class EvaluatedModelMapper(private val input: ReporterInput) {
private val vulnerabilitiesResolutions = mutableListOf<VulnerabilityResolution>()

private val curationsMatcher = FindingCurationMatcher()
private val findingsMatcher = FindingsMatcher(RootLicenseMatcher(input.ortConfig.licenseFilePatterns))
private val findingsMatcher = FindingsMatcher(ParentLicenseMatcher(input.ortConfig.licenseFilePatterns))

private data class PackageExcludeInfo(
var id: Identifier,
Expand Down

0 comments on commit 75a737c

Please sign in to comment.