Skip to content

Commit 7290fa3

Browse files
committed
WIP
1 parent ac6e185 commit 7290fa3

File tree

2 files changed

+14
-21
lines changed

2 files changed

+14
-21
lines changed

model/src/main/kotlin/licenses/ResolvedLicenseInfo.kt

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import org.ossreviewtoolkit.model.config.CopyrightGarbage
2828
import org.ossreviewtoolkit.model.config.LicenseFilePatterns
2929
import org.ossreviewtoolkit.model.config.PathExclude
3030
import org.ossreviewtoolkit.model.utils.PathLicenseMatcher
31+
import org.ossreviewtoolkit.model.utils.vcsPath
3132
import org.ossreviewtoolkit.utils.common.FileMatcher
3233
import org.ossreviewtoolkit.utils.ort.CopyrightStatementsProcessor
3334
import org.ossreviewtoolkit.utils.spdx.SpdxExpression
@@ -85,38 +86,31 @@ data class ResolvedLicenseInfo(
8586
*/
8687
fun mainLicense(): SpdxExpression? {
8788
val licenseFilePatterns = LicenseFilePatterns.getInstance()
88-
val fileMatcher = FileMatcher(licenseFilePatterns.allLicenseFilenames, ignoreCase = true)
8989
val licenseMatcher = PathLicenseMatcher(licenseFilePatterns)
9090

91-
// Only keep those resolved licenses that can contribute to the main license as they match the configured
92-
// license file patterns. This vastly reduces the search for applicable license files for scan results with a
93-
// lot of detected license findings, like from file headers in a large code base.
94-
val relevantResolvedLicenses = mapNotNull { resolvedLicense ->
95-
val locations = resolvedLicense.locations.filterTo(mutableSetOf()) { fileMatcher.matches(it.location.path) }
96-
if (locations.isNotEmpty()) resolvedLicense.copy(locations = locations) else null
97-
}
91+
val declaredLicenses = filter(LicenseView.ONLY_DECLARED, filterSources = true)
92+
val detectedLicenses = filter(LicenseView.ONLY_DETECTED, filterSources = true)
9893

99-
val licensePaths = relevantResolvedLicenses.flatMap { resolvedLicense ->
94+
val licensePaths = detectedLicenses.flatMapTo(mutableSetOf()) { resolvedLicense ->
10095
resolvedLicense.locations.map { it.location.path }
10196
}
10297

103-
val detectedLicenses = relevantResolvedLicenses.filterTo(mutableSetOf()) { resolvedLicense ->
104-
resolvedLicense.locations.any {
105-
val packageRoot = (it.provenance as? RepositoryProvenance)?.vcsInfo?.path.orEmpty()
98+
val packageRoot = detectedLicenses.firstOrNull()?.locations?.firstOrNull()?.provenance.vcsPath
10699

107-
val applicableLicensePaths = licenseMatcher.getApplicableLicenseFilesForDirectories(
108-
licensePaths,
109-
listOf(packageRoot)
110-
)
100+
val applicableLicensePaths = licenseMatcher.getApplicableLicenseFilesForDirectories(
101+
licensePaths,
102+
listOf(packageRoot)
103+
)
111104

112-
val applicableLicenseFiles = applicableLicensePaths[packageRoot].orEmpty()
105+
val applicableLicenseFiles = applicableLicensePaths[packageRoot].orEmpty()
113106

107+
val relevantDetectedLicenses = detectedLicenses.filterTo(mutableSetOf()) { resolvedLicense ->
108+
resolvedLicense.locations.any {
114109
it.location.path in applicableLicenseFiles
115110
}
116111
}
117112

118-
val declaredLicenses = filter(LicenseView.ONLY_DECLARED)
119-
val mainLicenses = (detectedLicenses + declaredLicenses.licenses)
113+
val mainLicenses = (declaredLicenses.licenses + relevantDetectedLicenses)
120114
.flatMap { it.originalExpressions }
121115
.map { it.expression }
122116

model/src/test/kotlin/licenses/ResolvedLicenseInfoTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ import org.ossreviewtoolkit.utils.spdx.toSpdx
4949
class ResolvedLicenseInfoTest : WordSpec({
5050
"mainLicense()" should {
5151
"return declared and detected licenses, but no concluded license" {
52-
RESOLVED_LICENSE_INFO.mainLicense() shouldBe
53-
"($APACHE OR $MIT) AND ($MIT OR $GPL) AND ($BSD OR $GPL)".toSpdx()
52+
RESOLVED_LICENSE_INFO.mainLicense() shouldBe "($APACHE OR $MIT) AND ($MIT OR $GPL)".toSpdx()
5453
}
5554
}
5655

0 commit comments

Comments
 (0)