1
0
mirror of https://github.com/bluekitchen/btstack.git synced 2025-03-24 04:43:36 +00:00
btstack/tool/update_filename.py
David Lechner 73677349c9 tool: consistently use raw strings for regular expressions
Newer versions of Python raise a SyntaxWarning when a regular expression
contains a backslash that is not part of an escape sequence. To prevent
this warning and future exceptions, use raw strings for all regular
expressions. Even strings without escape sequences are converted for
consistency. Some IDEs will apply special syntax highlighting to raw
strings, which can make it easier to decipher regular expressions.
2025-01-22 08:40:56 +01:00

95 lines
2.6 KiB
Python
Executable File

#!/usr/bin/env python3
import os
import re
btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..')
filetag = '#define BTSTACK_FILE__ "%s"\n'
filetag_re = r'#define BTSTACK_FILE__ "(.*)"'
ignoreFolders = ["3rd-party", "pic32-harmony", "msp430", "cpputest", "test", "msp-exp430f5438-cc2564b", "msp430f5229lp-cc2564b", "ez430-rf2560", "ios", "chipset/cc256x", "docs", "mtk", "port"]
ignoreFiles = ["ant_cmds.h", "rijndael.c", "btstack_config.h", "btstack_version.h", "profile.h", "bluetoothdrv.h",
"ancs_client_demo.h", "spp_and_le_counter.h", "bluetoothdrv-stub.c", "minimal_peripheral.c", "BTstackDaemonRespawn.c"]
class State:
SearchStartComment = 0
SearchCopyrighter = 1
SearchEndComment = 2
ProcessRest = 3
def update_filename_tag(dir_name, file_name, has_tag):
infile = dir_name + "/" + file_name
outfile = dir_name + "/tmp_" + file_name
# print "Update copyright: ", infile
with open(outfile, 'wt') as fout:
bufferComment = ""
state = State.SearchStartComment
with open(infile, 'rt') as fin:
for line in fin:
if state == State.SearchStartComment:
fout.write(line)
parts = re.match(r'\s*(/\*).*(\*/)',line)
if parts:
if len(parts.groups()) == 2:
# one line comment
continue
parts = re.match(r'\s*(/\*).*',line)
if parts:
# beginning of comment
state = State.SearchCopyrighter
continue
if state == State.SearchCopyrighter:
fout.write(line)
parts = re.match(r'.*(\*/)',line)
if parts:
# end of comment
state = State.SearchStartComment
# add filename tag if missing
if not has_tag:
fout.write('\n')
fout.write(filetag % file_name)
state = State.ProcessRest
continue
if state == State.ProcessRest:
if has_tag:
parts = re.match(filetag_re,line)
if parts:
print('have tag, found tag')
fout.write(filetag % file_name)
continue
fout.write(line)
os.rename(outfile, infile)
def get_filename_tag(file_path):
basename = os.path.basename(file_path)
with open(file_path, "rt") as fin:
for line in fin:
parts = re.match(filetag_re,line)
if not parts:
continue
tag = parts.groups()[0]
return tag
return None
for root, dirs, files in os.walk(btstack_root, topdown=True):
dirs[:] = [d for d in dirs if d not in ignoreFolders]
files[:] = [f for f in files if f not in ignoreFiles]
for f in files:
if not f.endswith(".c"):
continue
file_path = root + "/" + f
tag = get_filename_tag(file_path)
if tag != f:
print('%s needs filetag' % file_path)
update_filename_tag(root, f, tag != None)