add update copyright function

This commit is contained in:
mila@ringwald.ch 2015-02-06 14:08:38 +00:00
parent 61ac449d15
commit 7b1f13c069
2 changed files with 70 additions and 38 deletions

View File

@ -1,8 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
copyright = """ copyright = """/*
/* * Copyright (C) 2014 BlueKitchen GmbH
* Copyright (C) 2009 by BlueKitchen GmbH
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions

View File

@ -2,8 +2,7 @@
import os import os
import re import re
copyright = """ copyright = """/*
/*
* Copyright (C) 2014 BlueKitchen GmbH * Copyright (C) 2014 BlueKitchen GmbH
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -41,7 +40,7 @@ copyright = """
*/ */
""" """
onlyDumpDifferentCopyright = True onlyDumpDifferentCopyright = False
copyrightString = "Copyright (C) 2014 BlueKitchen GmbH" copyrightString = "Copyright (C) 2014 BlueKitchen GmbH"
copyrighters = ["BlueKitchen", "Matthias Ringwald"] copyrighters = ["BlueKitchen", "Matthias Ringwald"]
@ -52,11 +51,48 @@ ignoreFiles = ["ant_cmds.h", "rijndael.c", "btstack-config.h", "version.h", "pro
class State: class State:
SearchStartComment = 0 SearchStartComment = 0
SearchCopyrighter = 1 SearchCopyrighter = 1
SearchEndComment = 2
def updateCopyright(file_name): def updateCopyright(file_name):
global onlyDumpDifferentCopyright print file_name, ": Update copyright"
if not onlyDumpDifferentCopyright:
print file_name, ": Update copyright" outfile = "tmp_"+file_name
with open(outfile, 'w') as fout:
fout.write(copyright)
bufferComment = ""
state = State.SearchStartComment
with open(file_name, 'rb') as fin:
for line in fin:
if state == State.SearchStartComment:
parts = re.match('\s*(/\*).*',line, re.I)
if parts:
state = State.SearchCopyrighter
else:
fout.write(line)
if state == State.SearchCopyrighter:
parts = re.match('.*(Copyright).*',line, re.I)
if parts:
# drop buffer
bufferComment = ""
state = State.SearchEndComment
else:
bufferComment = bufferComment + line
parts = re.match('\s*(\*/).*',line, re.I)
if parts:
# end of comment, no copyright
fout.write(bufferComment)
bufferComment = ""
state = State.SearchStartComment
if state == State.SearchEndComment:
parts = re.match('\s*(\*/).*',line, re.I)
if parts:
state = State.SearchStartComment
os.rename(outfile, file_name)
def requiresCopyrightUpdate(file_name): def requiresCopyrightUpdate(file_name):
global copyrightString, onlyDumpDifferentCopyright global copyrightString, onlyDumpDifferentCopyright
@ -64,39 +100,36 @@ def requiresCopyrightUpdate(file_name):
with open(file_name, "rb") as fin: with open(file_name, "rb") as fin:
parts = [] parts = []
allowedCopyrighters = [] allowedCopyrighters = []
state = State.SearchStartComment
for line in fin: for line in fin:
if state == State.SearchStartComment: parts = re.match('.*(Copyright).*',line, re.I)
parts = re.match('\s*(/\*).*',line, re.I) if parts:
if parts: allowedCopyrighterFound = False
state = State.SearchCopyrighter for name in copyrighters:
allowedCopyrighters = re.match('.*('+name+').*',line, re.I)
if state == State.SearchCopyrighter: if allowedCopyrighters:
parts = re.match('.*(Copyright).*',line, re.I) allowedCopyrighterFound = True
if parts: return not re.match('.*('+copyrightString+').*',line)
allowedCopyrighterFound = False
for name in copyrighters: if not allowedCopyrighterFound and onlyDumpDifferentCopyright:
allowedCopyrighters = re.match('.*('+name+').*',line, re.I) print file_name, ": Copyrighter not allowed > ", parts.group()
if allowedCopyrighters: return False
allowedCopyrighterFound = True
return re.match(copyrightString,line)
if onlyDumpDifferentCopyright:
print file_name, ": Copyrighter not allowed > ", parts.group()
return False
print file_name, ": File has no copyright" print file_name, ": File has no copyright"
return False return False
for root, dirs, files in os.walk('../', topdown=True):
dirs[:] = [d for d in dirs if d not in ignoreFolders] file_name = "att_server.h"
files[:] = [f for f in files if f not in ignoreFiles] updateCopyright(file_name)
for f in files:
if f.endswith(".h") or f.endswith(".c"): # for root, dirs, files in os.walk('../', topdown=True):
file_name = root + "/" + f # dirs[:] = [d for d in dirs if d not in ignoreFolders]
if requiresCopyrightUpdate(file_name): # files[:] = [f for f in files if f not in ignoreFiles]
updateCopyright(file_name) # for f in files:
# if f.endswith(".h") or f.endswith(".c"):
# file_name = root + "/" + f
# if requiresCopyrightUpdate(file_name):
# updateCopyright(file_name)