Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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