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
Comments:
<< Home
I think we should use Ruby's object oriented concepts more. When we are writing script we should move useful stuff into separate modules so they can be reused.
I have not gone through fully but when given a search criteria one section of code returns results. If we move this to a module we can reuse in a different scripts when needed.
When we write more scripts like that we will build a personal reusable library of scripts I guess.
Post a Comment
I have not gone through fully but when given a search criteria one section of code returns results. If we move this to a module we can reuse in a different scripts when needed.
When we write more scripts like that we will build a personal reusable library of scripts I guess.
Subscribe to Post Comments [Atom]
<< Home
Subscribe to Posts [Atom]