[groovy-user] Can't have the whole output of a process executed with String::execute()

5 views
Skip to first unread message

Francois Marot

unread,
Mar 4, 2013, 10:44:41 AM3/4/13
to us...@groovy.codehaus.org
Hi groovy users,

I recently tried to analyze with a script the output of git commands but it seems that I can't get all the lines the process output...

Concrete exemple:

francois@francois:~/java-workspaces/$ git checkout master
Already on 'master'
francois@francois:~/java-workspaces/$ groovy -e "println 'git checkout master'.execute(null, new File('.')).text"

francois@francois:~/java-workspaces/Olea/oleasphere$

As you can see in the exemple I don't get any output. I got other exemples where only the last line is returned (in case of multilines created by the process). My Groovy version: Groovy Version: 2.1.0 JVM: 1.6.0_27

Francois

Tim Yates

unread,
Mar 4, 2013, 10:53:00 AM3/4/13
to us...@groovy.codehaus.org
The message Already on 'master' is sent to the Error Stream, whereas Process.text returns the output from the Output Stream...

You can do this sort of thing:

new StringWriter().with { sw ->
    'git checkout master'.execute().with { proc ->
        consumeProcessOutput( sw, sw )
        proc.waitFor()
    }
    println sw.toString()
}

To send both the output and error streams to a StringWriter, then print out it's contents

Tim

Jim White

unread,
Mar 4, 2013, 1:47:40 PM3/4/13
to us...@groovy.codehaus.org
And the reason for this is that the reading of the output stream done by the Groovy Process.getText method will only block until it gets *some* characters.  After that it will just read up whatever is available.  So to get all of the stdout from a process you'd have to call Process.getText repeatedly.  Using the method Tim shows is better, although I wish we had a groovier way of doing this.  Its also arguable that Process.getText should wait for all the output because I think it is what most folks expect, but of course that won't help you at the moment.

Jim

Francois Marot

unread,
Mar 5, 2013, 6:17:08 AM3/5/13
to user
perfect Tim, great thanks !

One more question though :)
Say I have created a function that fulfills my needs (see below). Is it possible to add it globally so that whenever I execute a groovy script, whatever my current directory, I could use this function ?
Or would it be possible to add it to the Java String class (thanks to groovy metaclass) in the way that would be repeated each time I launch a scipt on the command line ?
I imagine it would involve a (kind of) bootstrap script in ${HOME}/.groovy/.
Do you have any usefull tips using this ${HOME}/.groovy/ directory ?
Do I have no choice but to compile into jar files the utilities I put in ${HOME}/.groovy/lib ?


String executeCommand(String command) {

  new StringWriter().with { sw ->
      'git checkout master'.execute().with { proc ->
      consumeProcessOutput( sw, sw )
      proc.waitFor()
      }
      return sw.toString()
  }
}

Reply all
Reply to author
Forward
0 new messages