-
Notifications
You must be signed in to change notification settings - Fork 315
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
Several scanner and plugin related refactorings #7572
Changes from 5 commits
087b883
8082cac
ba58760
ce9237d
5e80610
87d7a06
d362423
3995d61
d829827
73afc0e
f14e2dc
1c993d0
e626462
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 |
---|---|---|
|
@@ -20,23 +20,11 @@ | |
package org.ossreviewtoolkit.scanner | ||
|
||
import org.ossreviewtoolkit.model.ScannerDetails | ||
import org.ossreviewtoolkit.model.config.ScannerConfiguration | ||
import org.ossreviewtoolkit.model.config.Options | ||
|
||
import org.semver4j.Semver | ||
import org.semver4j.Semver.VersionDiff | ||
|
||
/** | ||
* Definition of a predicate to check whether the configuration of a scanner is compatible with the requirements | ||
* specified by a [ScannerCriteria]. | ||
* | ||
* When testing whether a scan result is compatible with specific criteria this function is invoked on the | ||
* scanner configuration data stored in the result. By having different, scanner-specific matcher functions, this | ||
* compatibility check can be made very flexible. | ||
* | ||
* TODO: Switch to a more advanced type than String to represent the scanner configuration. | ||
*/ | ||
typealias ScannerConfigMatcher = (String) -> Boolean | ||
|
||
/** | ||
* A data class defining selection criteria for scanners. | ||
* | ||
|
@@ -69,17 +57,12 @@ data class ScannerCriteria( | |
val maxVersion: Semver, | ||
|
||
/** | ||
* A function to check whether the configuration of a scanner is compatible with this [ScannerCriteria]. | ||
* Criterion to match the [configuration][ScannerDetails.configuration] of the scanner. If `null`, all | ||
* configurations are matched. | ||
*/ | ||
val configMatcher: ScannerConfigMatcher | ||
val configuration: String? | ||
) { | ||
companion object { | ||
/** | ||
* A matcher for scanner configurations that accepts all configurations passed in. This function can be | ||
* used if the concrete configuration of a scanner is irrelevant. | ||
*/ | ||
val ALL_CONFIG_MATCHER: ScannerConfigMatcher = { true } | ||
|
||
/** | ||
* The name of the property defining the regular expression for the scanner name as part of [ScannerCriteria]. | ||
* The [scanner details][ScannerDetails] of the corresponding scanner must match the criteria. | ||
|
@@ -99,10 +82,10 @@ data class ScannerCriteria( | |
const val PROP_CRITERIA_MAX_VERSION = "maxVersion" | ||
|
||
/** | ||
* A matcher for scanner configurations that accepts only exact matches of the [originalConfig]. This | ||
* function can be used by scanners that are extremely sensitive about their configuration. | ||
* The name of the property defining the configuration of the scanner as part of [ScannerCriteria]. The | ||
* [scanner details][ScannerDetails] of the corresponding scanner must match the criteria. | ||
*/ | ||
fun exactConfigMatcher(originalConfig: String): ScannerConfigMatcher = { config -> originalConfig == config } | ||
const val PROP_CRITERIA_CONFIGURATION = "configuration" | ||
|
||
/** | ||
* Generate a [ScannerCriteria] instance that is compatible with the given [details] and versions that differ | ||
|
@@ -122,27 +105,25 @@ data class ScannerCriteria( | |
regScannerName = details.name, | ||
minVersion = minVersion, | ||
maxVersion = maxVersion, | ||
configMatcher = exactConfigMatcher(details.configuration) | ||
configuration = details.configuration | ||
) | ||
} | ||
|
||
/** | ||
* Return a [ScannerCriteria] instance that is to be used when looking up existing scan results from a | ||
* [ScanResultsStorage]. By default, the properties of this instance are initialized to match the scanner | ||
* [details]. These default can be overridden by the [ScannerConfiguration.options] property in the provided | ||
* [config]: Use properties of the form `scannerName.property`, where `scannerName` is the name of the scanner | ||
* the configuration applies to, and `property` is the name of a property of the [ScannerCriteria] class. For | ||
* instance, to specify that a specific minimum version of ScanCode is allowed, set this property: | ||
* `options.ScanCode.minVersion=3.0.2`. | ||
* [details]. These defaults can be overridden by the provided [options]. The keys of the option map must match | ||
* names of the [ScannerCriteria] class. For example, to specify that a specific minimum version of the scanner | ||
* is allowed, set this option: `minVersion=3.0.2`. | ||
*/ | ||
fun fromConfig(details: ScannerDetails, config: ScannerConfiguration): ScannerCriteria { | ||
val options = config.options?.get(details.name).orEmpty() | ||
fun create(details: ScannerDetails, options: Options = emptyMap()): ScannerCriteria { | ||
val scannerVersion = Semver(normalizeVersion(details.version)) | ||
val minVersion = parseVersion(options[PROP_CRITERIA_MIN_VERSION]) ?: scannerVersion | ||
val maxVersion = parseVersion(options[PROP_CRITERIA_MAX_VERSION]) ?: minVersion.nextMinor() | ||
val name = options[PROP_CRITERIA_NAME] ?: details.name | ||
val configuration = options[PROP_CRITERIA_CONFIGURATION] ?: details.configuration | ||
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. @mnonnenmacher, I just realized a general problem with this: |
||
|
||
return ScannerCriteria(name, minVersion, maxVersion, exactConfigMatcher(details.configuration)) | ||
return ScannerCriteria(name, minVersion, maxVersion, configuration) | ||
} | ||
} | ||
|
||
|
@@ -163,7 +144,8 @@ data class ScannerCriteria( | |
if (!nameRegex.matches(details.name)) return false | ||
|
||
val version = Semver(details.version) | ||
return minVersion <= version && version < maxVersion && configMatcher(details.configuration) | ||
return minVersion <= version && version < maxVersion && | ||
(configuration == null || configuration == details.configuration) | ||
} | ||
} | ||
|
||
|
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.
Just FYI, this reminds me of @oheger-bosch's #3372, which unfortunately we never managed to merge. But it's probably right by now to start anew and rethink the scanner configuration matching.
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.
Yes, I wanted to close the PR after this is merged. I think in the beginning we expected that we would change the ScanCode options more often but over time it turned out that we barely ever change them and the current approach works well enough. So to me the complexity of the proposed solution is not appropriate to the relevance of the problem.