btstack/tool/misc/update_copyright.py

156 lines
4.9 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"]
ignoreFolders = ["cpputest", "test", "msp-exp430f5438-cc2564b", "msp430f5229lp-cc2564b", "ez430-rf2560", "ios", "chipset/cc256x", "docs", "mtk"]
2016-01-21 14:41:16 +00:00
ignoreFiles = ["ant_cmds.h", "rijndael.c", "btstack_config.h", "btstack_version.h", "profile.h", "bluetoothdrv.h",
"ancs_client_demo.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 16:12:25 +00:00
SkipCopyright = 3
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-12-18 20:44:02 +00:00
with open(outfile, 'wt') as fout:
2015-02-06 14:08:38 +00:00
fout.write(copyright)
bufferComment = ""
state = State.SearchStartComment
2015-12-18 20:44:02 +00:00
with open(infile, 'rt') as fin:
2015-02-06 14:08:38 +00:00
for line in fin:
if state == State.SearchStartComment:
2015-02-06 16:12:25 +00:00
parts = re.match('\s*(/\*).*(\*/)',line)
2015-02-06 14:08:38 +00:00
if parts:
2015-02-06 15:48:39 +00:00
if len(parts.groups()) == 2:
# one line comment
fout.write(line)
continue
2015-02-06 16:12:25 +00:00
parts = re.match('\s*(/\*).*',line)
if parts:
# beginning of comment
state = State.SearchCopyrighter
2015-02-06 14:08:38 +00:00
else:
2015-02-06 16:12:25 +00:00
# command line
2015-02-06 14:08:38 +00:00
fout.write(line)
2015-02-06 15:48:39 +00:00
continue
2015-02-06 14:08:38 +00:00
if state == State.SearchCopyrighter:
2015-02-06 16:12:25 +00:00
parts = re.match('.*(Copyright).*',line)
2015-02-06 14:08:38 +00:00
if parts:
2015-02-06 16:12:25 +00:00
# ignore Copyright
2015-02-06 14:08:38 +00:00
# drop buffer
bufferComment = ""
2015-02-06 16:12:25 +00:00
state = State.SkipCopyright
2015-02-06 14:08:38 +00:00
else:
bufferComment = bufferComment + line
2015-02-06 16:12:25 +00:00
parts = re.match('.*(\*/)',line)
2015-02-06 14:08:38 +00:00
if parts:
2015-02-06 16:12:25 +00:00
# end of comment
2015-02-06 14:08:38 +00:00
fout.write(bufferComment)
bufferComment = ""
state = State.SearchStartComment
2015-02-06 16:12:25 +00:00
if state == State.SkipCopyright:
parts = re.match('.*(\*/)',line)
2015-02-06 14:08:38 +00:00
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 16:12:25 +00:00
exactCopyrightFound = False
2015-02-06 10:46:18 +00:00
with open(file_name, "rb") as fin:
for line in fin:
2015-02-06 16:12:25 +00:00
parts = re.match('.*('+copyrightString+').*',line)
2015-02-06 14:08:38 +00:00
if parts:
2015-02-06 16:12:25 +00:00
exactCopyrightFound = True
2015-02-06 14:34:38 +00:00
continue
2015-02-06 16:12:25 +00:00
parts = re.match('.*(Copyright).*',line)
2015-02-06 14:34:38 +00:00
if not parts:
continue
for name in copyrighters:
allowedCopyrighters = re.match('.*('+name+').*',line, re.I)
if allowedCopyrighters:
return True
if onlyDumpDifferentCopyright:
2015-12-18 20:44:02 +00:00
print(file_name, ": Copyrighter not allowed > ", parts.group())
2015-02-06 14:34:38 +00:00
return False
2015-02-06 14:08:38 +00:00
2015-02-06 16:12:25 +00:00
if not exactCopyrightFound:
2015-12-18 20:44:02 +00:00
print(file_name, ": File has no copyright")
2015-02-06 16:12:25 +00:00
2015-02-06 11:41:11 +00:00
return False
2015-02-06 10:46:18 +00:00
2015-02-06 14:08:38 +00:00
# if requiresCopyrightUpdate("../example/panu_demo.c"):
2015-02-06 16:12:25 +00:00
# print "UPdate"
# updateCopyright("../example", "panu_demo.c")
2015-02-06 16:12:25 +00:00
2015-02-06 15:48:39 +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