#!/usr/bin/python # usage: python autoGenConf.py interop_database_mtk.h --output=bt_mtk_iot_list.conf import argparse def comment(outputFile): outputFile.write("# This file is automatically generated by the autoGenConf.py script.\n") outputFile.write("# Any changes to this generated file will be lost when the script is re-run.\n") outputFile.write("# This file contains information of customerized IOT list.\n") outputFile.write("## Example:\n\n"); outputFile.write("# Interop Config Section\n"); outputFile.write("# [MtkXXX]\n\n"); outputFile.write("# Blacklisting by vendor prefix address:\n"); outputFile.write("# AddressBlacklist=12:34:56,22:22:22\n\n"); outputFile.write("# Blacklisting by Exact Name:\n"); outputFile.write("# ExactNameBlacklist=This is an example,MT-1234,T1\n\n"); outputFile.write("# Blacklisting by Partial Name (if name starts with)\n") outputFile.write("# PartialNameBlacklist=Test,MT\n\n"); outputFile.write("# Blacklisting by Version:\n"); outputFile.write("# VersionBlacklist=08:0046:1202,08:0046:1203\n\n"); parser = argparse.ArgumentParser(description='automatically generate bt_mtk_iot_list.conf') parser.add_argument("db", help="entries database path") parser.add_argument("header", help="entries header file path") parser.add_argument("out", help="output file") args = parser.parse_args() if not args.out: print("ERROR: Please use db header out.") exit(1) print("db: ", args.db, "header: ", args.header , "out: ", args.out) # Read file fp = open(args.db) inputFile = fp.readlines() fp.close() fp2 = open(args.header) headerFile = fp2.readlines() fp2.close() lines = [] formatString = "INTEROP_MTK" for i in range(len(inputFile)): line = inputFile[i].replace("\n","").lstrip() if formatString in line and line[-1] == "," and line[0] != "{": print("input", args.file, "error format", line) exit(1) if line and line[0] == "{" and line[-1] == ",": lines.append(line[1:-2].replace("{","").replace("}","").split(",")) #sort lines by black list section lines.sort(key = lambda x: x[-1]) print("Parsing input file") # outLines format # [[BlackListSection-A, A-address, A-name, A-partial name, A-version], # [BlackListSection-B, B-address, B-name, B-partial name, B-version], # ...] outLines = [] sec = 0 addr = 1 name = 2 partial = 3 version = 4 sectionCnt = -1 for i in range(len(lines)): if not lines[i][-1].lstrip() in (item for sublist in outLines for item in sublist): newLine = ['', '', '', ''] newLine.insert(0, lines[i][-1].lstrip()) outLines.append(newLine) sectionCnt += 1 size = len(lines[i]) if size == 2: #partial name if not outLines[sectionCnt][partial]: outLines[sectionCnt][partial] = outLines[sectionCnt][partial] + lines[i][0].strip().replace('"','') else: outLines[sectionCnt][partial] = outLines[sectionCnt][partial] + "," + lines[i][0].strip().replace('"','') elif size == 3: #name if not outLines[sectionCnt][name]: outLines[sectionCnt][name] = outLines[sectionCnt][name] + lines[i][0].strip().replace('"','') else: outLines[sectionCnt][name] = outLines[sectionCnt][name] + "," + lines[i][0].strip().replace('"','') elif size == 8: #address addressSize = int(lines[i][6]) if not outLines[sectionCnt][addr]: outLines[sectionCnt][addr] = outLines[sectionCnt][addr] + ":".join(lines[i][0:addressSize]).replace('0x','').replace(' ','') else: outLines[sectionCnt][addr] = outLines[sectionCnt][addr] + "," + ":".join(lines[i][0:addressSize]).replace('0x','').replace(' ','') else: #version if not outLines[sectionCnt][version]: outLines[sectionCnt][version] = outLines[sectionCnt][version] + ":".join(lines[i][0:3]).replace('0x','').replace(' ','') else: outLines[sectionCnt][version] = outLines[sectionCnt][version] + ";" + ":".join(lines[i][0:3]).replace('0x','').replace(' ','') print("Start output bt_mtk_iot_list.conf") outputFile = open(args.out, "w+") comment(outputFile) for i in range(len(outLines)): section = "".join([x[0].upper()+x[1:].lower() for x in outLines[i][sec].replace('INTEROP_','').split('_')]) if not [s for s in headerFile if section in s]: print("error: cannot found ", section, " in ", args.header) exit(1) section = "[" + section + "]" + "\n" outputFile.write(section) if (outLines[i][addr]): AddressBlacklist = "AddressBlacklist=" + outLines[i][addr].lower() + "\n" outputFile.write(AddressBlacklist) if (outLines[i][name]): ExactNameBlacklist = "ExactNameBlacklist=" + outLines[i][name] + "\n" outputFile.write(ExactNameBlacklist) if (outLines[i][partial]): PartialNameBlacklist = "PartialNameBlacklist=" + outLines[i][partial] + "\n" outputFile.write(PartialNameBlacklist) if (outLines[i][version]): VersionBlacklist = "VersionBlacklist=" + outLines[i][version] + "\n" outputFile.write(VersionBlacklist) outputFile.write("\n") outputFile.close() print("autoGenConf.py success")