From 8516d2a0cfa8d25796e4f8ad83528ce4fe3fb98e Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Thu, 12 Dec 2024 11:18:40 +0100 Subject: [PATCH] refactor: Replace some remaining custom `ProcessCapture` calls Prefer to use properly isolated commands. Signed-off-by: Sebastian Schuberth --- .../bazel/src/main/kotlin/Bazel.kt | 17 +++++++---------- .../mercurial/src/main/kotlin/Mercurial.kt | 3 +-- .../src/main/kotlin/MercurialWorkingTree.kt | 4 +--- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/plugins/package-managers/bazel/src/main/kotlin/Bazel.kt b/plugins/package-managers/bazel/src/main/kotlin/Bazel.kt index 141b96af1e0bb..e626011e40904 100644 --- a/plugins/package-managers/bazel/src/main/kotlin/Bazel.kt +++ b/plugins/package-managers/bazel/src/main/kotlin/Bazel.kt @@ -71,7 +71,6 @@ import org.semver4j.RangesListFactory private const val BAZEL_FALLBACK_VERSION = "7.0.1" private const val LOCKFILE_NAME = "MODULE.bazel.lock" -private const val BUILDOZER_COMMAND = "buildozer" private const val BUILDOZER_MISSING_VALUE = "(missing)" internal object BazelCommand : CommandLineTool { @@ -96,6 +95,10 @@ internal object BazelCommand : CommandLineTool { override fun getVersionRequirement(): RangesList = RangesListFactory.create(">=7.0") } +internal object BuildozerCommand : CommandLineTool { + override fun command(workingDir: File?) = "buildozer" +} + class Bazel( name: String, analysisRoot: File, @@ -191,8 +194,7 @@ class Bazel( ) } - val process = ProcessCapture( - BUILDOZER_COMMAND, + val process = BuildozerCommand.run( "-f", commandsFile.absolutePath, workingDir = workingDir ) @@ -216,16 +218,11 @@ class Bazel( * override is defined. */ private fun getArchiveOverrides(workingDir: File): Map { - val process = ProcessCapture( - BUILDOZER_COMMAND, + val process = BuildozerCommand.run( "print module_name urls integrity patches", "//MODULE.bazel:%archive_override", workingDir = workingDir - ) - - require(process.isSuccess) { - "Failed to get archive overrides from 'buildozer': ${process.stderr}" - } + ).requireSuccess() // If an optional attribute is missing, buildozer outputs first a warning line and then the result with the // value "(missing"): diff --git a/plugins/version-control-systems/mercurial/src/main/kotlin/Mercurial.kt b/plugins/version-control-systems/mercurial/src/main/kotlin/Mercurial.kt index cab1961d4b09f..1d9244fc1c1de 100644 --- a/plugins/version-control-systems/mercurial/src/main/kotlin/Mercurial.kt +++ b/plugins/version-control-systems/mercurial/src/main/kotlin/Mercurial.kt @@ -28,7 +28,6 @@ import org.ossreviewtoolkit.downloader.WorkingTree import org.ossreviewtoolkit.model.VcsInfo import org.ossreviewtoolkit.model.VcsType import org.ossreviewtoolkit.utils.common.CommandLineTool -import org.ossreviewtoolkit.utils.common.ProcessCapture const val MERCURIAL_LARGE_FILES_EXTENSION = "largefiles = " const val MERCURIAL_SPARSE_EXTENSION = "sparse = " @@ -59,7 +58,7 @@ class Mercurial : VersionControlSystem(MercurialCommand) { override fun getWorkingTree(vcsDirectory: File): WorkingTree = MercurialWorkingTree(vcsDirectory, VcsType.forName(type)) - override fun isApplicableUrlInternal(vcsUrl: String) = ProcessCapture("hg", "identify", vcsUrl).isSuccess + override fun isApplicableUrlInternal(vcsUrl: String) = MercurialCommand.run("identify", vcsUrl).isSuccess override fun initWorkingTree(targetDir: File, vcs: VcsInfo): WorkingTree { // We cannot detect beforehand if the Large Files extension would be required, so enable it by default. diff --git a/plugins/version-control-systems/mercurial/src/main/kotlin/MercurialWorkingTree.kt b/plugins/version-control-systems/mercurial/src/main/kotlin/MercurialWorkingTree.kt index 702eeff23758c..78f646b2c3adb 100644 --- a/plugins/version-control-systems/mercurial/src/main/kotlin/MercurialWorkingTree.kt +++ b/plugins/version-control-systems/mercurial/src/main/kotlin/MercurialWorkingTree.kt @@ -23,14 +23,12 @@ import java.io.File import org.ossreviewtoolkit.downloader.WorkingTree import org.ossreviewtoolkit.model.VcsType -import org.ossreviewtoolkit.utils.common.ProcessCapture internal class MercurialWorkingTree(workingDir: File, vcsType: VcsType) : WorkingTree(workingDir, vcsType) { override fun isValid(): Boolean { if (!workingDir.isDirectory) return false - // Do not use runMercurialCommand() here as we do not require the command to succeed. - val hgRootPath = ProcessCapture(workingDir, "hg", "root") + val hgRootPath = MercurialCommand.run(workingDir, "root") return hgRootPath.isSuccess && workingDir.path.startsWith(hgRootPath.stdout.trimEnd()) }