scala.sys.process.ProcessLogger Scala Examples

The following examples show how to use scala.sys.process.ProcessLogger. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: ChangedFilesBuilder.scala    From mvn_scalafmt   with Apache License 2.0 5 votes vote down vote up
package org.antipathy.mvn_scalafmt.builder

import java.io.File
import java.nio.file.Paths

import org.apache.maven.plugin.logging.Log

import scala.sys.process.{Process, ProcessLogger}
import scala.util.{Failure, Success, Try}


  override def build(input: Seq[File]): Seq[File] =
    if (diff) {
      log.info(s"Checking for files changed from $branch")
      Try {
        val changedFiles = changeFunction()
        log.info(changedFiles.mkString(s"Changed from $branch:\n", "\n", ""))
        changedFiles.filter(isSupportedFile)
      } match {
        case Success(value) => value
        case Failure(e) =>
          log.error("Could not obtain list of changed files", e)
          throw e
      }
    } else {
      input
    }

}

// $COVERAGE-OFF$
object ChangedFilesBuilder {

  def apply(log: Log, diff: Boolean, branch: String, workingDirectory: File): ChangedFilesBuilder = {
    val logger: ProcessLogger = ProcessLogger(_ => (), err => log.error(err))

    def run(cmd: String) = Process(cmd, workingDirectory).!!(logger).trim

    val prefix = ": "
    val actualBranch =
      if (!branch.startsWith(prefix)) branch
      else run(branch.substring(prefix.length))

    def processFunction(): Seq[File] = {
      val diffOutput    = run(s"git diff --name-only --diff-filter=d $actualBranch")
      val gitRootOutput = run("git rev-parse --show-toplevel")
      val gitRootPath   = Paths.get(gitRootOutput)
      diffOutput.linesIterator
        .map(gitRootPath.resolve)
        .map(_.toFile)
        .toSeq
    }

    new ChangedFilesBuilder(log, diff, actualBranch, processFunction)
  }
}
// $COVERAGE-ON$ 
Example 2
Source File: ShellAction.scala    From berilia   with Apache License 2.0 5 votes vote down vote up
package com.criteo.dev.cluster.command

import com.criteo.dev.cluster.{DevClusterProcess, GeneralUtilities, Public}
import org.slf4j.LoggerFactory

import scala.sys.process.ProcessLogger


@Public
object ShellAction {
  private val logger = LoggerFactory.getLogger(this.getClass)

  private val suppressingProcessLogger = ProcessLogger(_ => ())

  private val processLogger = ProcessLogger(
    (o: String) => logger.info(o),
    (e: String) => logger.error(e))

  def apply(script: String, returnResult: Boolean = false, ignoreFailure: Boolean = false): String = {
    val p = DevClusterProcess.processSeq(script.split("\\s+"))

    (returnResult, ignoreFailure) match {
      case (false, true) => {
        p.!(suppressingProcessLogger)
        return null
      }
      case (false, false) => {
        GeneralUtilities.checkStatus(p.!(processLogger))
        return null
      }
      case (true, true) => {
        try {
          p.!!(suppressingProcessLogger)
        } catch {
          case e: Exception => null
        }
      }
      case (true, false) => return p.!!(processLogger)
    }
  }
} 
Example 3
Source File: RsyncAction.scala    From berilia   with Apache License 2.0 5 votes vote down vote up
package com.criteo.dev.cluster.command

import com.criteo.dev.cluster._
import org.slf4j.LoggerFactory

import scala.sys.process.ProcessLogger


@Public
object RsyncAction {
  private val logger = LoggerFactory.getLogger(ScpAction.getClass)
  private val processLogger = ProcessLogger(
    (o: String) => logger.info(o),
    (e: String) => logger.error(e))

  def apply(srcPath: String, targetN: Node, targetPath: String, sudo: Boolean = false) : Unit = {
    var command = scala.collection.mutable.ListBuffer[String]()
    command += "rsync"
    command += "-rvvz"
    command += "-e"

    val sshStr = new StringBuilder("ssh -o StrictHostKeyChecking=no ")
    val targetKey = targetN.key
    val targetUser = targetN.user
    val targetIp = targetN.ip
    val targetPort = targetN.port

    if (targetKey.isDefined) {
      sshStr.append(s"-i ${targetKey.get}")
    }

    if (targetPort.isDefined) {
      sshStr.append(s"-P ${targetPort.get}")
    }
    command += sshStr.toString

    if (sudo) {
      command += "--rsync-path=sudo rsync"
    }

    command += srcPath

    val targetPathFull = new StringBuilder()
    if (targetUser.isDefined) {
      targetPathFull.append(s"${targetUser.get}@")
    }
    targetPathFull.append(s"$targetIp:")
    targetPathFull.append(targetPath)
    command += targetPathFull.toString

    val p = DevClusterProcess.processSeq(command)
    GeneralUtilities.checkStatus(p.!(processLogger))
  }
} 
Example 4
Source File: SshAction.scala    From berilia   with Apache License 2.0 5 votes vote down vote up
package com.criteo.dev.cluster.command

import com.criteo.dev.cluster.{DevClusterProcess, GeneralUtilities, Node, Public}
import org.slf4j.LoggerFactory

import scala.sys.process.ProcessLogger


  def apply(node: Node, script: String, returnResult: Boolean = false, ignoreFailure: Boolean = false): String = {
    val command = s"ssh -o StrictHostKeyChecking=no ${GeneralUtilities.nodeString(node)}"

    val commands = command.split("\\s+")
    val p = DevClusterProcess.processSeq(commands :+ script)

    (returnResult, ignoreFailure) match {
      case (false, true) => {
        p.!(suppressingProcessLogger)
        return null
      }
      case (false, false) => {
        GeneralUtilities.checkStatus(p.!(processLogger))
        return null
      }
      case (true, true) => {
        try {
          p.!!(suppressingProcessLogger)
        } catch {
          case e: Exception => null
        }
      }
      case (true, false) => return p.!!(processLogger)
    }
  }

} 
Example 5
Source File: SshMultiAction.scala    From berilia   with Apache License 2.0 5 votes vote down vote up
package com.criteo.dev.cluster.command

import java.io.{File, PrintWriter}

import com.criteo.dev.cluster._
import org.slf4j.LoggerFactory

import scala.collection.mutable.ListBuffer
import scala.sys.process.ProcessLogger


@Public
case class SshMultiAction(node: Node) extends MultiAction {

  private val logger = LoggerFactory.getLogger(this.getClass)
  private val commands = new ListBuffer[String]

  //to allow concurrency
  val localFilepath = s"${GeneralUtilities.getHomeDir}/${GeneralUtilities.getTempPrefix}.sh"
  val remoteFilePath = s"/tmp/${GeneralUtilities.getTempPrefix}.sh"

  def add(command : String): Unit = {
    commands.+=(command)
  }

  def run(returnResult: Boolean = false, ignoreError: Boolean = false) : String = {
    val localTmpShellFile = new File(localFilepath)
    SshAction(node, " rm " + remoteFilePath, returnResult = false, true)

    //Write a temp shell script
    val writer = new PrintWriter(localTmpShellFile)
    commands.foreach(s => writer.write(s"$s\n"))
    writer.close

    localTmpShellFile.setExecutable(true)
    localTmpShellFile.setReadable(true)
    localTmpShellFile.deleteOnExit()

    commands.foreach(s => logger.info(s))

    ScpAction(None, localFilepath, Some(node), remoteFilePath)
    val res = SshAction(node, s"source $remoteFilePath", returnResult, ignoreError)
    SshAction(node, s"rm $remoteFilePath", returnResult = false, true)
    localTmpShellFile.delete()
    res
  }
}

object SshMultiAction {
  def apply(node: Node,
            commands: List[String],
            returnResult: Boolean = false,
            ignoreError: Boolean = false) : String = {
    val action = new SshMultiAction(node)
    commands.foreach(action.add)
    action.run(returnResult = returnResult, ignoreError = ignoreError)
  }
} 
Example 6
Source File: ShellMultiAction.scala    From berilia   with Apache License 2.0 5 votes vote down vote up
package com.criteo.dev.cluster.command

import java.io.{File, PrintWriter}

import com.criteo.dev.cluster.{GeneralUtilities, Public}
import org.slf4j.LoggerFactory

import scala.collection.mutable.ListBuffer
import scala.sys.process.ProcessLogger


@Public
case class ShellMultiAction() extends MultiAction {
  private val logger = LoggerFactory.getLogger(this.getClass)
  private val commands = new ListBuffer[String]

  //to allow concurrency
  val filepath = s"${GeneralUtilities.getHomeDir}/${GeneralUtilities.getTempPrefix}.sh"

  def add(command: String): Unit = {
    commands.+=(command)
  }

  def run(returnResult: Boolean = false, ignoreError: Boolean = false): String = {
    val localTmpShellFile = new File(filepath)
    ShellAction(s"rm $filepath", returnResult = false, true)

    //Write a temp shell script
    val writer = new PrintWriter(localTmpShellFile)
    commands.foreach(s => writer.write(s"$s\n"))
    writer.close

    localTmpShellFile.setExecutable(true)
    localTmpShellFile.setReadable(true)
    localTmpShellFile.deleteOnExit()

    commands.foreach(s => logger.info(s))
    val res = ShellAction(filepath, returnResult, ignoreError)
    localTmpShellFile.delete()
    res
  }
}

object ShellMultiAction {
  def apply(
             commands: List[String],
             returnResult: Boolean = false,
             ignoreError: Boolean = false
           ): String = {
    val action = new ShellMultiAction()
    commands.foreach(action.add)
    action.run(returnResult, ignoreError)
  }
} 
Example 7
Source File: DockerBuildAction.scala    From berilia   with Apache License 2.0 5 votes vote down vote up
package com.criteo.dev.cluster.docker

import com.criteo.dev.cluster.DevClusterProcess
import org.slf4j.LoggerFactory

import scala.collection.mutable.ListBuffer
import scala.sys.process.{Process, ProcessLogger}



class DockerBuildAction (dockerFile: String, dockerImage: String) {

  private val logger = LoggerFactory.getLogger(classOf[DockerBuildAction])

  private val processLogger = ProcessLogger(
    (e: String) => logger.info("err " + e))

  private val args = new ListBuffer[Pair[String, String]]
  private val ports = new ListBuffer[PortMeta]

  def addArg(key: String, value: String) = {
     args.+=(Pair(key, value))
  }

  def run() : Unit = {
    val sb = new StringBuilder("docker build")
    sb.append(s" -t $dockerImage")
    sb.append(s" -f ./${DockerConstants.dockerBaseDir}/$dockerFile")
    args.foreach(p =>  sb.append(s" --build-arg ${p._1}=${p._2}"))
    sb.append(s" ${DockerConstants.dockerBaseDir}")
    val out = DevClusterProcess.process(sb.toString).!
    if (out != 0) {
      throw new Exception("Failure running docker command.")
    }
  }
}

object DockerBuildAction {
  def apply(dockerFile: String, dockerImage: String) = {
    val dba = new DockerBuildAction(dockerFile, dockerImage)
    dba.run
  }
} 
Example 8
Source File: DockerRunAction.scala    From berilia   with Apache License 2.0 5 votes vote down vote up
package com.criteo.dev.cluster.docker

import java.io.{File, PrintWriter}

import com.criteo.dev.cluster.{DevClusterProcess, GeneralConstants, GeneralUtilities}
import org.slf4j.LoggerFactory

import scala.collection.mutable.ListBuffer
import scala.sys.process.{Process, ProcessLogger}


object DockerRunAction {

  private val logger = LoggerFactory.getLogger(classOf[DockerBuildAction])

  private val processLogger = ProcessLogger(
    (e: String) => logger.info("err " + e))

  private val ports = new ListBuffer[String]

  def apply(hosts: Map[String, String],
            image: String,
            mountDir: Option[String] = None,
            command: Option[String] = None,
            ports: Array[PortMeta],
            conf: Map[String, String],
            background: Boolean = false) : Option[String] = {
    val sb = new StringBuilder("docker run -P")
    if (background) {
      sb.append(" -d")
    } else {
      sb.append(" -it")
    }
    hosts.foreach {
      case (ip, name) => sb.append(s" --add-host=$name:$ip")
    }

    ports.foreach(p => {
      if (p.exposedPort.isDefined) {
        sb.append(s" -p ${p.exposedPort.get}:${p.port}")
      } else {
        sb.append(s" -p ${p.port}")
      }
    })

    if (mountDir.isDefined) {
      sb.append(s" -v ${mountDir.get}")
      sb.append(":/mount")
    }

    sb.append(s" $image")

    if (command.isDefined) {
      sb.append(s" ${command.get}")
    }

    val commandString = sb.toString
    println(commandString)

    if (background) {
      val output = DevClusterProcess.process(sb.toString).!!.stripLineEnd
      Some(output)
    } else {
      //write command to execute later (in dev-cluster script)
      DockerUtilities.writeDockerCommand(commandString)
      None
    }
  }
} 
Example 9
Source File: ProcessRunner.scala    From stryker4s   with Apache License 2.0 5 votes vote down vote up
package stryker4s.run.process

import better.files.File
import grizzled.slf4j.Logging

import scala.concurrent.duration.{Duration, MINUTES}
import scala.sys.process.{Process, ProcessLogger}
import scala.util.Try
import cats.effect.IO

trait ProcessRunner extends Logging {
  def apply(command: Command, workingDir: File): Try[Seq[String]] = {
    Try {
      Process(s"${command.command} ${command.args}", workingDir.toJava)
        .!!<(ProcessLogger(debug(_)))
        .linesIterator
        .toSeq
    }
  }

  def apply(command: Command, workingDir: File, envVar: (String, String)): Try[Int] = {
    val mutantProcess = Process(s"${command.command} ${command.args}", workingDir.toJava, envVar)
      .run(ProcessLogger(debug(_)))

    val exitCodeFuture = IO(mutantProcess.exitValue())
    // TODO: Maybe don't use unsafeRunTimed
    // TODO: Use timeout decided by initial test-run duration
    Try(exitCodeFuture.unsafeRunTimed(Duration(2, MINUTES)).get)
  }
}

object ProcessRunner {
  private def isWindows: Boolean = sys.props("os.name").toLowerCase.contains("windows")

  def apply(): ProcessRunner = {
    if (isWindows) new WindowsProcessRunner
    else new UnixProcessRunner
  }
} 
Example 10
Source File: ProcessUtil.scala    From sigmastate-interpreter   with MIT License 5 votes vote down vote up
package scalan.util

import java.io.File

import scala.collection.mutable
import scala.sys.process.ProcessLogger

case class ProcessOutput(stdOutLines: Seq[String], stdErrLines: Seq[String], interleavedLines: Seq[String]) {
  lazy val stdOutAll = stdOutLines.mkString("\n")
  lazy val stdErrAll = stdErrLines.mkString("\n")
  lazy val interleavedAll = interleavedLines.mkString("\n")
}

object ProcessUtil {
  
  def launch(command: Seq[String], workingDir: File = FileUtil.currentWorkingDir, extraEnv: Map[String, String] = Map.empty, printToConsole: Boolean = false): ProcessOutput = {
    val absoluteWorkingDir = workingDir.getAbsoluteFile
    val builder = scala.sys.process.Process(command, absoluteWorkingDir, extraEnv.toSeq: _*)
    val stdOutBuffer = mutable.ArrayBuffer.empty[String]
    val stdErrBuffer = mutable.ArrayBuffer.empty[String]
    val interleavedBuffer = mutable.ArrayBuffer.empty[String]
    val logger = ProcessLogger(
      outLine => {
        if (printToConsole) { Console.out.println(outLine) }
        stdOutBuffer += outLine
        interleavedBuffer += outLine
      },
      errLine => {
        if (printToConsole) { Console.err.println(errLine) }
        stdErrBuffer += errLine
        interleavedBuffer += errLine
      }
    )
    (builder ! logger) match {
      case 0 =>
        ProcessOutput(stdOutBuffer, stdErrBuffer, interleavedBuffer)
      case exitCode =>
        val envPrefix = extraEnv.map {
          case (name, value) => s"$name=${escapeCommandLineArg(value)} "
        }.mkString("")
        val commandString = command.map(escapeCommandLineArg).mkString(" ")
        throw new RuntimeException(s"Executing `$envPrefix$commandString` in directory $absoluteWorkingDir returned exit code $exitCode with following output:\n${interleavedBuffer.mkString("\n")}")
    }
  }

  private def escapeCommandLineArg(arg: String) = {
    if (arg.contains(" ") && !arg.contains("'"))
      "'" + arg + "'"
    else {
      val escaped = arg.replace("""\""", """\\""").replace("$", """\$""").
        replace("`", """\`""").replace("\"", """\"""").replace("\n", "\\\n")
      if (escaped.contains(" ") || escaped != arg) StringUtil.quote(escaped) else arg
    }
  }
} 
Example 11
Source File: WindowsPluginFrontendSpec.scala    From protoc-bridge   with Apache License 2.0 5 votes vote down vote up
package protocbridge.frontend

import java.io.ByteArrayInputStream

import protocbridge.ProtocCodeGenerator

import scala.sys.process.ProcessLogger
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.must.Matchers

class WindowsPluginFrontendSpec extends AnyFlatSpec with Matchers {
  if (PluginFrontend.isWindows) {
    it must "execute a program that forwards input and output to given stream" in {
      val toSend = "ping"
      val toReceive = "pong"

      val fakeGenerator = new ProtocCodeGenerator {
        override def run(request: Array[Byte]): Array[Byte] = {
          request mustBe toSend.getBytes
          toReceive.getBytes
        }
      }
      val (path, state) = WindowsPluginFrontend.prepare(fakeGenerator)
      val actualOutput = scala.collection.mutable.Buffer.empty[String]
      val process = sys.process
        .Process(path.toAbsolutePath.toString)
        .#<(new ByteArrayInputStream(toSend.getBytes))
        .run(ProcessLogger(o => actualOutput.append(o)))
      process.exitValue()
      actualOutput.mkString mustBe toReceive
      WindowsPluginFrontend.cleanup(state)
    }
  }
} 
Example 12
Source File: ProcessLogRedirector.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.util

import java.io.{Closeable, Flushable}
import scala.sys.process.ProcessLogger

import org.slf4j.LoggerFactory


class ProcessLogRedirector extends ProcessLogger with Closeable with Flushable with ConsoleOutput {
  private val LOG = LoggerFactory.getLogger("redirect")

  // We only capture the first 1K chars
  private final val LENGTH = 1000
  private var _error: String = ""
  private var _output: String = ""

  def error: String = _error
  def output: String = _output

  def out(s: => String): Unit = {
    if (_output.length <= LENGTH) {
      _output += "\n" + s
    }
    LOG.info(s)
  }
  def err(s: => String): Unit = {
    if (_error.length <= LENGTH) {
      _error += "\n" + s
    }
    LOG.error(s)
  }
  def buffer[T](f: => T): T = f
  def close(): Unit = Unit
  def flush(): Unit = Unit
} 
Example 13
Source File: ProcessJobRunnerSrv.scala    From Cortex   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.thp.cortex.services

import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Path, Paths}

import akka.actor.ActorSystem
import javax.inject.{Inject, Singleton}
import org.elastic4play.utils.RichFuture
import org.thp.cortex.models._
import play.api.Logger
import play.api.libs.json.Json

import scala.concurrent.duration.FiniteDuration
import scala.concurrent.{ExecutionContext, Future}
import scala.sys.process.{Process, ProcessLogger, _}
import scala.util.Try

@Singleton
class ProcessJobRunnerSrv @Inject()(implicit val system: ActorSystem) {

  lazy val logger = Logger(getClass)

  private val pythonPackageVersionRegex = "^Version: ([0-9]*)\\.([0-9]*)\\.([0-9]*)".r

  def checkCortexUtilsVersion(pythonVersion: String): Option[(Int, Int, Int)] =
    Try {
      (s"pip$pythonVersion" :: "show" :: "cortexutils" :: Nil)
        .lineStream
        .collectFirst {
          case pythonPackageVersionRegex(major, minor, patch) ⇒ (major.toInt, minor.toInt, patch.toInt)
        }
    }.getOrElse(None)

  def run(jobDirectory: Path, command: String, job: Job, timeout: Option[FiniteDuration])(implicit ec: ExecutionContext): Future[Unit] = {
    val baseDirectory = Paths.get(command).getParent.getParent
    val output        = StringBuilder.newBuilder
    logger.info(s"Execute $command in $baseDirectory, timeout is ${timeout.fold("none")(_.toString)}")
    val process = Process(Seq(command, jobDirectory.toString), baseDirectory.toFile)
      .run(ProcessLogger { s ⇒
        logger.info(s"  Job ${job.id}: $s")
        output ++= s
      })
    val execution = Future
      .apply {
        process.exitValue()
        ()
      }
      .map { _ ⇒
        val outputFile = jobDirectory.resolve("output").resolve("output.json")
        if (!Files.exists(outputFile) || Files.size(outputFile) == 0) {
          val report = Json.obj("success" → false, "errorMessage" → output.toString)
          Files.write(outputFile, report.toString.getBytes(StandardCharsets.UTF_8))
        }
        ()
      }
      .recoverWith {
        case error ⇒
          logger.error(s"Execution of command $command failed", error)
          Future.apply {
            val report = Json.obj("success" → false, "errorMessage" → s"${error.getMessage}\n$output")
            Files.write(jobDirectory.resolve("output").resolve("output.json"), report.toString.getBytes(StandardCharsets.UTF_8))
            ()
          }
      }
    timeout.fold(execution)(t ⇒ execution.withTimeout(t, killProcess(process)))
  }

  def killProcess(process: Process): Unit = {
    logger.info("Timeout reached, killing process")
    process.destroy()
  }
} 
Example 14
Source File: InstallRouteMgmt.scala    From openwhisk   with Apache License 2.0 5 votes vote down vote up
package org.apache.openwhisk.standalone

import java.io.File

import akka.http.scaladsl.model.Uri
import org.apache.commons.io.{FileUtils, IOUtils}
import org.apache.openwhisk.common.TransactionId.systemPrefix
import org.apache.openwhisk.common.{Logging, TransactionId}

import scala.sys.process.ProcessLogger
import scala.util.Try
import scala.sys.process._

case class InstallRouteMgmt(workDir: File,
                            authKey: String,
                            apiHost: Uri,
                            namespace: String,
                            gatewayUrl: Uri,
                            wsk: String)(implicit log: Logging) {
  case class Action(name: String, desc: String)
  private val noopLogger = ProcessLogger(_ => ())
  private implicit val tid: TransactionId = TransactionId(systemPrefix + "apiMgmt")
  val actionNames = Array(
    Action("createApi", "Create an API"),
    Action("deleteApi", "Delete the API"),
    Action("getApi", "Retrieve the specified API configuration (in JSON format)"))

  def run(): Unit = {
    require(wskExists, s"wsk command not found at $wsk. Route management actions cannot be installed")
    log.info(this, packageUpdateCmd.!!.trim)
    //TODO Optimize to ignore this if package already installed
    actionNames.foreach { action =>
      val name = action.name
      val actionZip = new File(workDir, s"$name.zip")
      FileUtils.copyURLToFile(IOUtils.resourceToURL(s"/$name.zip"), actionZip)
      val cmd = createActionUpdateCmd(action, name, actionZip)
      val result = cmd.!!.trim
      log.info(this, s"Installed $name - $result")
      FileUtils.deleteQuietly(actionZip)
    }
    //This log message is used by tests to confirm that actions are installed
    log.info(this, "Installed Route Management Actions")
  }

  private def createActionUpdateCmd(action: Action, name: String, actionZip: File) = {
    Seq(
      wsk,
      "--apihost",
      apiHost.toString(),
      "--auth",
      authKey,
      "action",
      "update",
      s"$namespace/apimgmt/$name",
      actionZip.getAbsolutePath,
      "-a",
      "description",
      action.desc,
      "--kind",
      "nodejs:default",
      "-a",
      "web-export",
      "true",
      "-a",
      "final",
      "true")
  }

  private def packageUpdateCmd = {
    Seq(
      wsk,
      "--apihost",
      apiHost.toString(),
      "--auth",
      authKey,
      "package",
      "update",
      s"$namespace/apimgmt",
      "--shared",
      "no",
      "-a",
      "description",
      "This package manages the gateway API configuration.",
      "-p",
      "gwUrlV2",
      gatewayUrl.toString())
  }

  def wskExists: Boolean = Try(s"$wsk property get --cliversion".!(noopLogger)).getOrElse(-1) == 0
} 
Example 15
Source File: SimpleExec.scala    From openwhisk   with Apache License 2.0 5 votes vote down vote up
package common

import org.apache.openwhisk.common.{Logging, TransactionId}

import scala.sys.process.{stringSeqToProcess, ProcessLogger}


  def syncRunCmd(cmd: Seq[String])(implicit transid: TransactionId, logging: Logging): (String, String, Int) = {
    logging.info(this, s"Running command: ${cmd.mkString(" ")}")
    val pb = stringSeqToProcess(cmd)

    val outs = new StringBuilder()
    val errs = new StringBuilder()

    val exitCode = pb ! ProcessLogger(outStr => {
      outs.append(outStr)
      outs.append("\n")
    }, errStr => {
      errs.append(errStr)
      errs.append("\n")
    })

    logging.debug(this, s"Done running command: ${cmd.mkString(" ")}")

    def noLastNewLine(sb: StringBuilder) = {
      if (sb.isEmpty) "" else sb.substring(0, sb.size - 1)
    }

    (noLastNewLine(outs), noLastNewLine(errs), exitCode)
  }
} 
Example 16
Source File: package.scala    From sbt-reactive-app   with Apache License 2.0 5 votes vote down vote up
package com.lightbend.rp.sbtreactiveapp

import java.io.File
import java.nio.file.Paths
import org.apache.tools.ant.filters.StringInputStream
import sbt.Logger
import scala.collection.immutable.Seq
import scala.sys.process.{ Process, ProcessLogger }

package object cmd {
  
  private[cmd] def run(
    cwd: File = Paths.get(".").toRealPath().toFile,
    env: Map[String, String] = Map.empty,
    input: Option[String] = None,
    logStdErr: Option[Logger] = None,
    logStdOut: Option[Logger] = None)(args: String*): (Int, Seq[String], Seq[String]) = {
    var outList = List.empty[String]
    var errList = List.empty[String]

    val stringLogger = ProcessLogger(
      { s =>
        outList = s :: outList

        logStdOut.foreach(_.info(s))
      },
      { s =>
        errList = s :: errList

        logStdErr.foreach(_.error(s))
      })

    val exitCode =
      input
        .map(new StringInputStream(_))
        .foldLeft(Process(args, cwd = cwd, env.toVector: _*))(_ #< _)
        .run(stringLogger)
        .exitValue()

    (exitCode, outList.reverse, errList.reverse)
  }

  private[cmd] def runSuccess(failMsg: String)(result: (Int, Seq[String], Seq[String])): Unit = {
    if (result._1 != 0) {
      sys.error(s"$failMsg [${result._1}]")
    }
  }
} 
Example 17
Source File: RunProcess.scala    From zorechka-bot   with MIT License 5 votes vote down vote up
package com.wix.zorechka.clients.process

import java.nio.file.Path

import zio.{Task, ZIO}

import scala.collection.mutable.ListBuffer
import scala.sys.process.{Process, ProcessLogger}

case class ClientOutput(value: List[String]) extends AnyVal

object RunProcess {
  def execCmd(command: List[String], workDir: Path, extraEnv: List[(String, String)] = List.empty): Task[ClientOutput] = ZIO.effect {
    val lb = ListBuffer.empty[String]
    val log = new ProcessLogger {
      override def out(s: => String): Unit = {
        println(s)
        lb.append(s)
      }
      override def err(s: => String): Unit = {
        println(s)
        lb.append(s)
      }
      override def buffer[T](f: => T): T = f
    }

    println(command.mkString(" "))
    val exitStatus = Process(command, Some(workDir.toFile), extraEnv: _*).!(log)
    if (exitStatus != 0 && exitStatus != 3)
      throw new IllegalStateException(s"Got status $exitStatus")
    ClientOutput(lb.result())
  }
} 
Example 18
Source File: HeadSandbox.scala    From daml   with Apache License 2.0 5 votes vote down vote up
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.navigator.test.runner

import java.io.File

import scala.concurrent._
import scala.concurrent.duration._
import scala.sys.process.{Process, ProcessLogger}
import scala.util.Success


    def buffer[T](f: => T): T = f
  }

  def runAsync(port: Int, darFile: File, scenario: String): Unit => Unit = {
    // Run the sandbox.
    val logger = new SandboxLogger
    val sandbox = Process(
      Seq("sbt", s"sandbox/run ${darFile.getAbsolutePath} --port $port --scenario $scenario"),
      new File("../../../ledger"))
      .run(logger)

    // Sbt takes a long time to compile and start up, longer than Navigator keeps trying to connect.
    // Block for a while until the sandbox shows signs of being started up.
    logger.waitForStartup(300.seconds)

    val shutdown = (_: Unit) => {
      sandbox.destroy()
    }

    sys addShutdownHook shutdown(())
    _ =>
      shutdown(())
  }
} 
Example 19
Source File: Runner.scala    From daml   with Apache License 2.0 3 votes vote down vote up
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.navigator.test.runner

import com.typesafe.scalalogging.LazyLogging

import scala.sys.process.{Process, ProcessLogger}
import java.io.File

object Runner extends LazyLogging {

  class LazyProcessLogger(val prefix: String = "") extends ProcessLogger with LazyLogging {
    def out(s: => String): Unit = logger.info(prefix + s)
    def err(s: => String): Unit = logger.warn(prefix + s)
    def buffer[T](f: => T): T = f
  }

  def execute(
      command: Seq[String],
      log: Option[ProcessLogger] = None,
      cwd: Option[File] = None): Int = {
    logger.info(s"Executing `${command.mkString(" ")}`${cwd.map(f => s" in `$f`").getOrElse("")}")
    log.fold(Process(command, cwd).!)(l => Process(command, cwd).!(l))
  }

  def executeAsync(
      command: Seq[String],
      log: Option[ProcessLogger] = None,
      cwd: Option[File] = None): Process = {
    logger.info(s"Executing `${command.mkString(" ")}`${cwd.map(f => s" in `$f`").getOrElse("")}")
    val process = log.fold(Process(command, cwd).run())(l => Process(command, cwd).run(l))
    sys addShutdownHook {
      if (process.isAlive()) {
        process.destroy()
      }
    }
    process
  }
}