diff options
Diffstat (limited to 'git_superproject.py')
| -rw-r--r-- | git_superproject.py | 70 |
1 files changed, 53 insertions, 17 deletions
diff --git a/git_superproject.py b/git_superproject.py index 1ada173b7..12ac3dec6 100644 --- a/git_superproject.py +++ b/git_superproject.py | |||
| @@ -23,9 +23,11 @@ Examples: | |||
| 23 | """ | 23 | """ |
| 24 | 24 | ||
| 25 | import functools | 25 | import functools |
| 26 | import glob | ||
| 26 | import hashlib | 27 | import hashlib |
| 27 | import os | 28 | import os |
| 28 | import sys | 29 | import sys |
| 30 | import tempfile | ||
| 29 | import time | 31 | import time |
| 30 | from typing import NamedTuple | 32 | from typing import NamedTuple |
| 31 | import urllib.parse | 33 | import urllib.parse |
| @@ -34,6 +36,7 @@ from git_command import git_require | |||
| 34 | from git_command import GitCommand | 36 | from git_command import GitCommand |
| 35 | from git_config import RepoConfig | 37 | from git_config import RepoConfig |
| 36 | from git_refs import GitRefs | 38 | from git_refs import GitRefs |
| 39 | import platform_utils | ||
| 37 | 40 | ||
| 38 | 41 | ||
| 39 | _SUPERPROJECT_GIT_NAME = "superproject.git" | 42 | _SUPERPROJECT_GIT_NAME = "superproject.git" |
| @@ -215,30 +218,63 @@ class Superproject: | |||
| 215 | """ | 218 | """ |
| 216 | if not os.path.exists(self._superproject_path): | 219 | if not os.path.exists(self._superproject_path): |
| 217 | os.mkdir(self._superproject_path) | 220 | os.mkdir(self._superproject_path) |
| 218 | if not self._quiet and not os.path.exists(self._work_git): | 221 | |
| 222 | if os.path.exists(self._work_git): | ||
| 223 | return True | ||
| 224 | |||
| 225 | if not self._quiet: | ||
| 219 | print( | 226 | print( |
| 220 | "%s: Performing initial setup for superproject; this might " | 227 | "%s: Performing initial setup for superproject; this might " |
| 221 | "take several minutes." % self._work_git | 228 | "take several minutes." % self._work_git |
| 222 | ) | 229 | ) |
| 223 | cmd = ["init", "--bare", self._work_git_name] | 230 | |
| 224 | p = GitCommand( | 231 | tmp_gitdir_prefix = ".tmp-superproject-initgitdir-" |
| 225 | None, | 232 | tmp_gitdir = tempfile.mkdtemp( |
| 226 | cmd, | 233 | prefix=tmp_gitdir_prefix, |
| 227 | cwd=self._superproject_path, | 234 | dir=self._superproject_path, |
| 228 | capture_stdout=True, | ||
| 229 | capture_stderr=True, | ||
| 230 | ) | 235 | ) |
| 231 | retval = p.Wait() | 236 | tmp_git_name = os.path.basename(tmp_gitdir) |
| 232 | if retval: | 237 | |
| 233 | self._LogWarning( | 238 | try: |
| 234 | "git init call failed, command: git {}, " | 239 | cmd = ["init", "--bare", tmp_git_name] |
| 235 | "return code: {}, stderr: {}", | 240 | p = GitCommand( |
| 241 | None, | ||
| 236 | cmd, | 242 | cmd, |
| 237 | retval, | 243 | cwd=self._superproject_path, |
| 238 | p.stderr, | 244 | capture_stdout=True, |
| 245 | capture_stderr=True, | ||
| 239 | ) | 246 | ) |
| 240 | return False | 247 | retval = p.Wait() |
| 241 | return True | 248 | if retval: |
| 249 | self._LogWarning( | ||
| 250 | "git init call failed, command: git {}, " | ||
| 251 | "return code: {}, stderr: {}", | ||
| 252 | cmd, | ||
| 253 | retval, | ||
| 254 | p.stderr, | ||
| 255 | ) | ||
| 256 | return False | ||
| 257 | |||
| 258 | platform_utils.rename(tmp_gitdir, self._work_git) | ||
| 259 | tmp_gitdir = None | ||
| 260 | return True | ||
| 261 | finally: | ||
| 262 | # Clean up the temporary directory created during the process, | ||
| 263 | # as well as any stale ones left over from previous attempts. | ||
| 264 | if tmp_gitdir and os.path.exists(tmp_gitdir): | ||
| 265 | platform_utils.rmtree(tmp_gitdir) | ||
| 266 | |||
| 267 | age_threshold = 60 * 60 * 24 # 1 day in seconds | ||
| 268 | now = time.time() | ||
| 269 | for tmp_dir in glob.glob( | ||
| 270 | os.path.join(self._superproject_path, f"{tmp_gitdir_prefix}*") | ||
| 271 | ): | ||
| 272 | try: | ||
| 273 | mtime = os.path.getmtime(tmp_dir) | ||
| 274 | if now - mtime > age_threshold: | ||
| 275 | platform_utils.rmtree(tmp_dir) | ||
| 276 | except OSError: | ||
| 277 | pass | ||
| 242 | 278 | ||
| 243 | def _Fetch(self): | 279 | def _Fetch(self): |
| 244 | """Fetches a superproject for the manifest based on |_remote_url|. | 280 | """Fetches a superproject for the manifest based on |_remote_url|. |
