mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-03-24 22:43:35 +00:00
config: tool for updating port configurations
This commit is contained in:
parent
9eb40423ac
commit
da3afb49ed
111
tool/misc/update_btstack_config.py
Executable file
111
tool/misc/update_btstack_config.py
Executable file
@ -0,0 +1,111 @@
|
||||
#!/usr/bin/env python3
|
||||
import fileinput
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
|
||||
defines_to_add = []
|
||||
defines_to_remove = []
|
||||
|
||||
# dictionary with existing lines as keys, and new text as value
|
||||
lines_to_replace = {}
|
||||
|
||||
def get_line_ending(full_path):
|
||||
with open(full_path, "r", newline='') as fin:
|
||||
line = fin.readline()
|
||||
if line.endswith('\r\n'):
|
||||
return '\r\n'
|
||||
if line.endswith('\r'):
|
||||
return '\r'
|
||||
return '\n'
|
||||
|
||||
|
||||
def configuration_from_block(block, line_ending):
|
||||
global defines_to_add, defines_to_remove
|
||||
configuration = ""
|
||||
|
||||
if len(block) == 0:
|
||||
return ""
|
||||
|
||||
for item in defines_to_remove:
|
||||
if item in block:
|
||||
block.remove(item)
|
||||
|
||||
for item in temp_defines_to_add:
|
||||
if item in block:
|
||||
temp_defines_to_add.remove(item)
|
||||
continue
|
||||
|
||||
if len(block) > 0:
|
||||
prefix = block[0].split("_")[0]
|
||||
if item.startswith(prefix):
|
||||
block.append(item)
|
||||
temp_defines_to_add.remove(item)
|
||||
|
||||
block.sort()
|
||||
for item in block:
|
||||
configuration += ("#define %s%s" % (item, line_ending))
|
||||
|
||||
configuration += ("%s" % (line_ending))
|
||||
return configuration
|
||||
|
||||
def read_and_update_configuration(full_path, line_ending):
|
||||
global lines_to_replace, temp_defines_to_add
|
||||
|
||||
configuration = ""
|
||||
block = []
|
||||
temp_defines_to_add = defines_to_add.copy()
|
||||
|
||||
with open(full_path, "rt") as fin:
|
||||
for unstripped_line in fin:
|
||||
line = unstripped_line.strip()
|
||||
|
||||
if not line:
|
||||
if len(block) == 0:
|
||||
# we have newline to deal with
|
||||
configuration += line_ending
|
||||
else:
|
||||
configuration += configuration_from_block(block, line_ending)
|
||||
block = []
|
||||
else:
|
||||
if line in lines_to_replace.keys():
|
||||
configuration += ("%s%s" % (lines_to_replace[line], line_ending))
|
||||
continue
|
||||
|
||||
parts = re.match("#define\\s*(.*)", line)
|
||||
if parts:
|
||||
block.append(parts[1])
|
||||
else:
|
||||
# if commented code follows directly block, process block, and start a new one
|
||||
configuration += configuration_from_block(block, line_ending)
|
||||
block = []
|
||||
configuration += ("%s%s" % (line, line_ending))
|
||||
|
||||
if len(temp_defines_to_add) > 0:
|
||||
print("Cannot add defines: " % temp_defines_to_add)
|
||||
sys.exit(10)
|
||||
|
||||
# if end of file could not be detected, process last block
|
||||
configuration += configuration_from_block(block, line_ending)
|
||||
return configuration
|
||||
|
||||
def write_configuration(full_path, config_file):
|
||||
with open(full_path, "wb") as fout:
|
||||
bytes = configuration.encode('utf-8')
|
||||
fout.write(bytes)
|
||||
|
||||
btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/../../port')
|
||||
|
||||
config_file = "/Users/mringwal/Projects/btstack/port/arduino/btstack_config.h"
|
||||
line_ending = get_line_ending(config_file)
|
||||
# configuration = read_and_update_configuration(config_file, line_ending)
|
||||
# write_configuration(config_file, configuration)
|
||||
# sys.exit(0)
|
||||
|
||||
for root, dirs, files in os.walk(btstack_root, topdown=True):
|
||||
for f in files:
|
||||
if f.endswith("btstack_config.h"):
|
||||
config_file = root + "/" + f
|
||||
line_ending = get_line_ending(config_file)
|
||||
configuration = read_and_update_configuration(config_file, line_ending)
|
||||
write_configuration(config_file, configuration)
|
Loading…
x
Reference in New Issue
Block a user