|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# This file is part of the MicroPython project, http://micropython.org/ |
| 4 | +# |
| 5 | +# The MIT License (MIT) |
| 6 | +# |
| 7 | +# Copyright (c) 2020 Michael Schroeder |
| 8 | +# |
| 9 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +# of this software and associated documentation files (the "Software"), to deal |
| 11 | +# in the Software without restriction, including without limitation the rights |
| 12 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +# copies of the Software, and to permit persons to whom the Software is |
| 14 | +# furnished to do so, subject to the following conditions: |
| 15 | +# |
| 16 | +# The above copyright notice and this permission notice shall be included in |
| 17 | +# all copies or substantial portions of the Software. |
| 18 | +# |
| 19 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | +# THE SOFTWARE. |
| 26 | + |
| 27 | +import argparse |
| 28 | +import pathlib |
| 29 | +import re |
| 30 | +import sys |
| 31 | + |
| 32 | +DEFAULT_WHITELIST = [ |
| 33 | + "circuitplayground_express", |
| 34 | + "circuitplayground_express_crickit", |
| 35 | + "circuitplayground_express_displayio", |
| 36 | + "pycubed", |
| 37 | + "pycubed_mram", |
| 38 | + "pygamer", |
| 39 | + "pygamer_advance", |
| 40 | + "trinket_m0", |
| 41 | + "trinket_m0_haxpress" |
| 42 | +] |
| 43 | + |
| 44 | +cli_parser = argparse.ArgumentParser(description="USB VID/PID Duplicate Checker") |
| 45 | +cli_parser.add_argument( |
| 46 | + "--whitelist", |
| 47 | + dest="whitelist", |
| 48 | + nargs="?", |
| 49 | + action="store", |
| 50 | + default=DEFAULT_WHITELIST, |
| 51 | + help=( |
| 52 | + "Board names to ignore duplicate VID/PID combinations. Pass an empty " |
| 53 | + "string to disable all duplicate ignoring. Defaults are: " |
| 54 | + f"{', '.join(DEFAULT_WHITELIST)}" |
| 55 | + ) |
| 56 | +) |
| 57 | + |
| 58 | +def configboard_files(): |
| 59 | + """ A pathlib glob search for all ports/*/boards/*/mpconfigboard.mk file |
| 60 | + paths. |
| 61 | +
|
| 62 | + :returns: A ``pathlib.Path.glob()`` genarator object |
| 63 | + """ |
| 64 | + working_dir = pathlib.Path().resolve() |
| 65 | + if not working_dir.name.startswith("circuitpython"): |
| 66 | + raise RuntimeError( |
| 67 | + "Please run USB VID/PID duplicate verification at the " |
| 68 | + "top-level directory." |
| 69 | + ) |
| 70 | + return working_dir.glob("ports/**/boards/**/mpconfigboard.mk") |
| 71 | + |
| 72 | +def check_vid_pid(files, whitelist): |
| 73 | + """ Compiles a list of USB VID & PID values for all boards, and checks |
| 74 | + for duplicates. Raises a `RuntimeError` if duplicates are found, and |
| 75 | + lists the duplicates. |
| 76 | + """ |
| 77 | + |
| 78 | + duplicates_found = False |
| 79 | + |
| 80 | + usb_ids = {} |
| 81 | + |
| 82 | + vid_pattern = re.compile(r"^USB_VID\s*\=\s*(.*)", flags=re.M) |
| 83 | + pid_pattern = re.compile(r"^USB_PID\s*\=\s*(.*)", flags=re.M) |
| 84 | + |
| 85 | + for board_config in files: |
| 86 | + src_text = board_config.read_text() |
| 87 | + |
| 88 | + usb_vid = vid_pattern.search(src_text) |
| 89 | + usb_pid = pid_pattern.search(src_text) |
| 90 | + |
| 91 | + board_name = board_config.parts[-2] |
| 92 | + |
| 93 | + board_whitelisted = False |
| 94 | + if board_name in whitelist: |
| 95 | + board_whitelisted = True |
| 96 | + board_name += " (whitelisted)" |
| 97 | + |
| 98 | + if usb_vid and usb_pid: |
| 99 | + id_group = f"{usb_vid.group(1)}:{usb_pid.group(1)}" |
| 100 | + if id_group not in usb_ids: |
| 101 | + usb_ids[id_group] = { |
| 102 | + "boards": [board_name], |
| 103 | + "duplicate": False |
| 104 | + } |
| 105 | + else: |
| 106 | + usb_ids[id_group]['boards'].append(board_name) |
| 107 | + if not board_whitelisted: |
| 108 | + usb_ids[id_group]['duplicate'] = True |
| 109 | + duplicates_found = True |
| 110 | + |
| 111 | + if duplicates_found: |
| 112 | + duplicates = "" |
| 113 | + for key, value in usb_ids.items(): |
| 114 | + if value["duplicate"]: |
| 115 | + duplicates += ( |
| 116 | + f"- VID/PID: {key}\n" |
| 117 | + f" Boards: {', '.join(value['boards'])}\n" |
| 118 | + ) |
| 119 | + |
| 120 | + duplicate_message = ( |
| 121 | + f"Duplicate VID/PID usage found!\n{duplicates}" |
| 122 | + ) |
| 123 | + sys.exit(duplicate_message) |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + arguments = cli_parser.parse_args() |
| 128 | + |
| 129 | + print("Running USB VID/PID Duplicate Checker...") |
| 130 | + print( |
| 131 | + f"Ignoring the following boards: {', '.join(arguments.whitelist)}", |
| 132 | + end="\n\n" |
| 133 | + ) |
| 134 | + |
| 135 | + board_files = configboard_files() |
| 136 | + check_vid_pid(board_files, arguments.whitelist) |
0 commit comments