Saturday, September 5, 2009

[Perl] Search and Replace Copyright in Source Code

Without getting into a debate over the legalities and application of copyright law, it was necessary to change the company name in the copyright statement in a large swath of code (the company had been purchased, then resold).

For the record, let me say that programmers, IT administrators and managers generally make poor lawyers.  This probably has something to do with the total lack of legal education and training.

This simple problem has a very elegant solution, which can be accomplished with a one line Perl program, executed from the command line.

As I put forth the solution, it was dismissed.

Over the next few weeks, the conversation turned.  Time was spent trying to develop an Ant script that would check and modify the code at build time.  Next, building an Eclipse plugin was investigated.  Finally, a developer had found an article on the Internet suggesting you should never, ever change the copyright text.

I never heard anything about it after that.

In any event, here is the solution.  The problem is that spread throughout thousands of files are these textual statements that need to be normalized:

   1: Copyright © 1983 Umbrella Corporation; All rights reserved.
   2:  
   3: Copyright 1923 Umbrella Corporation; All rights reserved.
   4:  
   5: Copyright 2050 Umbrella Inc.; All rights reserved.
   6:  
   7: Copyright 1234 Umbrella LLC  

The easiest way is to use Perl.  With Perl we can search and replace with a regular expression in a file.  For example, lets say I want to replace "hello" with "hello world" in myfile.java.  Easy:

   1: Perl –pi –w –e ‘s/hello/Hello world/g’ myfile.java

We can easily search and replace text in one file, we just need to build a regular expression that will match the copyright statements, and replace it the "correct" one.  Also, we use find to find all of the files we need to do the search and replace and use xargs to format them for the command line.

For example, to change the copyright year ("Copyright nnnn" to "Copyright 2009") you would use:

   1: perl -pi -w -e 's/Copyright \d+/Copyright 2009/g;' \
   2: `find -name "*.java" | xargs`

To change the company name:

   1: perl -pi -w -e 's/Umbrella LLC/Umbrella Corporation/g' \
   2: `find -name "*.java" | xargs`

Of course, these are just simple examples.  You will have to write a better regular expression and might have to take several stabs at it, but it isn't a three week issue. 

Labels: , , ,