|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import re |
| 6 | +import sys |
| 7 | + |
| 8 | + |
| 9 | +def parse_information_from_header(file_path): |
| 10 | + """ |
| 11 | + Parses the file that contains information about the host system |
| 12 | + and NuttX configuration(sysinfo.h). |
| 13 | +
|
| 14 | + Args: |
| 15 | + file_path (str): Path of the file to parse. |
| 16 | +
|
| 17 | + Returns: |
| 18 | + dict: The contents parsed from file_path (sysinfo.h) header file. |
| 19 | + """ |
| 20 | + |
| 21 | + VARIABLE_NAMES_REGEX = r"static\s+const\s+char\s+\**([A-Za-z0-9_]+)\s*" |
| 22 | + VARIABLE_VALUES_REGEX = r'"([^"]*)"|{([^}]+)};' |
| 23 | + result = {} |
| 24 | + var_name_to_print_dict = { |
| 25 | + "NUTTX_CFLAGS": "NuttX CFLAGS", |
| 26 | + "NUTTX_CXXFLAGS": "NuttX CXXFLAGS", |
| 27 | + "NUTTX_LDFLAGS": "NuttX LDFLAGS", |
| 28 | + "NUTTX_CONFIG": "NuttX configuration options", |
| 29 | + "SYSTEM_PATH": "Host system PATH", |
| 30 | + "OS_VERSION": "Host system OS", |
| 31 | + "INSTALLED_PACKAGES": "Host system installed packages", |
| 32 | + "PYTHON_MODULES": "Host system installed python modules", |
| 33 | + "ESPRESSIF_BOOTLOADER": "Espressif specific information:\n\nToolchain version", |
| 34 | + "ESPRESSIF_TOOLCHAIN": "Toolchain version", |
| 35 | + "ESPRESSIF_ESPTOOL": "Esptool version", |
| 36 | + "ESPRESSIF_HAL": "HAL version", |
| 37 | + "ESPRESSIF_CHIP_ID": "CHIP ID", |
| 38 | + "ESPRESSIF_FLASH_ID": "Flash ID", |
| 39 | + "ESPRESSIF_SECURITY_INFO": "Security information", |
| 40 | + "ESPRESSIF_FLASH_STAT": "Flash status", |
| 41 | + "ESPRESSIF_MAC_ADDR": "MAC address", |
| 42 | + } |
| 43 | + |
| 44 | + # Regular expression pattern to match array definition |
| 45 | + |
| 46 | + keys_pattern = re.compile(VARIABLE_NAMES_REGEX, re.DOTALL) |
| 47 | + values_pattern = re.compile(VARIABLE_VALUES_REGEX, re.DOTALL) |
| 48 | + |
| 49 | + with open(file_path, "r") as file: |
| 50 | + content = file.read() |
| 51 | + |
| 52 | + # Match array definition using the regex |
| 53 | + |
| 54 | + keys_array = keys_pattern.findall(content) |
| 55 | + values_array = values_pattern.findall(content) |
| 56 | + |
| 57 | + # Process values to print it prettier |
| 58 | + |
| 59 | + for i in range(len(values_array)): |
| 60 | + tmp_list = [] |
| 61 | + for y in range(len(values_array[i])): |
| 62 | + tmp_str = values_array[i][y] |
| 63 | + tmp_str = tmp_str.replace('"', "") |
| 64 | + tmp_str = tmp_str.replace("\n ", "", 1) |
| 65 | + tmp_str = tmp_str.replace(",", "") |
| 66 | + |
| 67 | + if tmp_str != "": |
| 68 | + tmp_list.append(tmp_str) |
| 69 | + |
| 70 | + values_array[i] = tuple(tmp_list) |
| 71 | + |
| 72 | + keys_values_to_return = [var_name_to_print_dict[x] for x in keys_array] |
| 73 | + result = dict(zip(keys_values_to_return, values_array)) |
| 74 | + |
| 75 | + return result |
| 76 | + |
| 77 | + |
| 78 | +# Main # |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + """ |
| 83 | + Main function for the script. This function is called when the script is |
| 84 | + executed directly. It prints the output generated to stdout. |
| 85 | +
|
| 86 | + Required arguments: |
| 87 | + nuttx_path: The path to the NuttX source directory. |
| 88 | +
|
| 89 | + Optional arguments: |
| 90 | + The command line arguments. The available arguments are: |
| 91 | + -h, --help: |
| 92 | + Show the help message and exit. |
| 93 | + -f, --file <SYSINFO_FILE>: |
| 94 | + Provide the diagnostic file output(sysinfo.h). |
| 95 | + """ |
| 96 | + |
| 97 | + # Generic arguments |
| 98 | + |
| 99 | + parser = argparse.ArgumentParser(add_help=False) |
| 100 | + parser.add_argument("nuttx_path", help="NuttX source directory path.") |
| 101 | + parser.add_argument( |
| 102 | + "-h", |
| 103 | + "--help", |
| 104 | + action="help", |
| 105 | + default=argparse.SUPPRESS, |
| 106 | + help="Show this help message and exit.", |
| 107 | + ) |
| 108 | + parser.add_argument( |
| 109 | + "-f", |
| 110 | + "--file", |
| 111 | + default="", |
| 112 | + metavar=("SYSINFO_FILE"), |
| 113 | + help="Provide the diagnostic file output(sysinfo.h).", |
| 114 | + ) |
| 115 | + # Parse arguments |
| 116 | + |
| 117 | + if len(sys.argv) == 1: |
| 118 | + parser.print_help() |
| 119 | + sys.exit(1) |
| 120 | + |
| 121 | + args = parser.parse_args() |
| 122 | + os.chdir(args.nuttx_path) |
| 123 | + |
| 124 | + parsed_data = parse_information_from_header(args.file) |
| 125 | + |
| 126 | + # Print the extracted name and values |
| 127 | + |
| 128 | + if parsed_data: |
| 129 | + for each_key in parsed_data.keys(): |
| 130 | + print("{}:".format(each_key)) |
| 131 | + for each_value in parsed_data[each_key]: |
| 132 | + print(" {}".format(each_value)) |
| 133 | + print("") |
| 134 | + else: |
| 135 | + print("No matching array found.") |
0 commit comments