|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2019 Michael Schroeder |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +# THE SOFTWARE. |
| 22 | +# |
| 23 | + |
| 24 | +import json |
| 25 | +import os |
| 26 | +import re |
| 27 | + |
| 28 | + |
| 29 | +SUPPORTED_PORTS = ["atmel-samd", "nrf"] |
| 30 | + |
| 31 | + |
| 32 | +def get_shared_bindings(): |
| 33 | + """ Get a list of modules in shared-bindings based on folder names |
| 34 | + """ |
| 35 | + return [item for item in os.listdir("./shared-bindings")] |
| 36 | + |
| 37 | + |
| 38 | +def read_mpconfig(): |
| 39 | + """ Open 'circuitpy_mpconfig.mk' and return the contents. |
| 40 | + """ |
| 41 | + configs = [] |
| 42 | + with open("py/circuitpy_mpconfig.mk") as mpconfig: |
| 43 | + configs = mpconfig.read() |
| 44 | + |
| 45 | + return configs |
| 46 | + |
| 47 | + |
| 48 | +def build_json(modules, configs): |
| 49 | + """ Establish the base of the JSON file, based on the contents from |
| 50 | + `configs`. Base will contain module names, if they're part of |
| 51 | + the `FULL_BUILD`, or their default value (0 | 1). |
| 52 | +
|
| 53 | + """ |
| 54 | + base_json = dict() |
| 55 | + full_build = False |
| 56 | + for module in modules: |
| 57 | + full_name = module |
| 58 | + search_name = module.lstrip("_") |
| 59 | + re_pattern = "CIRCUITPY_{}\s=\s(.+)".format(search_name.upper()) |
| 60 | + find_config = re.search(re_pattern, configs) |
| 61 | + #print(module, "|", find_config) |
| 62 | + if not find_config: |
| 63 | + continue |
| 64 | + full_build = int("FULL_BUILD" in find_config[0]) |
| 65 | + #print(find_config[1]) |
| 66 | + if not full_build: |
| 67 | + default_val = find_config[1] |
| 68 | + else: |
| 69 | + default_val = "None" |
| 70 | + base_json[search_name] = { |
| 71 | + "name": full_name, |
| 72 | + "full_build": str(full_build), |
| 73 | + "default_value": default_val, |
| 74 | + "excluded": [] |
| 75 | + } |
| 76 | + |
| 77 | + return get_excluded_boards(base_json) |
| 78 | + |
| 79 | + |
| 80 | +def get_excluded_boards(base_json): |
| 81 | + """ Cycles through each board's `mpconfigboard.mk` file to determine |
| 82 | + if each module is included or not. Boards are selected by existence |
| 83 | + in a port listed in `SUPPORTED_PORTS` (e.g. `/port/nrf/feather_52840`) |
| 84 | + """ |
| 85 | + modules = list(base_json.keys()) |
| 86 | + for port in SUPPORTED_PORTS: |
| 87 | + port_dir = "ports/{}/boards".format(port) |
| 88 | + with os.scandir(port_dir) as boards: |
| 89 | + for entry in boards: |
| 90 | + if not entry.is_dir(): |
| 91 | + continue |
| 92 | + contents = "" |
| 93 | + board_dir = os.path.join(entry.path, "mpconfigboard.mk") |
| 94 | + #print(board_dir) |
| 95 | + with open(board_dir) as board: |
| 96 | + contents = board.read() |
| 97 | + for module in modules: |
| 98 | + # check if board uses `SMALL_BUILD`. if yes, and current |
| 99 | + # module is marked as `FULL_BUILD`, board is excluded |
| 100 | + small_build = re.search("CIRCUITPY_SMALL_BUILD = 1", contents) |
| 101 | + if small_build and base_json[module]["full_build"] == "1": |
| 102 | + base_json[module]["excluded"].append(entry.name) |
| 103 | + continue |
| 104 | + |
| 105 | + # check if module is specifically disabled for this board |
| 106 | + re_pattern = "CIRCUITPY_{}\s=\s(\w)".format(module.upper()) |
| 107 | + find_module = re.search(re_pattern, contents) |
| 108 | + if not find_module: |
| 109 | + continue |
| 110 | + if (find_module[1] == "0" and |
| 111 | + find_module[1] != base_json[module]["default_value"]): |
| 112 | + base_json[module]["excluded"].append(entry.name) |
| 113 | + |
| 114 | + return base_json |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + modules = get_shared_bindings() |
| 118 | + configs = read_mpconfig() |
| 119 | + base = build_json(sorted(modules), configs) |
| 120 | + |
| 121 | + final_json = json.dumps(base, indent=2) |
| 122 | + |
| 123 | + shared_bindings_json = 'support_matrix.json' |
| 124 | + if 'TRAVIS' in os.environ: |
| 125 | + shared_bindings_json = os.path.join('$HOME', shared_bindings_json) |
| 126 | + else: |
| 127 | + print(final_json) |
| 128 | + shared_bindings_json = os.path.join('shared-bindings', shared_bindings_json) |
| 129 | + with open(shared_bindings_json, "w") as matrix: |
| 130 | + json.dump(base, matrix, indent=2) |
0 commit comments