From 31bfcd64d5e742e99ba69102c511a1d0693e1f8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20Kr=C3=B3lik?= <66667989+Damian-Nordic@users.noreply.github.com> Date: Tue, 16 Aug 2022 01:15:59 +0200 Subject: [PATCH] [scripts] Enhance checkout_submodules.py script (#21852) Add options to force submodule updates, and deinitialize submodules not matched by the platform filter. Signed-off-by: Damian Krolik Signed-off-by: Damian Krolik --- scripts/checkout_submodules.py | 38 +++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/scripts/checkout_submodules.py b/scripts/checkout_submodules.py index 11ab89fbbb841d..486a3e7b3b355d 100755 --- a/scripts/checkout_submodules.py +++ b/scripts/checkout_submodules.py @@ -49,7 +49,7 @@ Module = namedtuple('Module', 'name path platforms') -def load_module_info() -> list: +def load_module_info() -> None: config = configparser.ConfigParser() config.read(os.path.join(CHIP_ROOT, '.gitmodules')) @@ -69,17 +69,31 @@ def module_matches_platforms(module: Module, platforms: set) -> bool: return bool(platforms & module.platforms) -def checkout_modules(modules: list, shallow: bool) -> None: +def make_chip_root_safe_directory() -> None: + # ensure no errors regarding ownership in the directory. Newer GIT seems to require this: + subprocess.check_call(['git', 'config', '--global', '--add', 'safe.directory', CHIP_ROOT]) + + +def checkout_modules(modules: list, shallow: bool, force: bool) -> None: names = [module.name.replace('submodule "', '').replace('"', '') for module in modules] names = ', '.join(names) logging.info(f'Checking out: {names}') - # ensure no errors regarding ownership in the directory. Newer GIT seems to - # require this: - subprocess.check_call(['git', 'config', '--global', '--add', 'safe.directory', CHIP_ROOT]) - cmd = ['git', '-C', CHIP_ROOT, 'submodule', 'update', '--init'] cmd += ['--depth', '1'] if shallow else [] + cmd += ['--force'] if force else [] + cmd += [module.path for module in modules] + + subprocess.check_call(cmd) + + +def deinit_modules(modules: list, force: bool) -> None: + names = [module.name.replace('submodule "', '').replace('"', '') for module in modules] + names = ', '.join(names) + logging.info(f'Deinitializing: {names}') + + cmd = ['git', '-C', CHIP_ROOT, 'submodule', 'deinit'] + cmd += ['--force'] if force else [] cmd += [module.path for module in modules] subprocess.check_call(cmd) @@ -92,13 +106,21 @@ def main(): 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('--deinit-unmatched', action='store_true', + help='Deinitialize submodules for non-matching platforms') args = parser.parse_args() - modules = load_module_info() + modules = list(load_module_info()) selected_platforms = set(args.platform) 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)] + + make_chip_root_safe_directory() + checkout_modules(selected_modules, args.shallow, args.force) - checkout_modules(selected_modules, args.shallow) + if args.deinit_unmatched: + deinit_modules(unmatched_modules, args.force) if __name__ == '__main__':