Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 1 | # Copyright (C) 2021 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 15 | """Provide functionality to get all projects and their commit ids from Superproject. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 16 | |
| 17 | For more information on superproject, check out: |
| 18 | https://en.wikibooks.org/wiki/Git/Submodules_and_Superprojects |
| 19 | |
| 20 | Examples: |
| 21 | superproject = Superproject() |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 22 | UpdateProjectsResult = superproject.UpdateProjectsRevisionId(projects) |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 23 | """ |
| 24 | |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 25 | import hashlib |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 26 | import os |
| 27 | import sys |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 28 | from typing import NamedTuple |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 29 | |
Raman Tenneti | e253b43 | 2021-06-02 10:05:54 -0700 | [diff] [blame] | 30 | from git_command import git_require, GitCommand |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 31 | from git_refs import R_HEADS |
Raman Tenneti | 78f4dd3 | 2021-06-07 13:27:37 -0700 | [diff] [blame] | 32 | from manifest_xml import LOCAL_MANIFEST_GROUP_PREFIX |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 33 | |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 34 | _SUPERPROJECT_GIT_NAME = 'superproject.git' |
| 35 | _SUPERPROJECT_MANIFEST_NAME = 'superproject_override.xml' |
| 36 | |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 37 | |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 38 | class SyncResult(NamedTuple): |
| 39 | """Return the status of sync and whether caller should exit.""" |
| 40 | |
| 41 | # Whether the superproject sync was successful. |
| 42 | success: bool |
| 43 | # Whether the caller should exit. |
| 44 | fatal: bool |
| 45 | |
| 46 | |
| 47 | class CommitIdsResult(NamedTuple): |
| 48 | """Return the commit ids and whether caller should exit.""" |
| 49 | |
| 50 | # A dictionary with the projects/commit ids on success, otherwise None. |
| 51 | commit_ids: dict |
| 52 | # Whether the caller should exit. |
| 53 | fatal: bool |
| 54 | |
| 55 | |
| 56 | class UpdateProjectsResult(NamedTuple): |
| 57 | """Return the overriding manifest file and whether caller should exit.""" |
| 58 | |
| 59 | # Path name of the overriding manfiest file if successful, otherwise None. |
| 60 | manifest_path: str |
| 61 | # Whether the caller should exit. |
| 62 | fatal: bool |
| 63 | |
| 64 | |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 65 | class Superproject(object): |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 66 | """Get commit ids from superproject. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 67 | |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 68 | Initializes a local copy of a superproject for the manifest. This allows |
| 69 | lookup of commit ids for all projects. It contains _project_commit_ids which |
| 70 | is a dictionary with project/commit id entries. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 71 | """ |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 72 | def __init__(self, manifest, repodir, git_event_log, |
| 73 | superproject_dir='exp-superproject', quiet=False): |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 74 | """Initializes superproject. |
| 75 | |
| 76 | Args: |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 77 | manifest: A Manifest object that is to be written to a file. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 78 | repodir: Path to the .repo/ dir for holding all internal checkout state. |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 79 | It must be in the top directory of the repo client checkout. |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 80 | git_event_log: A git trace2 event log to log events. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 81 | superproject_dir: Relative path under |repodir| to checkout superproject. |
Raman Tenneti | ef99ec0 | 2021-03-04 10:29:40 -0800 | [diff] [blame] | 82 | quiet: If True then only print the progress messages. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 83 | """ |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 84 | self._project_commit_ids = None |
| 85 | self._manifest = manifest |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 86 | self._git_event_log = git_event_log |
Raman Tenneti | ef99ec0 | 2021-03-04 10:29:40 -0800 | [diff] [blame] | 87 | self._quiet = quiet |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 88 | self._branch = self._GetBranch() |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 89 | self._repodir = os.path.abspath(repodir) |
| 90 | self._superproject_dir = superproject_dir |
| 91 | self._superproject_path = os.path.join(self._repodir, superproject_dir) |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 92 | self._manifest_path = os.path.join(self._superproject_path, |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 93 | _SUPERPROJECT_MANIFEST_NAME) |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 94 | git_name = '' |
| 95 | if self._manifest.superproject: |
| 96 | remote_name = self._manifest.superproject['remote'].name |
| 97 | git_name = hashlib.md5(remote_name.encode('utf8')).hexdigest() + '-' |
| 98 | self._work_git_name = git_name + _SUPERPROJECT_GIT_NAME |
| 99 | self._work_git = os.path.join(self._superproject_path, self._work_git_name) |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 100 | |
| 101 | @property |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 102 | def project_commit_ids(self): |
| 103 | """Returns a dictionary of projects and their commit ids.""" |
| 104 | return self._project_commit_ids |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 105 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 106 | def _GetBranch(self): |
| 107 | """Returns the branch name for getting the approved manifest.""" |
| 108 | p = self._manifest.manifestProject |
| 109 | b = p.GetBranch(p.CurrentBranch) |
| 110 | if not b: |
| 111 | return None |
| 112 | branch = b.merge |
| 113 | if branch and branch.startswith(R_HEADS): |
| 114 | branch = branch[len(R_HEADS):] |
| 115 | return branch |
| 116 | |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 117 | def _Init(self): |
| 118 | """Sets up a local Git repository to get a copy of a superproject. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 119 | |
| 120 | Returns: |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 121 | True if initialization is successful, or False. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 122 | """ |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 123 | if not os.path.exists(self._superproject_path): |
| 124 | os.mkdir(self._superproject_path) |
Raman Tenneti | ef99ec0 | 2021-03-04 10:29:40 -0800 | [diff] [blame] | 125 | if not self._quiet and not os.path.exists(self._work_git): |
| 126 | print('%s: Performing initial setup for superproject; this might take ' |
| 127 | 'several minutes.' % self._work_git) |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 128 | cmd = ['init', '--bare', self._work_git_name] |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 129 | p = GitCommand(None, |
| 130 | cmd, |
| 131 | cwd=self._superproject_path, |
| 132 | capture_stdout=True, |
| 133 | capture_stderr=True) |
| 134 | retval = p.Wait() |
| 135 | if retval: |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 136 | print('repo: error: git init call failed with return code: %r, stderr: %r' % |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 137 | (retval, p.stderr), file=sys.stderr) |
| 138 | return False |
| 139 | return True |
| 140 | |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 141 | def _Fetch(self, url): |
| 142 | """Fetches a local copy of a superproject for the manifest based on url. |
| 143 | |
| 144 | Args: |
| 145 | url: superproject's url. |
Raman Tenneti | 9e78753 | 2021-02-01 11:47:06 -0800 | [diff] [blame] | 146 | |
| 147 | Returns: |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 148 | True if fetch is successful, or False. |
Raman Tenneti | 9e78753 | 2021-02-01 11:47:06 -0800 | [diff] [blame] | 149 | """ |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 150 | if not os.path.exists(self._work_git): |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 151 | print('git fetch missing drectory: %s' % self._work_git, |
| 152 | file=sys.stderr) |
| 153 | return False |
Raman Tenneti | e253b43 | 2021-06-02 10:05:54 -0700 | [diff] [blame] | 154 | if not git_require((2, 28, 0)): |
| 155 | print('superproject requires a git version 2.28 or later', file=sys.stderr) |
| 156 | return False |
Raman Tenneti | 8367096 | 2021-03-19 13:53:43 -0700 | [diff] [blame] | 157 | cmd = ['fetch', url, '--depth', '1', '--force', '--no-tags', '--filter', 'blob:none'] |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 158 | if self._branch: |
| 159 | cmd += [self._branch + ':' + self._branch] |
Raman Tenneti | 9e78753 | 2021-02-01 11:47:06 -0800 | [diff] [blame] | 160 | p = GitCommand(None, |
| 161 | cmd, |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 162 | cwd=self._work_git, |
Raman Tenneti | 9e78753 | 2021-02-01 11:47:06 -0800 | [diff] [blame] | 163 | capture_stdout=True, |
| 164 | capture_stderr=True) |
| 165 | retval = p.Wait() |
| 166 | if retval: |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 167 | print('repo: error: git fetch call failed with return code: %r, stderr: %r' % |
Raman Tenneti | 9e78753 | 2021-02-01 11:47:06 -0800 | [diff] [blame] | 168 | (retval, p.stderr), file=sys.stderr) |
| 169 | return False |
| 170 | return True |
| 171 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 172 | def _LsTree(self): |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 173 | """Gets the commit ids for all projects. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 174 | |
| 175 | Works only in git repositories. |
| 176 | |
| 177 | Returns: |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 178 | data: data returned from 'git ls-tree ...' instead of None. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 179 | """ |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 180 | if not os.path.exists(self._work_git): |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 181 | print('git ls-tree missing drectory: %s' % self._work_git, |
| 182 | file=sys.stderr) |
| 183 | return None |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 184 | data = None |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 185 | branch = 'HEAD' if not self._branch else self._branch |
Raman Tenneti | ce64e3d | 2021-02-08 13:27:41 -0800 | [diff] [blame] | 186 | cmd = ['ls-tree', '-z', '-r', branch] |
| 187 | |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 188 | p = GitCommand(None, |
| 189 | cmd, |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 190 | cwd=self._work_git, |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 191 | capture_stdout=True, |
| 192 | capture_stderr=True) |
| 193 | retval = p.Wait() |
| 194 | if retval == 0: |
| 195 | data = p.stdout |
| 196 | else: |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 197 | print('repo: error: git ls-tree call failed with return code: %r, stderr: %r' % ( |
| 198 | retval, p.stderr), file=sys.stderr) |
| 199 | return data |
| 200 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 201 | def Sync(self): |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 202 | """Gets a local copy of a superproject for the manifest. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 203 | |
| 204 | Returns: |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 205 | SyncResult |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 206 | """ |
Raman Tenneti | 2b37fa3 | 2021-06-02 17:46:25 -0700 | [diff] [blame] | 207 | print('NOTICE: --use-superproject is in beta; report any issues to the ' |
| 208 | 'address described in `repo version`', file=sys.stderr) |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 209 | |
| 210 | if not self._manifest.superproject: |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 211 | msg = (f'repo error: superproject tag is not defined in manifest: ' |
| 212 | f'{self._manifest.manifestFile}') |
| 213 | print(msg, file=sys.stderr) |
| 214 | self._git_event_log.ErrorEvent(msg, '') |
| 215 | return SyncResult(False, False) |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 216 | |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 217 | should_exit = True |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 218 | url = self._manifest.superproject['remote'].url |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 219 | if not url: |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 220 | print('error: superproject URL is not defined in manifest', |
| 221 | file=sys.stderr) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 222 | return SyncResult(False, should_exit) |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 223 | |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 224 | if not self._Init(): |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 225 | return SyncResult(False, should_exit) |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 226 | if not self._Fetch(url): |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 227 | return SyncResult(False, should_exit) |
Raman Tenneti | ef99ec0 | 2021-03-04 10:29:40 -0800 | [diff] [blame] | 228 | if not self._quiet: |
| 229 | print('%s: Initial setup for superproject completed.' % self._work_git) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 230 | return SyncResult(True, False) |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 231 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 232 | def _GetAllProjectsCommitIds(self): |
| 233 | """Get commit ids for all projects from superproject and save them in _project_commit_ids. |
| 234 | |
| 235 | Returns: |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 236 | CommitIdsResult |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 237 | """ |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 238 | sync_result = self.Sync() |
| 239 | if not sync_result.success: |
| 240 | return CommitIdsResult(None, sync_result.fatal) |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 241 | |
| 242 | data = self._LsTree() |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 243 | if not data: |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 244 | print('error: git ls-tree failed to return data for superproject', |
| 245 | file=sys.stderr) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 246 | return CommitIdsResult(None, True) |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 247 | |
| 248 | # Parse lines like the following to select lines starting with '160000' and |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 249 | # build a dictionary with project path (last element) and its commit id (3rd element). |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 250 | # |
| 251 | # 160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00 |
| 252 | # 120000 blob acc2cbdf438f9d2141f0ae424cec1d8fc4b5d97f\tbootstrap.bash\x00 |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 253 | commit_ids = {} |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 254 | for line in data.split('\x00'): |
| 255 | ls_data = line.split(None, 3) |
| 256 | if not ls_data: |
| 257 | break |
| 258 | if ls_data[0] == '160000': |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 259 | commit_ids[ls_data[3]] = ls_data[2] |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 260 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 261 | self._project_commit_ids = commit_ids |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 262 | return CommitIdsResult(commit_ids, False) |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 263 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 264 | def _WriteManfiestFile(self): |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 265 | """Writes manifest to a file. |
| 266 | |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 267 | Returns: |
| 268 | manifest_path: Path name of the file into which manifest is written instead of None. |
| 269 | """ |
| 270 | if not os.path.exists(self._superproject_path): |
| 271 | print('error: missing superproject directory %s' % |
| 272 | self._superproject_path, |
| 273 | file=sys.stderr) |
| 274 | return None |
Raman Tenneti | 080877e | 2021-03-09 15:19:06 -0800 | [diff] [blame] | 275 | manifest_str = self._manifest.ToXml(groups=self._manifest.GetGroupsStr()).toxml() |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 276 | manifest_path = self._manifest_path |
| 277 | try: |
| 278 | with open(manifest_path, 'w', encoding='utf-8') as fp: |
| 279 | fp.write(manifest_str) |
| 280 | except IOError as e: |
| 281 | print('error: cannot write manifest to %s:\n%s' |
| 282 | % (manifest_path, e), |
| 283 | file=sys.stderr) |
| 284 | return None |
| 285 | return manifest_path |
| 286 | |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 287 | def _SkipUpdatingProjectRevisionId(self, project): |
| 288 | """Checks if a project's revision id needs to be updated or not. |
| 289 | |
| 290 | Revision id for projects from local manifest will not be updated. |
| 291 | |
| 292 | Args: |
| 293 | project: project whose revision id is being updated. |
| 294 | |
| 295 | Returns: |
| 296 | True if a project's revision id should not be updated, or False, |
| 297 | """ |
| 298 | path = project.relpath |
| 299 | if not path: |
| 300 | return True |
Raman Tenneti | 1da6f30 | 2021-06-28 19:21:38 -0700 | [diff] [blame] | 301 | # Skip the project with revisionId. |
| 302 | if project.revisionId: |
| 303 | return True |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 304 | # Skip the project if it comes from the local manifest. |
| 305 | return any(s.startswith(LOCAL_MANIFEST_GROUP_PREFIX) for s in project.groups) |
| 306 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 307 | def UpdateProjectsRevisionId(self, projects): |
| 308 | """Update revisionId of every project in projects with the commit id. |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 309 | |
| 310 | Args: |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 311 | projects: List of projects whose revisionId needs to be updated. |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 312 | |
| 313 | Returns: |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 314 | UpdateProjectsResult |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 315 | """ |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 316 | commit_ids_result = self._GetAllProjectsCommitIds() |
| 317 | commit_ids = commit_ids_result.commit_ids |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 318 | if not commit_ids: |
| 319 | print('error: Cannot get project commit ids from manifest', file=sys.stderr) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 320 | return UpdateProjectsResult(None, commit_ids_result.fatal) |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 321 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 322 | projects_missing_commit_ids = [] |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 323 | for project in projects: |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 324 | if self._SkipUpdatingProjectRevisionId(project): |
| 325 | continue |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 326 | path = project.relpath |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 327 | commit_id = commit_ids.get(path) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 328 | if not commit_id: |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 329 | projects_missing_commit_ids.append(path) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 330 | |
| 331 | # If superproject doesn't have a commit id for a project, then report an |
| 332 | # error event and continue as if do not use superproject is specified. |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 333 | if projects_missing_commit_ids: |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 334 | msg = (f'error: please file a bug using {self._manifest.contactinfo.bugurl} ' |
| 335 | f'to report missing commit_ids for: {projects_missing_commit_ids}') |
| 336 | print(msg, file=sys.stderr) |
| 337 | self._git_event_log.ErrorEvent(msg, '') |
| 338 | return UpdateProjectsResult(None, False) |
| 339 | |
| 340 | for project in projects: |
| 341 | if not self._SkipUpdatingProjectRevisionId(project): |
| 342 | project.SetRevisionId(commit_ids.get(project.relpath)) |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 343 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 344 | manifest_path = self._WriteManfiestFile() |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 345 | return UpdateProjectsResult(manifest_path, False) |