Skip to content

Commit 191c92a

Browse files
author
frank
committed
add shell/python support in copyright header tool
1 parent 52610ff commit 191c92a

1 file changed

Lines changed: 29 additions & 11 deletions

File tree

tools/build/addcopyright.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,26 @@ class CopyRightDetecter(object):
2727
def isCopyRightLine(self, txt):
2828
return False
2929

30+
# if more than 1/2 words in one line are keyword, we treat it as copyright line
3031
class KeyWordCopyRightDetecter(CopyRightDetecter):
3132
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/>']
3233

3334
def isCopyRightLine(self, txt):
34-
words = [ c.strip().strip('.').strip('\n').strip(',') for c in txt.split(" ") ]
35+
words = [ c.strip().strip('.').strip('\n').strip(',').strip() for c in txt.split(" ") ]
3536
total = len(words)
3637
if total == 0: return False
3738

3839
numKeyWord = 0
3940
for w in words:
41+
if w == "": continue
4042
if w in self.keywords: numKeyWord+=1
4143

42-
if float(numKeyWord)/float(total) > float(1)/float(3): return True
44+
if float(numKeyWord)/float(total) >= float(1)/float(2): return True
4345
return False
4446

4547
copyRightDetectingFactory = {"KeyWord":KeyWordCopyRightDetecter.__name__}
4648

47-
49+
logfd = open("/tmp/addcopyright.log", 'w')
4850
class Logger(object):
4951
@staticmethod
5052
def info(msg):
@@ -54,9 +56,8 @@ def info(msg):
5456

5557
@staticmethod
5658
def debug(msg):
57-
sys.stdout.write("DEBUG: %s"%msg)
58-
sys.stdout.write("\n")
59-
sys.stdout.flush()
59+
logfd.write("DEBUG: %s"%msg)
60+
logfd.write("\n")
6061

6162
@staticmethod
6263
def warn(msg):
@@ -124,15 +125,18 @@ def getFileBody(self):
124125
self.fileBody = file(self.targetFile).readlines()
125126

126127
def isCommentLine(self, line):
127-
if line.lstrip().startswith(self.COMMENT_NOTATION):
128+
if line.strip().startswith(self.COMMENT_NOTATION):
128129
return True
129130
return False
130131

131132
def removeOldCopyRight(self):
132133
newBody = []
133134
removed = False
134135
for line in self.fileBody:
135-
if self.isCommentLine(line) and self.decter.isCopyRightLine(line): removed = True; continue
136+
if self.isCommentLine(line) and self.decter.isCopyRightLine(line):
137+
removed = True
138+
Logger.debug("remove old copyright: %s" % line)
139+
continue
136140
newBody.append(line)
137141

138142
self.fileBody = newBody
@@ -142,7 +146,8 @@ def removeOldCopyRight(self):
142146
def cleanBlankComment(self):
143147
newBody = []
144148
for l in self.fileBody:
145-
if self.isCommentLine(l) and l.strip(self.COMMENT_NOTATION).strip().strip('\n') == "":
149+
if self.isCommentLine(l) and l.strip().strip(self.COMMENT_NOTATION).strip().strip('\n') == "":
150+
Logger.debug("Blank Comment: %s" % l)
146151
continue
147152
newBody.append(l)
148153
self.fileBody = newBody
@@ -160,9 +165,22 @@ class SqlAdder(Adder):
160165
def __init__(self):
161166
super(SqlAdder, self).__init__()
162167
self.COMMENT_NOTATION = "#"
163-
164168

165-
copyRightAdderFactory = {".sql":SqlAdder.__name__}
169+
class InterpreterAdder(Adder):
170+
def __init__(self):
171+
super(InterpreterAdder, self).__init__()
172+
self.COMMENT_NOTATION = "#"
173+
174+
def pasteCopyRight(self):
175+
if len(self.fileBody) > 0 and self.isCommentLine(self.fileBody[0]):
176+
# Don't cover the first line of interpreter comment
177+
self.fileBody = [self.fileBody[0]] + self.copyRightTxt + self.fileBody[1:]
178+
else:
179+
self.fileBody = self.copyRightTxt + self.fileBody
180+
file(self.targetFile, 'w').write("".join(self.fileBody))
181+
Logger.info("Added copyright header to %s"%self.targetFile)
182+
183+
copyRightAdderFactory = {".sql":SqlAdder.__name__, ".sh":InterpreterAdder.__name__, ".py":InterpreterAdder.__name__}
166184
class CopyRightAdder(object):
167185
parser = None
168186
options = None

0 commit comments

Comments
 (0)