Skip to content

Commit

Permalink
refactor: Replace some remaining custom ProcessCapture calls
Browse files Browse the repository at this point in the history
Prefer to use properly isolated commands.

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Dec 12, 2024
1 parent a88a0f3 commit 8516d2a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
17 changes: 7 additions & 10 deletions plugins/package-managers/bazel/src/main/kotlin/Bazel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -191,8 +194,7 @@ class Bazel(
)
}

val process = ProcessCapture(
BUILDOZER_COMMAND,
val process = BuildozerCommand.run(
"-f", commandsFile.absolutePath,
workingDir = workingDir
)
Expand All @@ -216,16 +218,11 @@ class Bazel(
* override is defined.
*/
private fun getArchiveOverrides(workingDir: File): Map<String, ArchiveOverride> {
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"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down

0 comments on commit 8516d2a

Please sign in to comment.