diff --git a/lib/python/qmk/cli/find.py b/lib/python/qmk/cli/find.py index 2836eb8a542..b11f35fce5a 100644 --- a/lib/python/qmk/cli/find.py +++ b/lib/python/qmk/cli/find.py @@ -15,6 +15,7 @@ from qmk.search import search_keymap_targets ) @cli.argument('-p', '--print', arg_only=True, action='append', default=[], help="For each matched target, print the value of the supplied info.json key. May be passed multiple times.") @cli.argument('-km', '--keymap', type=str, default='default', help="The keymap name to build. Default is 'default'.") +@cli.argument('-x', '--disable-parallel-parsing', arg_only=True, action='store_true', help="Disables parallel parsing of files, useful for debugging stalls.") @cli.subcommand('Find builds which match supplied search criteria.') def find(cli): """Search through all keyboards and keymaps for a given search criteria. @@ -23,7 +24,7 @@ def find(cli): if len(cli.args.filter) == 0 and len(cli.args.print) > 0: cli.log.warning('No filters supplied -- keymaps not parsed, unable to print requested values.') - targets = search_keymap_targets(cli.args.keymap, cli.args.filter, cli.args.print) + targets = search_keymap_targets(cli.args.keymap, cli.args.filter, cli.args.print, parallel=not cli.args.disable_parallel_parsing) for keyboard, keymap, print_vals in targets: print(f'{keyboard}:{keymap}') diff --git a/lib/python/qmk/cli/mass_compile.py b/lib/python/qmk/cli/mass_compile.py index 1227f435e74..16ee64515e4 100755 --- a/lib/python/qmk/cli/mass_compile.py +++ b/lib/python/qmk/cli/mass_compile.py @@ -79,6 +79,7 @@ all: {keyboard_safe}_{keymap_name}_binary @cli.argument('-j', '--parallel', type=int, default=1, help="Set the number of parallel make jobs; 0 means unlimited.") @cli.argument('-c', '--clean', arg_only=True, action='store_true', help="Remove object files before compiling.") @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the commands to be run.") +@cli.argument('-x', '--disable-parallel-parsing', arg_only=True, action='store_true', help="Disables parallel parsing of files, useful for debugging stalls.") @cli.argument( '-f', '--filter', @@ -95,8 +96,8 @@ def mass_compile(cli): """Compile QMK Firmware against all keyboards. """ if len(cli.args.builds) > 0: - targets = search_make_targets(cli.args.builds, cli.args.filter) + targets = search_make_targets(cli.args.builds, cli.args.filter, parallel=not cli.args.disable_parallel_parsing) else: - targets = search_keymap_targets(cli.args.keymap, cli.args.filter) + targets = search_keymap_targets(cli.args.keymap, cli.args.filter, parallel=not cli.args.disable_parallel_parsing) return mass_compile_targets(targets, cli.args.clean, cli.args.dry_run, cli.args.no_temp, cli.args.parallel, cli.args.env) diff --git a/lib/python/qmk/search.py b/lib/python/qmk/search.py index 0b5d4892189..5da083a4556 100644 --- a/lib/python/qmk/search.py +++ b/lib/python/qmk/search.py @@ -54,7 +54,7 @@ def _load_keymap_info(kb_km): return (kb_km[0], kb_km[1], keymap_json(kb_km[0], kb_km[1])) -def expand_make_targets(targets: List[str]) -> List[Tuple[str, str]]: +def expand_make_targets(targets: List[str], parallel=True) -> List[Tuple[str, str]]: """Expand a list of make targets into a list of (keyboard, keymap) tuples. Caters for 'all' in either keyboard or keymap, or both. @@ -66,10 +66,10 @@ def expand_make_targets(targets: List[str]) -> List[Tuple[str, str]]: cli.log.error(f"Invalid build target: {target}") return [] split_targets.append((split_target[0], split_target[1])) - return expand_keymap_targets(split_targets) + return expand_keymap_targets(split_targets, parallel) -def _expand_keymap_target(keyboard: str, keymap: str, all_keyboards: List[str] = None) -> List[Tuple[str, str]]: +def _expand_keymap_target(keyboard: str, keymap: str, all_keyboards: List[str] = None, parallel=True) -> List[Tuple[str, str]]: """Expand a keyboard input and keymap input into a list of (keyboard, keymap) tuples. Caters for 'all' in either keyboard or keymap, or both. @@ -78,17 +78,25 @@ def _expand_keymap_target(keyboard: str, keymap: str, all_keyboards: List[str] = all_keyboards = qmk.keyboard.list_keyboards() if keyboard == 'all': - with multiprocessing.Pool() as pool: + + def _inner_func(pool): + _map_func = pool.imap_unordered if pool is not None else map if keymap == 'all': cli.log.info('Retrieving list of all keyboards and keymaps...') targets = [] - for kb in pool.imap_unordered(_all_keymaps, all_keyboards): + for kb in _map_func(_all_keymaps, all_keyboards): targets.extend(kb) return targets else: cli.log.info(f'Retrieving list of keyboards with keymap "{keymap}"...') keyboard_filter = functools.partial(_keymap_exists, keymap=keymap) - return [(kb, keymap) for kb in filter(lambda e: e is not None, pool.imap_unordered(keyboard_filter, all_keyboards))] + return [(kb, keymap) for kb in filter(lambda e: e is not None, _map_func(keyboard_filter, all_keyboards))] + + if parallel: + with multiprocessing.Pool() as pool: + return _inner_func(pool) + return _inner_func(None) + else: if keymap == 'all': keyboard = qmk.keyboard.resolve_keyboard(keyboard) @@ -98,17 +106,17 @@ def _expand_keymap_target(keyboard: str, keymap: str, all_keyboards: List[str] = return [(qmk.keyboard.resolve_keyboard(keyboard), keymap)] -def expand_keymap_targets(targets: List[Tuple[str, str]]) -> List[Tuple[str, str]]: +def expand_keymap_targets(targets: List[Tuple[str, str]], parallel=True) -> List[Tuple[str, str]]: """Expand a list of (keyboard, keymap) tuples inclusive of 'all', into a list of explicit (keyboard, keymap) tuples. """ overall_targets = [] all_keyboards = qmk.keyboard.list_keyboards() for target in targets: - overall_targets.extend(_expand_keymap_target(target[0], target[1], all_keyboards)) + overall_targets.extend(_expand_keymap_target(target[0], target[1], all_keyboards, parallel)) return list(sorted(set(overall_targets))) -def _filter_keymap_targets(target_list: List[Tuple[str, str]], filters: List[str] = [], print_vals: List[str] = []) -> List[Tuple[str, str, List[Tuple[str, str]]]]: +def _filter_keymap_targets(target_list: List[Tuple[str, str]], filters: List[str] = [], print_vals: List[str] = [], parallel=True) -> List[Tuple[str, str, List[Tuple[str, str]]]]: """Filter a list of (keyboard, keymap) tuples based on the supplied filters. Optionally includes the values of the queried info.json keys. @@ -117,8 +125,16 @@ def _filter_keymap_targets(target_list: List[Tuple[str, str]], filters: List[str targets = [(kb, km, {}) for kb, km in target_list] else: cli.log.info('Parsing data for all matching keyboard/keymap combinations...') - with multiprocessing.Pool() as pool: - valid_keymaps = [(e[0], e[1], dotty(e[2])) for e in pool.imap_unordered(_load_keymap_info, target_list)] + + def _inner_func(pool): + _map_func = pool.imap_unordered if pool is not None else map + return [(e[0], e[1], dotty(e[2])) for e in _map_func(_load_keymap_info, target_list)] + + if parallel: + with multiprocessing.Pool() as pool: + valid_keymaps = _inner_func(pool) + else: + valid_keymaps = _inner_func(None) function_re = re.compile(r'^(?P[a-zA-Z]+)\((?P[a-zA-Z0-9_\.]+)(,\s*(?P[^#]+))?\)$') equals_re = re.compile(r'^(?P[a-zA-Z0-9_\.]+)\s*=\s*(?P[^#]+)$') @@ -179,13 +195,13 @@ def _filter_keymap_targets(target_list: List[Tuple[str, str]], filters: List[str return targets -def search_keymap_targets(keymap='default', filters: List[str] = [], print_vals: List[str] = []) -> List[Tuple[str, str, List[Tuple[str, str]]]]: +def search_keymap_targets(keymap='default', filters: List[str] = [], print_vals: List[str] = [], parallel=True) -> List[Tuple[str, str, List[Tuple[str, str]]]]: """Search for build targets matching the supplied criteria. """ - return list(sorted(_filter_keymap_targets(expand_keymap_targets([('all', keymap)]), filters, print_vals), key=lambda e: (e[0], e[1]))) + return list(sorted(_filter_keymap_targets(expand_keymap_targets([('all', keymap)], parallel), filters, print_vals, parallel), key=lambda e: (e[0], e[1]))) -def search_make_targets(targets: List[str], filters: List[str] = [], print_vals: List[str] = []) -> List[Tuple[str, str, List[Tuple[str, str]]]]: +def search_make_targets(targets: List[str], filters: List[str] = [], print_vals: List[str] = [], parallel=True) -> List[Tuple[str, str, List[Tuple[str, str]]]]: """Search for build targets matching the supplied criteria. """ - return list(sorted(_filter_keymap_targets(expand_make_targets(targets), filters, print_vals), key=lambda e: (e[0], e[1]))) + return list(sorted(_filter_keymap_targets(expand_make_targets(targets, parallel), filters, print_vals, parallel), key=lambda e: (e[0], e[1])))