From de0afcdf2dbdf1c22cb1f9fb5d4c5c3f8781ca45 Mon Sep 17 00:00:00 2001 From: sommersoft Date: Wed, 27 May 2020 16:51:57 -0500 Subject: [PATCH] add ci_check_duplicate_usb_vid_pid.py; checks that a new board doesn't use duplicate USB VID/PID values unless explicitly whitelisted to do so --- tools/ci_check_duplicate_usb_vid_pid.py | 136 ++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 tools/ci_check_duplicate_usb_vid_pid.py diff --git a/tools/ci_check_duplicate_usb_vid_pid.py b/tools/ci_check_duplicate_usb_vid_pid.py new file mode 100644 index 0000000000..2890070db1 --- /dev/null +++ b/tools/ci_check_duplicate_usb_vid_pid.py @@ -0,0 +1,136 @@ +#!/usr/bin/env python3 +# +# This file is part of the MicroPython project, http://micropython.org/ +# +# The MIT License (MIT) +# +# Copyright (c) 2020 Michael Schroeder +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +import argparse +import pathlib +import re +import sys + +DEFAULT_WHITELIST = [ + "circuitplayground_express", + "circuitplayground_express_crickit", + "circuitplayground_express_displayio", + "pycubed", + "pycubed_mram", + "pygamer", + "pygamer_advance", + "trinket_m0", + "trinket_m0_haxpress" +] + +cli_parser = argparse.ArgumentParser(description="USB VID/PID Duplicate Checker") +cli_parser.add_argument( + "--whitelist", + dest="whitelist", + nargs="?", + action="store", + default=DEFAULT_WHITELIST, + help=( + "Board names to ignore duplicate VID/PID combinations. Pass an empty " + "string to disable all duplicate ignoring. Defaults are: " + f"{', '.join(DEFAULT_WHITELIST)}" + ) +) + +def configboard_files(): + """ A pathlib glob search for all ports/*/boards/*/mpconfigboard.mk file + paths. + + :returns: A ``pathlib.Path.glob()`` genarator object + """ + working_dir = pathlib.Path().resolve() + if not working_dir.name.startswith("circuitpython"): + raise RuntimeError( + "Please run USB VID/PID duplicate verification at the " + "top-level directory." + ) + return working_dir.glob("ports/**/boards/**/mpconfigboard.mk") + +def check_vid_pid(files, whitelist): + """ Compiles a list of USB VID & PID values for all boards, and checks + for duplicates. Raises a `RuntimeError` if duplicates are found, and + lists the duplicates. + """ + + duplicates_found = False + + usb_ids = {} + + vid_pattern = re.compile(r"^USB_VID\s*\=\s*(.*)", flags=re.M) + pid_pattern = re.compile(r"^USB_PID\s*\=\s*(.*)", flags=re.M) + + for board_config in files: + src_text = board_config.read_text() + + usb_vid = vid_pattern.search(src_text) + usb_pid = pid_pattern.search(src_text) + + board_name = board_config.parts[-2] + + board_whitelisted = False + if board_name in whitelist: + board_whitelisted = True + board_name += " (whitelisted)" + + if usb_vid and usb_pid: + id_group = f"{usb_vid.group(1)}:{usb_pid.group(1)}" + if id_group not in usb_ids: + usb_ids[id_group] = { + "boards": [board_name], + "duplicate": False + } + else: + usb_ids[id_group]['boards'].append(board_name) + if not board_whitelisted: + usb_ids[id_group]['duplicate'] = True + duplicates_found = True + + if duplicates_found: + duplicates = "" + for key, value in usb_ids.items(): + if value["duplicate"]: + duplicates += ( + f"- VID/PID: {key}\n" + f" Boards: {', '.join(value['boards'])}\n" + ) + + duplicate_message = ( + f"Duplicate VID/PID usage found!\n{duplicates}" + ) + sys.exit(duplicate_message) + + +if __name__ == "__main__": + arguments = cli_parser.parse_args() + + print("Running USB VID/PID Duplicate Checker...") + print( + f"Ignoring the following boards: {', '.join(arguments.whitelist)}", + end="\n\n" + ) + + board_files = configboard_files() + check_vid_pid(board_files, arguments.whitelist)