Wednesday, February 27, 2008

 

Useful RUBY script to add Copyright information to java files


If you are have ever implemented software for an organization, you must be aware that copyright information of the organization should be included to each and every file you implement. I work for a software company which has a history of about 8 years. The project I work in has over thousands of classes and is implemented using J2EE. Since, different people have different coding practices, copyright information was included in some java source files while majority of source files did not have the copyright information.
Recently, management has decided to refine the code by including copyright information to source files that did not have copyright information while updating the files which had copyright information to match latest information.
Although, it is very easy to include copyright information to the files which would be implemented from the scratch using the power of IDEs in the current market, the tedious task was to modify the existing source files. The power of RUBY helped me in this task. I was able to implement a ruby script which includes copyright information to java files which did not have copyright information and update existing java files which had copyright information to match the current copyright year information.
Following is a simplified version of the script I have implemented. I hope this would be helpful for all of you who work for software companies since, you may also come across this kind requirement.


shell_command="find -name *.java"

#Simplified version of regular expression to search 
#copyright information in the file. Complex regular
#expression can be used to make the filtering stronger.
#Example:
#/*
# * (C) Copyright 1999-2008 [company name]. All Rights Reserved.
# *
# * These materials are unpublished, proprietary, 
# * confidential source code of [company name] and 
# * constitute a TRADE SECRET of [company name].
# *
# * [company name] retains all title to and intellectual
# * property rights in these materials. 
# */

copyRightRegEx  = ^\s*\*\s*\(\s*(Cc\s*)\)\s*Copyright\
s*\d{4,4}\s*-\s*\d{4,4}\s*<company name>\.\sAll\sRights\sReserved\./

#Regular expression to search the 4 digit year information
endYearRegx = /\d{4,4}/

#Write path of each java file to 'file_list.log'
result =  %x[#{shell_command}]
File.open("file_list.log","w") do file
file.write(result)
end

#Load each java file written in 'file_list.log' and 
#perform add/modify copyright information
File.open("file_list.log","r") do file
while line = file.gets
is_copyrighted = false
file_name = line.strip
backup_file_name = file_name + ".back"

#open the file & check for the Copyright text
File.open(file_name,"r") do file1
while line1 = file1.gets
new_line = line1
matchObj = copyRightRegEx.match(line1)

#If file already contains copyright information
#execute necessary updates and flag the file as 
#copyrighted (For example the end year could be
#updated to 2008 as given below)
if matchObj != nil
new_line = line1.sub(/-\s*\d{4,4}/, "-2008")
is_copyrighted=true
end

#Write the modified file content to a temporary file
File.open(backup_file_name,"a") do file2
file2.write(new_line)
end
end
end

#Add the copyright information to java file which 
#does not have copyright information
#This can be accomplished by file concatenation
if is_copyrighted == false then
backup_file_name = file_name + ".back"   
copy_command = "cp " + file_name + " " +
backup_file_name

#copy_rights.txt :
#file which contains copyright informaion of the company
#This file is concatenated with temparory file.  
cat_command = "cat copy_rights.txt " + backup_file_name +
" > " + file_name
del_command = "rm " + backup_file_name

%x[#{copy_command}]
%x[#{cat_command}]
%x[#{del_command}]

File.open("non_copyrighted_file.log","a") do file3
file3.write(line.strip + "\n")
end
else
del_command = "rm " + file_name
move_command = "mv " + backup_file_name + " " + file_name
%x[#{del_command}]
%x[#{move_command}]

File.open("copyrighted_files.log","a") do file4
file4.write(line.strip + "\n")
end
end
end
end

Tuesday, February 26, 2008

 

A Remarkable Gift


Nowadays, exchanging gifts has become a fashion. The gifts that are given may either be a small bottle of perfume, novel, some electronic device like ipod, or an expensive jewelry etc.. But how many of you have thought about how useful it would be to receiver. Although, I am not an expert in selecting gifts, one gift I recently received, made me interested about the art of selecting gifts (I would like to say it is an art)
Senthoor, the team leader of the project I work in and, one of the best friends I have met in my life, suggested me to start a blog some time back. Although, I had the interest of having my own blog, due to examinations and tough schedules of work, I could not start writing for few months. Recently, he reminded me about the idea of starting a blog. The remarkable point is not only did he remind me to start a blog but, he just logged in to http://www.doteasy.com/ and purchased a new domain as the gift for my next birthday.
I have never though that a domain would be a better choice for a birthday gift. Then I realized that selecting better gifts is an art which is worth mastering because, someday, we also will happen to select a gift for someone. So, think twice or even thrice before selecting a gift.
Thanking to Senthoor, I would welcome all of you to my web log !!!

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]