btstack/tool/misc/update_copyright.py

108 lines
2.7 KiB
Python
Raw Normal View History

2020-07-09 16:24:31 +02:00
#!/usr/bin/env python3
2015-02-06 10:46:18 +00:00
import os
import re
import sys
copyrightTitle = ".*(Copyright).*(BlueKitchen GmbH)"
copyrightSubtitle = ".*All rights reserved.*"
findAndReplace = {
"MATTHIAS" : "BLUEKITCHEN",
"RINGWALD" : "GMBH"
}
ignoreFolders = ["cpputest", "test", "docs", "3rd-party"]
ignoreFiles = ["ant_cmds.h", "btstack_config.h", "bluetoothdrv.h", "bluetoothdrv-stub.c", "BTstackDaemonRespawn.c"]
2015-02-06 10:46:18 +00:00
class State:
SearchStartCopyright = 0
SearchEndCopyright = 1
CopyrightEnd = 2
2015-02-06 10:46:18 +00:00
2015-02-06 14:16:26 +00:00
def updateCopyright(dir_name, file_name):
global copyrightTitle, copyrightSubtitle
2015-02-06 14:16:26 +00:00
infile = dir_name + "/" + file_name
outfile = dir_name + "/tmp_" + file_name
2015-12-18 21:44:02 +01:00
with open(outfile, 'wt') as fout:
2015-02-06 14:08:38 +00:00
bufferComment = ""
state = State.SearchStartCopyright
2015-02-06 14:08:38 +00:00
2015-12-18 21:44:02 +01:00
with open(infile, 'rt') as fin:
2015-02-06 14:08:38 +00:00
for line in fin:
# search Copyright start
if state == State.SearchStartCopyright:
fout.write(line)
parts = re.match(copyrightTitle, line)
2015-02-06 14:08:38 +00:00
if parts:
fout.write(" * All rights reserved\n")
state = State.SearchEndCopyright
2015-02-06 15:48:39 +00:00
continue
if state == State.SearchEndCopyright:
parts = re.match(copyrightSubtitle, line)
2015-02-06 14:08:38 +00:00
if parts:
continue
# search end of Copyright
parts = re.match('\s*(\*\/)\s*',line)
2015-02-06 14:08:38 +00:00
if parts:
state = State.CopyrightEnd
else:
for key, value in findAndReplace.items():
line = line.replace(key, value)
fout.write(line)
continue
2015-02-06 14:08:38 +00:00
# write rest of the file
if state == State.CopyrightEnd:
fout.write(line)
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):
global copyrightTitle, copyrightSubtitle
2015-02-06 16:12:25 +00:00
state = State.SearchStartCopyright
2020-07-09 16:24:31 +02:00
with open(file_name, "rt") as fin:
try:
for line in fin:
if state == State.SearchStartCopyright:
parts = re.match(copyrightTitle, line)
if parts:
state = State.SearchEndCopyright
continue
if state == State.SearchEndCopyright:
parts = re.match(copyrightSubtitle, line)
if parts:
return False
2015-02-06 14:34:38 +00:00
return True
except UnicodeDecodeError:
2015-02-06 14:34:38 +00:00
return False
2015-02-06 10:46:18 +00:00
return False
2015-02-06 14:08:38 +00:00
2015-02-06 16:12:25 +00:00
2020-11-06 14:20:22 +01:00
btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/../..')
2015-02-06 15:48:39 +00:00
# file_name = btstack_root + "/example/panu_demo.c"
# if requiresCopyrightUpdate(file_name):
# print(file_name, ": update")
# updateCopyright(btstack_root + "/example", "panu_demo.c")
2020-11-06 14:20:22 +01:00
for root, dirs, files in os.walk(btstack_root, topdown=True):
2015-02-06 14:16:26 +00:00
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):
print(file_name)
updateCopyright(root, f)