-
Notifications
You must be signed in to change notification settings - Fork 354
scanner: Add Nomos plugin #10631
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
Open
Prakash-Mishra-9ghz
wants to merge
1
commit into
oss-review-toolkit:main
from
Prakash-Mishra-9ghz:nomos
Open
scanner: Add Nomos plugin #10631
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright (C) 2025 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* License-Filename: LICENSE | ||
*/ | ||
|
||
plugins { | ||
// Apply precompiled plugins. | ||
id("ort-plugin-conventions") | ||
|
||
// Apply third-party plugins. | ||
alias(libs.plugins.kotlinSerialization) | ||
} | ||
|
||
dependencies { | ||
api(projects.model) | ||
api(projects.scanner) | ||
|
||
implementation(projects.utils.commonUtils) | ||
implementation(projects.utils.ortUtils) | ||
implementation(projects.utils.spdxUtils) | ||
|
||
implementation(libs.kotlinx.serialization.json) | ||
|
||
ksp(projects.scanner) | ||
|
||
funTestApi(testFixtures(projects.scanner)) | ||
|
||
} |
38 changes: 38 additions & 0 deletions
plugins/scanners/fossologynomossa/src/funTest/kotlin/FossologyNomossaFunTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (C) 2025 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* License-Filename: LICENSE | ||
*/ | ||
|
||
package org.ossreviewtoolkit.plugins.scanners.fossologynomossa | ||
|
||
import org.ossreviewtoolkit.model.LicenseFinding | ||
import org.ossreviewtoolkit.model.TextLocation | ||
import org.ossreviewtoolkit.scanner.AbstractPathScannerWrapperFunTest | ||
|
||
class FossologyNomossaFunTest : AbstractPathScannerWrapperFunTest() { | ||
override val scanner = NomossaFactory.create() | ||
|
||
override val expectedFileLicenses = listOf( | ||
LicenseFinding("Apache-2.0", TextLocation("LICENSE", 1, 195), 100.0f) | ||
) | ||
|
||
override val expectedDirectoryLicenses = listOf( | ||
LicenseFinding("Apache-2.0", TextLocation("COPYING", 1, 195), 100.0f), | ||
LicenseFinding("Apache-2.0", TextLocation("LICENCE", 1, 195), 100.0f), | ||
LicenseFinding("Apache-2.0", TextLocation("LICENSE", 1, 195), 100.0f) | ||
) | ||
} |
126 changes: 126 additions & 0 deletions
plugins/scanners/fossologynomossa/src/main/kotlin/Nomossa.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Copyright (C) 2025 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* License-Filename: LICENSE | ||
*/ | ||
|
||
package org.ossreviewtoolkit.plugins.scanners.fossologynomossa | ||
|
||
import java.io.File | ||
import java.time.Instant | ||
|
||
import kotlin.math.max | ||
|
||
import org.apache.logging.log4j.kotlin.logger | ||
|
||
import org.ossreviewtoolkit.model.ScanSummary | ||
import org.ossreviewtoolkit.plugins.api.OrtPlugin | ||
import org.ossreviewtoolkit.plugins.api.PluginDescriptor | ||
import org.ossreviewtoolkit.scanner.LocalPathScannerWrapper | ||
import org.ossreviewtoolkit.scanner.ScanContext | ||
import org.ossreviewtoolkit.scanner.ScannerMatcher | ||
import org.ossreviewtoolkit.scanner.ScannerWrapperFactory | ||
import org.ossreviewtoolkit.utils.common.CommandLineTool | ||
import org.ossreviewtoolkit.utils.common.ProcessCapture | ||
|
||
object NomossaCommand : CommandLineTool { | ||
override fun command(workingDir: File?) = | ||
listOfNotNull(workingDir, "FOSSology-nomossa").joinToString(File.separator) | ||
|
||
override fun transformVersion(output: String) = | ||
// Example output: | ||
// nomos build version: 4.5.1.1 r(ff4fa7) | ||
output.removePrefix("nomos build version: ").substringBefore(' ') // Returns 4.5.1 | ||
|
||
override fun getVersionArguments() = "-V" | ||
} | ||
|
||
/** | ||
* A wrapper for [Nomossa](https://github.com/fossology/fossology/tree/master/src/nomos). | ||
* | ||
* This plugin integrates FOSSology's Nomossa scanner into ORT by calling its CLI | ||
* and mapping its output to ORT's scan result format. | ||
*/ | ||
@OrtPlugin( | ||
id = "Nomossa", | ||
displayName = "Nomossa (FOSSology)", | ||
description = "A wrapper for [Nomossa](https://github.com/fossology/fossology/tree/master/src/nomos).", | ||
factory = ScannerWrapperFactory::class | ||
) | ||
class Nomossa( | ||
override val descriptor: PluginDescriptor = NomossaFactory.descriptor, | ||
private val config: NomossaConfig | ||
) : LocalPathScannerWrapper() { | ||
private val commandLineOptions by lazy { getCommandLineOptions() } | ||
|
||
internal fun getCommandLineOptions() = | ||
buildList { | ||
addAll(config.additionalOptions) | ||
} | ||
|
||
override val configuration by lazy { | ||
config.additionalOptions.joinToString(" ") | ||
} | ||
|
||
override val matcher by lazy { ScannerMatcher.create(details, config) } | ||
|
||
override val version by lazy { | ||
require(NomossaCommand.isInPath()) { | ||
"The '${NomossaCommand.command()}' command is not available in the PATH environment." | ||
} | ||
|
||
NomossaCommand.getVersion() | ||
} | ||
|
||
override val readFromStorage = config.readFromStorage | ||
override val writeToStorage = config.writeToStorage | ||
|
||
override fun runScanner(path: File, context: ScanContext): String { | ||
val process = runNomossa(path) | ||
|
||
return with(process) { | ||
if (isError && stdout.isNotBlank()) logger.debug { stdout } | ||
if (stderr.isNotBlank()) logger.debug { stderr } | ||
|
||
stdout | ||
} | ||
} | ||
|
||
override fun createSummary(result: String, startTime: Instant, endTime: Instant): ScanSummary = | ||
parseNomossaResult(result).toScanSummary(startTime, endTime) | ||
|
||
/** | ||
* Execute Nomossa with the configured arguments to scan the given [path]. | ||
*/ | ||
internal fun runNomossa(path: File): ProcessCapture { | ||
val options = mutableListOf<String>() | ||
options.addAll(commandLineOptions) | ||
|
||
if (path.isDirectory) { | ||
options += listOf( | ||
"-n", max(2, Runtime.getRuntime().availableProcessors() - 1).toString(), | ||
"-d", path.absolutePath | ||
) | ||
} else { | ||
options += path.absolutePath | ||
} | ||
|
||
return ProcessCapture( | ||
NomossaCommand.command(), | ||
*options.toTypedArray() | ||
) | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
plugins/scanners/fossologynomossa/src/main/kotlin/NomossaConfig.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright (C) 2025 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* License-Filename: LICENSE | ||
*/ | ||
|
||
package org.ossreviewtoolkit.plugins.scanners.fossologynomossa | ||
|
||
import org.ossreviewtoolkit.plugins.api.OrtPluginOption | ||
import org.ossreviewtoolkit.scanner.ScannerMatcherCriteria | ||
|
||
/** | ||
* Configuration options for the Nomossa scanner. | ||
*/ | ||
data class NomossaConfig( | ||
/** | ||
* Command line options that affect scan results. These are used when matching stored results. | ||
*/ | ||
@OrtPluginOption(defaultValue = "-J,-S,-l") | ||
val additionalOptions: List<String>, | ||
|
||
/** | ||
* The scanner name pattern used when looking up scan results from storage. | ||
*/ | ||
override val regScannerName: String?, | ||
|
||
/** | ||
* The minimum version of scan results to use from storage. | ||
*/ | ||
override val minVersion: String?, | ||
|
||
/** | ||
* The maximum version of scan results to use from storage. | ||
*/ | ||
override val maxVersion: String?, | ||
|
||
/** | ||
* The configuration string for identifying matching scan results in storage. | ||
*/ | ||
override val configuration: String?, | ||
|
||
/** | ||
* Whether to read scan results from storage. | ||
*/ | ||
@OrtPluginOption(defaultValue = "false") | ||
val readFromStorage: Boolean, | ||
|
||
/** | ||
* Whether to write scan results to storage. | ||
*/ | ||
@OrtPluginOption(defaultValue = "false") | ||
val writeToStorage: Boolean | ||
) : ScannerMatcherCriteria |
87 changes: 87 additions & 0 deletions
plugins/scanners/fossologynomossa/src/main/kotlin/NomossaResultExtensions.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright (C) 2025 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* License-Filename: LICENSE | ||
*/ | ||
|
||
package org.ossreviewtoolkit.plugins.scanners.fossologynomossa | ||
|
||
import java.io.File | ||
import java.time.Instant | ||
|
||
import org.ossreviewtoolkit.model.LicenseFinding | ||
import org.ossreviewtoolkit.model.ScanSummary | ||
import org.ossreviewtoolkit.model.TextLocation | ||
import org.ossreviewtoolkit.utils.spdx.SpdxConstants | ||
import org.ossreviewtoolkit.utils.spdx.SpdxExpression | ||
|
||
internal fun NomossaResult.toScanSummary(startTime: Instant, endTime: Instant): ScanSummary { | ||
val licenseFindings = results.flatMap { fileResult -> | ||
val fileContent = File(fileResult.file).readText() | ||
|
||
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. Nit: Omit this superfluous blank line. |
||
fileResult.licenses.map { licenseInfo -> | ||
val licenseExpression = runCatching { SpdxExpression.parse(licenseInfo.license) }.getOrNull() | ||
|
||
val safeLicense = when { | ||
licenseExpression == null -> SpdxConstants.NOASSERTION | ||
licenseExpression.isValid() -> licenseInfo.license | ||
else -> "LicenseRef-Nomossa-${licenseInfo.license.replace(Regex("[^A-Za-z0-9.+-]"), "-")}" | ||
} | ||
val (startLine, endLine) = byteOffsetsToLineNumbers(fileContent, licenseInfo.start, licenseInfo.end) | ||
|
||
Triple(fileResult.file, safeLicense, startLine to endLine) | ||
} | ||
}.groupBy { (file, license, _) -> file to license } | ||
.map { (fileAndLicense, entries) -> | ||
val (file, license) = fileAndLicense | ||
val startLine = entries.minOf { it.third.first } | ||
val endLine = entries.maxOf { it.third.second } | ||
|
||
LicenseFinding( | ||
license = license, | ||
location = TextLocation( | ||
path = file, | ||
startLine = startLine, | ||
endLine = endLine | ||
), | ||
score = 100.0f | ||
) | ||
}.toSet() | ||
|
||
return ScanSummary( | ||
startTime = startTime, | ||
endTime = endTime, | ||
licenseFindings = licenseFindings, | ||
issues = emptyList(), | ||
copyrightFindings = sortedSetOf() | ||
) | ||
} | ||
|
||
internal fun byteOffsetsToLineNumbers(fileContent: String, startOffset: Int, endOffset: Int): Pair<Int, Int> { | ||
var startLine = 1 | ||
var endLine = 1 | ||
|
||
for ((index, char) in fileContent.withIndex()) { | ||
if (index >= startOffset && index > endOffset) break | ||
|
||
if (char == '\n') { | ||
if (index < startOffset) startLine++ | ||
if (index < endOffset) endLine++ | ||
} | ||
} | ||
|
||
return Pair(startLine, endLine) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.