add shell/python support in copyright header tool

This commit is contained in:
frank 2012-01-11 18:42:18 -08:00
parent 52610ffcb3
commit 191c92a5c3

View File

@ -27,24 +27,26 @@ class CopyRightDetecter(object):
def isCopyRightLine(self, txt): def isCopyRightLine(self, txt):
return False return False
# if more than 1/2 words in one line are keyword, we treat it as copyright line
class KeyWordCopyRightDetecter(CopyRightDetecter): 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/>']
def isCopyRightLine(self, txt): def isCopyRightLine(self, txt):
words = [ c.strip().strip('.').strip('\n').strip(',') for c in txt.split(" ") ] words = [ c.strip().strip('.').strip('\n').strip(',').strip() for c in txt.split(" ") ]
total = len(words) total = len(words)
if total == 0: return False if total == 0: return False
numKeyWord = 0 numKeyWord = 0
for w in words: for w in words:
if w == "": continue
if w in self.keywords: numKeyWord+=1 if w in self.keywords: numKeyWord+=1
if float(numKeyWord)/float(total) > float(1)/float(3): return True if float(numKeyWord)/float(total) >= float(1)/float(2): return True
return False return False
copyRightDetectingFactory = {"KeyWord":KeyWordCopyRightDetecter.__name__} copyRightDetectingFactory = {"KeyWord":KeyWordCopyRightDetecter.__name__}
logfd = open("/tmp/addcopyright.log", 'w')
class Logger(object): class Logger(object):
@staticmethod @staticmethod
def info(msg): def info(msg):
@ -54,9 +56,8 @@ class Logger(object):
@staticmethod @staticmethod
def debug(msg): def debug(msg):
sys.stdout.write("DEBUG: %s"%msg) logfd.write("DEBUG: %s"%msg)
sys.stdout.write("\n") logfd.write("\n")
sys.stdout.flush()
@staticmethod @staticmethod
def warn(msg): def warn(msg):
@ -124,7 +125,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
self.fileBody = file(self.targetFile).readlines() self.fileBody = file(self.targetFile).readlines()
def isCommentLine(self, line): def isCommentLine(self, line):
if line.lstrip().startswith(self.COMMENT_NOTATION): if line.strip().startswith(self.COMMENT_NOTATION):
return True return True
return False return False
@ -132,7 +133,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
newBody = [] newBody = []
removed = False removed = False
for line in self.fileBody: for line in self.fileBody:
if self.isCommentLine(line) and self.decter.isCopyRightLine(line): removed = True; continue if self.isCommentLine(line) and self.decter.isCopyRightLine(line):
removed = True
Logger.debug("remove old copyright: %s" % line)
continue
newBody.append(line) newBody.append(line)
self.fileBody = newBody self.fileBody = newBody
@ -142,7 +146,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
def cleanBlankComment(self): def cleanBlankComment(self):
newBody = [] newBody = []
for l in self.fileBody: for l in self.fileBody:
if self.isCommentLine(l) and l.strip(self.COMMENT_NOTATION).strip().strip('\n') == "": if self.isCommentLine(l) and l.strip().strip(self.COMMENT_NOTATION).strip().strip('\n') == "":
Logger.debug("Blank Comment: %s" % l)
continue continue
newBody.append(l) newBody.append(l)
self.fileBody = newBody self.fileBody = newBody
@ -161,8 +166,21 @@ class SqlAdder(Adder):
super(SqlAdder, self).__init__() super(SqlAdder, self).__init__()
self.COMMENT_NOTATION = "#" self.COMMENT_NOTATION = "#"
class InterpreterAdder(Adder):
def __init__(self):
super(InterpreterAdder, self).__init__()
self.COMMENT_NOTATION = "#"
copyRightAdderFactory = {".sql":SqlAdder.__name__} def pasteCopyRight(self):
if len(self.fileBody) > 0 and self.isCommentLine(self.fileBody[0]):
# Don't cover the first line of interpreter comment
self.fileBody = [self.fileBody[0]] + self.copyRightTxt + self.fileBody[1:]
else:
self.fileBody = self.copyRightTxt + self.fileBody
file(self.targetFile, 'w').write("".join(self.fileBody))
Logger.info("Added copyright header to %s"%self.targetFile)
copyRightAdderFactory = {".sql":SqlAdder.__name__, ".sh":InterpreterAdder.__name__, ".py":InterpreterAdder.__name__}
class CopyRightAdder(object): class CopyRightAdder(object):
parser = None parser = None
options = None options = None