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