Clone of official tools

Committer:
theotherjimmy
Date:
Tue Sep 25 13:43:09 2018 -0500
Revision:
43:2a7da56ebd24
Release 5.10.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
theotherjimmy 43:2a7da56ebd24 1 """A linting utility for targets.json
theotherjimmy 43:2a7da56ebd24 2
theotherjimmy 43:2a7da56ebd24 3 This linting utility may be called as follows:
theotherjimmy 43:2a7da56ebd24 4 python <path-to>/lint.py targets TARGET [TARGET ...]
theotherjimmy 43:2a7da56ebd24 5
theotherjimmy 43:2a7da56ebd24 6 all targets will be linted
theotherjimmy 43:2a7da56ebd24 7 """
theotherjimmy 43:2a7da56ebd24 8
theotherjimmy 43:2a7da56ebd24 9 # mbed SDK
theotherjimmy 43:2a7da56ebd24 10 # Copyright (c) 2017 ARM Limited
theotherjimmy 43:2a7da56ebd24 11 #
theotherjimmy 43:2a7da56ebd24 12 # Licensed under the Apache License, Version 2.0 (the "License");
theotherjimmy 43:2a7da56ebd24 13 # you may not use this file except in compliance with the License.
theotherjimmy 43:2a7da56ebd24 14 # You may obtain a copy of the License at
theotherjimmy 43:2a7da56ebd24 15 #
theotherjimmy 43:2a7da56ebd24 16 # http://www.apache.org/licenses/LICENSE-2.0
theotherjimmy 43:2a7da56ebd24 17 #
theotherjimmy 43:2a7da56ebd24 18 # Unless required by applicable law or agreed to in writing, software
theotherjimmy 43:2a7da56ebd24 19 # distributed under the License is distributed on an "AS IS" BASIS,
theotherjimmy 43:2a7da56ebd24 20 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
theotherjimmy 43:2a7da56ebd24 21 # See the License for the specific language governing permissions and
theotherjimmy 43:2a7da56ebd24 22 # limitations under the License.
theotherjimmy 43:2a7da56ebd24 23
theotherjimmy 43:2a7da56ebd24 24 from os.path import join, abspath, dirname
theotherjimmy 43:2a7da56ebd24 25 if __name__ == "__main__":
theotherjimmy 43:2a7da56ebd24 26 import sys
theotherjimmy 43:2a7da56ebd24 27 ROOT = abspath(join(dirname(__file__), "..", ".."))
theotherjimmy 43:2a7da56ebd24 28 sys.path.insert(0, ROOT)
theotherjimmy 43:2a7da56ebd24 29 from copy import copy
theotherjimmy 43:2a7da56ebd24 30 from yaml import dump_all
theotherjimmy 43:2a7da56ebd24 31 import argparse
theotherjimmy 43:2a7da56ebd24 32
theotherjimmy 43:2a7da56ebd24 33 from tools.targets import Target, set_targets_json_location, TARGET_MAP
theotherjimmy 43:2a7da56ebd24 34
theotherjimmy 43:2a7da56ebd24 35 def must_have_keys(keys, dict):
theotherjimmy 43:2a7da56ebd24 36 """Require keys in an MCU/Board
theotherjimmy 43:2a7da56ebd24 37
theotherjimmy 43:2a7da56ebd24 38 is a generator for errors
theotherjimmy 43:2a7da56ebd24 39 """
theotherjimmy 43:2a7da56ebd24 40 for key in keys:
theotherjimmy 43:2a7da56ebd24 41 if key not in dict:
theotherjimmy 43:2a7da56ebd24 42 yield "%s not found, and is required" % key
theotherjimmy 43:2a7da56ebd24 43
theotherjimmy 43:2a7da56ebd24 44 def may_have_keys(keys, dict):
theotherjimmy 43:2a7da56ebd24 45 """Disable all other keys in an MCU/Board
theotherjimmy 43:2a7da56ebd24 46
theotherjimmy 43:2a7da56ebd24 47 is a generator for errors
theotherjimmy 43:2a7da56ebd24 48 """
theotherjimmy 43:2a7da56ebd24 49 for key in dict.keys():
theotherjimmy 43:2a7da56ebd24 50 if key not in keys:
theotherjimmy 43:2a7da56ebd24 51 yield "%s found, and is not allowed" % key
theotherjimmy 43:2a7da56ebd24 52
theotherjimmy 43:2a7da56ebd24 53 def check_extra_labels(dict):
theotherjimmy 43:2a7da56ebd24 54 """Check that extra_labels does not contain any Target names
theotherjimmy 43:2a7da56ebd24 55
theotherjimmy 43:2a7da56ebd24 56 is a generator for errors
theotherjimmy 43:2a7da56ebd24 57 """
theotherjimmy 43:2a7da56ebd24 58 for label in (dict.get("extra_labels", []) +
theotherjimmy 43:2a7da56ebd24 59 dict.get("extra_labels_add", [])):
theotherjimmy 43:2a7da56ebd24 60 if label in Target.get_json_target_data():
theotherjimmy 43:2a7da56ebd24 61 yield "%s is not allowed in extra_labels" % label
theotherjimmy 43:2a7da56ebd24 62
theotherjimmy 43:2a7da56ebd24 63 def check_release_version(dict):
theotherjimmy 43:2a7da56ebd24 64 """Verify that release version 5 is combined with support for all toolcahins
theotherjimmy 43:2a7da56ebd24 65
theotherjimmy 43:2a7da56ebd24 66 is a generator for errors
theotherjimmy 43:2a7da56ebd24 67 """
theotherjimmy 43:2a7da56ebd24 68 if ("release_versions" in dict and
theotherjimmy 43:2a7da56ebd24 69 "5" in dict["release_versions"] and
theotherjimmy 43:2a7da56ebd24 70 "supported_toolchains" in dict):
theotherjimmy 43:2a7da56ebd24 71 for toolc in ["GCC_ARM", "ARM", "IAR"]:
theotherjimmy 43:2a7da56ebd24 72 if toolc not in dict["supported_toolchains"]:
theotherjimmy 43:2a7da56ebd24 73 yield ("%s not found in supported_toolchains, and is "
theotherjimmy 43:2a7da56ebd24 74 "required by mbed OS 5" % toolc)
theotherjimmy 43:2a7da56ebd24 75
theotherjimmy 43:2a7da56ebd24 76 def check_inherits(dict):
theotherjimmy 43:2a7da56ebd24 77 if ("inherits" in dict and len(dict["inherits"]) > 1):
theotherjimmy 43:2a7da56ebd24 78 yield "multiple inheritance is forbidden"
theotherjimmy 43:2a7da56ebd24 79
theotherjimmy 43:2a7da56ebd24 80 DEVICE_HAS_ALLOWED = ["ANALOGIN", "ANALOGOUT", "CAN", "ETHERNET", "EMAC",
theotherjimmy 43:2a7da56ebd24 81 "FLASH", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN",
theotherjimmy 43:2a7da56ebd24 82 "LPTICKER", "PORTIN", "PORTINOUT", "PORTOUT",
theotherjimmy 43:2a7da56ebd24 83 "PWMOUT", "RTC", "TRNG","SERIAL", "SERIAL_ASYNCH",
theotherjimmy 43:2a7da56ebd24 84 "SERIAL_FC", "SLEEP", "SPI", "SPI_ASYNCH", "SPISLAVE",
theotherjimmy 43:2a7da56ebd24 85 "STORAGE", "STCLK_OFF_DURING_SLEEP"]
theotherjimmy 43:2a7da56ebd24 86 def check_device_has(dict):
theotherjimmy 43:2a7da56ebd24 87 for name in dict.get("device_has", []):
theotherjimmy 43:2a7da56ebd24 88 if name not in DEVICE_HAS_ALLOWED:
theotherjimmy 43:2a7da56ebd24 89 yield "%s is not allowed in device_has" % name
theotherjimmy 43:2a7da56ebd24 90
theotherjimmy 43:2a7da56ebd24 91 MCU_REQUIRED_KEYS = ["release_versions", "supported_toolchains",
theotherjimmy 43:2a7da56ebd24 92 "default_lib", "public", "inherits", "device_has"]
theotherjimmy 43:2a7da56ebd24 93 MCU_ALLOWED_KEYS = ["device_has_add", "device_has_remove", "core",
theotherjimmy 43:2a7da56ebd24 94 "extra_labels", "features", "features_add",
theotherjimmy 43:2a7da56ebd24 95 "features_remove", "bootloader_supported", "device_name",
theotherjimmy 43:2a7da56ebd24 96 "post_binary_hook", "default_toolchain", "config",
theotherjimmy 43:2a7da56ebd24 97 "extra_labels_add", "extra_labels_remove",
theotherjimmy 43:2a7da56ebd24 98 "target_overrides"] + MCU_REQUIRED_KEYS
theotherjimmy 43:2a7da56ebd24 99 def check_mcu(mcu_json, strict=False):
theotherjimmy 43:2a7da56ebd24 100 """Generate a list of problems with an MCU
theotherjimmy 43:2a7da56ebd24 101
theotherjimmy 43:2a7da56ebd24 102 :param: mcu_json the MCU's dict to check
theotherjimmy 43:2a7da56ebd24 103 :param: strict enforce required keys
theotherjimmy 43:2a7da56ebd24 104 """
theotherjimmy 43:2a7da56ebd24 105 errors = list(may_have_keys(MCU_ALLOWED_KEYS, mcu_json))
theotherjimmy 43:2a7da56ebd24 106 if strict:
theotherjimmy 43:2a7da56ebd24 107 errors.extend(must_have_keys(MCU_REQUIRED_KEYS, mcu_json))
theotherjimmy 43:2a7da56ebd24 108 errors.extend(check_extra_labels(mcu_json))
theotherjimmy 43:2a7da56ebd24 109 errors.extend(check_release_version(mcu_json))
theotherjimmy 43:2a7da56ebd24 110 errors.extend(check_inherits(mcu_json))
theotherjimmy 43:2a7da56ebd24 111 errors.extend(check_device_has(mcu_json))
theotherjimmy 43:2a7da56ebd24 112 if 'public' in mcu_json and mcu_json['public']:
theotherjimmy 43:2a7da56ebd24 113 errors.append("public must be false")
theotherjimmy 43:2a7da56ebd24 114 return errors
theotherjimmy 43:2a7da56ebd24 115
theotherjimmy 43:2a7da56ebd24 116 BOARD_REQUIRED_KEYS = ["inherits"]
theotherjimmy 43:2a7da56ebd24 117 BOARD_ALLOWED_KEYS = ["supported_form_factors", "is_disk_virtual",
theotherjimmy 43:2a7da56ebd24 118 "detect_code", "extra_labels", "extra_labels_add",
theotherjimmy 43:2a7da56ebd24 119 "extra_labels_remove", "public", "config",
theotherjimmy 43:2a7da56ebd24 120 "forced_reset_timeout", "target_overrides"] + BOARD_REQUIRED_KEYS
theotherjimmy 43:2a7da56ebd24 121 def check_board(board_json, strict=False):
theotherjimmy 43:2a7da56ebd24 122 """Generate a list of problems with an board
theotherjimmy 43:2a7da56ebd24 123
theotherjimmy 43:2a7da56ebd24 124 :param: board_json the mcus dict to check
theotherjimmy 43:2a7da56ebd24 125 :param: strict enforce required keys
theotherjimmy 43:2a7da56ebd24 126 """
theotherjimmy 43:2a7da56ebd24 127 errors = list(may_have_keys(BOARD_ALLOWED_KEYS, board_json))
theotherjimmy 43:2a7da56ebd24 128 if strict:
theotherjimmy 43:2a7da56ebd24 129 errors.extend(must_have_keys(BOARD_REQUIRED_KEYS, board_json))
theotherjimmy 43:2a7da56ebd24 130 errors.extend(check_extra_labels(board_json))
theotherjimmy 43:2a7da56ebd24 131 errors.extend(check_inherits(board_json))
theotherjimmy 43:2a7da56ebd24 132 return errors
theotherjimmy 43:2a7da56ebd24 133
theotherjimmy 43:2a7da56ebd24 134 def add_if(dict, key, val):
theotherjimmy 43:2a7da56ebd24 135 """Add a value to a dict if it's non-empty"""
theotherjimmy 43:2a7da56ebd24 136 if val:
theotherjimmy 43:2a7da56ebd24 137 dict[key] = val
theotherjimmy 43:2a7da56ebd24 138
theotherjimmy 43:2a7da56ebd24 139 def _split_boards(resolution_order, tgt):
theotherjimmy 43:2a7da56ebd24 140 """Split the resolution order between boards and mcus"""
theotherjimmy 43:2a7da56ebd24 141 mcus = []
theotherjimmy 43:2a7da56ebd24 142 boards = []
theotherjimmy 43:2a7da56ebd24 143 iterable = iter(resolution_order)
theotherjimmy 43:2a7da56ebd24 144 for name in iterable:
theotherjimmy 43:2a7da56ebd24 145 mcu_json = tgt.json_data[name]
theotherjimmy 43:2a7da56ebd24 146 if (len(list(check_mcu(mcu_json, True))) >
theotherjimmy 43:2a7da56ebd24 147 len(list(check_board(mcu_json, True)))):
theotherjimmy 43:2a7da56ebd24 148 boards.append(name)
theotherjimmy 43:2a7da56ebd24 149 else:
theotherjimmy 43:2a7da56ebd24 150 mcus.append(name)
theotherjimmy 43:2a7da56ebd24 151 break
theotherjimmy 43:2a7da56ebd24 152 mcus.extend(iterable)
theotherjimmy 43:2a7da56ebd24 153 mcus.reverse()
theotherjimmy 43:2a7da56ebd24 154 boards.reverse()
theotherjimmy 43:2a7da56ebd24 155 return mcus, boards
theotherjimmy 43:2a7da56ebd24 156
theotherjimmy 43:2a7da56ebd24 157
theotherjimmy 43:2a7da56ebd24 158 MCU_FORMAT_STRING = {1: "MCU (%s) ->",
theotherjimmy 43:2a7da56ebd24 159 2: "Family (%s) -> MCU (%s) ->",
theotherjimmy 43:2a7da56ebd24 160 3: "Family (%s) -> SubFamily (%s) -> MCU (%s) ->"}
theotherjimmy 43:2a7da56ebd24 161 BOARD_FORMAT_STRING = {1: "Board (%s)",
theotherjimmy 43:2a7da56ebd24 162 2: "Module (%s) -> Board (%s)"}
theotherjimmy 43:2a7da56ebd24 163 def _generate_hierarchy_string(mcus, boards):
theotherjimmy 43:2a7da56ebd24 164 global_errors = []
theotherjimmy 43:2a7da56ebd24 165 if len(mcus) < 1:
theotherjimmy 43:2a7da56ebd24 166 global_errors.append("No MCUS found in hierarchy")
theotherjimmy 43:2a7da56ebd24 167 mcus_string = "??? ->"
theotherjimmy 43:2a7da56ebd24 168 elif len(mcus) > 3:
theotherjimmy 43:2a7da56ebd24 169 global_errors.append("No name for targets %s" % ", ".join(mcus[3:]))
theotherjimmy 43:2a7da56ebd24 170 mcus_string = MCU_FORMAT_STRING[3] % tuple(mcus[:3])
theotherjimmy 43:2a7da56ebd24 171 for name in mcus[3:]:
theotherjimmy 43:2a7da56ebd24 172 mcus_string += " ??? (%s) ->" % name
theotherjimmy 43:2a7da56ebd24 173 else:
theotherjimmy 43:2a7da56ebd24 174 mcus_string = MCU_FORMAT_STRING[len(mcus)] % tuple(mcus)
theotherjimmy 43:2a7da56ebd24 175
theotherjimmy 43:2a7da56ebd24 176 if len(boards) < 1:
theotherjimmy 43:2a7da56ebd24 177 global_errors.append("no boards found in hierarchy")
theotherjimmy 43:2a7da56ebd24 178 boards_string = "???"
theotherjimmy 43:2a7da56ebd24 179 elif len(boards) > 2:
theotherjimmy 43:2a7da56ebd24 180 global_errors.append("no name for targets %s" % ", ".join(boards[2:]))
theotherjimmy 43:2a7da56ebd24 181 boards_string = BOARD_FORMAT_STRING[2] % tuple(boards[:2])
theotherjimmy 43:2a7da56ebd24 182 for name in boards[2:]:
theotherjimmy 43:2a7da56ebd24 183 boards_string += " -> ??? (%s)" % name
theotherjimmy 43:2a7da56ebd24 184 else:
theotherjimmy 43:2a7da56ebd24 185 boards_string = BOARD_FORMAT_STRING[len(boards)] % tuple(boards)
theotherjimmy 43:2a7da56ebd24 186 return mcus_string + " " + boards_string, global_errors
theotherjimmy 43:2a7da56ebd24 187
theotherjimmy 43:2a7da56ebd24 188
theotherjimmy 43:2a7da56ebd24 189 def check_hierarchy(tgt):
theotherjimmy 43:2a7da56ebd24 190 """Atempts to assign labels to the hierarchy"""
theotherjimmy 43:2a7da56ebd24 191 resolution_order = copy(tgt.resolution_order_names[:-1])
theotherjimmy 43:2a7da56ebd24 192 mcus, boards = _split_boards(resolution_order, tgt)
theotherjimmy 43:2a7da56ebd24 193
theotherjimmy 43:2a7da56ebd24 194 target_errors = {}
theotherjimmy 43:2a7da56ebd24 195 hierachy_string, hierachy_errors = _generate_hierarchy_string(mcus, boards)
theotherjimmy 43:2a7da56ebd24 196 to_ret = {"hierarchy": hierachy_string}
theotherjimmy 43:2a7da56ebd24 197 add_if(to_ret, "hierarchy errors", hierachy_errors)
theotherjimmy 43:2a7da56ebd24 198
theotherjimmy 43:2a7da56ebd24 199 for name in mcus[:-1]:
theotherjimmy 43:2a7da56ebd24 200 add_if(target_errors, name, list(check_mcu(tgt.json_data[name])))
theotherjimmy 43:2a7da56ebd24 201 if len(mcus) >= 1:
theotherjimmy 43:2a7da56ebd24 202 add_if(target_errors, mcus[-1],
theotherjimmy 43:2a7da56ebd24 203 list(check_mcu(tgt.json_data[mcus[-1]], True)))
theotherjimmy 43:2a7da56ebd24 204 for name in boards:
theotherjimmy 43:2a7da56ebd24 205 add_if(target_errors, name, list(check_board(tgt.json_data[name])))
theotherjimmy 43:2a7da56ebd24 206 if len(boards) >= 1:
theotherjimmy 43:2a7da56ebd24 207 add_if(target_errors, boards[-1],
theotherjimmy 43:2a7da56ebd24 208 list(check_board(tgt.json_data[boards[-1]], True)))
theotherjimmy 43:2a7da56ebd24 209 add_if(to_ret, "target errors", target_errors)
theotherjimmy 43:2a7da56ebd24 210 return to_ret
theotherjimmy 43:2a7da56ebd24 211
theotherjimmy 43:2a7da56ebd24 212 PARSER = argparse.ArgumentParser(prog="targets/lint.py")
theotherjimmy 43:2a7da56ebd24 213 SUBPARSERS = PARSER.add_subparsers(title="Commands")
theotherjimmy 43:2a7da56ebd24 214
theotherjimmy 43:2a7da56ebd24 215 def subcommand(name, *args, **kwargs):
theotherjimmy 43:2a7da56ebd24 216 def __subcommand(command):
theotherjimmy 43:2a7da56ebd24 217 kwargs['description'] = command.__doc__
theotherjimmy 43:2a7da56ebd24 218 subparser = SUBPARSERS.add_parser(name, **kwargs)
theotherjimmy 43:2a7da56ebd24 219 for arg in args:
theotherjimmy 43:2a7da56ebd24 220 arg = dict(arg)
theotherjimmy 43:2a7da56ebd24 221 opt = arg['name']
theotherjimmy 43:2a7da56ebd24 222 del arg['name']
theotherjimmy 43:2a7da56ebd24 223
theotherjimmy 43:2a7da56ebd24 224 if isinstance(opt, basestring):
theotherjimmy 43:2a7da56ebd24 225 subparser.add_argument(opt, **arg)
theotherjimmy 43:2a7da56ebd24 226 else:
theotherjimmy 43:2a7da56ebd24 227 subparser.add_argument(*opt, **arg)
theotherjimmy 43:2a7da56ebd24 228
theotherjimmy 43:2a7da56ebd24 229 def _thunk(parsed_args):
theotherjimmy 43:2a7da56ebd24 230 argv = [arg['dest'] if 'dest' in arg else arg['name']
theotherjimmy 43:2a7da56ebd24 231 for arg in args]
theotherjimmy 43:2a7da56ebd24 232 argv = [(arg if isinstance(arg, basestring)
theotherjimmy 43:2a7da56ebd24 233 else arg[-1]).strip('-').replace('-', '_')
theotherjimmy 43:2a7da56ebd24 234 for arg in argv]
theotherjimmy 43:2a7da56ebd24 235 argv = {arg: vars(parsed_args)[arg] for arg in argv
theotherjimmy 43:2a7da56ebd24 236 if vars(parsed_args)[arg] is not None}
theotherjimmy 43:2a7da56ebd24 237
theotherjimmy 43:2a7da56ebd24 238 return command(**argv)
theotherjimmy 43:2a7da56ebd24 239
theotherjimmy 43:2a7da56ebd24 240 subparser.set_defaults(command=_thunk)
theotherjimmy 43:2a7da56ebd24 241 return command
theotherjimmy 43:2a7da56ebd24 242 return __subcommand
theotherjimmy 43:2a7da56ebd24 243
theotherjimmy 43:2a7da56ebd24 244 @subcommand("targets",
theotherjimmy 43:2a7da56ebd24 245 dict(name="mcus", nargs="+", metavar="MCU",
theotherjimmy 43:2a7da56ebd24 246 choices=TARGET_MAP.keys(), type=str.upper))
theotherjimmy 43:2a7da56ebd24 247 def targets_cmd(mcus=[]):
theotherjimmy 43:2a7da56ebd24 248 """Find and print errors about specific targets"""
theotherjimmy 43:2a7da56ebd24 249 print dump_all([check_hierarchy(TARGET_MAP[m]) for m in mcus],
theotherjimmy 43:2a7da56ebd24 250 default_flow_style=False)
theotherjimmy 43:2a7da56ebd24 251
theotherjimmy 43:2a7da56ebd24 252 @subcommand("all-targets")
theotherjimmy 43:2a7da56ebd24 253 def all_targets_cmd():
theotherjimmy 43:2a7da56ebd24 254 """Print all errors about all parts"""
theotherjimmy 43:2a7da56ebd24 255 print dump_all([check_hierarchy(m) for m in TARGET_MAP.values()],
theotherjimmy 43:2a7da56ebd24 256 default_flow_style=False)
theotherjimmy 43:2a7da56ebd24 257
theotherjimmy 43:2a7da56ebd24 258 @subcommand("orphans")
theotherjimmy 43:2a7da56ebd24 259 def orphans_cmd():
theotherjimmy 43:2a7da56ebd24 260 """Find and print all orphan targets"""
theotherjimmy 43:2a7da56ebd24 261 orphans = Target.get_json_target_data().keys()
theotherjimmy 43:2a7da56ebd24 262 for tgt in TARGET_MAP.values():
theotherjimmy 43:2a7da56ebd24 263 for name in tgt.resolution_order_names:
theotherjimmy 43:2a7da56ebd24 264 if name in orphans:
theotherjimmy 43:2a7da56ebd24 265 orphans.remove(name)
theotherjimmy 43:2a7da56ebd24 266 if orphans:
theotherjimmy 43:2a7da56ebd24 267 print dump_all([orphans], default_flow_style=False)
theotherjimmy 43:2a7da56ebd24 268 return len(orphans)
theotherjimmy 43:2a7da56ebd24 269
theotherjimmy 43:2a7da56ebd24 270 def main():
theotherjimmy 43:2a7da56ebd24 271 """entry point"""
theotherjimmy 43:2a7da56ebd24 272 options = PARSER.parse_args()
theotherjimmy 43:2a7da56ebd24 273 return options.command(options)
theotherjimmy 43:2a7da56ebd24 274
theotherjimmy 43:2a7da56ebd24 275 if __name__ == "__main__":
theotherjimmy 43:2a7da56ebd24 276 sys.exit(main())
theotherjimmy 43:2a7da56ebd24 277