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)