|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + * License-Filename: LICENSE |
| 18 | + */ |
| 19 | + |
| 20 | +package org.ossreviewtoolkit.plugins.scanners.nomos |
| 21 | + |
| 22 | +import java.io.File |
| 23 | +import java.time.Instant |
| 24 | + |
| 25 | +import org.apache.logging.log4j.kotlin.logger |
| 26 | + |
| 27 | +import org.ossreviewtoolkit.model.ScanSummary |
| 28 | +import org.ossreviewtoolkit.model.ScannerDetails |
| 29 | +import org.ossreviewtoolkit.plugins.api.OrtPlugin |
| 30 | +import org.ossreviewtoolkit.plugins.api.PluginDescriptor |
| 31 | +import org.ossreviewtoolkit.scanner.LocalPathScannerWrapper |
| 32 | +import org.ossreviewtoolkit.scanner.ScanContext |
| 33 | +import org.ossreviewtoolkit.scanner.ScannerMatcher |
| 34 | +import org.ossreviewtoolkit.scanner.ScannerWrapperFactory |
| 35 | +import org.ossreviewtoolkit.utils.common.CommandLineTool |
| 36 | +import org.ossreviewtoolkit.utils.common.ProcessCapture |
| 37 | + |
| 38 | +object NomossaCommand : CommandLineTool { |
| 39 | + override fun command(workingDir: File?): String { |
| 40 | + return listOfNotNull(workingDir, "nomossa").joinToString(File.separator) |
| 41 | + } |
| 42 | + |
| 43 | + override fun transformVersion(output: String): String { |
| 44 | + // Example output: nomossasa build version: 4.5.1.1 r(ff4fa7) |
| 45 | + val versionRegex = Regex("""(\d+\.\d+\.\d+)(?:\.\d+)?""") |
| 46 | + return versionRegex.find(output)?.groupValues?.get(1).orEmpty() // Returns 4.5.1 |
| 47 | + } |
| 48 | + |
| 49 | + override fun getVersionArguments() = "-V" |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * A wrapper for [Nomossa](https://github.com/fossology/fossology). |
| 54 | + * |
| 55 | + * This plugin integrates FOSSology's Nomossa scanner into ORT by calling its CLI |
| 56 | + * and mapping its output to ORT's scan result format. |
| 57 | + */ |
| 58 | +@OrtPlugin( |
| 59 | + displayName = "Nomossa", |
| 60 | + description = "A wrapper for [Nomossa](https://github.com/fossology/fossology).", |
| 61 | + factory = ScannerWrapperFactory::class |
| 62 | +) |
| 63 | +class Nomossa( |
| 64 | + override val descriptor: PluginDescriptor = NomossaFactory.descriptor, |
| 65 | + private val config: NomossaConfig |
| 66 | +) : LocalPathScannerWrapper() { |
| 67 | + |
| 68 | + private val commandLineOptions by lazy { getCommandLineOptions() } |
| 69 | + |
| 70 | + internal fun getCommandLineOptions(): List<String> { |
| 71 | + val options = LinkedHashSet(config.additionalOptions) |
| 72 | + options.add("-J") |
| 73 | + options.add("-l") |
| 74 | + options.add("-n") |
| 75 | + options.add(config.cpuCount.toString()) |
| 76 | + return options.toList() |
| 77 | + } |
| 78 | + |
| 79 | + override val configuration by lazy { |
| 80 | + config.additionalOptions.joinToString(" ") |
| 81 | + } |
| 82 | + |
| 83 | + override val matcher by lazy { ScannerMatcher.create(details, config) } |
| 84 | + |
| 85 | + override val version by lazy { |
| 86 | + require(NomossaCommand.isInPath()) { |
| 87 | + "The '${NomossaCommand.command()}' command is not available in the PATH environment." |
| 88 | + } |
| 89 | + |
| 90 | + NomossaCommand.getVersion() |
| 91 | + } |
| 92 | + |
| 93 | + override val readFromStorage = config.readFromStorage |
| 94 | + override val writeToStorage = config.writeToStorage |
| 95 | + |
| 96 | + override fun runScanner(path: File, context: ScanContext): String { |
| 97 | + val process = runNomossa(path) |
| 98 | + |
| 99 | + val resultText = process.stdout |
| 100 | + logger.info { "Nomossa raw output:\n$resultText" } |
| 101 | + |
| 102 | + return with(process) { |
| 103 | + if (isError && stdout.isNotBlank()) logger.debug { stdout } |
| 104 | + if (stderr.isNotBlank()) logger.debug { stderr } |
| 105 | + |
| 106 | + stdout |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + override fun parseDetails(result: String): ScannerDetails { |
| 111 | + return ScannerDetails( |
| 112 | + name = descriptor.id, |
| 113 | + version = version, |
| 114 | + configuration = config.additionalOptions.joinToString(" ") |
| 115 | + ) |
| 116 | + } |
| 117 | + |
| 118 | + override fun createSummary(result: String, startTime: Instant, endTime: Instant): ScanSummary = |
| 119 | + parseNomossaResult(result).toScanSummary(startTime, endTime) |
| 120 | + |
| 121 | + /** |
| 122 | + * Execute Nomossa with the configured arguments to scan the given [path] and produce [resultFile]. |
| 123 | + */ |
| 124 | + internal fun runNomossa(path: File): ProcessCapture = |
| 125 | + ProcessCapture( |
| 126 | + NomossaCommand.command(), |
| 127 | + *commandLineOptions.toTypedArray(), |
| 128 | + "-d", path.absolutePath // Scan this directory |
| 129 | + ) |
| 130 | +} |
0 commit comments