|
20 | 20 | package org.ossreviewtoolkit.plugins.scanners.scanoss
|
21 | 21 |
|
22 | 22 | import com.scanoss.Scanner
|
| 23 | +import com.scanoss.filters.FilterConfig |
23 | 24 | import com.scanoss.utils.JsonUtils
|
24 | 25 | import com.scanoss.utils.PackageDetails
|
25 | 26 |
|
26 | 27 | import java.io.File
|
27 | 28 | import java.time.Instant
|
28 | 29 |
|
| 30 | +import org.apache.logging.log4j.kotlin.logger |
| 31 | + |
29 | 32 | import org.ossreviewtoolkit.model.ScanSummary
|
30 | 33 | import org.ossreviewtoolkit.plugins.api.OrtPlugin
|
31 | 34 | import org.ossreviewtoolkit.plugins.api.PluginDescriptor
|
@@ -61,14 +64,39 @@ class ScanOss(
|
61 | 64 | override fun scanPath(path: File, context: ScanContext): ScanSummary {
|
62 | 65 | val startTime = Instant.now()
|
63 | 66 |
|
| 67 | + // basePath: The reference path used for creating relative paths. |
| 68 | + val basePath = path.toPath() |
| 69 | + |
| 70 | + val filterConfig = FilterConfig.builder() |
| 71 | + .customFilter { currentPath -> |
| 72 | + // currentPath: A Path object representing the file or directory being evaluated by the filter. |
| 73 | + // This is provided by the Scanner and represents individual files/directories during traversal. |
| 74 | + // It is an absolute path to a file or directory within the scan target. |
| 75 | + try { |
| 76 | + // relativePath: The path of the current file relative to the base scan directory. |
| 77 | + // Example: If basePath is "/project" and currentPath is "/project/src/main/file.kt", |
| 78 | + // then relativePath becomes "src/main/file.kt". |
| 79 | + // This relative representation is what the exclusion patterns in context.excludes expect. |
| 80 | + val relativePath = basePath.relativize(currentPath).toString() |
| 81 | + val isExcluded = context.excludes?.isPathExcluded(relativePath) ?: false |
| 82 | + logger.debug("Path: $currentPath, relative: $relativePath, isExcluded: $isExcluded") |
| 83 | + isExcluded |
| 84 | + } catch (e: IllegalArgumentException) { |
| 85 | + logger.warn("Error processing path $currentPath: ${e.message}") |
| 86 | + false |
| 87 | + } |
| 88 | + } |
| 89 | + .build() |
| 90 | + |
64 | 91 | val scanoss = Scanner.builder()
|
65 | 92 | .url(config.apiUrl.removeSuffix("/") + "/scan/direct")
|
66 | 93 | .apiKey(config.apiKey.value)
|
| 94 | + .filterConfig(filterConfig) |
67 | 95 | .build()
|
68 | 96 |
|
69 | 97 | val rawResults: List<String> = when {
|
70 |
| - path.isFile -> listOf(scanoss.scanFile(path.absolutePath)) |
71 |
| - else -> scanoss.scanFolder(path.absolutePath) |
| 98 | + path.isFile -> listOf(scanoss.scanFile(path.toString())) |
| 99 | + else -> scanoss.scanFolder(path.toString()) |
72 | 100 | }
|
73 | 101 |
|
74 | 102 | val results = JsonUtils.toScanFileResults(rawResults)
|
|
0 commit comments