|
6 | 6 |
|
7 | 7 | from importlib import metadata |
8 | 8 | import os |
| 9 | +import shutil |
| 10 | +import subprocess |
9 | 11 | import time |
10 | 12 | from pathlib import Path |
11 | 13 | from typing import List, Tuple |
@@ -126,9 +128,64 @@ def setup_requirements() -> Tuple[List[str], List[str]]: |
126 | 128 | return [remove_dups(reqs) for reqs in [install_reqs, test_reqs]] |
127 | 129 |
|
128 | 130 |
|
| 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 | + |
129 | 184 | if __name__ == "__main__": |
130 | 185 | __version__ = te_version() |
131 | 186 |
|
| 187 | + git_check_submodules() |
| 188 | + |
132 | 189 | with open("README.rst", encoding="utf-8") as f: |
133 | 190 | long_description = f.read() |
134 | 191 |
|
|
0 commit comments