Skip to content

Commit a019c80

Browse files
authored
Submodule checkout during setup (#2293)
* Add checks to submodule during setup and automatically checkout Signed-off-by: Kirthi Shankar Sivamani <ksivamani@nvidia.com> * fix import and formatting Signed-off-by: Kirthi Shankar Sivamani <ksivamani@nvidia.com> * provide envvar to skip submodule init in setup Signed-off-by: Kirthi Shankar Sivamani <ksivamani@nvidia.com> * Fix formatting Signed-off-by: Kirthi Shankar Sivamani <ksivamani@nvidia.com> --------- Signed-off-by: Kirthi Shankar Sivamani <ksivamani@nvidia.com>
1 parent d7c9777 commit a019c80

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

setup.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from importlib import metadata
88
import os
9+
import shutil
10+
import subprocess
911
import time
1012
from pathlib import Path
1113
from typing import List, Tuple
@@ -126,9 +128,64 @@ def setup_requirements() -> Tuple[List[str], List[str]]:
126128
return [remove_dups(reqs) for reqs in [install_reqs, test_reqs]]
127129

128130

131+
def git_check_submodules() -> None:
132+
"""
133+
Attempt to checkout git submodules automatically during setup.
134+
135+
This runs successfully only if the submodules are
136+
either in the correct or uninitialized state.
137+
138+
Note to devs: With this, any updates to the submodules itself, e.g. moving to a newer
139+
commit, must be commited before build. This also ensures that stale submodules aren't
140+
being silently used by developers.
141+
"""
142+
143+
# Provide an option to skip these checks for development.
144+
if bool(int(os.getenv("NVTE_SKIP_SUBMODULE_CHECKS_DURING_BUILD", "0"))):
145+
return
146+
147+
# Require git executable.
148+
if shutil.which("git") is None:
149+
return
150+
151+
# Require a .gitmodules file.
152+
if not (current_file_path / ".gitmodules").exists():
153+
return
154+
155+
try:
156+
submodules = subprocess.check_output(
157+
["git", "submodule", "status", "--recursive"],
158+
cwd=str(current_file_path),
159+
text=True,
160+
).splitlines()
161+
162+
for submodule in submodules:
163+
# '-' start is for an uninitialized submodule.
164+
# ' ' start is for a submodule on the correct commit.
165+
assert submodule[0] in (
166+
" ",
167+
"-",
168+
), (
169+
"Submodules are initialized incorrectly. If this is intended, set the "
170+
"environment variable `NVTE_SKIP_SUBMODULE_CHECKS_DURING_BUILD` to a "
171+
"non-zero value to skip these checks during development. Otherwise, "
172+
"run `git submodule update --init --recursive` to checkout the correct"
173+
" submodule commits."
174+
)
175+
176+
subprocess.check_call(
177+
["git", "submodule", "update", "--init", "--recursive"],
178+
cwd=str(current_file_path),
179+
)
180+
except subprocess.CalledProcessError:
181+
return
182+
183+
129184
if __name__ == "__main__":
130185
__version__ = te_version()
131186

187+
git_check_submodules()
188+
132189
with open("README.rst", encoding="utf-8") as f:
133190
long_description = f.read()
134191

0 commit comments

Comments
 (0)