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