|
| 1 | +#! /usr/bin/env python |
| 2 | +""" |
| 3 | +mbed SDK |
| 4 | +Copyright (c) 2019 ARM Limited |
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +LIBRARIES BUILD |
| 15 | +""" |
| 16 | + |
| 17 | +# Script to automatically update the configuration parameters list in |
| 18 | +# each configuration doc page with the output from `mbed compile --config -v` |
| 19 | +# for the page's appropriate prefix. |
| 20 | +# |
| 21 | +# By default, when run from the check_tools directory, the script runs |
| 22 | +# through each Markdown file in `docs/reference/configuration/`. An |
| 23 | +# optional file or directory path may be passed in a parameter to run the |
| 24 | +# script on a specific file or directroy outside the default path. |
| 25 | +# |
| 26 | +# Note that you need to run this with a local copy of whichever version of |
| 27 | +# Mbed OS you wish to update the configuration parameters with. |
| 28 | +# |
| 29 | +# You can run this script with: |
| 30 | +# python config-update.py <OPTIONAL FILE/DIR PATH> |
| 31 | + |
| 32 | +import sys, os |
| 33 | +import re |
| 34 | +import subprocess |
| 35 | + |
| 36 | +def split_into_pairs(l): |
| 37 | + """ Split the provided list into a, b pairs. |
| 38 | + [1, 2, 3, 4] -> [[1, 2], [3, 4]] |
| 39 | + Args: |
| 40 | + l - list of values to be split |
| 41 | +
|
| 42 | + Returns: |
| 43 | + List of split pairs |
| 44 | + """ |
| 45 | + for i in range(0, len(l), 2): |
| 46 | + yield l[i:i + 2] |
| 47 | + |
| 48 | +def is_string(line): |
| 49 | + """ Determine if the provided string contains |
| 50 | + alphabetical characters (case insensitive) |
| 51 | + Args: |
| 52 | + line - string to scan |
| 53 | +
|
| 54 | + Returns: |
| 55 | + Match object if the string contains [a-z], else None |
| 56 | + """ |
| 57 | + regexp = re.compile(r'[a-z]', re.IGNORECASE) |
| 58 | + return regexp.search(line) |
| 59 | + |
| 60 | +def main(file): |
| 61 | + file_h = open(file, 'r+') |
| 62 | + file = file_h.read() |
| 63 | + |
| 64 | + # Collect indices of markdown code block ticks, split into start,end pairs |
| 65 | + # with `split_into_pairs` below. Collect the config parameter prefix used in |
| 66 | + # the current block if viable and replace the contents with the output of |
| 67 | + # the Mbed CLI config verbose list command. |
| 68 | + snippet_indices = [m.start() for m in re.finditer('```', file)] |
| 69 | + |
| 70 | + blocks = {} |
| 71 | + for i in range(0, int(len(snippet_indices) / 2)): |
| 72 | + # Need to rerun on every loop as the indices change each iteration |
| 73 | + snippet_indices = [m.start() for m in re.finditer('```', file)] |
| 74 | + ranges = list(split_into_pairs(snippet_indices)) |
| 75 | + start = ranges[i][0] |
| 76 | + end = ranges[i][1] |
| 77 | + |
| 78 | + try: |
| 79 | + blocks[i] = file[start : end + 3] |
| 80 | + if ('Name: ' in blocks[i]): |
| 81 | + lib = blocks[i].split('Name: ')[1].split('.')[0] |
| 82 | + print("================= %s =================" % lib) |
| 83 | + out = str(subprocess.check_output(["mbed", "compile", "--config", "-v", "--prefix", lib])) |
| 84 | + |
| 85 | + # Some APIs break config options into logical blocks in their config files. |
| 86 | + # If a tag is applied to a parameter block, only display parameter names that contain that tag |
| 87 | + # For example: |
| 88 | + # ```heap |
| 89 | + # mbed-mesh-api.heap-size |
| 90 | + # ... |
| 91 | + # mbed-mesh-api.heap-stat-info |
| 92 | + # .. |
| 93 | + # ...... |
| 94 | + # ``` |
| 95 | + # |
| 96 | + # On encountering a block with a tag, collect the common parameter token, |
| 97 | + # and split the configuration list output into its components. |
| 98 | + # Collect tag (if present), split <TAG> from ```<TAG> at current index |
| 99 | + # Check with regex for string to cover for potential trailing whitespaces |
| 100 | + tag = file[start : file.find('\n', start)].split('`')[-1] |
| 101 | + if is_string(tag): |
| 102 | + print("\t------- Tag: %s -------" % tag) |
| 103 | + |
| 104 | + start_of_config_block = file.find('Name:', start) |
| 105 | + updated_config = str(file[ : start_of_config_block]) |
| 106 | + for line in out.splitlines(): |
| 107 | + if 'Name' in line and tag in line: |
| 108 | + updated_config += line |
| 109 | + |
| 110 | + # Collect all text until next parameter name. If there's no following 'Name:' token, its the last |
| 111 | + # config option, match to 'Macros' instead to termiante the block. Offset starting index to avoid finding |
| 112 | + # the current line's 'Name:' token. |
| 113 | + eol = out.find('\n', out.find(line)) |
| 114 | + if out.find('Name:', out.find(line) + len('Name:')) > 0: |
| 115 | + updated_config += out[eol : out.find('Name:', out.find(line) + len('Name:'))] |
| 116 | + else: |
| 117 | + updated_config += out[eol : out.find('Macros', out.find(line))] |
| 118 | + |
| 119 | + updated_config += str(file[end:]) |
| 120 | + else: |
| 121 | + updated_config = str(file[:start+4] + out[:out.index("Macros") - 1] + file[end:]) |
| 122 | + |
| 123 | + file = updated_config |
| 124 | + |
| 125 | + # Originally added for debugging purposes, catch and display exceptions before |
| 126 | + # continuing without exiting to provide a complete list of errors found |
| 127 | + except Exception as e: |
| 128 | + print("Error") |
| 129 | + print(e) |
| 130 | + print("____________________") |
| 131 | + exc_type, exc_obj, exc_tb = sys.exc_info() |
| 132 | + fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 133 | + print(exc_type, fname, exc_tb.tb_lineno) |
| 134 | + pass |
| 135 | + |
| 136 | + file_h.truncate(0) |
| 137 | + file_h.seek(0) |
| 138 | + file_h.write(file) |
| 139 | + file_h.close() |
| 140 | + |
| 141 | +if __name__ == '__main__': |
| 142 | + if (len(sys.argv) < 2): |
| 143 | + path = '../docs/reference/configuration' |
| 144 | + else: |
| 145 | + path = sys.argv[1] |
| 146 | + |
| 147 | + if (path == '-h' or path == '--help'): |
| 148 | + print("By default the script runs out of the docs tools directory and iterates through reference/configuration.\n" |
| 149 | + "You may pass in a directory path that will run on all files contained within, or a single file path optionally.") |
| 150 | + exit(0) |
| 151 | + |
| 152 | + if (os.path.isfile(path)): |
| 153 | + main(path) |
| 154 | + elif (os.path.isdir(path)): |
| 155 | + for doc in os.listdir(path): |
| 156 | + if (doc != 'configuration.md'): |
| 157 | + print('_____ %s _____' % os.path.join(path, doc)) |
| 158 | + main(os.path.join(path, doc)) |
| 159 | + else: |
| 160 | + print("Please provide a valid file or directory path") |
0 commit comments