Skip to content

Commit 40761d1

Browse files
authored
Update the checkout_submodules.py script to support the recursive configuration in the .gitmodules file. (project-chip#33089)
1 parent 2d97bb7 commit 40761d1

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

scripts/checkout_submodules.py

+37-14
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
'silabs_docker',
5353
])
5454

55-
Module = namedtuple('Module', 'name path platforms')
55+
Module = namedtuple('Module', 'name path platforms recursive')
5656

5757

5858
def load_module_info() -> None:
@@ -63,9 +63,11 @@ def load_module_info() -> None:
6363
if name != 'DEFAULT':
6464
platforms = module.get('platforms', '').split(',')
6565
platforms = set(filter(None, platforms))
66-
assert not (platforms - ALL_PLATFORMS), "Submodule's platform not contained in ALL_PLATFORMS"
66+
assert not (
67+
platforms - ALL_PLATFORMS), "Submodule's platform not contained in ALL_PLATFORMS"
68+
recursive = module.getboolean('recursive', False)
6769
name = name.replace('submodule "', '').replace('"', '')
68-
yield Module(name=name, path=module['path'], platforms=platforms)
70+
yield Module(name=name, path=module['path'], platforms=platforms, recursive=recursive)
6971

7072

7173
def module_matches_platforms(module: Module, platforms: set) -> bool:
@@ -88,8 +90,10 @@ def make_chip_root_safe_directory() -> None:
8890
config.check_returncode()
8991
existing = config.stdout.split('\0')
9092
if CHIP_ROOT not in existing:
91-
logging.info("Adding CHIP_ROOT to global git safe.directory configuration")
92-
subprocess.check_call(['git', 'config', '--global', '--add', 'safe.directory', CHIP_ROOT])
93+
logging.info(
94+
"Adding CHIP_ROOT to global git safe.directory configuration")
95+
subprocess.check_call(
96+
['git', 'config', '--global', '--add', 'safe.directory', CHIP_ROOT])
9397

9498

9599
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,
101105
cmd += ['--force'] if force else []
102106
cmd += ['--recursive'] if recursive else []
103107
cmd += ['--jobs', f'{jobs}'] if jobs else []
104-
cmd += [module.path for module in modules]
108+
module_paths = [module.path for module in modules]
105109

106-
subprocess.check_call(cmd)
110+
subprocess.check_call(cmd + module_paths)
111+
112+
if recursive:
113+
# We've recursively checkouted all submodules.
114+
pass
115+
else:
116+
# We've checkouted all top-level submodules.
117+
# We're going to recursively checkout submodules whose recursive configuration is true.
118+
cmd += ['--recursive']
119+
module_paths = [module.path for module in modules if module.recursive]
120+
121+
if module_paths:
122+
subprocess.check_call(cmd + module_paths)
107123

108124

109125
def deinit_modules(modules: list, force: bool) -> None:
@@ -120,28 +136,35 @@ def deinit_modules(modules: list, force: bool) -> None:
120136
def main():
121137
logging.basicConfig(format='%(message)s', level=logging.INFO)
122138

123-
parser = argparse.ArgumentParser(description='Checkout or update relevant git submodules')
139+
parser = argparse.ArgumentParser(
140+
description='Checkout or update relevant git submodules')
124141
parser.add_argument('--allow-changing-global-git-config', action='store_true',
125142
help='Allow global git options to be modified if necessary, e.g. safe.directory')
126-
parser.add_argument('--shallow', action='store_true', help='Fetch submodules without history')
143+
parser.add_argument('--shallow', action='store_true',
144+
help='Fetch submodules without history')
127145
parser.add_argument('--platform', nargs='+', choices=ALL_PLATFORMS, default=[],
128146
help='Process submodules for specific platforms only')
129-
parser.add_argument('--force', action='store_true', help='Perform action despite of warnings')
147+
parser.add_argument('--force', action='store_true',
148+
help='Perform action despite of warnings')
130149
parser.add_argument('--deinit-unmatched', action='store_true',
131150
help='Deinitialize submodules for non-matching platforms')
132-
parser.add_argument('--recursive', action='store_true', help='Recursive init of the listed submodules')
133-
parser.add_argument('--jobs', type=int, metavar='N', help='Clone new submodules in parallel with N jobs')
151+
parser.add_argument('--recursive', action='store_true',
152+
help='Recursive init of the listed submodules')
153+
parser.add_argument('--jobs', type=int, metavar='N',
154+
help='Clone new submodules in parallel with N jobs')
134155
args = parser.parse_args()
135156

136157
modules = list(load_module_info())
137158
selected_platforms = set(args.platform)
138-
selected_modules = [m for m in modules if module_matches_platforms(m, selected_platforms)]
159+
selected_modules = [
160+
m for m in modules if module_matches_platforms(m, selected_platforms)]
139161
unmatched_modules = [m for m in modules if not module_matches_platforms(
140162
m, selected_platforms) and module_initialized(m)]
141163

142164
if args.allow_changing_global_git_config:
143165
make_chip_root_safe_directory() # ignore directory ownership issues for sub-modules
144-
checkout_modules(selected_modules, args.shallow, args.force, args.recursive, args.jobs)
166+
checkout_modules(selected_modules, args.shallow,
167+
args.force, args.recursive, args.jobs)
145168

146169
if args.deinit_unmatched and unmatched_modules:
147170
deinit_modules(unmatched_modules, args.force)

0 commit comments

Comments
 (0)