xref: /btstack/tool/misc/update_copyright.py (revision 077fecbb6ed539507f37505ebd8a5b00e01c55e9)
1#!/usr/bin/env python3
2import os
3import re
4import sys
5
6copyrightTitle = ".*(Copyright).*(BlueKitchen GmbH)"
7copyrightEndString = "Please inquire about"
8
9findAndReplace = {
10	"MATTHIAS" : "BLUEKITCHEN",
11	"RINGWALD" : "GMBH"
12}
13
14ignoreFolders = ["cpputest", "test", "docs", "3rd-party"]
15ignoreFiles = ["ant_cmds.h", "btstack_config.h", "bluetoothdrv.h", "bluetoothdrv-stub.c", "BTstackDaemonRespawn.c"]
16
17
18class State:
19	SearchStartCopyright = 0
20	SearchEndCopyright = 1
21	CopyrightEnd = 2
22
23def updateCopyright(dir_name, file_name):
24	global copyrightTitle
25
26	infile = dir_name + "/" + file_name
27	outfile = dir_name + "/tmp_" + file_name
28
29	with open(outfile, 'wt') as fout:
30		bufferComment = ""
31		state = State.SearchStartCopyright
32
33		with open(infile, 'rt') as fin:
34			for line in fin:
35				# search Copyright start
36				if state == State.SearchStartCopyright:
37					fout.write(line)
38					parts = re.match(copyrightTitle, line)
39					if parts:
40						state = State.SearchEndCopyright
41						continue
42
43				if state == State.SearchEndCopyright:
44					# search end of Copyright
45					parts = re.match('\s*(\*\/)\s*',line)
46					if parts:
47						state = State.CopyrightEnd
48					else:
49						for key, value in findAndReplace.items():
50							line = line.replace(key, value)
51
52					fout.write(line)
53					continue
54
55				# write rest of the file
56				if state == State.CopyrightEnd:
57					fout.write(line)
58
59	os.rename(outfile, infile)
60
61
62def requiresCopyrightUpdate(file_name):
63	global copyrightTitle, copyrightEndString
64
65	state = State.SearchStartCopyright
66	with open(file_name, "rt") as fin:
67		try:
68			for line in fin:
69				if state == State.SearchStartCopyright:
70					parts = re.match(copyrightTitle, line)
71					if parts:
72						state = State.SearchEndCopyright
73						continue
74				if state == State.SearchEndCopyright:
75					parts = re.match(copyrightEndString, line)
76					if parts:
77						return False
78					return True
79
80		except UnicodeDecodeError:
81			return False
82
83	return False
84
85
86btstack_root = os.path.abspath(os.path.dirname(sys.argv[0])) + "/../../"
87
88# file_name = btstack_root + "/panu_demo.c"
89# if requiresCopyrightUpdate(file_name):
90#  	print(file_name, ": update")
91# 	# updateCopyright(btstack_root + "/example", "panu_demo.c")
92
93
94for root, dirs, files in os.walk(btstack_root, topdown=True):
95	dirs[:] = [d for d in dirs if d not in ignoreFolders]
96	files[:] = [f for f in files if f not in ignoreFiles]
97	for f in files:
98		if f.endswith(".h") or f.endswith(".c"):
99			file_name = root + "/" + f
100			if requiresCopyrightUpdate(file_name):
101				print(file_name)
102				updateCopyright(root, f)
103