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)
zella
  • 4,645
  • 6
  • 35
  • 60

1 Answers1

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))
  }
}
jwvh
  • 50,871
  • 7
  • 38
  • 64