-
Notifications
You must be signed in to change notification settings - Fork 356
Improve the logic to determine the main license #10349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9739eca
43735c0
16bf895
53286ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ import org.ossreviewtoolkit.model.config.CopyrightGarbage | |
| import org.ossreviewtoolkit.model.config.LicenseFilePatterns | ||
| import org.ossreviewtoolkit.model.config.PathExclude | ||
| import org.ossreviewtoolkit.model.utils.PathLicenseMatcher | ||
| import org.ossreviewtoolkit.utils.common.FileMatcher | ||
| import org.ossreviewtoolkit.utils.ort.CopyrightStatementsProcessor | ||
| import org.ossreviewtoolkit.utils.spdx.SpdxExpression | ||
| import org.ossreviewtoolkit.utils.spdx.SpdxLicenseChoice | ||
|
|
@@ -83,22 +84,30 @@ data class ResolvedLicenseInfo( | |
| * in any of the configured [LicenseFilePatterns] matched against the root path of the package (or project). | ||
| */ | ||
| fun mainLicense(): SpdxExpression? { | ||
| val matcher = PathLicenseMatcher(LicenseFilePatterns.getInstance()) | ||
| val licensePaths = flatMap { resolvedLicense -> | ||
| val licenseFilePatterns = LicenseFilePatterns.getInstance() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. commit-message: If this fixes this serious performance issue, I believe this should be made more prominent. Furthermore, could you add some details, why exactly previously it was so slow?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For these topics, I'd refer to the original fix in ebc6fe0. |
||
| val fileMatcher = FileMatcher(licenseFilePatterns.allLicenseFilenames, ignoreCase = true) | ||
| val licenseMatcher = PathLicenseMatcher(licenseFilePatterns) | ||
|
|
||
| // Only keep those resolved licenses that can contribute to the main license as they match the configured | ||
| // license file patterns. This vastly reduces the search for applicable license files for scan results with a | ||
| // lot of detected license findings, like from file headers in a large code base. | ||
| val relevantResolvedLicenses = mapNotNull { resolvedLicense -> | ||
| val locations = resolvedLicense.locations.filterTo(mutableSetOf()) { fileMatcher.matches(it.location.path) } | ||
| if (locations.isNotEmpty()) resolvedLicense.copy(locations = locations) else null | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the filtering of resolved licenses against the paths was extracted to a function, and also some comment was added why filtering first is important for performance, it could be less likely the issue gets re-introduced. What do you think ?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does extracting the filtering to function help to avoid reintroducing the problem? Any user would still need to be aware of the function, and make use of it.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If someone attempts to refactor the code within
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided to add a code comment instead. |
||
|
|
||
| val licensePaths = relevantResolvedLicenses.flatMap { resolvedLicense -> | ||
| resolvedLicense.locations.map { it.location.path } | ||
| } | ||
|
|
||
| val applicablePathsCache = mutableMapOf<String, Map<String, Set<String>>>() | ||
| val detectedLicenses = filterTo(mutableSetOf()) { resolvedLicense -> | ||
| val detectedLicenses = relevantResolvedLicenses.filterTo(mutableSetOf()) { resolvedLicense -> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could the lines 87 -105 actually be simplyfied to something like this / would it make sense? (This would avoid calling |
||
| resolvedLicense.locations.any { | ||
| val rootPath = (it.provenance as? RepositoryProvenance)?.vcsInfo?.path.orEmpty() | ||
|
|
||
| val applicableLicensePaths = applicablePathsCache.getOrPut(rootPath) { | ||
| matcher.getApplicableLicenseFilesForDirectories( | ||
| licensePaths, | ||
| listOf(rootPath) | ||
| ) | ||
| } | ||
| val applicableLicensePaths = licenseMatcher.getApplicableLicenseFilesForDirectories( | ||
| licensePaths, | ||
| listOf(rootPath) | ||
| ) | ||
|
|
||
| val applicableLicenseFiles = applicableLicensePaths[rootPath].orEmpty() | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only maybe related: Are we actually missing some kind of
distinct()call onrelativeFilePathsin ...?As I just noticed the comment, that the issue came from large amount of paths.