BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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