1#!/usr/bin/env python3 2 3import os 4import sys 5import re 6 7config_header_template = """// btstack_config.h for PORT_NAME port 8// 9// Documentation: https://bluekitchen-gmbh.com/btstack/#how_to/ 10""" 11 12btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/../../port/') 13 14def write_configuration(full_path, configuration): 15 if configuration: 16 with open(full_path, "wb") as fout: 17 bytes = configuration.encode('utf-8') 18 fout.write(bytes) 19 20def get_line_ending(full_path): 21 with open(full_path, "r", newline='') as fin: 22 line = fin.readline() 23 if line.endswith('\r\n'): 24 return '\r\n' 25 if line.endswith('\r'): 26 return '\r' 27 return '\n' 28 29def read_and_update_configuration(full_path, line_ending, root): 30 configuration = "" 31 32 port_name = root.split("port/")[1].split("/")[0] 33 if port_name.startswith("archive"): 34 return 35 36 with open(full_path, "rt") as fin: 37 for unstripped_line in fin: 38 line = unstripped_line.strip() 39 40 parts = re.match('(//\s*btstack_config.h\s)(\w*)', line) 41 if parts and len(parts.groups()) == 2: 42 configuration += config_header_template.replace("PORT_NAME", port_name).replace("\r\n", line_ending) 43 else: 44 configuration += unstripped_line 45 return configuration 46 47for root, dirs, files in os.walk(btstack_root, topdown=True): 48 for f in files: 49 if f.endswith("btstack_config.h"): 50 config_file = root + "/" + f 51 line_ending = get_line_ending(config_file) 52 configuration = read_and_update_configuration(config_file, line_ending, root) 53 write_configuration(config_file, configuration) 54