fix match copyright

This commit is contained in:
mila@ringwald.ch 2015-02-06 16:12:25 +00:00
parent 9359c00d4b
commit 77cb307928

View File

@ -52,6 +52,7 @@ class State:
SearchStartComment = 0 SearchStartComment = 0
SearchCopyrighter = 1 SearchCopyrighter = 1
SearchEndComment = 2 SearchEndComment = 2
SkipCopyright = 3
def updateCopyright(dir_name, file_name): def updateCopyright(dir_name, file_name):
infile = dir_name + "/" + file_name infile = dir_name + "/" + file_name
@ -68,38 +69,40 @@ def updateCopyright(dir_name, file_name):
with open(infile, 'rb') as fin: with open(infile, 'rb') as fin:
for line in fin: for line in fin:
if state == State.SearchStartComment: if state == State.SearchStartComment:
parts = re.match('\s*(/\*).*(\*/)',line, re.I) parts = re.match('\s*(/\*).*(\*/)',line)
if parts: if parts:
if len(parts.groups()) == 2: if len(parts.groups()) == 2:
# one line comment # one line comment
fout.write(line) fout.write(line)
continue continue
else:
bufferComment = bufferComment + line parts = re.match('\s*(/\*).*',line)
if parts:
# beginning of comment
state = State.SearchCopyrighter state = State.SearchCopyrighter
else: else:
# code line # command line
fout.write(line) fout.write(line)
continue continue
if state == State.SearchCopyrighter: if state == State.SearchCopyrighter:
parts = re.match('.*(Copyright).*',line, re.I) parts = re.match('.*(Copyright).*',line)
if parts: if parts:
# ignore Copyright
# drop buffer # drop buffer
bufferComment = "" bufferComment = ""
state = State.SearchEndComment state = State.SkipCopyright
else: else:
bufferComment = bufferComment + line bufferComment = bufferComment + line
parts = re.match('\s*(\*/).*',line, re.I) parts = re.match('.*(\*/)',line)
if parts: if parts:
# end of comment, no copyright # end of comment
fout.write(bufferComment) fout.write(bufferComment)
bufferComment = "" bufferComment = ""
state = State.SearchStartComment state = State.SearchStartComment
continue
if state == State.SearchEndComment: if state == State.SkipCopyright:
parts = re.match('\s*(\*/).*',line, re.I) parts = re.match('.*(\*/)',line)
if parts: if parts:
state = State.SearchStartComment state = State.SearchStartComment
@ -108,18 +111,18 @@ def updateCopyright(dir_name, file_name):
def requiresCopyrightUpdate(file_name): def requiresCopyrightUpdate(file_name):
global copyrightString, onlyDumpDifferentCopyright global copyrightString, onlyDumpDifferentCopyright
exactCopyrightFound = False
with open(file_name, "rb") as fin: with open(file_name, "rb") as fin:
for line in fin: for line in fin:
parts = re.match('.*('+copyrightString+').*',line, re.I) parts = re.match('.*('+copyrightString+').*',line)
if parts: if parts:
print file_name, "found copyright, skip" exactCopyrightFound = True
continue continue
parts = re.match('.*(Copyright).*',line, re.I) parts = re.match('.*(Copyright).*',line)
if not parts: if not parts:
continue continue
for name in copyrighters: for name in copyrighters:
allowedCopyrighters = re.match('.*('+name+').*',line, re.I) allowedCopyrighters = re.match('.*('+name+').*',line, re.I)
if allowedCopyrighters: if allowedCopyrighters:
@ -129,12 +132,16 @@ def requiresCopyrightUpdate(file_name):
print file_name, ": Copyrighter not allowed > ", parts.group() print file_name, ": Copyrighter not allowed > ", parts.group()
return False return False
if not exactCopyrightFound:
print file_name, ": File has no copyright" print file_name, ": File has no copyright"
return False return False
# if requiresCopyrightUpdate("../example/embedded/panu_demo.c"):
# print "UPdate"
# updateCopyright("../example/embedded", "panu_demo.c") # updateCopyright("../example/embedded", "panu_demo.c")
# requiresCopyrightUpdate("../example/embedded/panu_demo.c")
for root, dirs, files in os.walk('../', topdown=True): for root, dirs, files in os.walk('../', topdown=True):
dirs[:] = [d for d in dirs if d not in ignoreFolders] dirs[:] = [d for d in dirs if d not in ignoreFolders]