Archive for December 2009
In groovy we trust!!
copying, zipping selecting files with Groovy
Today I had to select some files from different directories copy in another. Despite it seemed a simple task, it wasn’t. The files were a lot, so I had to pack it.
Basically the structure of the directories was something like:
/A/US/file1
/A/US/file2
/A/US/file3
/A/UK/file1
/A/UK/file2
/A/UK/file3
...
/B/US/file1
/B/US/file2
/B/US/file3
/B/UK/file1
/B/UK/file2
/B/UK/file3
and I had to select the N% of the files from each of the second level directories (AA,AB, AC…and so on).
I wrote this short script that select only N% (parameter inside the script) of the files from the directory, pack it (without any structure information), and copy on the destination directory:
import java.util.zip.*
def createZip(destination, fileList) {
try{
byte[] buffer = new byte[1024];
ZipOutputStream out = new ZipOutputStream(new
FileOutputStream(destination));
out.setLevel(Deflater.DEFAULT_COMPRESSION);
fileList.each { selected ->
FileInputStream in_ = new FileInputStream(selected);
//the new File(selected).getName() return just the name of the file,
//in order to remove the directory structure inside.
out.putNextEntry(new ZipEntry(new File(selected).getName()));
int len;
while ((len = in_.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.closeEntry();
in_.close();
}
out.close();
}
catch (IllegalArgumentException iae)
{
iae.printStackTrace();
}
catch (FileNotFoundException fnfe)
{
fnfe.printStackTrace();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
null
}
/**
* Script
**/
def rootDirectory = /C:\inputDirectory/
def outputDirectory = /C:\outputDirectory/
if ((!new File(rootDirectory).list()) || (! new File(outputDirectory).list())){
println "Error. Input or Output directory are not valid."
throw new RuntimeException() //TODO: understand how to exit without raising an exception
}
def directories = ['A', 'B', 'C', 'D']
def percentageRules = 5 //5 = 5% of the rules for each country
directories.each{ subDirectory ->
println "listing ${rootDirectory}\\${subDirectory}"
def directoryList = new File("${rootDirectory}\\${subDirectory}").list()
if( directoryList ){
directoryList.each{ elem ->
def fileList = new File("${rootDirectory}\\${subDirectory}\\${elem}").list().grep{ !it.contains('out') }
def numberFiles = (fileList.size() * 5 / 100) as Integer
if ((numberFiles == 0) && (fileList.size() > 0)) {
numberFiles = fileList.size()
}
println "Zipping ${numberFiles} (${percentageRules}%) of files from ${rootDirectory}\\${subDirectory}\\${elem} into ${outputDirectory}\\${subDirectory}-${elem}.zip"
if(numberFiles){
fileList = fileList.collect{ e ->
"${rootDirectory}\\${subDirectory}\\${elem}\\${e}"
}
createZip("${outputDirectory}\\${subDirectory}-${elem}.zip", fileList[0..<numberFiles])
/* The old version copied the files instead of zipping it, using AntBuilder,
you can use it if you don't need to zip the files. */
/*
fileList[0..<numberFiles].each{
( new AntBuilder ( ) ).copy ( file : "${rootDirectory}\\${subDirectory}\\${elem}\\${inputF}" , tofile : "${outputDirectory}\\${inputF}" )
}
*/
}
}
}
}
I wrote it rushing, and I copied somewhere the first method (which was java), so might be errors or better ways. Anyway any suggestion are welcome.
Elected for FAmSCo!
Yesterday I got the good news about fedora FAmSCo (Fedora Ambassador Sterling Committee) elections, I was running for: I got elected in the board!
I finally want to do something different for Fedora, writing less code and start getting some responsibility, in order to grow in some aspects I’ve seen before.
Many many compliments to who have been elected and also for who haven’t, I’m sure next time will go better
Groovy & Grails eXchange 2009
The Groovy & Grails eXchange, organized by skillsmatter, was the first international conferences about software development I’ve attended in my career. Before I was more concentrated mostly on Linux and the Fedoraproject (FOSDEM, Linux TAG for instance).
The conference was headed in London from December 9th to 10th. The first day was focuses on Groovy and the second on Grails. I went there with Raffaele and Davide, friends and colleague of mine (we work together in Holland). As the conferences last Wednesday and Thursday, we decided to get Friday off and stay in London during the week end to visit the city (I already been there almost one year ago but London is always lovely).
At the conference there were about 150 people, mostly developers (heavy twitter users), in particular freelancers and really small companies from all over the world.

I really found all the speech really interesting. Guillaume Laforge spoke about the features of Groovy 1.7 and above (we even had a brief introduction about what are they considering to include in the 1.8 release) and Graeme Rocher introduced Grails 1.2.0 and the new plug-in development approach.
Based on my work in Holland, I really appreciated the ‘Groovy code kata’ talk from Dierk Koening and the DSL speech from Verkat Subramaniam. I had a really interesting chat with him about DSLs, in particular about what we should consider more important to balance the DSL we designed and are using in our project (I swear, I’ll post about it). I found Verkat really good to make examples in order to help you to understand better complexed concepts (DSL by examples might be a suggestion for next book
).

You can find some photos about the conference itself here and here.
I’m really satisfied by this conference, I met a lot of people and exchange contact with them to keep in touch and, at the end I won Grails in Action book.
I also met also three guys from the NLGUG (Netherland Groovy User Group): Erik, Alex and Sebastien. They were really interested to my work with groovy and they invited me to give a speech, next year, to the Groovy User Group. I already accepted because it’s cool to meet new people on topics I like and I’ll get the opportunity to get more integrated into the local groups.
I met also Alberto Brandolini (ZioBrando for most of the people), who is a trainer for skillsmatter and a expert software architect in Italy. Before, he saw only a name and a photo but fortunately I had the opportunity to spoke with him to exchange some ideas and suggestion.
After the conferences we enjoy London for three days. I can say that I love London, and it’s strange because London is chaotic and I’m not use to it, but it has a fashion and a people integration level that is difficult(may be impossible) to find in any other cities (maybe in New York, but I’ve never been there, yet).
Maybe after my project in Holland will be finished, I’ll move there for a while, who knows. But we’ll see.
From London I can say that English people are really crazy, or they drink like sponges. I really would like to know how many drink they drink: I saw people with t-shirt in the middle of the night with around 0° or below, is it possible to survive? I would like to know how can they drink English beers: for me are too warm and they have a strange bitter after-taste.
Anyway London is fantastic and beautiful. Period.
I took some photos, you can find here.











