xref: /btstack/tool/update_filename.py (revision 41e404d30d48c72a8bde6c1112532dc3034f209f)
1#!/usr/bin/env python3
2import os
3import re
4
5btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..')
6
7filetag = '#define BTSTACK_FILE__ "%s"\n'
8filetag_re = '#define BTSTACK_FILE__ \"(.*)\"'
9
10ignoreFolders = ["3rd-party", "pic32-harmony", "msp430", "cpputest", "test", "msp-exp430f5438-cc2564b", "msp430f5229lp-cc2564b", "ez430-rf2560", "ios", "chipset/cc256x", "docs", "mtk", "port"]
11ignoreFiles =   ["ant_cmds.h", "rijndael.c", "btstack_config.h", "btstack_version.h", "profile.h", "bluetoothdrv.h",
12	"ancs_client_demo.h", "spp_and_le_counter.h", "bluetoothdrv-stub.c", "minimal_peripheral.c", "BTstackDaemonRespawn.c"]
13
14class State:
15	SearchStartComment = 0
16	SearchCopyrighter = 1
17	SearchEndComment = 2
18	ProcessRest = 3
19
20def update_filename_tag(dir_name, file_name, has_tag):
21	infile = dir_name + "/" + file_name
22	outfile = dir_name + "/tmp_" + file_name
23
24	# print "Update copyright: ", infile
25
26	with open(outfile, 'wt') as fout:
27
28		bufferComment = ""
29		state = State.SearchStartComment
30
31		with open(infile, 'rt') as fin:
32			for line in fin:
33				if state == State.SearchStartComment:
34					fout.write(line)
35					parts = re.match('\s*(/\*).*(\*/)',line)
36					if parts:
37						if len(parts.groups()) == 2:
38							# one line comment
39							continue
40
41					parts = re.match('\s*(/\*).*',line)
42					if parts:
43						# beginning of comment
44						state = State.SearchCopyrighter
45					continue
46
47				if state == State.SearchCopyrighter:
48					fout.write(line)
49					parts = re.match('.*(\*/)',line)
50					if parts:
51						# end of comment
52						state = State.SearchStartComment
53
54						# add filename tag if missing
55						if not has_tag:
56							fout.write('\n')
57							fout.write(filetag % file_name)
58						state = State.ProcessRest
59					continue
60
61				if state == State.ProcessRest:
62					if has_tag:
63						parts = re.match(filetag_re,line)
64						if parts:
65							print('have tag, found tag')
66							fout.write(filetag % file_name)
67							continue
68					fout.write(line)
69
70	os.rename(outfile, infile)
71
72
73def get_filename_tag(file_path):
74	basename = os.path.basename(file_path)
75	with open(file_path, "rt") as fin:
76		for line in fin:
77			parts = re.match(filetag_re,line)
78			if not parts:
79				continue
80			tag = parts.groups()[0]
81			return tag
82	return None
83
84for root, dirs, files in os.walk(btstack_root, topdown=True):
85	dirs[:]  = [d for d in dirs if d not in ignoreFolders]
86	files[:] = [f for f in files if f not in ignoreFiles]
87	for f in files:
88		if not f.endswith(".c"):
89			continue
90		file_path = root + "/" + f
91		tag = get_filename_tag(file_path)
92		if tag != f:
93			print('%s needs filetag' % file_path)
94			update_filename_tag(root, f, tag != None)
95