Groovy equivalent to Python's exec statement? - groovy

In Python I can execute arbitrary code using exec(string). How can I do this in Groovy? I'd like the code to execute in the context of my currently running application, not as if I were using the Groovy shell.

To execute a command-line program dynamically in Groovy:
"cmdstring".execute().text
You can also execute some Groovy code wrapped in a String with:
def myGroovyCode = 'println "hi"'
Eval.me(myGroovyCode) //prints hi
See http://groovy.codehaus.org/api/groovy/util/Eval.html

You can also pass in your current context parameters via the binding mechanism to the Groovy Shell.
def myname = 'Inigo Montoya'
def binding = new Binding( [ myname:myname ] )
new GroovyShell( binding ).evaluate( 'println "My Name is " + myname' )

Related

Do I need proc.out.close() (groovy execute shell command)?

Based on:
Groovy executing shell commands
I have this groovy script:
def proc = "some bash command".execute()
//proc.out.close() // hm does not seem to be needed...
proc.waitFor()
if (proc.exitValue()) {
def errorMsg = proc.getErrorStream().text
println "[ERROR] $errorMsg"
} else {
println proc.text
}
That I use the execute various linux bash commands. Currently it works fine even without the proc.out.close() statement.
What is the purpose of proc.out.close() and why is it (not?) needed
proc.text is actually proc.getText()
form groovy api doc: Read the text of the output stream of the Process. Closes all the streams associated with the process after retrieving the text.
http://docs.groovy-lang.org/docs/latest/html/groovy-jdk/java/lang/Process.html#getText()
So, when using proc.text you don't need to call proc.out.close()

Why JMeter varibles aren't set from JSR223 Groovy Assertion?

I'm trying to set Jmeter variables extracted from Jmeter properties inside JSR223 Groovy Assertion.
Jmeter properties in which I'm interested looks like:
...
created_blob_A_6= fde65de0-3e32-11e8-a5b4-3906549016d8
created_blob_A_8= fef92d70-3e32-11e8-a5b4-3906549016d8
created_blob_A_9= ff775e20-3e32-11e8-bac3-e51250ffea15
created_blob_B_1= fd7302a0-3e32-11e8-a5b4-3906549016d8
created_blob_B_10= 00141350-3e33-11e8-bac3-e51250ffea15
...
In order to extract values from Jmeter properties, I've created JSR223 Groovy the following Assertion script:
def readParamPrefix = 'created_blob'
def writeParamPrefix = 'blob_to_delete'
def chucnkTypes = 'A'..'E'
def newBlobCounter = 1
chucnkTypes.each{ chunkLetter ->
(1..10).each{ streamNumber ->
String readParamName = readParamPrefix + '_' + chunkLetter + '_' + streamNumber
log.info('Read param name: ' + readParamName)
String writeParamName = writeParamPrefix + '_' + newBlobCounter
log.info('Write param name: ' + writeParamName)
String blob_id_to_delete = props.get(readParamName).toString().trim()
log.info('' + readParamName + ' => ' + writeParamName + ' (' + blob_id_to_delete + ')')
vars.put(writeParamName.toString(), blob_id_to_delete.toString())
newBlobCounter++
}
}
The script doesn't work for JMeter variables, but works fine for JMeter properties. Here is how JMeter properies look like:
JMeterProperties:
...
blob_to_delete_1=9b1c4f40-3e36-11e8-a5b4-3906549016d8
blob_to_delete_10=9da5e050-3e36-11e8-bac3-e51250ffea15
blob_to_delete_11=9b235420-3e36-11e8-bac3-e51250ffea15
blob_to_delete_50=9b656630-3e36-11e8-bac3-e51250ffea15
Could you tell me, how I can fix my code for setting up JMeter variables correctly?
I don't see any problem with your code:
So I would recommend:
Checking jmeter.log file for any suspicious entries
Checking what variables are defined using Debug Sampler and View Results Tree listener combination. See How to Debug your Apache JMeter Script article for more information on getting to the bottom of JMeter script failure or unexpected behavior.
Don't use ${varName} in scripts, Notice JSR223 Best Practices:
ensure the script does not use any variable using ${varName} as caching would take only first value of ${varName}. Instead use :
vars.get("varName")
You can also pass them as Parameters to the script and use them this way.
After you change it, look for errors in logs if it still doesn't works

Execute shell command from groovy w/out timeout

In groovy, you can execute a shell command like so:
def process = "<some shell command>".execute()
println process.text()
But if the command is a long running command, I find that it times out. Is there a way to prevent this from happening?
I do some long running stufff (45 min+) doing this where I build up a cmdLine object that is the command line to run and then:
def fose = new FileOutputStream(logFileErr)
def foss = new FileOutputStream(logFileStd)
Process proc = cmdLine.execute()
fose << proc.in
fose << proc.err
foss << proc.out
proc.waitFor()
It's been working for me a couple of years now (to the point I haven't had to revisit this solution)

Passing script results to main program in Scala 2.11 ScriptEngine

Using Scala Scripting Engine in 2.11 Milestone 7, how do I get a typed value back from the script engine? I'm getting error messages like "mypackage.Holler cannot be cast to mypackage.Holler".
Here is the use case (reduced to essentials). I want to use scripts to prepare and configure objects of a standard type that I will process in my main program. I have a trait:
package mypackage
trait Holler {
def shout: Unit
}
I have a user script in Scala, saved in the file /home/me/Foo.scala
object Foo extends mypackage.Holler {
def shout: Unit = println("Hello World!")
}
When I run this script using IMain.eval(Reader), I expect that the object Foo will be returned since it is the result of the last statement. Here is a program, including a couple useful printouts to run the script:
package mypackage
import javax.script.ScriptEngineManager
import scala.tools.nsc.interpreter.IMain
object Runner {
def main(args: Array[String]): Unit = {
// Create the script engine
val javaxEngine = new ScriptEngineManager().getEngineByName("scala")
val scalaEngine = javaEngine.asInstanceOf[IMain]
// Configure script engine to use the Java classpath
val useJavaClassPath = scalaEngine.settings.usejavacp.tryToSet(List("true"))
println("Use Java CP? " + useJavaClassPath)
val script = new java.io.FileReader("/home/me/Foo.scala")
val result = scalaEngine.eval(script)
println("Script Result Type: " + result.getClass.getName)
println("Defined Symbols: " + scalaEngine.definedSymbolList)
val myHoller = result.asInstanceOf[mypackage.Holler]
}
}
The script runs just fine under the script engine. But the result cannot be cast to Holler. The output of this program is as follows:
Use Java CP? Some(List(true))
Script Result Type: $line3.$read$$iw$$iw$Foo$
Defined Symbols: List(value engine, object iw$Foo)
Exception in thread "main" java.lang.ClassCastException: $line3.$read$$iw$$iw$Foo$ cannot be cast to mypackage.Holler
This tells me that the classpath is successfully recognized by the script engine, and that the Foo object is being constructed. But the trait mypackage.Holler (from the common classpath) inside the script is different from the trait mypackage.Holler in the main program.
If I add the following lines to the script:
Foo.shout
val result: Holler = Foo
I see:
The shout method being exercised ("Hello World!" prints out),
"result" is added to the list of defined symbols
result is clearly compatible with type Holler.
I can bind a "catcher" object to the script engine. Its code looks like this:
package mypackage
class Catcher {
var item: Holler = null
}
And I bind with
val mycatcher = new Catcher
scalaEngine.bind("catcher", mycatcher)
scalaEngine.eval("catcher = Foo")
Now "catcher" shows up in the list of defined symbols to the script engine and I can use the catcher to go into the script engine with a command like
scalaScriptEngine.eval("catcher.item = result")
But then I get strange "compile time" ClassCastExceptions saying:
mypackage.Holler cannot be cast to mypackage.Holler
If I make the "item" in the Catcher an Any, then I don't get the exception until I do
mycatcher.item.asInstanceOf[Holler]
in the main program. But I still get pretty much the same exception. It is as if two incompatible class loaders are being used with the same classpath. So how, from the main program, do I access the Foo object as an instance of Holler (which it clearly implements in the script engine)?

Println as argument in groovy script

I have one groovy script which print some statistics: println: "..."
now I have another groovy script which needs this data. Is possible somehow run first script from second and save this data as paramater and then work with them from second script ? I just know how to run script: with GroovyShell() and then run(...) but this doesnt return output of first script
A few options:
If you're calling it from a script, redefine stdout.
Fix the first script so it prints data retrieved from a class, and re-write the calling script to use that class instead of relying on the printed output from the first. Long-term may be the best option.
Use a pipe on the command line: groovy s1.groovy | groovy s2.groovy
Personally, when composing things that do stuff with stdin/stdio, I prefer the last method. Example:
s1.groovy
5.times { println it }
s2.groovy
r = new BufferedReader(new InputStreamReader(System.in))
while (l = r.readLine()) { println((l as Integer) * 2) }
Output
$ groovy s1.groovy
0
1
2
3
4
$ groovy s1.groovy | groovy s2.groovy
0
2
4
6
8
One way to do this would be to set the out parameter in the binding when calling the first script:
So given a script s1.groovy:
//Print the letters of 'tim_yates', one per line
'tim_yates'.each this.&println
We can do (in s2.groovy)
// Create a StringWriter that will capture output
String output = new StringWriter().with { sw ->
// And make a Binding for our script
new Binding().with { b ->
// Set 'out' in the Binding to be our StringWriter
b[ 'out' ] = sw
// evaluate the file with the GroovyShell (using the binding)
new GroovyShell( b ).evaluate( new File( 's1.groovy' ) )
}
// And return the String captured in our writer
sw.toString()
}
println output
And then run it with groovy s2.groovy
Edit
I think this is option #1 in Dave's answer...

Resources