views:

1683

answers:

7

I'm looking for a tool that will, in bulk, add a license header to some source files, some of which already have the header. Is there a tool out there that will insert a header, if it is not already present?

Edit: I am intentionally not marking an answer to this question, since answers are basically all environment-specific and subjective

+2  A: 

Python solution, modify for your own need

Features:

  • handles UTF headers (important for most IDEs)
  • recursively updates all files in target directory passing given mask (modify the .endswith parameter for the filemask of your language (.c, .java, ..etc)
  • ability to overwrite previous copyright text (provide old copyright parameter to do this)
  • optionally omits directories given in the excludedir array

-

# updates the copyright information for all .cs files
# usage: call recursive_traversal, with the following parameters
# parent directory, old copyright text content, new copyright text content

import os

excludedir = ["..\\Lib"]

def update_source(filename, oldcopyright, copyright):
    utfstr = chr(0xef)+chr(0xbb)+chr(0xbf)
    fdata = file(filename,"r+").read()
    isUTF = False
    if (fdata.startswith(utfstr)):
        isUTF = True
        fdata = fdata[3:]
    if (oldcopyright != None):
        if (fdata.startswith(oldcopyright)):
            fdata = fdata[len(oldcopyright):]
    if not (fdata.startswith(copyright)):
        print "updating "+filename
        fdata = copyright + fdata
        if (isUTF):
            file(filename,"w").write(utfstr+fdata)
        else:
            file(filename,"w").write(fdata)

def recursive_traversal(dir,  oldcopyright, copyright):
    global excludedir
    fns = os.listdir(dir)
    print "listing "+dir
    for fn in fns:
        fullfn = os.path.join(dir,fn)
        if (fullfn in excludedir):
            continue
        if (os.path.isdir(fullfn)):
            recursive_traversal(fullfn, oldcopyright, copyright)
        else:
            if (fullfn.endswith(".cs")):
                update_source(fullfn, oldcopyright, copyright)


oldcright = file("oldcr.txt","r+").read()
cright = file("copyrightText.txt","r+").read()
recursive_traversal("..", oldcright, cright)
exit()
Silver Dragon
Probably wouldn't hurt to mention your script is in python.
Dana
+10  A: 
for i in *.cc # or whatever other pattern...
do
  if ! grep -q Copyright $i
  then
    cat copyright.txt $i >$i.new && mv $i.new $i
  fi
done
Tim
for i in "$@" is a pretty good choice. You can also get inventive with checkouts if your VCS system needs those.
Jonathan Leffler
+2  A: 

Here's a Bash script that'll do the trick, assuming you have the license header in the file license.txt:

File addlicense.sh:


#!/bin/bash
for x in $*; do
  head -$LICENSELEN $x | diff license.txt - || ( ( cat license.txt; echo; cat $x) > /tmp/file; mv /tmp/file $x )
done

Now run this:


export LICENSELEN=`wc -l license.txt | sed 's/[^0-9]//g`
find dir -type f -name \( \*.cpp -o -type \*.h \) -print0 | xargs -0 ./addlicense.sh
Adam Rosenfield
A: 

Here is one I used once (in ruby) which may help.

Michael Neale
+1  A: 

For Java, http://code.google.com/p/maven-license-plugin/

Kind Regards

marcospereira
+1  A: 

Here's one I found on the Apache list. Its written in Ruby and seems easy enough to read. You should even be able to call it from rake for extra special niceness. :)

RNHurt
+2  A: 

If you still need one, there is a little tool I have written, named SrcHead. You can find it at http://www.solvasoft.nl/downloads.html