Skip to content

Commit

Permalink
Update the checkout_submodules.py script to support the recursive con…
Browse files Browse the repository at this point in the history
…figuration in the .gitmodules file. (#33089)
  • Loading branch information
hsusid authored Apr 22, 2024
1 parent 2d97bb7 commit 40761d1
Showing 1 changed file with 37 additions and 14 deletions.
51 changes: 37 additions & 14 deletions scripts/checkout_submodules.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
'silabs_docker',
])

Module = namedtuple('Module', 'name path platforms')
Module = namedtuple('Module', 'name path platforms recursive')


def load_module_info() -> None:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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)
Expand Down

0 comments on commit 40761d1

Please sign in to comment.