Skip to content

Commit

Permalink
feat(scanner)!: Pass properties to configure storage usage to wrappers
Browse files Browse the repository at this point in the history
Pass the properties to configure scan storage usage to the scan wrappers
and add the required properties to `ScannerWrapper`. If the properties
are not configured in the config, the defaults depend on the `matcher`
property: If the `matcher` is `null, both properties are set to `false`,
otherwise they are set to `true`.

Signed-off-by: Martin Nonnenmacher <[email protected]>
  • Loading branch information
mnonnenmacher committed Nov 15, 2023
1 parent ab27a19 commit bd03101
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import org.ossreviewtoolkit.model.config.AnalyzerConfiguration
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
import org.ossreviewtoolkit.plugins.commands.api.OrtCommand
import org.ossreviewtoolkit.scanner.CommandLinePathScannerWrapper
import org.ossreviewtoolkit.scanner.ScannerMatcherConfig
import org.ossreviewtoolkit.scanner.ScannerWrapperConfig
import org.ossreviewtoolkit.utils.common.CommandLineTool
import org.ossreviewtoolkit.utils.spdx.scanCodeLicenseTextDir

Expand Down Expand Up @@ -89,8 +89,8 @@ class RequirementsCommand : OrtCommand(
logger.debug { "$it is a $category." }
it.getDeclaredConstructor(
String::class.java,
ScannerMatcherConfig::class.java
).newInstance("", ScannerMatcherConfig.EMPTY)
ScannerWrapperConfig::class.java
).newInstance("", ScannerWrapperConfig.EMPTY)
}

VersionControlSystem::class.java.isAssignableFrom(it) -> {
Expand Down
4 changes: 4 additions & 0 deletions plugins/scanners/askalono/src/main/kotlin/Askalono.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class Askalono internal constructor(name: String, private val wrapperConfig: Sca

override val matcher by lazy { ScannerMatcher.create(details, wrapperConfig.matcherConfig) }

override val readFromStorage by lazy { wrapperConfig.readFromStorageWithDefault(matcher) }

override val writeToStorage by lazy { wrapperConfig.writeToStorageWithDefault(matcher) }

override fun command(workingDir: File?) =
listOfNotNull(workingDir, if (Os.isWindows) "askalono.exe" else "askalono").joinToString(File.separator)

Expand Down
4 changes: 4 additions & 0 deletions plugins/scanners/boyterlc/src/main/kotlin/BoyterLc.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class BoyterLc internal constructor(name: String, private val wrapperConfig: Sca

override val matcher by lazy { ScannerMatcher.create(details, wrapperConfig.matcherConfig) }

override val readFromStorage by lazy { wrapperConfig.readFromStorageWithDefault(matcher) }

override val writeToStorage by lazy { wrapperConfig.writeToStorageWithDefault(matcher) }

override fun command(workingDir: File?) =
listOfNotNull(workingDir, if (Os.isWindows) "lc.exe" else "lc").joinToString(File.separator)

Expand Down
10 changes: 8 additions & 2 deletions plugins/scanners/fossid/src/main/kotlin/FossId.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ import org.ossreviewtoolkit.utils.ort.showStackTrace
*/
class FossId internal constructor(
override val name: String,
private val config: FossIdConfig
private val config: FossIdConfig,
private val wrapperConfig: ScannerWrapperConfig
) : PackageScannerWrapper {
companion object {
@JvmStatic
Expand Down Expand Up @@ -170,7 +171,8 @@ class FossId internal constructor(
}

class Factory : ScannerWrapperFactory<FossIdConfig>("FossId") {
override fun create(config: FossIdConfig, wrapperConfig: ScannerWrapperConfig) = FossId(type, config)
override fun create(config: FossIdConfig, wrapperConfig: ScannerWrapperConfig) =
FossId(type, config, wrapperConfig)

override fun parseConfig(options: Options, secrets: Options) = FossIdConfig.create(options, secrets)
}
Expand Down Expand Up @@ -206,6 +208,10 @@ class FossId internal constructor(

override val matcher: ScannerMatcher? = null

override val readFromStorage by lazy { wrapperConfig.readFromStorageWithDefault(matcher) }

override val writeToStorage by lazy { wrapperConfig.writeToStorageWithDefault(matcher) }

private suspend fun getProject(projectCode: String): Project? =
service.getProject(config.user, config.apiKey, projectCode).run {
when {
Expand Down
3 changes: 2 additions & 1 deletion plugins/scanners/fossid/src/test/kotlin/FossIdTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ import org.ossreviewtoolkit.plugins.scanners.fossid.FossId.Companion.SCAN_ID_KEY
import org.ossreviewtoolkit.plugins.scanners.fossid.FossId.Companion.SERVER_URL_KEY
import org.ossreviewtoolkit.plugins.scanners.fossid.FossId.Companion.convertGitUrlToProjectName
import org.ossreviewtoolkit.scanner.ScanContext
import org.ossreviewtoolkit.scanner.ScannerWrapperConfig
import org.ossreviewtoolkit.utils.spdx.SpdxExpression

@Suppress("LargeClass")
Expand Down Expand Up @@ -1151,7 +1152,7 @@ private val DEFAULT_IGNORE_RULE_SCOPE = RuleScope.SCAN
/**
* Create a new [FossId] instance with the specified [config].
*/
private fun createFossId(config: FossIdConfig): FossId = FossId("FossId", config)
private fun createFossId(config: FossIdConfig): FossId = FossId("FossId", config, ScannerWrapperConfig.EMPTY)

/**
* Create a standard [FossIdConfig] whose properties can be partly specified.
Expand Down
4 changes: 4 additions & 0 deletions plugins/scanners/licensee/src/main/kotlin/Licensee.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class Licensee internal constructor(name: String, private val wrapperConfig: Sca

override val matcher by lazy { ScannerMatcher.create(details, wrapperConfig.matcherConfig) }

override val readFromStorage by lazy { wrapperConfig.readFromStorageWithDefault(matcher) }

override val writeToStorage by lazy { wrapperConfig.writeToStorageWithDefault(matcher) }

override fun command(workingDir: File?) =
listOfNotNull(workingDir, if (Os.isWindows) "licensee.bat" else "licensee").joinToString(File.separator)

Expand Down
4 changes: 4 additions & 0 deletions plugins/scanners/scancode/src/main/kotlin/ScanCode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ class ScanCode internal constructor(

override val matcher by lazy { ScannerMatcher.create(details, wrapperConfig.matcherConfig) }

override val readFromStorage by lazy { wrapperConfig.readFromStorageWithDefault(matcher) }

override val writeToStorage by lazy { wrapperConfig.writeToStorageWithDefault(matcher) }

override val configuration by lazy {
buildList {
addAll(configurationOptions)
Expand Down
4 changes: 4 additions & 0 deletions plugins/scanners/scanoss/src/main/kotlin/ScanOss.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ class ScanOss internal constructor(

override val matcher by lazy { ScannerMatcher.create(details, wrapperConfig.matcherConfig) }

override val readFromStorage by lazy { wrapperConfig.readFromStorageWithDefault(matcher) }

override val writeToStorage by lazy { wrapperConfig.writeToStorageWithDefault(matcher) }

/**
* The name of the file corresponding to the fingerprints can be sent to SCANOSS for more precise matches.
* However, for anonymity, a unique identifier should be generated and used instead. This property holds the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ internal class DummyScanner(override val name: String = "Dummy") : PathScannerWr
override val configuration = ""

override val matcher = ScannerMatcher.create(details)
override val readFromStorage = true
override val writeToStorage = true

override fun scanPath(path: File, context: ScanContext): ScanSummary {
val relevantFiles = path.walk()
Expand Down
13 changes: 12 additions & 1 deletion scanner/src/main/kotlin/ScannerWrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,20 @@ sealed interface ScannerWrapper {
* name of a property of the [ScannerMatcher] class. For instance, to specify that a specific minimum version of
* ScanCode is allowed, set this property: `config.ScanCode.options.minVersion=3.0.2`.
*
* If this property is null, it means that the results of this [ScannerWrapper] cannot be stored in a scan storage.
* If this property is null, it means that the results of this [ScannerWrapper] cannot be read from a scan storage.
*/
val matcher: ScannerMatcher?

/**
* If `true`, scan results for this scanner shall be read from the configured scan storages. Enabling this option
* requires that the [matcher] is not `null`.
*/
val readFromStorage: Boolean

/**
* If `true`, scan results for this scanner shall be written to the configured scan storages.
*/
val writeToStorage: Boolean
}

/**
Expand Down
10 changes: 10 additions & 0 deletions scanner/src/main/kotlin/ScannerWrapperConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,14 @@ data class ScannerWrapperConfig(
) to filteredOptions
}
}

/**
* Return [readFromStorage] if it is not `null`, otherwise return `true` if the provided [matcher] is not `null`.
*/
fun readFromStorageWithDefault(matcher: ScannerMatcher?) = readFromStorage ?: (matcher != null)

/**
* Return [writeToStorage] if it is not `null`, otherwise return `true` if the provided [matcher] is not `null`.
*/
fun writeToStorageWithDefault(matcher: ScannerMatcher?) = writeToStorage ?: (matcher != null)
}
6 changes: 6 additions & 0 deletions scanner/src/test/kotlin/ScannerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,8 @@ private class FakePackageScannerWrapper(

// Explicit nullability is required here for a mock response.
override val matcher: ScannerMatcher? = ScannerMatcher.create(details)
override val readFromStorage = true
override val writeToStorage = true

override fun scanPackage(pkg: Package, context: ScanContext): ScanResult =
createScanResult(packageProvenanceResolver.resolveProvenance(pkg, sourceCodeOriginPriority), details)
Expand All @@ -823,6 +825,8 @@ private class FakeProvenanceScannerWrapper : ProvenanceScannerWrapper {
override val configuration = "config"

override val matcher = ScannerMatcher.create(details)
override val readFromStorage = true
override val writeToStorage = true

override fun scanProvenance(provenance: KnownProvenance, context: ScanContext): ScanResult =
createScanResult(provenance, details)
Expand All @@ -837,6 +841,8 @@ private class FakePathScannerWrapper : PathScannerWrapper {
override val configuration = "config"

override val matcher = ScannerMatcher.create(details)
override val readFromStorage = true
override val writeToStorage = true

override fun scanPath(path: File, context: ScanContext): ScanSummary {
val licenseFindings = path.walk().filter { it.isFile }.mapTo(mutableSetOf()) { file ->
Expand Down

0 comments on commit bd03101

Please sign in to comment.