Last updated January 04, 2010 21:28, by a.egger
Feedicon  

Groovy Groosh

Provides a shell-like capability for handling external processes in Groovy.

Groosh in Action

The following example shows Groosh in action:

  //Read a text file and write it to stdout
 @Grapes([
 @Grab(group='org.codehaus.groovy.modules.groosh',
    module='groovy-groosh',version='[0.3.5,)'),
 @GrabConfig(systemClassLoader=true)])
  
  import groosh.Groosh
  Groosh.withGroosh(this)
  
  cat('test_scripts/blah.txt') >> stdout


Another example :

  //Count the lines of all .java files in this directory and 
  //all its subdirectories.
  //Write the total number of lines to stdout.
  
 @Grapes([
 @Grab(group='org.codehaus.groovy.modules.groosh',
    module='groovy-groosh',version='[0.3.5,)'),
 @GrabConfig(systemClassLoader=true)])
  
  import groosh.Groosh
  Groosh.withGroosh(this)
  
  def f = find('.', '-name', '*.java', '-ls')
  def total = 0
  def lines = grid { values,w ->
    def x = values[2,4,6,10]
    def s = x.join('	')
    w.println(s)
    total += Integer.parseInt(values[6])
  }
  
  f | lines
  lines >> stdout
  println "Total: ${total}"
  

Sometimes the name of a shell command conflicts with a Groovy method (for example grep). This means that

 gsh.grep(...)

does not execute the shell command, but the Groovy method grep(...).

As a workaround for that you may prefix any shell command with _ this means the example above becomes

 gsh._grep(...)

The following example shows a more elaborate example. It uploads photos to a flickr account using the command line tool flickcurl. A photo set of this images is created and named after the current directory.

 import static groosh.Groosh.groosh
 import static org.codehaus.groovy.groosh.stream.DevNull.devnull
 
 ids = [:]
 shell = groosh()
 
 //get all images in this folder and upload it to flickr
 //remember the photo id we get from flickr
 shell.ls().grep(~/.*jpg/).each {
   println "Uploading file $it to flickr"
   flickcurl = shell.flickcurl("upload",it,"friend","family").useError(true)
   id = flickcurl.grep(~/.*Photo ID.*/)[0].split(":")[1].trim()
   ids[it] = id
   println "Photo ID is: $id"
 }
 //we need to know the first photo id
 firstKey = ids.keySet().toList()[0]
 
 //create a set with the name of the directory we are in right now
 //use the id of the first photo as set cover
 setName = shell.pwd().text.split("/")[-1]
 println "Creating set: $setName"
 flickcurl = shell.flickcurl("photosets.create",setName,
                                             setName,ids[firstKey]).useError(true)
 id = flickcurl.grep(~/.*Photoset.*/)[0].split(" ")[2].trim()
 println "Photoset ID is: $id"
 
 //make a backup of the ids in a file for later reference
 println "Writing ids to a file"
 file = new File(shell.pwd().text.trim() + "/.flickrset")
 file << "Photoset:" << id << "\n"
 ids.each {
   file << it.key << ":" << it.value << "\n"    
 }
 
 //the first photo is already part of the photo set so lets remove it
 ids.remove(firstKey)
 
 //add the remaining photos to the photo set
 ids.each {
   println "Adding photo to set at flickr: $it"
   shell.flickcurl("photosets.addPhoto",id,it.value) | devnull()
 }
 
 println "DONE"
  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2010, Oracle Corporation and/or its affiliates
(revision 20120127.ac94057)
 
 
Close
loading
Please Confirm
Close