xref: /btstack/tool/misc/update_copyright.py (revision 98451c7b102094e15e0a72ca2f7098d91aed2017)
1#!/usr/bin/env python3
2import os
3import re
4import sys
5
6copyrightTitle = ".*(Copyright).*(BlueKitchen GmbH)"
7copyrightSubtitle = ".*All rights reserved.*"
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, copyrightSubtitle
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						fout.write(" * All rights reserved\n")
41						state = State.SearchEndCopyright
42						continue
43
44				if state == State.SearchEndCopyright:
45					parts = re.match(copyrightSubtitle, line)
46					if parts:
47						continue
48
49					# search end of Copyright
50					parts = re.match('\s*(\*\/)\s*',line)
51					if parts:
52						state = State.CopyrightEnd
53					else:
54						for key, value in findAndReplace.items():
55							line = line.replace(key, value)
56
57					fout.write(line)
58					continue
59
60				# write rest of the file
61				if state == State.CopyrightEnd:
62					fout.write(line)
63
64	os.rename(outfile, infile)
65
66
67def requiresCopyrightUpdate(file_name):
68	global copyrightTitle, copyrightSubtitle
69
70	state = State.SearchStartCopyright
71	with open(file_name, "rt") as fin:
72		try:
73			for line in fin:
74				if state == State.SearchStartCopyright:
75					parts = re.match(copyrightTitle, line)
76					if parts:
77						state = State.SearchEndCopyright
78						continue
79				if state == State.SearchEndCopyright:
80					parts = re.match(copyrightSubtitle, line)
81					if parts:
82						return False
83					return True
84
85		except UnicodeDecodeError:
86			return False
87
88	return False
89
90
91btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/../..')
92
93# file_name = btstack_root + "/example/panu_demo.c"
94# if requiresCopyrightUpdate(file_name):
95# 	print(file_name, ": update")
96	# updateCopyright(btstack_root + "/example", "panu_demo.c")
97
98
99for root, dirs, files in os.walk(btstack_root, topdown=True):
100	dirs[:] = [d for d in dirs if d not in ignoreFolders]
101	files[:] = [f for f in files if f not in ignoreFiles]
102	for f in files:
103		if f.endswith(".h") or f.endswith(".c"):
104			file_name = root + "/" + f
105			if requiresCopyrightUpdate(file_name):
106				print(file_name)
107    			updateCopyright(root, f)
108