btstack/tools/update_copyright.py

136 lines
4.5 KiB
Python
Raw Normal View History

2015-02-06 10:46:18 +00:00
#!/usr/bin/env python
import os
import re
2015-02-06 14:08:38 +00:00
copyright = """/*
2015-02-06 10:46:18 +00:00
* Copyright (C) 2014 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
"""
2015-02-06 14:08:38 +00:00
onlyDumpDifferentCopyright = False
2015-02-06 14:34:38 +00:00
copyrightString = "Copyright \(C\) 2014 BlueKitchen GmbH"
2015-02-06 10:46:18 +00:00
copyrighters = ["BlueKitchen", "Matthias Ringwald"]
2015-02-06 11:41:11 +00:00
ignoreFolders = ["cpputest", "test", "msp-exp430f5438-cc2564b", "msp430f5229lp-cc2564b", "ez430-rf2560", "ios", "chipset-cc256x", "docs", "mtk"]
ignoreFiles = ["ant_cmds.h", "rijndael.c", "btstack-config.h", "version.h", "profile.h", "bluetoothdrv.h",
"ancs_client.h", "spp_and_le_counter.h", "bluetoothdrv-stub.c", "minimal_peripheral.c", "BTstackDaemonRespawn.c"]
2015-02-06 10:46:18 +00:00
class State:
SearchStartComment = 0
SearchCopyrighter = 1
2015-02-06 14:08:38 +00:00
SearchEndComment = 2
2015-02-06 10:46:18 +00:00
2015-02-06 14:16:26 +00:00
def updateCopyright(dir_name, file_name):
infile = dir_name + "/" + file_name
outfile = dir_name + "/tmp_" + file_name
2015-02-06 14:34:38 +00:00
#print "Update copyright: ", infile
2015-02-06 14:08:38 +00:00
with open(outfile, 'w') as fout:
fout.write(copyright)
bufferComment = ""
state = State.SearchStartComment
2015-02-06 14:16:26 +00:00
with open(infile, 'rb') as fin:
2015-02-06 14:08:38 +00:00
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
2015-02-06 14:16:26 +00:00
os.rename(outfile, infile)
2015-02-06 10:46:18 +00:00
2015-02-06 14:34:38 +00:00
2015-02-06 11:41:11 +00:00
def requiresCopyrightUpdate(file_name):
2015-02-06 10:46:18 +00:00
global copyrightString, onlyDumpDifferentCopyright
2015-02-06 11:41:11 +00:00
2015-02-06 10:46:18 +00:00
with open(file_name, "rb") as fin:
for line in fin:
2015-02-06 14:34:38 +00:00
parts = re.match('.*('+copyrightString+').*',line, re.I)
2015-02-06 14:08:38 +00:00
if parts:
2015-02-06 14:34:38 +00:00
continue
parts = re.match('.*(Copyright).*',line, re.I)
if not parts:
continue
for name in copyrighters:
allowedCopyrighters = re.match('.*('+name+').*',line, re.I)
if allowedCopyrighters:
return True
if onlyDumpDifferentCopyright:
print file_name, ": Copyrighter not allowed > ", parts.group()
return False
2015-02-06 14:08:38 +00:00
2015-02-06 11:41:11 +00:00
print file_name, ": File has no copyright"
return False
2015-02-06 10:46:18 +00:00
2015-02-06 14:08:38 +00:00
2015-02-06 14:16:26 +00:00
for root, dirs, files in os.walk('../', topdown=True):
dirs[:] = [d for d in dirs if d not in ignoreFolders]
files[:] = [f for f in files if f not in ignoreFiles]
for f in files:
if f.endswith(".h") or f.endswith(".c"):
file_name = root + "/" + f
if requiresCopyrightUpdate(file_name):
2015-02-06 14:34:38 +00:00
updateCopyright(root, f)
2015-02-06 10:46:18 +00:00