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