5

I have python code, for example:

import sys

print('1')
print("2")
print(sys.argv[1])

Note that it can contain single, double quotes and accept arguments.

In my service I receive code by network and should execute it. I dont want to temporary save code as script on disk, so I want execute it with python -c command.

How I execute it in bash(https://stackoverflow.com/a/29565580/1996639):

$ python3 -c $'import sys\nprint(\'1\')\nprint("2")\nprint(sys.argv[1])' 3
1
2
3

My questions: how I can execute sample code from scala?

  import sys.process._
  def runCommand(cmd: String): (Int, String, String) = {
    val stdoutStream = new ByteArrayOutputStream
    val stderrStream = new ByteArrayOutputStream
    val stdoutWriter = new PrintWriter(stdoutStream)
    val stderrWriter = new PrintWriter(stderrStream)
    val exitValue = cmd.!(ProcessLogger(stdoutWriter.println, stderrWriter.println))
    stdoutWriter.close()
    stderrWriter.close()
    (exitValue, stdoutStream.toString, stderrStream.toString)
  }


  val code =
    """
      |import sys
      |
      |print('1')
      |print("2")
      |print(sys.argv[1])
    """.stripMargin

  val arg = 3

  val cmd = ???

  val (exitCode, std, err) = runCommand(cmd)

1 Answer 1

1

You can take advantage of the fact that Scala's ProcessBuilder methods also accept Seq[String].

import sys.process._
object SO {
  def runCommand(cmd: String*): (Int, String, String) = {
    val stdoutStream = new java.io.ByteArrayOutputStream
    val stderrStream = new java.io.ByteArrayOutputStream
    val stdoutWriter = new java.io.PrintWriter(stdoutStream)
    val stderrWriter = new java.io.PrintWriter(stderrStream)
    val exitValue =
      cmd.!(ProcessLogger(stdoutWriter.println, stderrWriter.println))
    stdoutWriter.close()
    stderrWriter.close()
    (exitValue, stdoutStream.toString, stderrStream.toString)
  }

  def main(args: Array[String]): Unit = {
    val code =
      """
        |import sys
        |
        |print('1')
        |print("2")
        |print(sys.argv[1])
      """.stripMargin

    val arg = 3
    println(runCommand("python3","-c", code, arg.toString))
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.