When we started developing JFileContentManager, we didn’t even think to release it as an open-source project. So, when we finished the project, I particularly thought about this idea, because I was very interested in participating on an community involving project. So, we decided to release it under the LGPL licence, because we wanted the project audience to be wider as possible. But, before turning it public, we needed to add the LGPL license term to every Java file of the project.
Opening and editing each Java class, one by one, would be a tedious task, taking a lot of time and adding up to the fact I’m very interested on learning dynamic languages (like Python, Ruby), I developed a Python script to automate this task for us. Our Python code is intended to open each Java file, adding the license term on the start of the file as a Java comment and writing it to the disk. The script is as follows:

 1:# Python script to add the LGPL notices to each java file of the FileContentManager project.
 2:import os, glob, sys
 3:License = """\
 4:/**
 5:*FileContentManager is a Java based file manager desktop application, 
 6:*it can show, edit and manipulate the content of the files archived inside a zip.
 7:*
 8:*Copyright (C) 2008 
 9:*
10:*Created by Camila Sanchez [http://mimix.wordpress.com/], Rafael Naufal [http://rnaufal.livejournal.com] 
11:and Rodrigo [[email protected]]
12:*
13:*FileContentManager is free software; you can redistribute it and/or
14:*modify it under the terms of the GNU Lesser General Public
15:*License as published by the Free Software Foundation; either
16:*version 2.1 of the License, or (at your option) any later version.
17:*
18:*This library is distributed in the hope that it will be useful,
19:*but WITHOUT ANY WARRANTY; without even the implied warranty of
20:*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21:*Lesser General Public License for more details.
22:*
23:*You should have received a copy of the GNU Lesser General Public
24:*License along with FileContentManager; if not, write to the Free Software
25:*Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """
26:
27:size = len(sys.argv)
28:if size == 1 or size > 2:
29:    print "Usage: AddLicense.py $1"
30:    sys.exit(1)
31:inputPath = sys.argv[1]
32:if not os.path.exists(inputPath):
33:    print inputPath, "does not exist on disk"
34:    sys.exit(1)
35:if not os.path.isdir(inputPath):
36:    print inputPath, "isn't a dir"
37:    sys.exit(1)   
38:for path, dirs, files in os.walk(inputPath):
39:    fileWithLicense = ''
40:    for filepath in [ os.path.join(path, f)
41:            for f in files if f.endswith(".java")]:
42:        content = file(filepath).read()
43:        f = file(filepath, "w")
44:        print >>f, License + "\n" + content
45:        f.close()
46:        
47:    

License is a Python multiline string, denoted by the triple-quotes. The main action begins with the os.walk() expression that walks every file in the directory, entered by the user as a comannd line argument, like this:

python AddLicense.py ${path}

where ${path} defines user project root directory. The list comprehension syntax produces the full path of each of the Java files. When the match is found, the file is opened and its content is assigned to the variable content. So, the License string is added to the start of the file and the file is then written to the disk. The print statements use a redirection syntax, for example: print >>f, License + "\n" + content. The ‘>>f’ sends the results to f rather than the console. So, this Python script helped our team to release the project early and at the same time automated the task of adding the LGPL notices. I think Python is very useful for these kind of tasks. And you?