addcopyright.py

better license keywords library
This commit is contained in:
frank 2012-01-12 16:51:30 -08:00
parent 0156ec7cdd
commit 0b38d62c8e

View File

@ -22,6 +22,7 @@ from optparse import OptionParser
import sys
import os, os.path
import fnmatch
import time
class CopyRightDetecter(object):
def isCopyRightLine(self, txt):
@ -29,7 +30,20 @@ class CopyRightDetecter(object):
# if more than 1/2 words in one line are keyword, we treat it as copyright line
class KeyWordCopyRightDetecter(CopyRightDetecter):
keywords = ['Cloud.com', 'Copyright', '(C)', '2011', 'Citrix', 'Systems', 'Inc', 'All', 'rights', 'reserved', 'This', 'software', 'is', 'licensed', 'under', 'the', 'GNU', 'General', 'Public', 'License', 'v3', 'or', 'later', 'It', 'is', 'free', 'software:', 'you', 'can', 'redistribute', 'it', 'and/or', 'modify', 'it', 'under', 'the', 'terms', 'of', 'the', 'GNU', 'General', 'Public', 'License', 'as', 'published', 'by', 'the', 'Free', 'Software', 'Foundation', 'either', 'version', '3', 'of', 'the', 'License', 'or', 'any', 'later', 'version', 'This', 'program', 'is', 'distributed', 'in', 'the', 'hope', 'that', 'it', 'will', 'be', 'useful', 'but', 'WITHOUT', 'ANY', 'WARRANTY;', 'without', 'even', 'the', 'implied', 'warranty', 'of', 'MERCHANTABILITY', 'or', 'FITNESS', 'FOR', 'A', 'PARTICULAR', 'PURPOSE', 'See', 'the', 'GNU', 'General', 'Public', 'License', 'for', 'more', 'details', 'You', 'should', 'have', 'received', 'a', 'copy', 'of', 'the', 'GNU', 'General', 'Public', 'License', 'along', 'with', 'this', 'program', 'If', 'not', 'see', '<http://www.gnu.org/licenses/>']
keywords = ['Cloud.com', 'Copyright', '(C)', '2011', 'Citrix', 'Systems', 'Inc', 'All', 'rights', 'reserved', 'This', 'software', 'is', 'licensed', 'under', 'the',
'GNU', 'General', 'Public', 'License', 'v3', 'or', 'later', 'It', 'is', 'free', 'software:', 'you', 'can', 'redistribute', 'it', 'and/or', 'modify', 'it',
'under', 'the', 'terms', 'of', 'the', 'GNU', 'General', 'Public', 'License', 'as', 'published', 'by', 'the', 'Free', 'Software', 'Foundation', 'either',
'version', '3', 'of', 'the', 'License', 'or', 'any', 'later', 'version', 'This', 'program', 'is', 'distributed', 'in', 'the', 'hope', 'that', 'it', 'will',
'be', 'useful', 'but', 'WITHOUT', 'ANY', 'WARRANTY;', 'without', 'even', 'the', 'implied', 'warranty', 'of', 'MERCHANTABILITY', 'or', 'FITNESS', 'FOR', 'A',
'PARTICULAR', 'PURPOSE', 'See', 'the', 'GNU', 'General', 'Public', 'License', 'for', 'more', 'details', 'You', 'should', 'have', 'received', 'a', 'copy', 'of',
'the', 'GNU', 'General', 'Public', 'License', 'along', 'with', 'this', 'program', 'If', 'not', 'see', '<http://www.gnu.org/licenses/>', 'Version', '2', 'only',
'("GPL")', 'Common', 'Development', 'and', 'Distribution', 'License("CDDL")', '(collectively', 'License").', 'language', 'governing', 'permissions', 'limitations',
'License.', '-', 'When', 'distributing', 'include', 'Header', 'Notice', 'each', 'file', 'at', 'glassfish/bootstrap/legal/LICENSE.txt.', 'Sun', 'designates',
'particular', 'subject', 'to', 'Classpath', 'exception', 'provided', 'GPL', 'section', 'accompanied', 'code.', 'applicable', 'add', 'following', 'below',
'fields', 'enclosed', 'brackets', 'replaced', 'your', 'own', 'identifying', 'information', 'Portions', 'Copyrighted', 'year', 'name', 'copyright', 'owner]',
'Contributor(s)', 'indicate', 'decision', 'adding', '[Contributor', 'elects', 'distribution', 'CDDL', 'license.', "don't", 'single', 'choice', 'license',
'recipient', 'has', 'option', 'distribute', 'extend', 'its', 'licensees', 'above.', 'However', 'if', 'code', 'therefore', 'elected', 'then', 'applies', 'new',
'made', 'such', 'holder.']
def isCopyRightLine(self, txt):
words = [ c.strip().strip('.').strip('\n').strip(',').strip() for c in txt.split(" ") ]
@ -88,10 +102,12 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
copyRightTxt = None
targetFile = None
fileBody = None
decter = None
signature = 'Automatically generated by addcopyright.py at %s' % time.strftime("%m/%d/%Y", time.localtime())
COMMENT_NOTATION = ""
@ -118,6 +134,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
def composeCopyRight(self):
l = self.copyRightTxt
l.append(self.signature)
l = [ self.COMMENT_NOTATION + " " + line + "\n" for line in l ]
self.copyRightTxt = l
@ -132,25 +149,25 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
def removeOldCopyRight(self):
newBody = []
removed = False
for line in self.fileBody:
for line in self.fileBody[0:50]:
if self.isCommentLine(line) and self.decter.isCopyRightLine(line):
removed = True
Logger.debug("remove old copyright: %s" % line)
continue
newBody.append(line)
self.fileBody = newBody
self.fileBody = newBody + self.fileBody[50:]
if removed:
Logger.info("Removed old copyright header of %s"%self.targetFile)
def cleanBlankComment(self):
newBody = []
for l in self.fileBody:
for l in self.fileBody[0:50]:
if self.isCommentLine(l) and l.strip().strip(self.COMMENT_NOTATION).strip().strip('\n') == "":
Logger.debug("Blank Comment: %s" % l)
continue
newBody.append(l)
self.fileBody = newBody
self.fileBody = newBody + self.fileBody[50:]
def doWork(self):
self.checkParams()
@ -179,6 +196,32 @@ class InterpreterAdder(Adder):
self.fileBody = self.copyRightTxt + self.fileBody
file(self.targetFile, 'w').write("".join(self.fileBody))
Logger.info("Added copyright header to %s"%self.targetFile)
class JavaAdder(Adder):
commentOn = False
def __init__(self):
super(JavaAdder, self).__init__()
self.COMMENT_NOTATION = '//'
def isCommentLine(self, line):
if line.strip().startswith('//'):
return True
elif line.strip().startswith('/*'):
self.commentOn = True
return True
elif self.commentOn == True and line.find('*/') != -1:
self.commentOn = False
return True
elif self.commentOn:
return True
else:
return False
def cleanBlankComment(self):
#TODO
pass
copyRightAdderFactory = {".sql":SqlAdder.__name__, ".sh":InterpreterAdder.__name__, ".py":InterpreterAdder.__name__}
class CopyRightAdder(object):
@ -196,7 +239,7 @@ class CopyRightAdder(object):
sys.exit(1)
def __init__(self):
usage = '''Usage: addcopyright [file_name_pattern] [--exculdes=file_name_pattern] [--file=copyright_file] [--root=root_dir_of_files_to_add_header]
usage = '''Usage: addcopyright [file_name_pattern] [--exculdes=file_name_pattern] [--file=copyright_file] [--root=root_dir_of_files_to_add_header] [--replace]
Examples:
addcopyright
addcopyright *.sql