1919
2020package org.ossreviewtoolkit.plugins.scanners.scanoss
2121
22- import com.scanoss.Winnowing
23- import com.scanoss.dto.ScanFileResult
24- import com.scanoss.rest.ScanApi
22+ import com.scanoss.Scanner
2523import com.scanoss.utils.JsonUtils
2624import com.scanoss.utils.PackageDetails
2725
2826import java.io.File
2927import java.time.Instant
30- import java.util.UUID
31-
32- import org.apache.logging.log4j.kotlin.logger
3328
3429import org.ossreviewtoolkit.model.ScanSummary
3530import org.ossreviewtoolkit.plugins.api.OrtPlugin
3631import org.ossreviewtoolkit.plugins.api.PluginDescriptor
3732import org.ossreviewtoolkit.scanner.PathScannerWrapper
3833import org.ossreviewtoolkit.scanner.ScanContext
3934import org.ossreviewtoolkit.scanner.ScannerMatcher
40- import org.ossreviewtoolkit.scanner.ScannerMatcherConfig
4135import org.ossreviewtoolkit.scanner.ScannerWrapperFactory
42- import org.ossreviewtoolkit.utils.common.VCS_DIRECTORIES
4336
4437@OrtPlugin(
4538 id = " SCANOSS" ,
@@ -49,13 +42,8 @@ import org.ossreviewtoolkit.utils.common.VCS_DIRECTORIES
4942)
5043class ScanOss (
5144 override val descriptor : PluginDescriptor = ScanOssFactory .descriptor,
52- config : ScanOssConfig
45+ private var config : ScanOssConfig
5346) : PathScannerWrapper {
54- private val service = ScanApi .builder()
55- // As there is only a single endpoint, the SCANOSS API client expects the path to be part of the API URL.
56- .url(config.apiUrl.removeSuffix(" /" ) + " /scan/direct" )
57- .apiKey(config.apiKey.value)
58- .build()
5947
6048 override val version: String by lazy {
6149 // TODO: Find out the best / cheapest way to query the SCANOSS server for its version.
@@ -64,73 +52,27 @@ class ScanOss(
6452
6553 override val configuration = " "
6654
67- override val matcher by lazy {
68- ScannerMatcher .create(
69- details,
70- ScannerMatcherConfig (
71- config.regScannerName,
72- config.minVersion,
73- config.maxVersion,
74- configuration
75- )
76- )
77- }
55+ override val matcher: ScannerMatcher ? = null
7856
7957 override val readFromStorage = config.readFromStorage
8058
8159 override val writeToStorage = config.writeToStorage
8260
83- /* *
84- * The name of the file corresponding to the fingerprints can be sent to SCANOSS for more precise matches.
85- * However, for anonymity, a unique identifier should be generated and used instead. This property holds the
86- * mapping between the file paths and the unique identifiers. When receiving the response, the UUID will be
87- * replaced by the actual file path.
88- *
89- * TODO: This behavior should be driven by a configuration parameter enabled by default.
90- */
91- private val fileNamesAnonymizationMapping = mutableMapOf<UUID , String >()
92-
9361 override fun scanPath (path : File , context : ScanContext ): ScanSummary {
9462 val startTime = Instant .now()
9563
96- val wfpString = buildString {
97- path.walk()
98- .onEnter { it.name !in VCS_DIRECTORIES }
99- .filterNot { it.isDirectory }
100- .forEach {
101- logger.info { " Computing fingerprint for file ${it.absolutePath} ..." }
102- append(createWfpForFile(it))
103- }
104- }
105-
106- val result = service.scan(
107- wfpString,
108- context.labels[" scanOssContext" ],
109- context.labels[" scanOssId" ]?.toIntOrNull() ? : Thread .currentThread().threadId().toInt()
110- )
111-
112- // Replace the anonymized UUIDs by their file paths.
113- val results = JsonUtils .toScanFileResultsFromObject(JsonUtils .toJsonObject(result)).map {
114- val uuid = UUID .fromString(it.filePath)
64+ val scanoss = Scanner .builder()
65+ .url(config.apiUrl.removeSuffix(" /" ) + " /scan/direct" )
66+ .apiKey(config.apiKey.value)
67+ .build()
11568
116- val fileName = fileNamesAnonymizationMapping[uuid] ? : throw IllegalArgumentException (
117- " The ${descriptor.id} server returned UUID '$uuid ' which is not present in the mapping."
118- )
119-
120- ScanFileResult (fileName, it.fileDetails)
69+ val rawResults: List <String > = when {
70+ path.isFile -> listOf (scanoss.scanFile(path.absolutePath))
71+ else -> scanoss.scanFolder(path.absolutePath)
12172 }
12273
74+ val results = JsonUtils .toScanFileResults(rawResults)
12375 val endTime = Instant .now()
12476 return generateSummary(startTime, endTime, results)
12577 }
126-
127- internal fun generateRandomUUID () = UUID .randomUUID()
128-
129- internal fun createWfpForFile (file : File ): String {
130- generateRandomUUID().let { uuid ->
131- // TODO: Let's keep the original file extension to give SCANOSS some hint about the mime type.
132- fileNamesAnonymizationMapping[uuid] = file.path
133- return Winnowing .builder().build().wfpForFile(file.path, uuid.toString())
134- }
135- }
13678}
0 commit comments