From 40761d1a9bae658585cce73561bdffdbb92b8fe5 Mon Sep 17 00:00:00 2001 From: Sid Hsu Date: Tue, 23 Apr 2024 05:57:16 +0800 Subject: [PATCH] Update the checkout_submodules.py script to support the recursive configuration in the .gitmodules file. (#33089) --- scripts/checkout_submodules.py | 51 ++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/scripts/checkout_submodules.py b/scripts/checkout_submodules.py index aa220f7ff3b139..81dafa1fb91a29 100755 --- a/scripts/checkout_submodules.py +++ b/scripts/checkout_submodules.py @@ -52,7 +52,7 @@ 'silabs_docker', ]) -Module = namedtuple('Module', 'name path platforms') +Module = namedtuple('Module', 'name path platforms recursive') def load_module_info() -> None: @@ -63,9 +63,11 @@ def load_module_info() -> None: if name != 'DEFAULT': platforms = module.get('platforms', '').split(',') platforms = set(filter(None, platforms)) - assert not (platforms - ALL_PLATFORMS), "Submodule's platform not contained in ALL_PLATFORMS" + assert not ( + platforms - ALL_PLATFORMS), "Submodule's platform not contained in ALL_PLATFORMS" + recursive = module.getboolean('recursive', False) name = name.replace('submodule "', '').replace('"', '') - yield Module(name=name, path=module['path'], platforms=platforms) + yield Module(name=name, path=module['path'], platforms=platforms, recursive=recursive) def module_matches_platforms(module: Module, platforms: set) -> bool: @@ -88,8 +90,10 @@ def make_chip_root_safe_directory() -> None: config.check_returncode() existing = config.stdout.split('\0') if CHIP_ROOT not in existing: - logging.info("Adding CHIP_ROOT to global git safe.directory configuration") - subprocess.check_call(['git', 'config', '--global', '--add', 'safe.directory', CHIP_ROOT]) + logging.info( + "Adding CHIP_ROOT to global git safe.directory configuration") + subprocess.check_call( + ['git', 'config', '--global', '--add', 'safe.directory', CHIP_ROOT]) def checkout_modules(modules: list, shallow: bool, force: bool, recursive: bool, jobs: int) -> None: @@ -101,9 +105,21 @@ def checkout_modules(modules: list, shallow: bool, force: bool, recursive: bool, cmd += ['--force'] if force else [] cmd += ['--recursive'] if recursive else [] cmd += ['--jobs', f'{jobs}'] if jobs else [] - cmd += [module.path for module in modules] + module_paths = [module.path for module in modules] - subprocess.check_call(cmd) + subprocess.check_call(cmd + module_paths) + + if recursive: + # We've recursively checkouted all submodules. + pass + else: + # We've checkouted all top-level submodules. + # We're going to recursively checkout submodules whose recursive configuration is true. + cmd += ['--recursive'] + module_paths = [module.path for module in modules if module.recursive] + + if module_paths: + subprocess.check_call(cmd + module_paths) def deinit_modules(modules: list, force: bool) -> None: @@ -120,28 +136,35 @@ def deinit_modules(modules: list, force: bool) -> None: def main(): logging.basicConfig(format='%(message)s', level=logging.INFO) - parser = argparse.ArgumentParser(description='Checkout or update relevant git submodules') + parser = argparse.ArgumentParser( + description='Checkout or update relevant git submodules') parser.add_argument('--allow-changing-global-git-config', action='store_true', help='Allow global git options to be modified if necessary, e.g. safe.directory') - parser.add_argument('--shallow', action='store_true', help='Fetch submodules without history') + parser.add_argument('--shallow', action='store_true', + help='Fetch submodules without history') parser.add_argument('--platform', nargs='+', choices=ALL_PLATFORMS, default=[], help='Process submodules for specific platforms only') - parser.add_argument('--force', action='store_true', help='Perform action despite of warnings') + parser.add_argument('--force', action='store_true', + help='Perform action despite of warnings') parser.add_argument('--deinit-unmatched', action='store_true', help='Deinitialize submodules for non-matching platforms') - parser.add_argument('--recursive', action='store_true', help='Recursive init of the listed submodules') - parser.add_argument('--jobs', type=int, metavar='N', help='Clone new submodules in parallel with N jobs') + parser.add_argument('--recursive', action='store_true', + help='Recursive init of the listed submodules') + parser.add_argument('--jobs', type=int, metavar='N', + help='Clone new submodules in parallel with N jobs') args = parser.parse_args() modules = list(load_module_info()) selected_platforms = set(args.platform) - selected_modules = [m for m in modules if module_matches_platforms(m, selected_platforms)] + selected_modules = [ + m for m in modules if module_matches_platforms(m, selected_platforms)] unmatched_modules = [m for m in modules if not module_matches_platforms( m, selected_platforms) and module_initialized(m)] if args.allow_changing_global_git_config: make_chip_root_safe_directory() # ignore directory ownership issues for sub-modules - checkout_modules(selected_modules, args.shallow, args.force, args.recursive, args.jobs) + checkout_modules(selected_modules, args.shallow, + args.force, args.recursive, args.jobs) if args.deinit_unmatched and unmatched_modules: deinit_modules(unmatched_modules, args.force)