The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # Copyright (C) 2008 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 | 993af5e | 2021-05-12 12:00:31 -0700 | [diff] [blame] | 15 | import collections |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 16 | import itertools |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 17 | import os |
Raman Tenneti | 080877e | 2021-03-09 15:19:06 -0800 | [diff] [blame] | 18 | import platform |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 19 | import re |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | import sys |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 21 | import xml.dom.minidom |
Mike Frysinger | acf63b2 | 2019-06-13 02:24:21 -0400 | [diff] [blame] | 22 | import urllib.parse |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 23 | |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 24 | import gitc_utils |
Miguel Gaio | 1f20776 | 2020-07-17 14:09:13 +0200 | [diff] [blame] | 25 | from git_config import GitConfig, IsId |
David Pursehouse | e00aa6b | 2012-09-11 14:33:51 +0900 | [diff] [blame] | 26 | from git_refs import R_HEADS, HEAD |
LaMont Jones | d56e2eb | 2022-04-07 18:14:46 +0000 | [diff] [blame] | 27 | from git_superproject import Superproject |
Renaud Paquay | d5cec5e | 2016-11-01 11:24:03 -0700 | [diff] [blame] | 28 | import platform_utils |
LaMont Jones | 9b72cf2 | 2022-03-29 21:54:22 +0000 | [diff] [blame] | 29 | from project import (Annotation, RemoteSpec, Project, RepoProject, |
| 30 | ManifestProject) |
Mike Frysinger | 04122b7 | 2019-07-31 23:32:58 -0400 | [diff] [blame] | 31 | from error import (ManifestParseError, ManifestInvalidPathError, |
| 32 | ManifestInvalidRevisionError) |
Raman Tenneti | 993af5e | 2021-05-12 12:00:31 -0700 | [diff] [blame] | 33 | from wrapper import Wrapper |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 34 | |
| 35 | MANIFEST_FILE_NAME = 'manifest.xml' |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 36 | LOCAL_MANIFEST_NAME = 'local_manifest.xml' |
David Pursehouse | 2d5a0df | 2012-11-13 02:50:36 +0900 | [diff] [blame] | 37 | LOCAL_MANIFESTS_DIR_NAME = 'local_manifests' |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 38 | SUBMANIFEST_DIR = 'submanifests' |
| 39 | # Limit submanifests to an arbitrary depth for loop detection. |
| 40 | MAX_SUBMANIFEST_DEPTH = 8 |
LaMont Jones | b308db1 | 2022-02-25 17:05:21 +0000 | [diff] [blame] | 41 | # Add all projects from sub manifest into a group. |
| 42 | SUBMANIFEST_GROUP_PREFIX = 'submanifest:' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 43 | |
Raman Tenneti | 78f4dd3 | 2021-06-07 13:27:37 -0700 | [diff] [blame] | 44 | # Add all projects from local manifest into a group. |
| 45 | LOCAL_MANIFEST_GROUP_PREFIX = 'local:' |
| 46 | |
Raman Tenneti | 993af5e | 2021-05-12 12:00:31 -0700 | [diff] [blame] | 47 | # ContactInfo has the self-registered bug url, supplied by the manifest authors. |
| 48 | ContactInfo = collections.namedtuple('ContactInfo', 'bugurl') |
| 49 | |
Anthony King | cb07ba7 | 2015-03-28 23:26:04 +0000 | [diff] [blame] | 50 | # urljoin gets confused if the scheme is not known. |
Joe Kilner | 6e31079 | 2016-10-27 15:53:53 -0700 | [diff] [blame] | 51 | urllib.parse.uses_relative.extend([ |
| 52 | 'ssh', |
| 53 | 'git', |
| 54 | 'persistent-https', |
| 55 | 'sso', |
| 56 | 'rpc']) |
| 57 | urllib.parse.uses_netloc.extend([ |
| 58 | 'ssh', |
| 59 | 'git', |
| 60 | 'persistent-https', |
| 61 | 'sso', |
| 62 | 'rpc']) |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 63 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 64 | |
Mike Frysinger | bb8ee7f | 2020-02-22 05:30:12 -0500 | [diff] [blame] | 65 | def XmlBool(node, attr, default=None): |
| 66 | """Determine boolean value of |node|'s |attr|. |
| 67 | |
| 68 | Invalid values will issue a non-fatal warning. |
| 69 | |
| 70 | Args: |
| 71 | node: XML node whose attributes we access. |
| 72 | attr: The attribute to access. |
| 73 | default: If the attribute is not set (value is empty), then use this. |
| 74 | |
| 75 | Returns: |
| 76 | True if the attribute is a valid string representing true. |
| 77 | False if the attribute is a valid string representing false. |
| 78 | |default| otherwise. |
| 79 | """ |
| 80 | value = node.getAttribute(attr) |
| 81 | s = value.lower() |
| 82 | if s == '': |
| 83 | return default |
| 84 | elif s in {'yes', 'true', '1'}: |
| 85 | return True |
| 86 | elif s in {'no', 'false', '0'}: |
| 87 | return False |
| 88 | else: |
| 89 | print('warning: manifest: %s="%s": ignoring invalid XML boolean' % |
| 90 | (attr, value), file=sys.stderr) |
| 91 | return default |
| 92 | |
| 93 | |
| 94 | def XmlInt(node, attr, default=None): |
| 95 | """Determine integer value of |node|'s |attr|. |
| 96 | |
| 97 | Args: |
| 98 | node: XML node whose attributes we access. |
| 99 | attr: The attribute to access. |
| 100 | default: If the attribute is not set (value is empty), then use this. |
| 101 | |
| 102 | Returns: |
| 103 | The number if the attribute is a valid number. |
| 104 | |
| 105 | Raises: |
| 106 | ManifestParseError: The number is invalid. |
| 107 | """ |
| 108 | value = node.getAttribute(attr) |
| 109 | if not value: |
| 110 | return default |
| 111 | |
| 112 | try: |
| 113 | return int(value) |
| 114 | except ValueError: |
| 115 | raise ManifestParseError('manifest: invalid %s="%s" integer' % |
| 116 | (attr, value)) |
| 117 | |
| 118 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 119 | class _Default(object): |
| 120 | """Project defaults within the manifest.""" |
| 121 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 122 | revisionExpr = None |
Conley Owens | b6a16e6 | 2013-09-25 15:06:09 -0700 | [diff] [blame] | 123 | destBranchExpr = None |
Nasser Grainawi | da40341 | 2018-05-04 12:53:29 -0600 | [diff] [blame] | 124 | upstreamExpr = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 125 | remote = None |
Mike Frysinger | 355f439 | 2022-07-20 17:15:29 -0400 | [diff] [blame] | 126 | sync_j = None |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 127 | sync_c = False |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 128 | sync_s = False |
YOUNG HO CHA | a32c92c | 2018-02-14 16:57:31 +0900 | [diff] [blame] | 129 | sync_tags = True |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 130 | |
Julien Campergue | 7487992 | 2013-10-09 14:38:46 +0200 | [diff] [blame] | 131 | def __eq__(self, other): |
Jack Neus | 5ba2120 | 2021-06-09 15:21:25 +0000 | [diff] [blame] | 132 | if not isinstance(other, _Default): |
| 133 | return False |
Julien Campergue | 7487992 | 2013-10-09 14:38:46 +0200 | [diff] [blame] | 134 | return self.__dict__ == other.__dict__ |
| 135 | |
| 136 | def __ne__(self, other): |
Jack Neus | 5ba2120 | 2021-06-09 15:21:25 +0000 | [diff] [blame] | 137 | if not isinstance(other, _Default): |
| 138 | return True |
Julien Campergue | 7487992 | 2013-10-09 14:38:46 +0200 | [diff] [blame] | 139 | return self.__dict__ != other.__dict__ |
| 140 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 141 | |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 142 | class _XmlRemote(object): |
| 143 | def __init__(self, |
| 144 | name, |
Yestin Sun | b292b98 | 2012-07-02 07:32:50 -0700 | [diff] [blame] | 145 | alias=None, |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 146 | fetch=None, |
Steve Rae | d648045 | 2016-08-10 15:00:00 -0700 | [diff] [blame] | 147 | pushUrl=None, |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 148 | manifestUrl=None, |
Anthony King | 36ea2fb | 2014-05-06 11:54:01 +0100 | [diff] [blame] | 149 | review=None, |
Jonathan Nieder | 9371979 | 2015-03-17 11:29:58 -0700 | [diff] [blame] | 150 | revision=None): |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 151 | self.name = name |
| 152 | self.fetchUrl = fetch |
Steve Rae | d648045 | 2016-08-10 15:00:00 -0700 | [diff] [blame] | 153 | self.pushUrl = pushUrl |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 154 | self.manifestUrl = manifestUrl |
Yestin Sun | b292b98 | 2012-07-02 07:32:50 -0700 | [diff] [blame] | 155 | self.remoteAlias = alias |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 156 | self.reviewUrl = review |
Anthony King | 36ea2fb | 2014-05-06 11:54:01 +0100 | [diff] [blame] | 157 | self.revision = revision |
Conley Owens | ceea368 | 2011-10-20 10:45:47 -0700 | [diff] [blame] | 158 | self.resolvedFetchUrl = self._resolveFetchUrl() |
Jack Neus | 6ea0cae | 2021-07-20 20:52:33 +0000 | [diff] [blame] | 159 | self.annotations = [] |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 160 | |
David Pursehouse | 717ece9 | 2012-11-13 08:49:16 +0900 | [diff] [blame] | 161 | def __eq__(self, other): |
Jack Neus | 5ba2120 | 2021-06-09 15:21:25 +0000 | [diff] [blame] | 162 | if not isinstance(other, _XmlRemote): |
| 163 | return False |
Jack Neus | 6ea0cae | 2021-07-20 20:52:33 +0000 | [diff] [blame] | 164 | return (sorted(self.annotations) == sorted(other.annotations) and |
| 165 | self.name == other.name and self.fetchUrl == other.fetchUrl and |
| 166 | self.pushUrl == other.pushUrl and self.remoteAlias == other.remoteAlias |
| 167 | and self.reviewUrl == other.reviewUrl and self.revision == other.revision) |
David Pursehouse | 717ece9 | 2012-11-13 08:49:16 +0900 | [diff] [blame] | 168 | |
| 169 | def __ne__(self, other): |
Jack Neus | 6ea0cae | 2021-07-20 20:52:33 +0000 | [diff] [blame] | 170 | return not self.__eq__(other) |
David Pursehouse | 717ece9 | 2012-11-13 08:49:16 +0900 | [diff] [blame] | 171 | |
Conley Owens | ceea368 | 2011-10-20 10:45:47 -0700 | [diff] [blame] | 172 | def _resolveFetchUrl(self): |
Jack Neus | 5ba2120 | 2021-06-09 15:21:25 +0000 | [diff] [blame] | 173 | if self.fetchUrl is None: |
| 174 | return '' |
Conley Owens | ceea368 | 2011-10-20 10:45:47 -0700 | [diff] [blame] | 175 | url = self.fetchUrl.rstrip('/') |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 176 | manifestUrl = self.manifestUrl.rstrip('/') |
Conley Owens | 2d0f508 | 2014-01-31 15:03:51 -0800 | [diff] [blame] | 177 | # urljoin will gets confused over quite a few things. The ones we care |
| 178 | # about here are: |
| 179 | # * no scheme in the base url, like <hostname:port> |
Anthony King | cb07ba7 | 2015-03-28 23:26:04 +0000 | [diff] [blame] | 180 | # We handle no scheme by replacing it with an obscure protocol, gopher |
| 181 | # and then replacing it with the original when we are done. |
| 182 | |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 183 | if manifestUrl.find(':') != manifestUrl.find('/') - 1: |
Conley Owens | 4ccad75 | 2015-04-29 10:45:37 -0700 | [diff] [blame] | 184 | url = urllib.parse.urljoin('gopher://' + manifestUrl, url) |
| 185 | url = re.sub(r'^gopher://', '', url) |
Anthony King | cb07ba7 | 2015-03-28 23:26:04 +0000 | [diff] [blame] | 186 | else: |
| 187 | url = urllib.parse.urljoin(manifestUrl, url) |
Shawn Pearce | a9f11b3 | 2013-01-02 15:40:48 -0800 | [diff] [blame] | 188 | return url |
Conley Owens | ceea368 | 2011-10-20 10:45:47 -0700 | [diff] [blame] | 189 | |
| 190 | def ToRemoteSpec(self, projectName): |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 191 | fetchUrl = self.resolvedFetchUrl.rstrip('/') |
| 192 | url = fetchUrl + '/' + projectName |
Yestin Sun | b292b98 | 2012-07-02 07:32:50 -0700 | [diff] [blame] | 193 | remoteName = self.name |
Conley Owens | 1e7ab2a | 2013-10-08 17:26:57 -0700 | [diff] [blame] | 194 | if self.remoteAlias: |
David Pursehouse | 37128b6 | 2013-10-15 10:48:40 +0900 | [diff] [blame] | 195 | remoteName = self.remoteAlias |
Dan Willemsen | 96c2d65 | 2016-04-06 16:03:54 -0700 | [diff] [blame] | 196 | return RemoteSpec(remoteName, |
| 197 | url=url, |
Steve Rae | d648045 | 2016-08-10 15:00:00 -0700 | [diff] [blame] | 198 | pushUrl=self.pushUrl, |
Dan Willemsen | 96c2d65 | 2016-04-06 16:03:54 -0700 | [diff] [blame] | 199 | review=self.reviewUrl, |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 200 | orig_name=self.name, |
| 201 | fetchUrl=self.fetchUrl) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 202 | |
Jack Neus | 6ea0cae | 2021-07-20 20:52:33 +0000 | [diff] [blame] | 203 | def AddAnnotation(self, name, value, keep): |
| 204 | self.annotations.append(Annotation(name, value, keep)) |
| 205 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 206 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 207 | class _XmlSubmanifest: |
| 208 | """Manage the <submanifest> element specified in the manifest. |
| 209 | |
| 210 | Attributes: |
| 211 | name: a string, the name for this submanifest. |
| 212 | remote: a string, the remote.name for this submanifest. |
| 213 | project: a string, the name of the manifest project. |
| 214 | revision: a string, the commitish. |
| 215 | manifestName: a string, the submanifest file name. |
| 216 | groups: a list of strings, the groups to add to all projects in the submanifest. |
LaMont Jones | 501733c | 2022-04-20 16:42:32 +0000 | [diff] [blame] | 217 | default_groups: a list of strings, the default groups to sync. |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 218 | path: a string, the relative path for the submanifest checkout. |
LaMont Jones | b90a422 | 2022-04-14 15:00:09 +0000 | [diff] [blame] | 219 | parent: an XmlManifest, the parent manifest. |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 220 | annotations: (derived) a list of annotations. |
LaMont Jones | b90a422 | 2022-04-14 15:00:09 +0000 | [diff] [blame] | 221 | present: (derived) a boolean, whether the sub manifest file is present. |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 222 | """ |
| 223 | def __init__(self, |
| 224 | name, |
| 225 | remote=None, |
| 226 | project=None, |
| 227 | revision=None, |
| 228 | manifestName=None, |
| 229 | groups=None, |
LaMont Jones | 501733c | 2022-04-20 16:42:32 +0000 | [diff] [blame] | 230 | default_groups=None, |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 231 | path=None, |
| 232 | parent=None): |
| 233 | self.name = name |
| 234 | self.remote = remote |
| 235 | self.project = project |
| 236 | self.revision = revision |
| 237 | self.manifestName = manifestName |
| 238 | self.groups = groups |
LaMont Jones | 501733c | 2022-04-20 16:42:32 +0000 | [diff] [blame] | 239 | self.default_groups = default_groups |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 240 | self.path = path |
LaMont Jones | b90a422 | 2022-04-14 15:00:09 +0000 | [diff] [blame] | 241 | self.parent = parent |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 242 | self.annotations = [] |
| 243 | outer_client = parent._outer_client or parent |
| 244 | if self.remote and not self.project: |
| 245 | raise ManifestParseError( |
| 246 | f'Submanifest {name}: must specify project when remote is given.') |
LaMont Jones | 5d3291d | 2022-03-23 19:03:02 +0000 | [diff] [blame] | 247 | # Construct the absolute path to the manifest file using the parent's |
| 248 | # method, so that we can correctly create our repo_client. |
| 249 | manifestFile = parent.SubmanifestInfoDir( |
| 250 | os.path.join(parent.path_prefix, self.relpath), |
| 251 | os.path.join('manifests', manifestName or 'default.xml')) |
LaMont Jones | 55ee304 | 2022-04-06 17:10:21 +0000 | [diff] [blame] | 252 | linkFile = parent.SubmanifestInfoDir( |
| 253 | os.path.join(parent.path_prefix, self.relpath), MANIFEST_FILE_NAME) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 254 | rc = self.repo_client = RepoClient( |
LaMont Jones | 55ee304 | 2022-04-06 17:10:21 +0000 | [diff] [blame] | 255 | parent.repodir, linkFile, parent_groups=','.join(groups) or '', |
LaMont Jones | 501733c | 2022-04-20 16:42:32 +0000 | [diff] [blame] | 256 | submanifest_path=self.relpath, outer_client=outer_client, |
| 257 | default_groups=default_groups) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 258 | |
LaMont Jones | 55ee304 | 2022-04-06 17:10:21 +0000 | [diff] [blame] | 259 | self.present = os.path.exists(manifestFile) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 260 | |
| 261 | def __eq__(self, other): |
| 262 | if not isinstance(other, _XmlSubmanifest): |
| 263 | return False |
| 264 | return ( |
| 265 | self.name == other.name and |
| 266 | self.remote == other.remote and |
| 267 | self.project == other.project and |
| 268 | self.revision == other.revision and |
| 269 | self.manifestName == other.manifestName and |
| 270 | self.groups == other.groups and |
LaMont Jones | 501733c | 2022-04-20 16:42:32 +0000 | [diff] [blame] | 271 | self.default_groups == other.default_groups and |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 272 | self.path == other.path and |
| 273 | sorted(self.annotations) == sorted(other.annotations)) |
| 274 | |
| 275 | def __ne__(self, other): |
| 276 | return not self.__eq__(other) |
| 277 | |
LaMont Jones | b90a422 | 2022-04-14 15:00:09 +0000 | [diff] [blame] | 278 | def ToSubmanifestSpec(self): |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 279 | """Return a SubmanifestSpec object, populating attributes""" |
LaMont Jones | b90a422 | 2022-04-14 15:00:09 +0000 | [diff] [blame] | 280 | mp = self.parent.manifestProject |
| 281 | remote = self.parent.remotes[self.remote or self.parent.default.remote.name] |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 282 | # If a project was given, generate the url from the remote and project. |
| 283 | # If not, use this manifestProject's url. |
| 284 | if self.project: |
| 285 | manifestUrl = remote.ToRemoteSpec(self.project).url |
| 286 | else: |
Mike Frysinger | dede564 | 2022-07-10 04:56:04 -0400 | [diff] [blame] | 287 | manifestUrl = mp.GetRemote().url |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 288 | manifestName = self.manifestName or 'default.xml' |
| 289 | revision = self.revision or self.name |
| 290 | path = self.path or revision.split('/')[-1] |
| 291 | groups = self.groups or [] |
LaMont Jones | 501733c | 2022-04-20 16:42:32 +0000 | [diff] [blame] | 292 | default_groups = self.default_groups or [] |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 293 | |
| 294 | return SubmanifestSpec(self.name, manifestUrl, manifestName, revision, path, |
| 295 | groups) |
| 296 | |
| 297 | @property |
| 298 | def relpath(self): |
| 299 | """The path of this submanifest relative to the parent manifest.""" |
| 300 | revision = self.revision or self.name |
| 301 | return self.path or revision.split('/')[-1] |
| 302 | |
| 303 | def GetGroupsStr(self): |
| 304 | """Returns the `groups` given for this submanifest.""" |
| 305 | if self.groups: |
| 306 | return ','.join(self.groups) |
| 307 | return '' |
| 308 | |
LaMont Jones | 501733c | 2022-04-20 16:42:32 +0000 | [diff] [blame] | 309 | def GetDefaultGroupsStr(self): |
| 310 | """Returns the `default-groups` given for this submanifest.""" |
| 311 | return ','.join(self.default_groups or []) |
| 312 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 313 | def AddAnnotation(self, name, value, keep): |
| 314 | """Add annotations to the submanifest.""" |
| 315 | self.annotations.append(Annotation(name, value, keep)) |
| 316 | |
| 317 | |
| 318 | class SubmanifestSpec: |
| 319 | """The submanifest element, with all fields expanded.""" |
| 320 | |
| 321 | def __init__(self, |
| 322 | name, |
| 323 | manifestUrl, |
| 324 | manifestName, |
| 325 | revision, |
| 326 | path, |
| 327 | groups): |
| 328 | self.name = name |
| 329 | self.manifestUrl = manifestUrl |
| 330 | self.manifestName = manifestName |
| 331 | self.revision = revision |
| 332 | self.path = path |
| 333 | self.groups = groups or [] |
| 334 | |
| 335 | |
Shawn O. Pearce | c8a300f | 2009-05-18 13:19:57 -0700 | [diff] [blame] | 336 | class XmlManifest(object): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 337 | """manages the repo configuration file""" |
| 338 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 339 | def __init__(self, repodir, manifest_file, local_manifests=None, |
LaMont Jones | 501733c | 2022-04-20 16:42:32 +0000 | [diff] [blame] | 340 | outer_client=None, parent_groups='', submanifest_path='', |
| 341 | default_groups=None): |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 342 | """Initialize. |
| 343 | |
| 344 | Args: |
| 345 | repodir: Path to the .repo/ dir for holding all internal checkout state. |
| 346 | It must be in the top directory of the repo client checkout. |
| 347 | manifest_file: Full path to the manifest file to parse. This will usually |
| 348 | be |repodir|/|MANIFEST_FILE_NAME|. |
| 349 | local_manifests: Full path to the directory of local override manifests. |
| 350 | This will usually be |repodir|/|LOCAL_MANIFESTS_DIR_NAME|. |
LaMont Jones | ff6b1da | 2022-06-01 21:03:34 +0000 | [diff] [blame] | 351 | outer_client: RepoClient of the outer manifest. |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 352 | parent_groups: a string, the groups to apply to this projects. |
| 353 | submanifest_path: The submanifest root relative to the repo root. |
LaMont Jones | 501733c | 2022-04-20 16:42:32 +0000 | [diff] [blame] | 354 | default_groups: a string, the default manifest groups to use. |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 355 | """ |
| 356 | # TODO(vapier): Move this out of this class. |
| 357 | self.globalConfig = GitConfig.ForUser() |
| 358 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 359 | self.repodir = os.path.abspath(repodir) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 360 | self._CheckLocalPath(submanifest_path) |
LaMont Jones | fbd5dd3 | 2022-08-22 19:26:14 +0000 | [diff] [blame] | 361 | self.topdir = os.path.dirname(self.repodir) |
| 362 | if submanifest_path: |
| 363 | # This avoids a trailing os.path.sep when submanifest_path is empty. |
| 364 | self.topdir = os.path.join(self.topdir, submanifest_path) |
LaMont Jones | 5d3291d | 2022-03-23 19:03:02 +0000 | [diff] [blame] | 365 | if manifest_file != os.path.abspath(manifest_file): |
| 366 | raise ManifestParseError('manifest_file must be abspath') |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 367 | self.manifestFile = manifest_file |
LaMont Jones | b90a422 | 2022-04-14 15:00:09 +0000 | [diff] [blame] | 368 | if not outer_client or outer_client == self: |
| 369 | # manifestFileOverrides only exists in the outer_client's manifest, since |
| 370 | # that is the only instance left when Unload() is called on the outer |
| 371 | # manifest. |
| 372 | self.manifestFileOverrides = {} |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 373 | self.local_manifests = local_manifests |
Basil Gello | c745350 | 2018-05-25 20:23:52 +0300 | [diff] [blame] | 374 | self._load_local_manifests = True |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 375 | self.parent_groups = parent_groups |
LaMont Jones | 501733c | 2022-04-20 16:42:32 +0000 | [diff] [blame] | 376 | self.default_groups = default_groups |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 377 | |
| 378 | if outer_client and self.isGitcClient: |
| 379 | raise ManifestParseError('Multi-manifest is incompatible with `gitc-init`') |
| 380 | |
| 381 | if submanifest_path and not outer_client: |
| 382 | # If passing a submanifest_path, there must be an outer_client. |
| 383 | raise ManifestParseError(f'Bad call to {self.__class__.__name__}') |
| 384 | |
| 385 | # If self._outer_client is None, this is not a checkout that supports |
| 386 | # multi-tree. |
| 387 | self._outer_client = outer_client or self |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 388 | |
LaMont Jones | 9b72cf2 | 2022-03-29 21:54:22 +0000 | [diff] [blame] | 389 | self.repoProject = RepoProject(self, 'repo', |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 390 | gitdir=os.path.join(repodir, 'repo/.git'), |
| 391 | worktree=os.path.join(repodir, 'repo')) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 392 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 393 | mp = self.SubmanifestProject(self.path_prefix) |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 394 | self.manifestProject = mp |
| 395 | |
| 396 | # This is a bit hacky, but we're in a chicken & egg situation: all the |
| 397 | # normal repo settings live in the manifestProject which we just setup |
| 398 | # above, so we couldn't easily query before that. We assume Project() |
| 399 | # init doesn't care if this changes afterwards. |
LaMont Jones | d82be3e | 2022-04-05 19:30:46 +0000 | [diff] [blame] | 400 | if os.path.exists(mp.gitdir) and mp.use_worktree: |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 401 | mp.use_git_worktrees = True |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 402 | |
LaMont Jones | a2ff20d | 2022-04-07 16:49:06 +0000 | [diff] [blame] | 403 | self.Unload() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 404 | |
Basil Gello | c745350 | 2018-05-25 20:23:52 +0300 | [diff] [blame] | 405 | def Override(self, name, load_local_manifests=True): |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 406 | """Use a different manifest, just for the current instantiation. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 407 | """ |
Basil Gello | c745350 | 2018-05-25 20:23:52 +0300 | [diff] [blame] | 408 | path = None |
| 409 | |
| 410 | # Look for a manifest by path in the filesystem (including the cwd). |
| 411 | if not load_local_manifests: |
| 412 | local_path = os.path.abspath(name) |
| 413 | if os.path.isfile(local_path): |
| 414 | path = local_path |
| 415 | |
| 416 | # Look for manifests by name from the manifests repo. |
| 417 | if path is None: |
| 418 | path = os.path.join(self.manifestProject.worktree, name) |
| 419 | if not os.path.isfile(path): |
| 420 | raise ManifestParseError('manifest %s not found' % name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 421 | |
LaMont Jones | b90a422 | 2022-04-14 15:00:09 +0000 | [diff] [blame] | 422 | self._load_local_manifests = load_local_manifests |
| 423 | self._outer_client.manifestFileOverrides[self.path_prefix] = path |
| 424 | self.Unload() |
| 425 | self._Load() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 426 | |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 427 | def Link(self, name): |
| 428 | """Update the repo metadata to use a different manifest. |
| 429 | """ |
| 430 | self.Override(name) |
| 431 | |
Mike Frysinger | a269b1c | 2020-02-21 00:49:41 -0500 | [diff] [blame] | 432 | # Old versions of repo would generate symlinks we need to clean up. |
Mike Frysinger | 9d96f58 | 2021-09-28 11:27:24 -0400 | [diff] [blame] | 433 | platform_utils.remove(self.manifestFile, missing_ok=True) |
Mike Frysinger | a269b1c | 2020-02-21 00:49:41 -0500 | [diff] [blame] | 434 | # This file is interpreted as if it existed inside the manifest repo. |
| 435 | # That allows us to use <include> with the relative file name. |
| 436 | with open(self.manifestFile, 'w') as fp: |
| 437 | fp.write("""<?xml version="1.0" encoding="UTF-8"?> |
| 438 | <!-- |
| 439 | DO NOT EDIT THIS FILE! It is generated by repo and changes will be discarded. |
| 440 | If you want to use a different manifest, use `repo init -m <file>` instead. |
| 441 | |
| 442 | If you want to customize your checkout by overriding manifest settings, use |
| 443 | the local_manifests/ directory instead. |
| 444 | |
| 445 | For more information on repo manifests, check out: |
| 446 | https://gerrit.googlesource.com/git-repo/+/HEAD/docs/manifest-format.md |
| 447 | --> |
| 448 | <manifest> |
| 449 | <include name="%s" /> |
| 450 | </manifest> |
| 451 | """ % (name,)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 452 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 453 | def _RemoteToXml(self, r, doc, root): |
| 454 | e = doc.createElement('remote') |
| 455 | root.appendChild(e) |
| 456 | e.setAttribute('name', r.name) |
| 457 | e.setAttribute('fetch', r.fetchUrl) |
Steve Rae | d648045 | 2016-08-10 15:00:00 -0700 | [diff] [blame] | 458 | if r.pushUrl is not None: |
| 459 | e.setAttribute('pushurl', r.pushUrl) |
Conley Owens | 1e7ab2a | 2013-10-08 17:26:57 -0700 | [diff] [blame] | 460 | if r.remoteAlias is not None: |
| 461 | e.setAttribute('alias', r.remoteAlias) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 462 | if r.reviewUrl is not None: |
| 463 | e.setAttribute('review', r.reviewUrl) |
Anthony King | 36ea2fb | 2014-05-06 11:54:01 +0100 | [diff] [blame] | 464 | if r.revision is not None: |
| 465 | e.setAttribute('revision', r.revision) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 466 | |
Jack Neus | 6ea0cae | 2021-07-20 20:52:33 +0000 | [diff] [blame] | 467 | for a in r.annotations: |
| 468 | if a.keep == 'true': |
| 469 | ae = doc.createElement('annotation') |
| 470 | ae.setAttribute('name', a.name) |
| 471 | ae.setAttribute('value', a.value) |
| 472 | e.appendChild(ae) |
| 473 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 474 | def _SubmanifestToXml(self, r, doc, root): |
| 475 | """Generate XML <submanifest/> node.""" |
| 476 | e = doc.createElement('submanifest') |
| 477 | root.appendChild(e) |
| 478 | e.setAttribute('name', r.name) |
| 479 | if r.remote is not None: |
| 480 | e.setAttribute('remote', r.remote) |
| 481 | if r.project is not None: |
| 482 | e.setAttribute('project', r.project) |
| 483 | if r.manifestName is not None: |
| 484 | e.setAttribute('manifest-name', r.manifestName) |
| 485 | if r.revision is not None: |
| 486 | e.setAttribute('revision', r.revision) |
| 487 | if r.path is not None: |
| 488 | e.setAttribute('path', r.path) |
| 489 | if r.groups: |
| 490 | e.setAttribute('groups', r.GetGroupsStr()) |
LaMont Jones | 501733c | 2022-04-20 16:42:32 +0000 | [diff] [blame] | 491 | if r.default_groups: |
| 492 | e.setAttribute('default-groups', r.GetDefaultGroupsStr()) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 493 | |
| 494 | for a in r.annotations: |
| 495 | if a.keep == 'true': |
| 496 | ae = doc.createElement('annotation') |
| 497 | ae.setAttribute('name', a.name) |
| 498 | ae.setAttribute('value', a.value) |
| 499 | e.appendChild(ae) |
| 500 | |
Mike Frysinger | 51e39d5 | 2020-12-04 05:32:06 -0500 | [diff] [blame] | 501 | def _ParseList(self, field): |
| 502 | """Parse fields that contain flattened lists. |
| 503 | |
| 504 | These are whitespace & comma separated. Empty elements will be discarded. |
| 505 | """ |
| 506 | return [x for x in re.split(r'[,\s]+', field) if x] |
Josh Triplett | 884a387 | 2014-06-12 14:57:29 -0700 | [diff] [blame] | 507 | |
LaMont Jones | a8cf575 | 2022-07-15 20:31:33 +0000 | [diff] [blame] | 508 | def ToXml(self, peg_rev=False, peg_rev_upstream=True, |
| 509 | peg_rev_dest_branch=True, groups=None, omit_local=False): |
Mike Frysinger | 23411d3 | 2020-09-02 04:31:10 -0400 | [diff] [blame] | 510 | """Return the current manifest XML.""" |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 511 | mp = self.manifestProject |
| 512 | |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 513 | if groups is None: |
LaMont Jones | d82be3e | 2022-04-05 19:30:46 +0000 | [diff] [blame] | 514 | groups = mp.manifest_groups |
Matt Gumbel | 0c635bb | 2012-12-21 10:14:53 -0800 | [diff] [blame] | 515 | if groups: |
Mike Frysinger | 51e39d5 | 2020-12-04 05:32:06 -0500 | [diff] [blame] | 516 | groups = self._ParseList(groups) |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 517 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 518 | doc = xml.dom.minidom.Document() |
| 519 | root = doc.createElement('manifest') |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 520 | if self.is_submanifest: |
| 521 | root.setAttribute('path', self.path_prefix) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 522 | doc.appendChild(root) |
| 523 | |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 524 | # Save out the notice. There's a little bit of work here to give it the |
| 525 | # right whitespace, which assumes that the notice is automatically indented |
| 526 | # by 4 by minidom. |
| 527 | if self.notice: |
| 528 | notice_element = root.appendChild(doc.createElement('notice')) |
| 529 | notice_lines = self.notice.splitlines() |
David Pursehouse | 54a4e60 | 2020-02-12 14:31:05 +0900 | [diff] [blame] | 530 | indented_notice = ('\n'.join(" " * 4 + line for line in notice_lines))[4:] |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 531 | notice_element.appendChild(doc.createTextNode(indented_notice)) |
| 532 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 533 | d = self.default |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 534 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 535 | for r in sorted(self.remotes): |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 536 | self._RemoteToXml(self.remotes[r], doc, root) |
| 537 | if self.remotes: |
| 538 | root.appendChild(doc.createTextNode('')) |
| 539 | |
| 540 | have_default = False |
| 541 | e = doc.createElement('default') |
| 542 | if d.remote: |
| 543 | have_default = True |
| 544 | e.setAttribute('remote', d.remote.name) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 545 | if d.revisionExpr: |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 546 | have_default = True |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 547 | e.setAttribute('revision', d.revisionExpr) |
Simon Ruggier | 7e59de2 | 2015-07-24 12:50:06 +0200 | [diff] [blame] | 548 | if d.destBranchExpr: |
| 549 | have_default = True |
| 550 | e.setAttribute('dest-branch', d.destBranchExpr) |
Nasser Grainawi | da40341 | 2018-05-04 12:53:29 -0600 | [diff] [blame] | 551 | if d.upstreamExpr: |
| 552 | have_default = True |
| 553 | e.setAttribute('upstream', d.upstreamExpr) |
Mike Frysinger | 355f439 | 2022-07-20 17:15:29 -0400 | [diff] [blame] | 554 | if d.sync_j is not None: |
Shawn O. Pearce | 6392c87 | 2011-09-22 17:44:31 -0700 | [diff] [blame] | 555 | have_default = True |
| 556 | e.setAttribute('sync-j', '%d' % d.sync_j) |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 557 | if d.sync_c: |
| 558 | have_default = True |
| 559 | e.setAttribute('sync-c', 'true') |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 560 | if d.sync_s: |
| 561 | have_default = True |
| 562 | e.setAttribute('sync-s', 'true') |
YOUNG HO CHA | a32c92c | 2018-02-14 16:57:31 +0900 | [diff] [blame] | 563 | if not d.sync_tags: |
| 564 | have_default = True |
| 565 | e.setAttribute('sync-tags', 'false') |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 566 | if have_default: |
| 567 | root.appendChild(e) |
| 568 | root.appendChild(doc.createTextNode('')) |
| 569 | |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 570 | if self._manifest_server: |
| 571 | e = doc.createElement('manifest-server') |
| 572 | e.setAttribute('url', self._manifest_server) |
| 573 | root.appendChild(e) |
| 574 | root.appendChild(doc.createTextNode('')) |
| 575 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 576 | for r in sorted(self.submanifests): |
| 577 | self._SubmanifestToXml(self.submanifests[r], doc, root) |
| 578 | if self.submanifests: |
| 579 | root.appendChild(doc.createTextNode('')) |
| 580 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 581 | def output_projects(parent, parent_node, projects): |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 582 | for project_name in projects: |
| 583 | for project in self._projects[project_name]: |
| 584 | output_project(parent, parent_node, project) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 585 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 586 | def output_project(parent, parent_node, p): |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 587 | if not p.MatchesGroups(groups): |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 588 | return |
| 589 | |
LaMont Jones | a8cf575 | 2022-07-15 20:31:33 +0000 | [diff] [blame] | 590 | if omit_local and self.IsFromLocalManifest(p): |
| 591 | return |
| 592 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 593 | name = p.name |
| 594 | relpath = p.relpath |
| 595 | if parent: |
| 596 | name = self._UnjoinName(parent.name, name) |
| 597 | relpath = self._UnjoinRelpath(parent.relpath, relpath) |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 598 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 599 | e = doc.createElement('project') |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 600 | parent_node.appendChild(e) |
| 601 | e.setAttribute('name', name) |
| 602 | if relpath != name: |
| 603 | e.setAttribute('path', relpath) |
Conley Owens | a17d7af | 2013-10-16 14:38:09 -0700 | [diff] [blame] | 604 | remoteName = None |
| 605 | if d.remote: |
Dan Willemsen | 96c2d65 | 2016-04-06 16:03:54 -0700 | [diff] [blame] | 606 | remoteName = d.remote.name |
| 607 | if not d.remote or p.remote.orig_name != remoteName: |
| 608 | remoteName = p.remote.orig_name |
Anthony King | 36ea2fb | 2014-05-06 11:54:01 +0100 | [diff] [blame] | 609 | e.setAttribute('remote', remoteName) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 610 | if peg_rev: |
| 611 | if self.IsMirror: |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 612 | value = p.bare_git.rev_parse(p.revisionExpr + '^0') |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 613 | else: |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 614 | value = p.work_git.rev_parse(HEAD + '^0') |
| 615 | e.setAttribute('revision', value) |
Conley Owens | 551dfec | 2015-07-10 14:54:54 -0700 | [diff] [blame] | 616 | if peg_rev_upstream: |
| 617 | if p.upstream: |
| 618 | e.setAttribute('upstream', p.upstream) |
| 619 | elif value != p.revisionExpr: |
| 620 | # Only save the origin if the origin is not a sha1, and the default |
| 621 | # isn't our value |
| 622 | e.setAttribute('upstream', p.revisionExpr) |
Sean McAllister | af908cb | 2020-04-20 08:41:58 -0600 | [diff] [blame] | 623 | |
| 624 | if peg_rev_dest_branch: |
| 625 | if p.dest_branch: |
| 626 | e.setAttribute('dest-branch', p.dest_branch) |
| 627 | elif value != p.revisionExpr: |
| 628 | e.setAttribute('dest-branch', p.revisionExpr) |
| 629 | |
Anthony King | 36ea2fb | 2014-05-06 11:54:01 +0100 | [diff] [blame] | 630 | else: |
Dan Willemsen | 96c2d65 | 2016-04-06 16:03:54 -0700 | [diff] [blame] | 631 | revision = self.remotes[p.remote.orig_name].revision or d.revisionExpr |
Anthony King | 36ea2fb | 2014-05-06 11:54:01 +0100 | [diff] [blame] | 632 | if not revision or revision != p.revisionExpr: |
| 633 | e.setAttribute('revision', p.revisionExpr) |
Raman Tenneti | b5c5a5e | 2021-02-06 09:44:15 -0800 | [diff] [blame] | 634 | elif p.revisionId: |
| 635 | e.setAttribute('revision', p.revisionId) |
Nasser Grainawi | da40341 | 2018-05-04 12:53:29 -0600 | [diff] [blame] | 636 | if (p.upstream and (p.upstream != p.revisionExpr or |
| 637 | p.upstream != d.upstreamExpr)): |
Mani Chandel | 7a91d51 | 2014-07-24 16:27:08 +0530 | [diff] [blame] | 638 | e.setAttribute('upstream', p.upstream) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 639 | |
Simon Ruggier | 7e59de2 | 2015-07-24 12:50:06 +0200 | [diff] [blame] | 640 | if p.dest_branch and p.dest_branch != d.destBranchExpr: |
| 641 | e.setAttribute('dest-branch', p.dest_branch) |
| 642 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 643 | for c in p.copyfiles: |
| 644 | ce = doc.createElement('copyfile') |
| 645 | ce.setAttribute('src', c.src) |
| 646 | ce.setAttribute('dest', c.dest) |
| 647 | e.appendChild(ce) |
| 648 | |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 649 | for l in p.linkfiles: |
| 650 | le = doc.createElement('linkfile') |
| 651 | le.setAttribute('src', l.src) |
| 652 | le.setAttribute('dest', l.dest) |
| 653 | e.appendChild(le) |
| 654 | |
Conley Owens | bb1b5f5 | 2012-08-13 13:11:18 -0700 | [diff] [blame] | 655 | default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath] |
Dmitry Fink | 17f85ea | 2012-08-06 14:52:29 -0700 | [diff] [blame] | 656 | egroups = [g for g in p.groups if g not in default_groups] |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 657 | if egroups: |
| 658 | e.setAttribute('groups', ','.join(egroups)) |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 659 | |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 660 | for a in p.annotations: |
| 661 | if a.keep == "true": |
| 662 | ae = doc.createElement('annotation') |
| 663 | ae.setAttribute('name', a.name) |
| 664 | ae.setAttribute('value', a.value) |
| 665 | e.appendChild(ae) |
| 666 | |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 667 | if p.sync_c: |
| 668 | e.setAttribute('sync-c', 'true') |
| 669 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 670 | if p.sync_s: |
| 671 | e.setAttribute('sync-s', 'true') |
| 672 | |
YOUNG HO CHA | a32c92c | 2018-02-14 16:57:31 +0900 | [diff] [blame] | 673 | if not p.sync_tags: |
| 674 | e.setAttribute('sync-tags', 'false') |
| 675 | |
Dan Willemsen | 8840922 | 2015-08-17 15:29:10 -0700 | [diff] [blame] | 676 | if p.clone_depth: |
| 677 | e.setAttribute('clone-depth', str(p.clone_depth)) |
| 678 | |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 679 | self._output_manifest_project_extras(p, e) |
| 680 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 681 | if p.subprojects: |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 682 | subprojects = set(subp.name for subp in p.subprojects) |
| 683 | output_projects(p, e, list(sorted(subprojects))) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 684 | |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 685 | projects = set(p.name for p in self._paths.values() if not p.parent) |
| 686 | output_projects(None, root, list(sorted(projects))) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 687 | |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 688 | if self._repo_hooks_project: |
| 689 | root.appendChild(doc.createTextNode('')) |
| 690 | e = doc.createElement('repo-hooks') |
| 691 | e.setAttribute('in-project', self._repo_hooks_project.name) |
| 692 | e.setAttribute('enabled-list', |
| 693 | ' '.join(self._repo_hooks_project.enabled_repo_hooks)) |
| 694 | root.appendChild(e) |
| 695 | |
Raman Tenneti | 1bb4fb2 | 2021-01-07 16:50:45 -0800 | [diff] [blame] | 696 | if self._superproject: |
| 697 | root.appendChild(doc.createTextNode('')) |
| 698 | e = doc.createElement('superproject') |
LaMont Jones | d56e2eb | 2022-04-07 18:14:46 +0000 | [diff] [blame] | 699 | e.setAttribute('name', self._superproject.name) |
Raman Tenneti | 1bb4fb2 | 2021-01-07 16:50:45 -0800 | [diff] [blame] | 700 | remoteName = None |
| 701 | if d.remote: |
| 702 | remoteName = d.remote.name |
LaMont Jones | d56e2eb | 2022-04-07 18:14:46 +0000 | [diff] [blame] | 703 | remote = self._superproject.remote |
Raman Tenneti | 1bb4fb2 | 2021-01-07 16:50:45 -0800 | [diff] [blame] | 704 | if not d.remote or remote.orig_name != remoteName: |
| 705 | remoteName = remote.orig_name |
| 706 | e.setAttribute('remote', remoteName) |
Xin Li | e0b16a2 | 2021-09-26 23:20:32 -0700 | [diff] [blame] | 707 | revision = remote.revision or d.revisionExpr |
LaMont Jones | d56e2eb | 2022-04-07 18:14:46 +0000 | [diff] [blame] | 708 | if not revision or revision != self._superproject.revision: |
| 709 | e.setAttribute('revision', self._superproject.revision) |
Raman Tenneti | 1bb4fb2 | 2021-01-07 16:50:45 -0800 | [diff] [blame] | 710 | root.appendChild(e) |
| 711 | |
Raman Tenneti | 993af5e | 2021-05-12 12:00:31 -0700 | [diff] [blame] | 712 | if self._contactinfo.bugurl != Wrapper().BUG_URL: |
Raman Tenneti | 1c3f57e | 2021-05-04 12:32:13 -0700 | [diff] [blame] | 713 | root.appendChild(doc.createTextNode('')) |
| 714 | e = doc.createElement('contactinfo') |
Raman Tenneti | 993af5e | 2021-05-12 12:00:31 -0700 | [diff] [blame] | 715 | e.setAttribute('bugurl', self._contactinfo.bugurl) |
Raman Tenneti | 1c3f57e | 2021-05-04 12:32:13 -0700 | [diff] [blame] | 716 | root.appendChild(e) |
| 717 | |
Mike Frysinger | 23411d3 | 2020-09-02 04:31:10 -0400 | [diff] [blame] | 718 | return doc |
| 719 | |
| 720 | def ToDict(self, **kwargs): |
| 721 | """Return the current manifest as a dictionary.""" |
| 722 | # Elements that may only appear once. |
| 723 | SINGLE_ELEMENTS = { |
| 724 | 'notice', |
| 725 | 'default', |
| 726 | 'manifest-server', |
| 727 | 'repo-hooks', |
Raman Tenneti | 1bb4fb2 | 2021-01-07 16:50:45 -0800 | [diff] [blame] | 728 | 'superproject', |
Raman Tenneti | 1c3f57e | 2021-05-04 12:32:13 -0700 | [diff] [blame] | 729 | 'contactinfo', |
Mike Frysinger | 23411d3 | 2020-09-02 04:31:10 -0400 | [diff] [blame] | 730 | } |
| 731 | # Elements that may be repeated. |
| 732 | MULTI_ELEMENTS = { |
| 733 | 'remote', |
| 734 | 'remove-project', |
| 735 | 'project', |
| 736 | 'extend-project', |
| 737 | 'include', |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 738 | 'submanifest', |
Mike Frysinger | 23411d3 | 2020-09-02 04:31:10 -0400 | [diff] [blame] | 739 | # These are children of 'project' nodes. |
| 740 | 'annotation', |
| 741 | 'project', |
| 742 | 'copyfile', |
| 743 | 'linkfile', |
| 744 | } |
| 745 | |
| 746 | doc = self.ToXml(**kwargs) |
| 747 | ret = {} |
| 748 | |
| 749 | def append_children(ret, node): |
| 750 | for child in node.childNodes: |
| 751 | if child.nodeType == xml.dom.Node.ELEMENT_NODE: |
| 752 | attrs = child.attributes |
| 753 | element = dict((attrs.item(i).localName, attrs.item(i).value) |
| 754 | for i in range(attrs.length)) |
| 755 | if child.nodeName in SINGLE_ELEMENTS: |
| 756 | ret[child.nodeName] = element |
| 757 | elif child.nodeName in MULTI_ELEMENTS: |
| 758 | ret.setdefault(child.nodeName, []).append(element) |
| 759 | else: |
| 760 | raise ManifestParseError('Unhandled element "%s"' % (child.nodeName,)) |
| 761 | |
| 762 | append_children(element, child) |
| 763 | |
| 764 | append_children(ret, doc.firstChild) |
| 765 | |
| 766 | return ret |
| 767 | |
| 768 | def Save(self, fd, **kwargs): |
| 769 | """Write the current manifest out to the given file descriptor.""" |
| 770 | doc = self.ToXml(**kwargs) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 771 | doc.writexml(fd, '', ' ', '\n', 'UTF-8') |
| 772 | |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 773 | def _output_manifest_project_extras(self, p, e): |
| 774 | """Manifests can modify e if they support extra project attributes.""" |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 775 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 776 | @property |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 777 | def is_multimanifest(self): |
Mike Frysinger | 1d00a7e | 2021-12-21 00:40:31 -0500 | [diff] [blame] | 778 | """Whether this is a multimanifest checkout. |
| 779 | |
| 780 | This is safe to use as long as the outermost manifest XML has been parsed. |
| 781 | """ |
| 782 | return bool(self._outer_client._submanifests) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 783 | |
| 784 | @property |
| 785 | def is_submanifest(self): |
LaMont Jones | ff6b1da | 2022-06-01 21:03:34 +0000 | [diff] [blame] | 786 | """Whether this manifest is a submanifest. |
| 787 | |
| 788 | This is safe to use as long as the outermost manifest XML has been parsed. |
| 789 | """ |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 790 | return self._outer_client and self._outer_client != self |
| 791 | |
| 792 | @property |
| 793 | def outer_client(self): |
LaMont Jones | ff6b1da | 2022-06-01 21:03:34 +0000 | [diff] [blame] | 794 | """The instance of the outermost manifest client.""" |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 795 | self._Load() |
| 796 | return self._outer_client |
| 797 | |
| 798 | @property |
| 799 | def all_manifests(self): |
LaMont Jones | ff6b1da | 2022-06-01 21:03:34 +0000 | [diff] [blame] | 800 | """Generator yielding all (sub)manifests, in depth-first order.""" |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 801 | self._Load() |
| 802 | outer = self._outer_client |
| 803 | yield outer |
| 804 | for tree in outer.all_children: |
| 805 | yield tree |
| 806 | |
| 807 | @property |
| 808 | def all_children(self): |
LaMont Jones | ff6b1da | 2022-06-01 21:03:34 +0000 | [diff] [blame] | 809 | """Generator yielding all (present) child submanifests.""" |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 810 | self._Load() |
| 811 | for child in self._submanifests.values(): |
| 812 | if child.repo_client: |
| 813 | yield child.repo_client |
| 814 | for tree in child.repo_client.all_children: |
| 815 | yield tree |
| 816 | |
| 817 | @property |
| 818 | def path_prefix(self): |
| 819 | """The path of this submanifest, relative to the outermost manifest.""" |
| 820 | if not self._outer_client or self == self._outer_client: |
| 821 | return '' |
| 822 | return os.path.relpath(self.topdir, self._outer_client.topdir) |
| 823 | |
| 824 | @property |
| 825 | def all_paths(self): |
LaMont Jones | ff6b1da | 2022-06-01 21:03:34 +0000 | [diff] [blame] | 826 | """All project paths for all (sub)manifests. |
| 827 | |
| 828 | See also `paths`. |
| 829 | |
| 830 | Returns: |
| 831 | A dictionary of {path: Project()}. `path` is relative to the outer |
| 832 | manifest. |
| 833 | """ |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 834 | ret = {} |
| 835 | for tree in self.all_manifests: |
| 836 | prefix = tree.path_prefix |
| 837 | ret.update({os.path.join(prefix, k): v for k, v in tree.paths.items()}) |
| 838 | return ret |
| 839 | |
| 840 | @property |
| 841 | def all_projects(self): |
| 842 | """All projects for all (sub)manifests. See `projects`.""" |
| 843 | return list(itertools.chain.from_iterable(x._paths.values() for x in self.all_manifests)) |
| 844 | |
| 845 | @property |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 846 | def paths(self): |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 847 | """Return all paths for this manifest. |
| 848 | |
LaMont Jones | ff6b1da | 2022-06-01 21:03:34 +0000 | [diff] [blame] | 849 | Returns: |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 850 | A dictionary of {path: Project()}. `path` is relative to this manifest. |
| 851 | """ |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 852 | self._Load() |
| 853 | return self._paths |
| 854 | |
| 855 | @property |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 856 | def projects(self): |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 857 | """Return a list of all Projects in this manifest.""" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 858 | self._Load() |
Anthony King | d58bfe5 | 2014-05-05 23:30:49 +0100 | [diff] [blame] | 859 | return list(self._paths.values()) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 860 | |
| 861 | @property |
| 862 | def remotes(self): |
LaMont Jones | ff6b1da | 2022-06-01 21:03:34 +0000 | [diff] [blame] | 863 | """Return a list of remotes for this manifest.""" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 864 | self._Load() |
| 865 | return self._remotes |
| 866 | |
| 867 | @property |
| 868 | def default(self): |
LaMont Jones | ff6b1da | 2022-06-01 21:03:34 +0000 | [diff] [blame] | 869 | """Return default values for this manifest.""" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 870 | self._Load() |
| 871 | return self._default |
| 872 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 873 | @property |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 874 | def submanifests(self): |
| 875 | """All submanifests in this manifest.""" |
| 876 | self._Load() |
| 877 | return self._submanifests |
| 878 | |
| 879 | @property |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 880 | def repo_hooks_project(self): |
| 881 | self._Load() |
| 882 | return self._repo_hooks_project |
| 883 | |
| 884 | @property |
Raman Tenneti | 1bb4fb2 | 2021-01-07 16:50:45 -0800 | [diff] [blame] | 885 | def superproject(self): |
| 886 | self._Load() |
| 887 | return self._superproject |
| 888 | |
| 889 | @property |
Raman Tenneti | 1c3f57e | 2021-05-04 12:32:13 -0700 | [diff] [blame] | 890 | def contactinfo(self): |
| 891 | self._Load() |
| 892 | return self._contactinfo |
| 893 | |
| 894 | @property |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 895 | def notice(self): |
| 896 | self._Load() |
| 897 | return self._notice |
| 898 | |
| 899 | @property |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 900 | def manifest_server(self): |
| 901 | self._Load() |
Shawn O. Pearce | 34fb20f | 2011-11-30 13:41:02 -0800 | [diff] [blame] | 902 | return self._manifest_server |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 903 | |
| 904 | @property |
Xin Li | d79a4bc | 2020-05-20 16:03:45 -0700 | [diff] [blame] | 905 | def CloneBundle(self): |
LaMont Jones | d82be3e | 2022-04-05 19:30:46 +0000 | [diff] [blame] | 906 | clone_bundle = self.manifestProject.clone_bundle |
Xin Li | d79a4bc | 2020-05-20 16:03:45 -0700 | [diff] [blame] | 907 | if clone_bundle is None: |
LaMont Jones | d82be3e | 2022-04-05 19:30:46 +0000 | [diff] [blame] | 908 | return False if self.manifestProject.partial_clone else True |
Xin Li | d79a4bc | 2020-05-20 16:03:45 -0700 | [diff] [blame] | 909 | else: |
| 910 | return clone_bundle |
| 911 | |
| 912 | @property |
Xin Li | 745be2e | 2019-06-03 11:24:30 -0700 | [diff] [blame] | 913 | def CloneFilter(self): |
LaMont Jones | d82be3e | 2022-04-05 19:30:46 +0000 | [diff] [blame] | 914 | if self.manifestProject.partial_clone: |
| 915 | return self.manifestProject.clone_filter |
Xin Li | 745be2e | 2019-06-03 11:24:30 -0700 | [diff] [blame] | 916 | return None |
| 917 | |
| 918 | @property |
Raman Tenneti | f32f243 | 2021-04-12 20:57:25 -0700 | [diff] [blame] | 919 | def PartialCloneExclude(self): |
LaMont Jones | d82be3e | 2022-04-05 19:30:46 +0000 | [diff] [blame] | 920 | exclude = self.manifest.manifestProject.partial_clone_exclude or '' |
Raman Tenneti | f32f243 | 2021-04-12 20:57:25 -0700 | [diff] [blame] | 921 | return set(x.strip() for x in exclude.split(',')) |
| 922 | |
LaMont Jones | b90a422 | 2022-04-14 15:00:09 +0000 | [diff] [blame] | 923 | def SetManifestOverride(self, path): |
| 924 | """Override manifestFile. The caller must call Unload()""" |
| 925 | self._outer_client.manifest.manifestFileOverrides[self.path_prefix] = path |
| 926 | |
Raman Tenneti | f32f243 | 2021-04-12 20:57:25 -0700 | [diff] [blame] | 927 | @property |
Michael Kelly | c34b91c | 2021-07-02 09:25:48 -0700 | [diff] [blame] | 928 | def UseLocalManifests(self): |
| 929 | return self._load_local_manifests |
| 930 | |
| 931 | def SetUseLocalManifests(self, value): |
| 932 | self._load_local_manifests = value |
| 933 | |
| 934 | @property |
Raman Tenneti | feb2891 | 2021-05-02 19:47:29 -0700 | [diff] [blame] | 935 | def HasLocalManifests(self): |
| 936 | return self._load_local_manifests and self.local_manifests |
| 937 | |
LaMont Jones | 87cce68 | 2022-02-14 17:48:31 +0000 | [diff] [blame] | 938 | def IsFromLocalManifest(self, project): |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 939 | """Is the project from a local manifest?""" |
LaMont Jones | 87cce68 | 2022-02-14 17:48:31 +0000 | [diff] [blame] | 940 | return any(x.startswith(LOCAL_MANIFEST_GROUP_PREFIX) |
| 941 | for x in project.groups) |
| 942 | |
Raman Tenneti | feb2891 | 2021-05-02 19:47:29 -0700 | [diff] [blame] | 943 | @property |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 944 | def IsMirror(self): |
LaMont Jones | d82be3e | 2022-04-05 19:30:46 +0000 | [diff] [blame] | 945 | return self.manifestProject.mirror |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 946 | |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 947 | @property |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 948 | def UseGitWorktrees(self): |
LaMont Jones | d82be3e | 2022-04-05 19:30:46 +0000 | [diff] [blame] | 949 | return self.manifestProject.use_worktree |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 950 | |
| 951 | @property |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 952 | def IsArchive(self): |
LaMont Jones | d82be3e | 2022-04-05 19:30:46 +0000 | [diff] [blame] | 953 | return self.manifestProject.archive |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 954 | |
Martin Kelly | e4e94d2 | 2017-03-21 16:05:12 -0700 | [diff] [blame] | 955 | @property |
| 956 | def HasSubmodules(self): |
LaMont Jones | d82be3e | 2022-04-05 19:30:46 +0000 | [diff] [blame] | 957 | return self.manifestProject.submodules |
Martin Kelly | e4e94d2 | 2017-03-21 16:05:12 -0700 | [diff] [blame] | 958 | |
XD Trol | 630876f | 2022-01-17 23:29:04 +0800 | [diff] [blame] | 959 | @property |
| 960 | def EnableGitLfs(self): |
LaMont Jones | d82be3e | 2022-04-05 19:30:46 +0000 | [diff] [blame] | 961 | return self.manifestProject.git_lfs |
XD Trol | 630876f | 2022-01-17 23:29:04 +0800 | [diff] [blame] | 962 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 963 | def FindManifestByPath(self, path): |
| 964 | """Returns the manifest containing path.""" |
| 965 | path = os.path.abspath(path) |
| 966 | manifest = self._outer_client or self |
| 967 | old = None |
| 968 | while manifest._submanifests and manifest != old: |
| 969 | old = manifest |
| 970 | for name in manifest._submanifests: |
| 971 | tree = manifest._submanifests[name] |
| 972 | if path.startswith(tree.repo_client.manifest.topdir): |
| 973 | manifest = tree.repo_client |
| 974 | break |
| 975 | return manifest |
| 976 | |
| 977 | @property |
| 978 | def subdir(self): |
| 979 | """Returns the path for per-submanifest objects for this manifest.""" |
| 980 | return self.SubmanifestInfoDir(self.path_prefix) |
| 981 | |
| 982 | def SubmanifestInfoDir(self, submanifest_path, object_path=''): |
| 983 | """Return the path to submanifest-specific info for a submanifest. |
| 984 | |
| 985 | Return the full path of the directory in which to put per-manifest objects. |
| 986 | |
| 987 | Args: |
| 988 | submanifest_path: a string, the path of the submanifest, relative to the |
| 989 | outermost topdir. If empty, then repodir is returned. |
| 990 | object_path: a string, relative path to append to the submanifest info |
| 991 | directory path. |
| 992 | """ |
| 993 | if submanifest_path: |
| 994 | return os.path.join(self.repodir, SUBMANIFEST_DIR, submanifest_path, |
| 995 | object_path) |
| 996 | else: |
| 997 | return os.path.join(self.repodir, object_path) |
| 998 | |
| 999 | def SubmanifestProject(self, submanifest_path): |
| 1000 | """Return a manifestProject for a submanifest.""" |
| 1001 | subdir = self.SubmanifestInfoDir(submanifest_path) |
LaMont Jones | 9b72cf2 | 2022-03-29 21:54:22 +0000 | [diff] [blame] | 1002 | mp = ManifestProject(self, 'manifests', |
| 1003 | gitdir=os.path.join(subdir, 'manifests.git'), |
| 1004 | worktree=os.path.join(subdir, 'manifests')) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1005 | return mp |
| 1006 | |
LaMont Jones | 501733c | 2022-04-20 16:42:32 +0000 | [diff] [blame] | 1007 | def GetDefaultGroupsStr(self, with_platform=True): |
| 1008 | """Returns the default group string to use. |
| 1009 | |
| 1010 | Args: |
| 1011 | with_platform: a boolean, whether to include the group for the |
| 1012 | underlying platform. |
| 1013 | """ |
| 1014 | groups = ','.join(self.default_groups or ['default']) |
| 1015 | if with_platform: |
| 1016 | groups += f',platform-{platform.system().lower()}' |
| 1017 | return groups |
Raman Tenneti | 080877e | 2021-03-09 15:19:06 -0800 | [diff] [blame] | 1018 | |
| 1019 | def GetGroupsStr(self): |
| 1020 | """Returns the manifest group string that should be synced.""" |
LaMont Jones | 501733c | 2022-04-20 16:42:32 +0000 | [diff] [blame] | 1021 | return self.manifestProject.manifest_groups or self.GetDefaultGroupsStr() |
Raman Tenneti | 080877e | 2021-03-09 15:19:06 -0800 | [diff] [blame] | 1022 | |
LaMont Jones | a2ff20d | 2022-04-07 16:49:06 +0000 | [diff] [blame] | 1023 | def Unload(self): |
| 1024 | """Unload the manifest. |
| 1025 | |
| 1026 | If the manifest files have been changed since Load() was called, this will |
| 1027 | cause the new/updated manifest to be used. |
| 1028 | |
| 1029 | """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1030 | self._loaded = False |
| 1031 | self._projects = {} |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1032 | self._paths = {} |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1033 | self._remotes = {} |
| 1034 | self._default = None |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1035 | self._submanifests = {} |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 1036 | self._repo_hooks_project = None |
LaMont Jones | d56e2eb | 2022-04-07 18:14:46 +0000 | [diff] [blame] | 1037 | self._superproject = None |
Raman Tenneti | 993af5e | 2021-05-12 12:00:31 -0700 | [diff] [blame] | 1038 | self._contactinfo = ContactInfo(Wrapper().BUG_URL) |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 1039 | self._notice = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1040 | self.branch = None |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 1041 | self._manifest_server = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1042 | |
LaMont Jones | a2ff20d | 2022-04-07 16:49:06 +0000 | [diff] [blame] | 1043 | def Load(self): |
| 1044 | """Read the manifest into memory.""" |
| 1045 | # Do not expose internal arguments. |
| 1046 | self._Load() |
| 1047 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1048 | def _Load(self, initial_client=None, submanifest_depth=0): |
| 1049 | if submanifest_depth > MAX_SUBMANIFEST_DEPTH: |
| 1050 | raise ManifestParseError('maximum submanifest depth %d exceeded.' % |
| 1051 | MAX_SUBMANIFEST_DEPTH) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1052 | if not self._loaded: |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1053 | if self._outer_client and self._outer_client != self: |
| 1054 | # This will load all clients. |
| 1055 | self._outer_client._Load(initial_client=self) |
| 1056 | |
LaMont Jones | b90a422 | 2022-04-14 15:00:09 +0000 | [diff] [blame] | 1057 | savedManifestFile = self.manifestFile |
| 1058 | override = self._outer_client.manifestFileOverrides.get(self.path_prefix) |
| 1059 | if override: |
| 1060 | self.manifestFile = override |
David Pursehouse | 2d5a0df | 2012-11-13 02:50:36 +0900 | [diff] [blame] | 1061 | |
Joe Onorato | 26e2475 | 2013-01-11 12:35:53 -0800 | [diff] [blame] | 1062 | try: |
LaMont Jones | b90a422 | 2022-04-14 15:00:09 +0000 | [diff] [blame] | 1063 | m = self.manifestProject |
| 1064 | b = m.GetBranch(m.CurrentBranch).merge |
| 1065 | if b is not None and b.startswith(R_HEADS): |
| 1066 | b = b[len(R_HEADS):] |
| 1067 | self.branch = b |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 1068 | |
LaMont Jones | b90a422 | 2022-04-14 15:00:09 +0000 | [diff] [blame] | 1069 | parent_groups = self.parent_groups |
| 1070 | if self.path_prefix: |
| 1071 | parent_groups = f'{SUBMANIFEST_GROUP_PREFIX}:path:{self.path_prefix},{parent_groups}' |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 1072 | |
LaMont Jones | b90a422 | 2022-04-14 15:00:09 +0000 | [diff] [blame] | 1073 | # The manifestFile was specified by the user which is why we allow include |
| 1074 | # paths to point anywhere. |
| 1075 | nodes = [] |
| 1076 | nodes.append(self._ParseManifestXml( |
| 1077 | self.manifestFile, self.manifestProject.worktree, |
| 1078 | parent_groups=parent_groups, restrict_includes=False)) |
| 1079 | |
| 1080 | if self._load_local_manifests and self.local_manifests: |
| 1081 | try: |
| 1082 | for local_file in sorted(platform_utils.listdir(self.local_manifests)): |
| 1083 | if local_file.endswith('.xml'): |
| 1084 | local = os.path.join(self.local_manifests, local_file) |
| 1085 | # Since local manifests are entirely managed by the user, allow |
| 1086 | # them to point anywhere the user wants. |
| 1087 | local_group = f'{LOCAL_MANIFEST_GROUP_PREFIX}:{local_file[:-4]}' |
| 1088 | nodes.append(self._ParseManifestXml( |
| 1089 | local, self.subdir, |
| 1090 | parent_groups=f'{local_group},{parent_groups}', |
| 1091 | restrict_includes=False)) |
| 1092 | except OSError: |
| 1093 | pass |
| 1094 | |
| 1095 | try: |
| 1096 | self._ParseManifest(nodes) |
| 1097 | except ManifestParseError as e: |
| 1098 | # There was a problem parsing, unload ourselves in case they catch |
| 1099 | # this error and try again later, we will show the correct error |
| 1100 | self.Unload() |
| 1101 | raise e |
| 1102 | |
| 1103 | if self.IsMirror: |
| 1104 | self._AddMetaProjectMirror(self.repoProject) |
| 1105 | self._AddMetaProjectMirror(self.manifestProject) |
| 1106 | |
| 1107 | self._loaded = True |
| 1108 | finally: |
| 1109 | if override: |
| 1110 | self.manifestFile = savedManifestFile |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1111 | |
LaMont Jones | ff6b1da | 2022-06-01 21:03:34 +0000 | [diff] [blame] | 1112 | # Now that we have loaded this manifest, load any submanifests as well. |
| 1113 | # We need to do this after self._loaded is set to avoid looping. |
LaMont Jones | d56e2eb | 2022-04-07 18:14:46 +0000 | [diff] [blame] | 1114 | for name in self._submanifests: |
| 1115 | tree = self._submanifests[name] |
LaMont Jones | b90a422 | 2022-04-14 15:00:09 +0000 | [diff] [blame] | 1116 | spec = tree.ToSubmanifestSpec() |
LaMont Jones | d56e2eb | 2022-04-07 18:14:46 +0000 | [diff] [blame] | 1117 | present = os.path.exists(os.path.join(self.subdir, MANIFEST_FILE_NAME)) |
| 1118 | if present and tree.present and not tree.repo_client: |
| 1119 | if initial_client and initial_client.topdir == self.topdir: |
| 1120 | tree.repo_client = self |
| 1121 | tree.present = present |
| 1122 | elif not os.path.exists(self.subdir): |
| 1123 | tree.present = False |
| 1124 | if present and tree.present: |
| 1125 | tree.repo_client._Load(initial_client=initial_client, |
| 1126 | submanifest_depth=submanifest_depth + 1) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1127 | |
Mike Frysinger | 5413397 | 2021-03-01 21:38:08 -0500 | [diff] [blame] | 1128 | def _ParseManifestXml(self, path, include_root, parent_groups='', |
| 1129 | restrict_includes=True): |
| 1130 | """Parse a manifest XML and return the computed nodes. |
| 1131 | |
| 1132 | Args: |
| 1133 | path: The XML file to read & parse. |
| 1134 | include_root: The path to interpret include "name"s relative to. |
| 1135 | parent_groups: The groups to apply to this projects. |
| 1136 | restrict_includes: Whether to constrain the "name" attribute of includes. |
| 1137 | |
| 1138 | Returns: |
| 1139 | List of XML nodes. |
| 1140 | """ |
David Pursehouse | f7fc8a9 | 2012-11-13 04:00:28 +0900 | [diff] [blame] | 1141 | try: |
| 1142 | root = xml.dom.minidom.parse(path) |
David Pursehouse | 2d5a0df | 2012-11-13 02:50:36 +0900 | [diff] [blame] | 1143 | except (OSError, xml.parsers.expat.ExpatError) as e: |
David Pursehouse | f7fc8a9 | 2012-11-13 04:00:28 +0900 | [diff] [blame] | 1144 | raise ManifestParseError("error parsing manifest %s: %s" % (path, e)) |
| 1145 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1146 | if not root or not root.childNodes: |
Brian Harring | 2644874 | 2011-04-28 05:04:41 -0700 | [diff] [blame] | 1147 | raise ManifestParseError("no root node in %s" % (path,)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1148 | |
Jooncheol Park | 34acdd2 | 2012-08-27 02:25:59 +0900 | [diff] [blame] | 1149 | for manifest in root.childNodes: |
| 1150 | if manifest.nodeName == 'manifest': |
| 1151 | break |
| 1152 | else: |
Brian Harring | 2644874 | 2011-04-28 05:04:41 -0700 | [diff] [blame] | 1153 | raise ManifestParseError("no <manifest> in %s" % (path,)) |
| 1154 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 1155 | nodes = [] |
David Pursehouse | 65b0ba5 | 2018-06-24 16:21:51 +0900 | [diff] [blame] | 1156 | for node in manifest.childNodes: |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 1157 | if node.nodeName == 'include': |
| 1158 | name = self._reqatt(node, 'name') |
Mike Frysinger | 5413397 | 2021-03-01 21:38:08 -0500 | [diff] [blame] | 1159 | if restrict_includes: |
| 1160 | msg = self._CheckLocalPath(name) |
| 1161 | if msg: |
| 1162 | raise ManifestInvalidPathError( |
| 1163 | '<include> invalid "name": %s: %s' % (name, msg)) |
Fredrik de Groot | 352c93b | 2020-10-06 12:55:14 +0200 | [diff] [blame] | 1164 | include_groups = '' |
| 1165 | if parent_groups: |
| 1166 | include_groups = parent_groups |
| 1167 | if node.hasAttribute('groups'): |
| 1168 | include_groups = node.getAttribute('groups') + ',' + include_groups |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 1169 | fp = os.path.join(include_root, name) |
| 1170 | if not os.path.isfile(fp): |
Mike Frysinger | 5413397 | 2021-03-01 21:38:08 -0500 | [diff] [blame] | 1171 | raise ManifestParseError("include [%s/]%s doesn't exist or isn't a file" |
| 1172 | % (include_root, name)) |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 1173 | try: |
Fredrik de Groot | 352c93b | 2020-10-06 12:55:14 +0200 | [diff] [blame] | 1174 | nodes.extend(self._ParseManifestXml(fp, include_root, include_groups)) |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 1175 | # should isolate this to the exact exception, but that's |
| 1176 | # tricky. actual parsing implementation may vary. |
Mike Frysinger | 5413397 | 2021-03-01 21:38:08 -0500 | [diff] [blame] | 1177 | except (KeyboardInterrupt, RuntimeError, SystemExit, ManifestParseError): |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 1178 | raise |
| 1179 | except Exception as e: |
| 1180 | raise ManifestParseError( |
Mike Frysinger | ec558df | 2019-07-05 01:38:05 -0400 | [diff] [blame] | 1181 | "failed parsing included manifest %s: %s" % (name, e)) |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 1182 | else: |
Fredrik de Groot | 352c93b | 2020-10-06 12:55:14 +0200 | [diff] [blame] | 1183 | if parent_groups and node.nodeName == 'project': |
| 1184 | nodeGroups = parent_groups |
| 1185 | if node.hasAttribute('groups'): |
| 1186 | nodeGroups = node.getAttribute('groups') + ',' + nodeGroups |
| 1187 | node.setAttribute('groups', nodeGroups) |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 1188 | nodes.append(node) |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 1189 | return nodes |
Brian Harring | 2644874 | 2011-04-28 05:04:41 -0700 | [diff] [blame] | 1190 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 1191 | def _ParseManifest(self, node_list): |
| 1192 | for node in itertools.chain(*node_list): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1193 | if node.nodeName == 'remote': |
| 1194 | remote = self._ParseRemote(node) |
David Pursehouse | 717ece9 | 2012-11-13 08:49:16 +0900 | [diff] [blame] | 1195 | if remote: |
| 1196 | if remote.name in self._remotes: |
| 1197 | if remote != self._remotes[remote.name]: |
| 1198 | raise ManifestParseError( |
| 1199 | 'remote %s already exists with different attributes' % |
| 1200 | (remote.name)) |
| 1201 | else: |
| 1202 | self._remotes[remote.name] = remote |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1203 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 1204 | for node in itertools.chain(*node_list): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1205 | if node.nodeName == 'default': |
Julien Campergue | 7487992 | 2013-10-09 14:38:46 +0200 | [diff] [blame] | 1206 | new_default = self._ParseDefault(node) |
Jack Neus | b8c8448 | 2021-06-15 14:28:30 +0000 | [diff] [blame] | 1207 | emptyDefault = not node.hasAttributes() and not node.hasChildNodes() |
Julien Campergue | 7487992 | 2013-10-09 14:38:46 +0200 | [diff] [blame] | 1208 | if self._default is None: |
| 1209 | self._default = new_default |
Jack Neus | b8c8448 | 2021-06-15 14:28:30 +0000 | [diff] [blame] | 1210 | elif not emptyDefault and new_default != self._default: |
David Pursehouse | 37128b6 | 2013-10-15 10:48:40 +0900 | [diff] [blame] | 1211 | raise ManifestParseError('duplicate default in %s' % |
| 1212 | (self.manifestFile)) |
Julien Campergue | 7487992 | 2013-10-09 14:38:46 +0200 | [diff] [blame] | 1213 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1214 | if self._default is None: |
| 1215 | self._default = _Default() |
| 1216 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1217 | submanifest_paths = set() |
| 1218 | for node in itertools.chain(*node_list): |
| 1219 | if node.nodeName == 'submanifest': |
| 1220 | submanifest = self._ParseSubmanifest(node) |
| 1221 | if submanifest: |
| 1222 | if submanifest.name in self._submanifests: |
| 1223 | if submanifest != self._submanifests[submanifest.name]: |
| 1224 | raise ManifestParseError( |
| 1225 | 'submanifest %s already exists with different attributes' % |
| 1226 | (submanifest.name)) |
| 1227 | else: |
| 1228 | self._submanifests[submanifest.name] = submanifest |
| 1229 | submanifest_paths.add(submanifest.relpath) |
| 1230 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 1231 | for node in itertools.chain(*node_list): |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 1232 | if node.nodeName == 'notice': |
| 1233 | if self._notice is not None: |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 1234 | raise ManifestParseError( |
| 1235 | 'duplicate notice in %s' % |
| 1236 | (self.manifestFile)) |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 1237 | self._notice = self._ParseNotice(node) |
| 1238 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 1239 | for node in itertools.chain(*node_list): |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 1240 | if node.nodeName == 'manifest-server': |
| 1241 | url = self._reqatt(node, 'url') |
| 1242 | if self._manifest_server is not None: |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 1243 | raise ManifestParseError( |
| 1244 | 'duplicate manifest-server in %s' % |
| 1245 | (self.manifestFile)) |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 1246 | self._manifest_server = url |
| 1247 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1248 | def recursively_add_projects(project): |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1249 | projects = self._projects.setdefault(project.name, []) |
| 1250 | if project.relpath is None: |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1251 | raise ManifestParseError( |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1252 | 'missing path for %s in %s' % |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1253 | (project.name, self.manifestFile)) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1254 | if project.relpath in self._paths: |
| 1255 | raise ManifestParseError( |
| 1256 | 'duplicate path %s in %s' % |
| 1257 | (project.relpath, self.manifestFile)) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1258 | for tree in submanifest_paths: |
| 1259 | if project.relpath.startswith(tree): |
| 1260 | raise ManifestParseError( |
| 1261 | 'project %s conflicts with submanifest path %s' % |
| 1262 | (project.relpath, tree)) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1263 | self._paths[project.relpath] = project |
| 1264 | projects.append(project) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1265 | for subproject in project.subprojects: |
| 1266 | recursively_add_projects(subproject) |
| 1267 | |
Jack Neus | a84f43a | 2021-09-21 22:23:55 +0000 | [diff] [blame] | 1268 | repo_hooks_project = None |
| 1269 | enabled_repo_hooks = None |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 1270 | for node in itertools.chain(*node_list): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1271 | if node.nodeName == 'project': |
| 1272 | project = self._ParseProject(node) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1273 | recursively_add_projects(project) |
Josh Triplett | 884a387 | 2014-06-12 14:57:29 -0700 | [diff] [blame] | 1274 | if node.nodeName == 'extend-project': |
| 1275 | name = self._reqatt(node, 'name') |
| 1276 | |
| 1277 | if name not in self._projects: |
| 1278 | raise ManifestParseError('extend-project element specifies non-existent ' |
| 1279 | 'project: %s' % name) |
| 1280 | |
| 1281 | path = node.getAttribute('path') |
Michael Kelly | 37c21c2 | 2020-06-13 02:10:40 -0700 | [diff] [blame] | 1282 | dest_path = node.getAttribute('dest-path') |
Josh Triplett | 884a387 | 2014-06-12 14:57:29 -0700 | [diff] [blame] | 1283 | groups = node.getAttribute('groups') |
| 1284 | if groups: |
Mike Frysinger | 51e39d5 | 2020-12-04 05:32:06 -0500 | [diff] [blame] | 1285 | groups = self._ParseList(groups) |
Luis Hector Chavez | 7d52585 | 2018-03-15 09:54:08 -0700 | [diff] [blame] | 1286 | revision = node.getAttribute('revision') |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1287 | remote_name = node.getAttribute('remote') |
| 1288 | if not remote_name: |
| 1289 | remote = self._default.remote |
| 1290 | else: |
Kyunam Jo | bd0aae9 | 2020-02-04 11:38:53 +0900 | [diff] [blame] | 1291 | remote = self._get_remote(node) |
Erik Elmeke | 4cdfdb7 | 2022-09-09 17:13:17 +0200 | [diff] [blame] | 1292 | dest_branch = node.getAttribute('dest-branch') |
| 1293 | upstream = node.getAttribute('upstream') |
Josh Triplett | 884a387 | 2014-06-12 14:57:29 -0700 | [diff] [blame] | 1294 | |
Michael Kelly | 37c21c2 | 2020-06-13 02:10:40 -0700 | [diff] [blame] | 1295 | named_projects = self._projects[name] |
| 1296 | if dest_path and not path and len(named_projects) > 1: |
| 1297 | raise ManifestParseError('extend-project cannot use dest-path when ' |
| 1298 | 'matching multiple projects: %s' % name) |
Josh Triplett | 884a387 | 2014-06-12 14:57:29 -0700 | [diff] [blame] | 1299 | for p in self._projects[name]: |
| 1300 | if path and p.relpath != path: |
| 1301 | continue |
| 1302 | if groups: |
| 1303 | p.groups.extend(groups) |
Luis Hector Chavez | 7d52585 | 2018-03-15 09:54:08 -0700 | [diff] [blame] | 1304 | if revision: |
Michael Kelly | 2f3c331 | 2020-07-21 19:40:38 -0700 | [diff] [blame] | 1305 | p.SetRevision(revision) |
| 1306 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1307 | if remote_name: |
Kyunam Jo | bd0aae9 | 2020-02-04 11:38:53 +0900 | [diff] [blame] | 1308 | p.remote = remote.ToRemoteSpec(name) |
Erik Elmeke | 4cdfdb7 | 2022-09-09 17:13:17 +0200 | [diff] [blame] | 1309 | if dest_branch: |
| 1310 | p.dest_branch = dest_branch |
| 1311 | if upstream: |
| 1312 | p.upstream = upstream |
Michael Kelly | 2f3c331 | 2020-07-21 19:40:38 -0700 | [diff] [blame] | 1313 | |
Michael Kelly | 37c21c2 | 2020-06-13 02:10:40 -0700 | [diff] [blame] | 1314 | if dest_path: |
| 1315 | del self._paths[p.relpath] |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1316 | relpath, worktree, gitdir, objdir, _ = self.GetProjectPaths( |
| 1317 | name, dest_path, remote.name) |
Michael Kelly | 37c21c2 | 2020-06-13 02:10:40 -0700 | [diff] [blame] | 1318 | p.UpdatePaths(relpath, worktree, gitdir, objdir) |
| 1319 | self._paths[p.relpath] = p |
| 1320 | |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 1321 | if node.nodeName == 'repo-hooks': |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 1322 | # Only one project can be the hooks project |
Jack Neus | a84f43a | 2021-09-21 22:23:55 +0000 | [diff] [blame] | 1323 | if repo_hooks_project is not None: |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 1324 | raise ManifestParseError( |
| 1325 | 'duplicate repo-hooks in %s' % |
| 1326 | (self.manifestFile)) |
| 1327 | |
Jack Neus | a84f43a | 2021-09-21 22:23:55 +0000 | [diff] [blame] | 1328 | # Get the name of the project and the (space-separated) list of enabled. |
| 1329 | repo_hooks_project = self._reqatt(node, 'in-project') |
| 1330 | enabled_repo_hooks = self._ParseList(self._reqatt(node, 'enabled-list')) |
Raman Tenneti | 1bb4fb2 | 2021-01-07 16:50:45 -0800 | [diff] [blame] | 1331 | if node.nodeName == 'superproject': |
| 1332 | name = self._reqatt(node, 'name') |
| 1333 | # There can only be one superproject. |
LaMont Jones | d56e2eb | 2022-04-07 18:14:46 +0000 | [diff] [blame] | 1334 | if self._superproject: |
Raman Tenneti | 1bb4fb2 | 2021-01-07 16:50:45 -0800 | [diff] [blame] | 1335 | raise ManifestParseError( |
| 1336 | 'duplicate superproject in %s' % |
| 1337 | (self.manifestFile)) |
Raman Tenneti | 1bb4fb2 | 2021-01-07 16:50:45 -0800 | [diff] [blame] | 1338 | remote_name = node.getAttribute('remote') |
| 1339 | if not remote_name: |
| 1340 | remote = self._default.remote |
| 1341 | else: |
| 1342 | remote = self._get_remote(node) |
| 1343 | if remote is None: |
| 1344 | raise ManifestParseError("no remote for superproject %s within %s" % |
| 1345 | (name, self.manifestFile)) |
Xin Li | e0b16a2 | 2021-09-26 23:20:32 -0700 | [diff] [blame] | 1346 | revision = node.getAttribute('revision') or remote.revision |
| 1347 | if not revision: |
| 1348 | revision = self._default.revisionExpr |
| 1349 | if not revision: |
| 1350 | raise ManifestParseError('no revision for superproject %s within %s' % |
| 1351 | (name, self.manifestFile)) |
LaMont Jones | d56e2eb | 2022-04-07 18:14:46 +0000 | [diff] [blame] | 1352 | self._superproject = Superproject(self, |
| 1353 | name=name, |
| 1354 | remote=remote.ToRemoteSpec(name), |
| 1355 | revision=revision) |
Raman Tenneti | 1c3f57e | 2021-05-04 12:32:13 -0700 | [diff] [blame] | 1356 | if node.nodeName == 'contactinfo': |
| 1357 | bugurl = self._reqatt(node, 'bugurl') |
| 1358 | # This element can be repeated, later entries will clobber earlier ones. |
Raman Tenneti | 993af5e | 2021-05-12 12:00:31 -0700 | [diff] [blame] | 1359 | self._contactinfo = ContactInfo(bugurl) |
| 1360 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 1361 | if node.nodeName == 'remove-project': |
| 1362 | name = self._reqatt(node, 'name') |
David James | b8433df | 2014-01-30 10:11:17 -0800 | [diff] [blame] | 1363 | |
Michael Kelly | 06da998 | 2021-06-30 01:58:28 -0700 | [diff] [blame] | 1364 | if name in self._projects: |
| 1365 | for p in self._projects[name]: |
| 1366 | del self._paths[p.relpath] |
| 1367 | del self._projects[name] |
| 1368 | |
| 1369 | # If the manifest removes the hooks project, treat it as if it deleted |
| 1370 | # the repo-hooks element too. |
Jack Neus | a84f43a | 2021-09-21 22:23:55 +0000 | [diff] [blame] | 1371 | if repo_hooks_project == name: |
| 1372 | repo_hooks_project = None |
Michael Kelly | 06da998 | 2021-06-30 01:58:28 -0700 | [diff] [blame] | 1373 | elif not XmlBool(node, 'optional', False): |
David Pursehouse | f910748 | 2012-11-16 19:12:32 +0900 | [diff] [blame] | 1374 | raise ManifestParseError('remove-project element specifies non-existent ' |
| 1375 | 'project: %s' % name) |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 1376 | |
Jack Neus | a84f43a | 2021-09-21 22:23:55 +0000 | [diff] [blame] | 1377 | # Store repo hooks project information. |
| 1378 | if repo_hooks_project: |
| 1379 | # Store a reference to the Project. |
| 1380 | try: |
| 1381 | repo_hooks_projects = self._projects[repo_hooks_project] |
| 1382 | except KeyError: |
| 1383 | raise ManifestParseError( |
| 1384 | 'project %s not found for repo-hooks' % |
| 1385 | (repo_hooks_project)) |
| 1386 | |
| 1387 | if len(repo_hooks_projects) != 1: |
| 1388 | raise ManifestParseError( |
| 1389 | 'internal error parsing repo-hooks in %s' % |
| 1390 | (self.manifestFile)) |
| 1391 | self._repo_hooks_project = repo_hooks_projects[0] |
| 1392 | # Store the enabled hooks in the Project object. |
| 1393 | self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks |
| 1394 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 1395 | def _AddMetaProjectMirror(self, m): |
| 1396 | name = None |
Mike Frysinger | dede564 | 2022-07-10 04:56:04 -0400 | [diff] [blame] | 1397 | m_url = m.GetRemote().url |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 1398 | if m_url.endswith('/.git'): |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 1399 | raise ManifestParseError('refusing to mirror %s' % m_url) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 1400 | |
| 1401 | if self._default and self._default.remote: |
Conley Owens | ceea368 | 2011-10-20 10:45:47 -0700 | [diff] [blame] | 1402 | url = self._default.remote.resolvedFetchUrl |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 1403 | if not url.endswith('/'): |
| 1404 | url += '/' |
| 1405 | if m_url.startswith(url): |
| 1406 | remote = self._default.remote |
| 1407 | name = m_url[len(url):] |
| 1408 | |
| 1409 | if name is None: |
| 1410 | s = m_url.rindex('/') + 1 |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 1411 | manifestUrl = self.manifestProject.config.GetString('remote.origin.url') |
Shawn O. Pearce | f35b2d9 | 2012-08-02 11:46:22 -0700 | [diff] [blame] | 1412 | remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 1413 | name = m_url[s:] |
| 1414 | |
| 1415 | if name.endswith('.git'): |
| 1416 | name = name[:-4] |
| 1417 | |
| 1418 | if name not in self._projects: |
| 1419 | m.PreSync() |
| 1420 | gitdir = os.path.join(self.topdir, '%s.git' % name) |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 1421 | project = Project(manifest=self, |
| 1422 | name=name, |
| 1423 | remote=remote.ToRemoteSpec(name), |
| 1424 | gitdir=gitdir, |
| 1425 | objdir=gitdir, |
| 1426 | worktree=None, |
| 1427 | relpath=name or None, |
| 1428 | revisionExpr=m.revisionExpr, |
| 1429 | revisionId=None) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1430 | self._projects[project.name] = [project] |
Kwanhong Lee | ccd218c | 2014-02-17 13:07:32 +0900 | [diff] [blame] | 1431 | self._paths[project.relpath] = project |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 1432 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1433 | def _ParseRemote(self, node): |
| 1434 | """ |
| 1435 | reads a <remote> element from the manifest file |
| 1436 | """ |
| 1437 | name = self._reqatt(node, 'name') |
Yestin Sun | b292b98 | 2012-07-02 07:32:50 -0700 | [diff] [blame] | 1438 | alias = node.getAttribute('alias') |
| 1439 | if alias == '': |
| 1440 | alias = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1441 | fetch = self._reqatt(node, 'fetch') |
Steve Rae | d648045 | 2016-08-10 15:00:00 -0700 | [diff] [blame] | 1442 | pushUrl = node.getAttribute('pushurl') |
| 1443 | if pushUrl == '': |
| 1444 | pushUrl = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1445 | review = node.getAttribute('review') |
Shawn O. Pearce | ae6e094 | 2008-11-06 10:25:35 -0800 | [diff] [blame] | 1446 | if review == '': |
| 1447 | review = None |
Anthony King | 36ea2fb | 2014-05-06 11:54:01 +0100 | [diff] [blame] | 1448 | revision = node.getAttribute('revision') |
| 1449 | if revision == '': |
| 1450 | revision = None |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 1451 | manifestUrl = self.manifestProject.config.GetString('remote.origin.url') |
Jack Neus | 6ea0cae | 2021-07-20 20:52:33 +0000 | [diff] [blame] | 1452 | |
| 1453 | remote = _XmlRemote(name, alias, fetch, pushUrl, manifestUrl, review, revision) |
| 1454 | |
| 1455 | for n in node.childNodes: |
| 1456 | if n.nodeName == 'annotation': |
| 1457 | self._ParseAnnotation(remote, n) |
| 1458 | |
| 1459 | return remote |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1460 | |
| 1461 | def _ParseDefault(self, node): |
| 1462 | """ |
| 1463 | reads a <default> element from the manifest file |
| 1464 | """ |
| 1465 | d = _Default() |
| 1466 | d.remote = self._get_remote(node) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1467 | d.revisionExpr = node.getAttribute('revision') |
| 1468 | if d.revisionExpr == '': |
| 1469 | d.revisionExpr = None |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 1470 | |
Bryan Jacobs | f609f91 | 2013-05-06 13:36:24 -0400 | [diff] [blame] | 1471 | d.destBranchExpr = node.getAttribute('dest-branch') or None |
Nasser Grainawi | da40341 | 2018-05-04 12:53:29 -0600 | [diff] [blame] | 1472 | d.upstreamExpr = node.getAttribute('upstream') or None |
Bryan Jacobs | f609f91 | 2013-05-06 13:36:24 -0400 | [diff] [blame] | 1473 | |
Mike Frysinger | 355f439 | 2022-07-20 17:15:29 -0400 | [diff] [blame] | 1474 | d.sync_j = XmlInt(node, 'sync-j', None) |
| 1475 | if d.sync_j is not None and d.sync_j <= 0: |
Mike Frysinger | bb8ee7f | 2020-02-22 05:30:12 -0500 | [diff] [blame] | 1476 | raise ManifestParseError('%s: sync-j must be greater than 0, not "%s"' % |
| 1477 | (self.manifestFile, d.sync_j)) |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 1478 | |
Mike Frysinger | bb8ee7f | 2020-02-22 05:30:12 -0500 | [diff] [blame] | 1479 | d.sync_c = XmlBool(node, 'sync-c', False) |
| 1480 | d.sync_s = XmlBool(node, 'sync-s', False) |
| 1481 | d.sync_tags = XmlBool(node, 'sync-tags', True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1482 | return d |
| 1483 | |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 1484 | def _ParseNotice(self, node): |
| 1485 | """ |
| 1486 | reads a <notice> element from the manifest file |
| 1487 | |
| 1488 | The <notice> element is distinct from other tags in the XML in that the |
| 1489 | data is conveyed between the start and end tag (it's not an empty-element |
| 1490 | tag). |
| 1491 | |
| 1492 | The white space (carriage returns, indentation) for the notice element is |
| 1493 | relevant and is parsed in a way that is based on how python docstrings work. |
| 1494 | In fact, the code is remarkably similar to here: |
| 1495 | http://www.python.org/dev/peps/pep-0257/ |
| 1496 | """ |
| 1497 | # Get the data out of the node... |
| 1498 | notice = node.childNodes[0].data |
| 1499 | |
| 1500 | # Figure out minimum indentation, skipping the first line (the same line |
| 1501 | # as the <notice> tag)... |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 1502 | minIndent = sys.maxsize |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 1503 | lines = notice.splitlines() |
| 1504 | for line in lines[1:]: |
| 1505 | lstrippedLine = line.lstrip() |
| 1506 | if lstrippedLine: |
| 1507 | indent = len(line) - len(lstrippedLine) |
| 1508 | minIndent = min(indent, minIndent) |
| 1509 | |
| 1510 | # Strip leading / trailing blank lines and also indentation. |
| 1511 | cleanLines = [lines[0].strip()] |
| 1512 | for line in lines[1:]: |
| 1513 | cleanLines.append(line[minIndent:].rstrip()) |
| 1514 | |
| 1515 | # Clear completely blank lines from front and back... |
| 1516 | while cleanLines and not cleanLines[0]: |
| 1517 | del cleanLines[0] |
| 1518 | while cleanLines and not cleanLines[-1]: |
| 1519 | del cleanLines[-1] |
| 1520 | |
| 1521 | return '\n'.join(cleanLines) |
| 1522 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1523 | def _ParseSubmanifest(self, node): |
| 1524 | """Reads a <submanifest> element from the manifest file.""" |
| 1525 | name = self._reqatt(node, 'name') |
| 1526 | remote = node.getAttribute('remote') |
| 1527 | if remote == '': |
| 1528 | remote = None |
| 1529 | project = node.getAttribute('project') |
| 1530 | if project == '': |
| 1531 | project = None |
| 1532 | revision = node.getAttribute('revision') |
| 1533 | if revision == '': |
| 1534 | revision = None |
| 1535 | manifestName = node.getAttribute('manifest-name') |
| 1536 | if manifestName == '': |
| 1537 | manifestName = None |
| 1538 | groups = '' |
| 1539 | if node.hasAttribute('groups'): |
| 1540 | groups = node.getAttribute('groups') |
| 1541 | groups = self._ParseList(groups) |
LaMont Jones | 501733c | 2022-04-20 16:42:32 +0000 | [diff] [blame] | 1542 | default_groups = self._ParseList(node.getAttribute('default-groups')) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1543 | path = node.getAttribute('path') |
| 1544 | if path == '': |
| 1545 | path = None |
| 1546 | if revision: |
| 1547 | msg = self._CheckLocalPath(revision.split('/')[-1]) |
| 1548 | if msg: |
| 1549 | raise ManifestInvalidPathError( |
| 1550 | '<submanifest> invalid "revision": %s: %s' % (revision, msg)) |
| 1551 | else: |
| 1552 | msg = self._CheckLocalPath(name) |
| 1553 | if msg: |
| 1554 | raise ManifestInvalidPathError( |
| 1555 | '<submanifest> invalid "name": %s: %s' % (name, msg)) |
| 1556 | else: |
| 1557 | msg = self._CheckLocalPath(path) |
| 1558 | if msg: |
| 1559 | raise ManifestInvalidPathError( |
| 1560 | '<submanifest> invalid "path": %s: %s' % (path, msg)) |
| 1561 | |
| 1562 | submanifest = _XmlSubmanifest(name, remote, project, revision, manifestName, |
LaMont Jones | 501733c | 2022-04-20 16:42:32 +0000 | [diff] [blame] | 1563 | groups, default_groups, path, self) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1564 | |
| 1565 | for n in node.childNodes: |
| 1566 | if n.nodeName == 'annotation': |
| 1567 | self._ParseAnnotation(submanifest, n) |
| 1568 | |
| 1569 | return submanifest |
| 1570 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1571 | def _JoinName(self, parent_name, name): |
| 1572 | return os.path.join(parent_name, name) |
| 1573 | |
| 1574 | def _UnjoinName(self, parent_name, name): |
| 1575 | return os.path.relpath(name, parent_name) |
| 1576 | |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 1577 | def _ParseProject(self, node, parent=None, **extra_proj_attrs): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1578 | """ |
| 1579 | reads a <project> element from the manifest file |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 1580 | """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1581 | name = self._reqatt(node, 'name') |
Mike Frysinger | a29424e | 2021-02-25 21:53:49 -0500 | [diff] [blame] | 1582 | msg = self._CheckLocalPath(name, dir_ok=True) |
| 1583 | if msg: |
| 1584 | raise ManifestInvalidPathError( |
| 1585 | '<project> invalid "name": %s: %s' % (name, msg)) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1586 | if parent: |
| 1587 | name = self._JoinName(parent.name, name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1588 | |
| 1589 | remote = self._get_remote(node) |
| 1590 | if remote is None: |
| 1591 | remote = self._default.remote |
| 1592 | if remote is None: |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 1593 | raise ManifestParseError("no remote for project %s within %s" % |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 1594 | (name, self.manifestFile)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1595 | |
Anthony King | 36ea2fb | 2014-05-06 11:54:01 +0100 | [diff] [blame] | 1596 | revisionExpr = node.getAttribute('revision') or remote.revision |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1597 | if not revisionExpr: |
| 1598 | revisionExpr = self._default.revisionExpr |
| 1599 | if not revisionExpr: |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 1600 | raise ManifestParseError("no revision for project %s within %s" % |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 1601 | (name, self.manifestFile)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1602 | |
| 1603 | path = node.getAttribute('path') |
| 1604 | if not path: |
| 1605 | path = name |
Mike Frysinger | a29424e | 2021-02-25 21:53:49 -0500 | [diff] [blame] | 1606 | else: |
Mike Frysinger | 0458faa | 2021-03-10 23:35:44 -0500 | [diff] [blame] | 1607 | # NB: The "." project is handled specially in Project.Sync_LocalHalf. |
| 1608 | msg = self._CheckLocalPath(path, dir_ok=True, cwd_dot_ok=True) |
Mike Frysinger | a29424e | 2021-02-25 21:53:49 -0500 | [diff] [blame] | 1609 | if msg: |
| 1610 | raise ManifestInvalidPathError( |
| 1611 | '<project> invalid "path": %s: %s' % (path, msg)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1612 | |
Mike Frysinger | bb8ee7f | 2020-02-22 05:30:12 -0500 | [diff] [blame] | 1613 | rebase = XmlBool(node, 'rebase', True) |
| 1614 | sync_c = XmlBool(node, 'sync-c', False) |
| 1615 | sync_s = XmlBool(node, 'sync-s', self._default.sync_s) |
| 1616 | sync_tags = XmlBool(node, 'sync-tags', self._default.sync_tags) |
Mike Pontillo | d315382 | 2012-02-28 11:53:24 -0800 | [diff] [blame] | 1617 | |
Mike Frysinger | bb8ee7f | 2020-02-22 05:30:12 -0500 | [diff] [blame] | 1618 | clone_depth = XmlInt(node, 'clone-depth') |
| 1619 | if clone_depth is not None and clone_depth <= 0: |
| 1620 | raise ManifestParseError('%s: clone-depth must be greater than 0, not "%s"' % |
| 1621 | (self.manifestFile, clone_depth)) |
David Pursehouse | ede7f12 | 2012-11-27 22:25:30 +0900 | [diff] [blame] | 1622 | |
Bryan Jacobs | f609f91 | 2013-05-06 13:36:24 -0400 | [diff] [blame] | 1623 | dest_branch = node.getAttribute('dest-branch') or self._default.destBranchExpr |
| 1624 | |
Nasser Grainawi | da40341 | 2018-05-04 12:53:29 -0600 | [diff] [blame] | 1625 | upstream = node.getAttribute('upstream') or self._default.upstreamExpr |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 1626 | |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 1627 | groups = '' |
| 1628 | if node.hasAttribute('groups'): |
| 1629 | groups = node.getAttribute('groups') |
Mike Frysinger | 51e39d5 | 2020-12-04 05:32:06 -0500 | [diff] [blame] | 1630 | groups = self._ParseList(groups) |
Brian Harring | 7da1314 | 2012-06-15 02:24:20 -0700 | [diff] [blame] | 1631 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1632 | if parent is None: |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 1633 | relpath, worktree, gitdir, objdir, use_git_worktrees = \ |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1634 | self.GetProjectPaths(name, path, remote.name) |
Shawn O. Pearce | cd81dd6 | 2012-10-26 12:18:00 -0700 | [diff] [blame] | 1635 | else: |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 1636 | use_git_worktrees = False |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1637 | relpath, worktree, gitdir, objdir = \ |
| 1638 | self.GetSubprojectPaths(parent, name, path) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1639 | |
| 1640 | default_groups = ['all', 'name:%s' % name, 'path:%s' % relpath] |
| 1641 | groups.extend(set(default_groups).difference(groups)) |
Shawn O. Pearce | cd81dd6 | 2012-10-26 12:18:00 -0700 | [diff] [blame] | 1642 | |
Scott Fan | db83b1b | 2013-02-28 09:34:14 +0800 | [diff] [blame] | 1643 | if self.IsMirror and node.hasAttribute('force-path'): |
Mike Frysinger | bb8ee7f | 2020-02-22 05:30:12 -0500 | [diff] [blame] | 1644 | if XmlBool(node, 'force-path', False): |
Scott Fan | db83b1b | 2013-02-28 09:34:14 +0800 | [diff] [blame] | 1645 | gitdir = os.path.join(self.topdir, '%s.git' % path) |
| 1646 | |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 1647 | project = Project(manifest=self, |
| 1648 | name=name, |
| 1649 | remote=remote.ToRemoteSpec(name), |
| 1650 | gitdir=gitdir, |
| 1651 | objdir=objdir, |
| 1652 | worktree=worktree, |
| 1653 | relpath=relpath, |
| 1654 | revisionExpr=revisionExpr, |
| 1655 | revisionId=None, |
| 1656 | rebase=rebase, |
| 1657 | groups=groups, |
| 1658 | sync_c=sync_c, |
| 1659 | sync_s=sync_s, |
| 1660 | sync_tags=sync_tags, |
| 1661 | clone_depth=clone_depth, |
| 1662 | upstream=upstream, |
| 1663 | parent=parent, |
| 1664 | dest_branch=dest_branch, |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 1665 | use_git_worktrees=use_git_worktrees, |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1666 | **extra_proj_attrs) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1667 | |
| 1668 | for n in node.childNodes: |
Shawn O. Pearce | 242b526 | 2009-05-19 13:00:29 -0700 | [diff] [blame] | 1669 | if n.nodeName == 'copyfile': |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1670 | self._ParseCopyFile(project, n) |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1671 | if n.nodeName == 'linkfile': |
| 1672 | self._ParseLinkFile(project, n) |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 1673 | if n.nodeName == 'annotation': |
| 1674 | self._ParseAnnotation(project, n) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1675 | if n.nodeName == 'project': |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 1676 | project.subprojects.append(self._ParseProject(n, parent=project)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1677 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1678 | return project |
| 1679 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1680 | def GetProjectPaths(self, name, path, remote): |
| 1681 | """Return the paths for a project. |
| 1682 | |
| 1683 | Args: |
| 1684 | name: a string, the name of the project. |
| 1685 | path: a string, the path of the project. |
| 1686 | remote: a string, the remote.name of the project. |
LaMont Jones | ff6b1da | 2022-06-01 21:03:34 +0000 | [diff] [blame] | 1687 | |
| 1688 | Returns: |
| 1689 | A tuple of (relpath, worktree, gitdir, objdir, use_git_worktrees) for the |
| 1690 | project with |name| and |path|. |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1691 | """ |
Mike Frysinger | cebf227 | 2020-05-26 01:02:29 -0400 | [diff] [blame] | 1692 | # The manifest entries might have trailing slashes. Normalize them to avoid |
| 1693 | # unexpected filesystem behavior since we do string concatenation below. |
| 1694 | path = path.rstrip('/') |
| 1695 | name = name.rstrip('/') |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1696 | remote = remote.rstrip('/') |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 1697 | use_git_worktrees = False |
LaMont Jones | ff6b1da | 2022-06-01 21:03:34 +0000 | [diff] [blame] | 1698 | use_remote_name = self.is_multimanifest |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1699 | relpath = path |
| 1700 | if self.IsMirror: |
| 1701 | worktree = None |
| 1702 | gitdir = os.path.join(self.topdir, '%s.git' % name) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1703 | objdir = gitdir |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1704 | else: |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1705 | if use_remote_name: |
| 1706 | namepath = os.path.join(remote, f'{name}.git') |
| 1707 | else: |
| 1708 | namepath = f'{name}.git' |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1709 | worktree = os.path.join(self.topdir, path).replace('\\', '/') |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1710 | gitdir = os.path.join(self.subdir, 'projects', '%s.git' % path) |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 1711 | # We allow people to mix git worktrees & non-git worktrees for now. |
| 1712 | # This allows for in situ migration of repo clients. |
| 1713 | if os.path.exists(gitdir) or not self.UseGitWorktrees: |
Mike Frysinger | 1d00a7e | 2021-12-21 00:40:31 -0500 | [diff] [blame] | 1714 | objdir = os.path.join(self.repodir, 'project-objects', namepath) |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 1715 | else: |
| 1716 | use_git_worktrees = True |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1717 | gitdir = os.path.join(self.repodir, 'worktrees', namepath) |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 1718 | objdir = gitdir |
| 1719 | return relpath, worktree, gitdir, objdir, use_git_worktrees |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1720 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1721 | def GetProjectsWithName(self, name, all_manifests=False): |
| 1722 | """All projects with |name|. |
| 1723 | |
| 1724 | Args: |
| 1725 | name: a string, the name of the project. |
| 1726 | all_manifests: a boolean, if True, then all manifests are searched. If |
| 1727 | False, then only this manifest is searched. |
LaMont Jones | ff6b1da | 2022-06-01 21:03:34 +0000 | [diff] [blame] | 1728 | |
| 1729 | Returns: |
| 1730 | A list of Project instances with name |name|. |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1731 | """ |
| 1732 | if all_manifests: |
| 1733 | return list(itertools.chain.from_iterable( |
| 1734 | x._projects.get(name, []) for x in self.all_manifests)) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1735 | return self._projects.get(name, []) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1736 | |
| 1737 | def GetSubprojectName(self, parent, submodule_path): |
| 1738 | return os.path.join(parent.name, submodule_path) |
| 1739 | |
| 1740 | def _JoinRelpath(self, parent_relpath, relpath): |
| 1741 | return os.path.join(parent_relpath, relpath) |
| 1742 | |
| 1743 | def _UnjoinRelpath(self, parent_relpath, relpath): |
| 1744 | return os.path.relpath(relpath, parent_relpath) |
| 1745 | |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1746 | def GetSubprojectPaths(self, parent, name, path): |
Mike Frysinger | cebf227 | 2020-05-26 01:02:29 -0400 | [diff] [blame] | 1747 | # The manifest entries might have trailing slashes. Normalize them to avoid |
| 1748 | # unexpected filesystem behavior since we do string concatenation below. |
| 1749 | path = path.rstrip('/') |
| 1750 | name = name.rstrip('/') |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1751 | relpath = self._JoinRelpath(parent.relpath, path) |
| 1752 | gitdir = os.path.join(parent.gitdir, 'subprojects', '%s.git' % path) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1753 | objdir = os.path.join(parent.gitdir, 'subproject-objects', '%s.git' % name) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1754 | if self.IsMirror: |
| 1755 | worktree = None |
| 1756 | else: |
| 1757 | worktree = os.path.join(parent.worktree, path).replace('\\', '/') |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1758 | return relpath, worktree, gitdir, objdir |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1759 | |
Mike Frysinger | 04122b7 | 2019-07-31 23:32:58 -0400 | [diff] [blame] | 1760 | @staticmethod |
Mike Frysinger | a00c5f4 | 2021-02-25 18:26:31 -0500 | [diff] [blame] | 1761 | def _CheckLocalPath(path, dir_ok=False, cwd_dot_ok=False): |
| 1762 | """Verify |path| is reasonable for use in filesystem paths. |
| 1763 | |
Mike Frysinger | a29424e | 2021-02-25 21:53:49 -0500 | [diff] [blame] | 1764 | Used with <copyfile> & <linkfile> & <project> elements. |
Mike Frysinger | a00c5f4 | 2021-02-25 18:26:31 -0500 | [diff] [blame] | 1765 | |
| 1766 | This only validates the |path| in isolation: it does not check against the |
| 1767 | current filesystem state. Thus it is suitable as a first-past in a parser. |
| 1768 | |
| 1769 | It enforces a number of constraints: |
| 1770 | * No empty paths. |
| 1771 | * No "~" in paths. |
| 1772 | * No Unicode codepoints that filesystems might elide when normalizing. |
| 1773 | * No relative path components like "." or "..". |
| 1774 | * No absolute paths. |
| 1775 | * No ".git" or ".repo*" path components. |
| 1776 | |
| 1777 | Args: |
| 1778 | path: The path name to validate. |
| 1779 | dir_ok: Whether |path| may force a directory (e.g. end in a /). |
| 1780 | cwd_dot_ok: Whether |path| may be just ".". |
| 1781 | |
| 1782 | Returns: |
| 1783 | None if |path| is OK, a failure message otherwise. |
| 1784 | """ |
| 1785 | if not path: |
| 1786 | return 'empty paths not allowed' |
| 1787 | |
Mike Frysinger | 04122b7 | 2019-07-31 23:32:58 -0400 | [diff] [blame] | 1788 | if '~' in path: |
| 1789 | return '~ not allowed (due to 8.3 filenames on Windows filesystems)' |
| 1790 | |
Mike Frysinger | f69c7ee | 2021-04-29 23:15:31 -0400 | [diff] [blame] | 1791 | path_codepoints = set(path) |
| 1792 | |
Mike Frysinger | 04122b7 | 2019-07-31 23:32:58 -0400 | [diff] [blame] | 1793 | # Some filesystems (like Apple's HFS+) try to normalize Unicode codepoints |
| 1794 | # which means there are alternative names for ".git". Reject paths with |
| 1795 | # these in it as there shouldn't be any reasonable need for them here. |
| 1796 | # The set of codepoints here was cribbed from jgit's implementation: |
| 1797 | # https://eclipse.googlesource.com/jgit/jgit/+/9110037e3e9461ff4dac22fee84ef3694ed57648/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectChecker.java#884 |
| 1798 | BAD_CODEPOINTS = { |
| 1799 | u'\u200C', # ZERO WIDTH NON-JOINER |
| 1800 | u'\u200D', # ZERO WIDTH JOINER |
| 1801 | u'\u200E', # LEFT-TO-RIGHT MARK |
| 1802 | u'\u200F', # RIGHT-TO-LEFT MARK |
| 1803 | u'\u202A', # LEFT-TO-RIGHT EMBEDDING |
| 1804 | u'\u202B', # RIGHT-TO-LEFT EMBEDDING |
| 1805 | u'\u202C', # POP DIRECTIONAL FORMATTING |
| 1806 | u'\u202D', # LEFT-TO-RIGHT OVERRIDE |
| 1807 | u'\u202E', # RIGHT-TO-LEFT OVERRIDE |
| 1808 | u'\u206A', # INHIBIT SYMMETRIC SWAPPING |
| 1809 | u'\u206B', # ACTIVATE SYMMETRIC SWAPPING |
| 1810 | u'\u206C', # INHIBIT ARABIC FORM SHAPING |
| 1811 | u'\u206D', # ACTIVATE ARABIC FORM SHAPING |
| 1812 | u'\u206E', # NATIONAL DIGIT SHAPES |
| 1813 | u'\u206F', # NOMINAL DIGIT SHAPES |
| 1814 | u'\uFEFF', # ZERO WIDTH NO-BREAK SPACE |
| 1815 | } |
Mike Frysinger | f69c7ee | 2021-04-29 23:15:31 -0400 | [diff] [blame] | 1816 | if BAD_CODEPOINTS & path_codepoints: |
Mike Frysinger | 04122b7 | 2019-07-31 23:32:58 -0400 | [diff] [blame] | 1817 | # This message is more expansive than reality, but should be fine. |
| 1818 | return 'Unicode combining characters not allowed' |
| 1819 | |
Mike Frysinger | f69c7ee | 2021-04-29 23:15:31 -0400 | [diff] [blame] | 1820 | # Reject newlines as there shouldn't be any legitmate use for them, they'll |
| 1821 | # be confusing to users, and they can easily break tools that expect to be |
| 1822 | # able to iterate over newline delimited lists. This even applies to our |
| 1823 | # own code like .repo/project.list. |
| 1824 | if {'\r', '\n'} & path_codepoints: |
| 1825 | return 'Newlines not allowed' |
| 1826 | |
Mike Frysinger | 04122b7 | 2019-07-31 23:32:58 -0400 | [diff] [blame] | 1827 | # Assume paths might be used on case-insensitive filesystems. |
| 1828 | path = path.lower() |
| 1829 | |
Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 1830 | # Split up the path by its components. We can't use os.path.sep exclusively |
| 1831 | # as some platforms (like Windows) will convert / to \ and that bypasses all |
| 1832 | # our constructed logic here. Especially since manifest authors only use |
| 1833 | # / in their paths. |
| 1834 | resep = re.compile(r'[/%s]' % re.escape(os.path.sep)) |
Mike Frysinger | 0458faa | 2021-03-10 23:35:44 -0500 | [diff] [blame] | 1835 | # Strip off trailing slashes as those only produce '' elements, and we use |
| 1836 | # parts to look for individual bad components. |
| 1837 | parts = resep.split(path.rstrip('/')) |
Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 1838 | |
Mike Frysinger | ae62541 | 2020-02-10 17:10:03 -0500 | [diff] [blame] | 1839 | # Some people use src="." to create stable links to projects. Lets allow |
| 1840 | # that but reject all other uses of "." to keep things simple. |
Mike Frysinger | a00c5f4 | 2021-02-25 18:26:31 -0500 | [diff] [blame] | 1841 | if not cwd_dot_ok or parts != ['.']: |
Mike Frysinger | ae62541 | 2020-02-10 17:10:03 -0500 | [diff] [blame] | 1842 | for part in set(parts): |
| 1843 | if part in {'.', '..', '.git'} or part.startswith('.repo'): |
| 1844 | return 'bad component: %s' % (part,) |
Mike Frysinger | 04122b7 | 2019-07-31 23:32:58 -0400 | [diff] [blame] | 1845 | |
Mike Frysinger | a00c5f4 | 2021-02-25 18:26:31 -0500 | [diff] [blame] | 1846 | if not dir_ok and resep.match(path[-1]): |
Mike Frysinger | 04122b7 | 2019-07-31 23:32:58 -0400 | [diff] [blame] | 1847 | return 'dirs not allowed' |
| 1848 | |
Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 1849 | # NB: The two abspath checks here are to handle platforms with multiple |
| 1850 | # filesystem path styles (e.g. Windows). |
Mike Frysinger | 04122b7 | 2019-07-31 23:32:58 -0400 | [diff] [blame] | 1851 | norm = os.path.normpath(path) |
Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 1852 | if (norm == '..' or |
| 1853 | (len(norm) >= 3 and norm.startswith('..') and resep.match(norm[0])) or |
| 1854 | os.path.isabs(norm) or |
| 1855 | norm.startswith('/')): |
Mike Frysinger | 04122b7 | 2019-07-31 23:32:58 -0400 | [diff] [blame] | 1856 | return 'path cannot be outside' |
| 1857 | |
| 1858 | @classmethod |
| 1859 | def _ValidateFilePaths(cls, element, src, dest): |
| 1860 | """Verify |src| & |dest| are reasonable for <copyfile> & <linkfile>. |
| 1861 | |
| 1862 | We verify the path independent of any filesystem state as we won't have a |
| 1863 | checkout available to compare to. i.e. This is for parsing validation |
| 1864 | purposes only. |
| 1865 | |
| 1866 | We'll do full/live sanity checking before we do the actual filesystem |
| 1867 | modifications in _CopyFile/_LinkFile/etc... |
| 1868 | """ |
| 1869 | # |dest| is the file we write to or symlink we create. |
| 1870 | # It is relative to the top of the repo client checkout. |
| 1871 | msg = cls._CheckLocalPath(dest) |
| 1872 | if msg: |
| 1873 | raise ManifestInvalidPathError( |
| 1874 | '<%s> invalid "dest": %s: %s' % (element, dest, msg)) |
| 1875 | |
| 1876 | # |src| is the file we read from or path we point to for symlinks. |
| 1877 | # It is relative to the top of the git project checkout. |
Mike Frysinger | a00c5f4 | 2021-02-25 18:26:31 -0500 | [diff] [blame] | 1878 | is_linkfile = element == 'linkfile' |
| 1879 | msg = cls._CheckLocalPath(src, dir_ok=is_linkfile, cwd_dot_ok=is_linkfile) |
Mike Frysinger | 04122b7 | 2019-07-31 23:32:58 -0400 | [diff] [blame] | 1880 | if msg: |
| 1881 | raise ManifestInvalidPathError( |
| 1882 | '<%s> invalid "src": %s: %s' % (element, src, msg)) |
| 1883 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1884 | def _ParseCopyFile(self, project, node): |
| 1885 | src = self._reqatt(node, 'src') |
| 1886 | dest = self._reqatt(node, 'dest') |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 1887 | if not self.IsMirror: |
| 1888 | # src is project relative; |
Mike Frysinger | 04122b7 | 2019-07-31 23:32:58 -0400 | [diff] [blame] | 1889 | # dest is relative to the top of the tree. |
| 1890 | # We only validate paths if we actually plan to process them. |
| 1891 | self._ValidateFilePaths('copyfile', src, dest) |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 1892 | project.AddCopyFile(src, dest, self.topdir) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1893 | |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1894 | def _ParseLinkFile(self, project, node): |
| 1895 | src = self._reqatt(node, 'src') |
| 1896 | dest = self._reqatt(node, 'dest') |
| 1897 | if not self.IsMirror: |
| 1898 | # src is project relative; |
Mike Frysinger | 04122b7 | 2019-07-31 23:32:58 -0400 | [diff] [blame] | 1899 | # dest is relative to the top of the tree. |
| 1900 | # We only validate paths if we actually plan to process them. |
| 1901 | self._ValidateFilePaths('linkfile', src, dest) |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 1902 | project.AddLinkFile(src, dest, self.topdir) |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1903 | |
Jack Neus | 6ea0cae | 2021-07-20 20:52:33 +0000 | [diff] [blame] | 1904 | def _ParseAnnotation(self, element, node): |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 1905 | name = self._reqatt(node, 'name') |
| 1906 | value = self._reqatt(node, 'value') |
| 1907 | try: |
| 1908 | keep = self._reqatt(node, 'keep').lower() |
| 1909 | except ManifestParseError: |
| 1910 | keep = "true" |
| 1911 | if keep != "true" and keep != "false": |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 1912 | raise ManifestParseError('optional "keep" attribute must be ' |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 1913 | '"true" or "false"') |
Jack Neus | 6ea0cae | 2021-07-20 20:52:33 +0000 | [diff] [blame] | 1914 | element.AddAnnotation(name, value, keep) |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 1915 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1916 | def _get_remote(self, node): |
| 1917 | name = node.getAttribute('remote') |
| 1918 | if not name: |
| 1919 | return None |
| 1920 | |
| 1921 | v = self._remotes.get(name) |
| 1922 | if not v: |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 1923 | raise ManifestParseError("remote %s not defined in %s" % |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 1924 | (name, self.manifestFile)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1925 | return v |
| 1926 | |
| 1927 | def _reqatt(self, node, attname): |
| 1928 | """ |
| 1929 | reads a required attribute from the node. |
| 1930 | """ |
| 1931 | v = node.getAttribute(attname) |
| 1932 | if not v: |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 1933 | raise ManifestParseError("no %s in <%s> within %s" % |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 1934 | (attname, node.nodeName, self.manifestFile)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1935 | return v |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame] | 1936 | |
| 1937 | def projectsDiff(self, manifest): |
| 1938 | """return the projects differences between two manifests. |
| 1939 | |
| 1940 | The diff will be from self to given manifest. |
| 1941 | |
| 1942 | """ |
| 1943 | fromProjects = self.paths |
| 1944 | toProjects = manifest.paths |
| 1945 | |
Anthony King | 7446c59 | 2014-05-06 09:19:39 +0100 | [diff] [blame] | 1946 | fromKeys = sorted(fromProjects.keys()) |
| 1947 | toKeys = sorted(toProjects.keys()) |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame] | 1948 | |
Shashank Devaraj | 4ba29c4 | 2022-09-05 12:05:47 +0530 | [diff] [blame] | 1949 | diff = {'added': [], 'removed': [], 'missing': [], 'changed': [], 'unreachable': []} |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame] | 1950 | |
| 1951 | for proj in fromKeys: |
David Pursehouse | eeff353 | 2020-02-12 11:24:10 +0900 | [diff] [blame] | 1952 | if proj not in toKeys: |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame] | 1953 | diff['removed'].append(fromProjects[proj]) |
Shashank Devaraj | 4ba29c4 | 2022-09-05 12:05:47 +0530 | [diff] [blame] | 1954 | elif not fromProjects[proj].Exists: |
| 1955 | diff['missing'].append(toProjects[proj]) |
| 1956 | toKeys.remove(proj) |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame] | 1957 | else: |
| 1958 | fromProj = fromProjects[proj] |
| 1959 | toProj = toProjects[proj] |
| 1960 | try: |
| 1961 | fromRevId = fromProj.GetCommitRevisionId() |
| 1962 | toRevId = toProj.GetCommitRevisionId() |
| 1963 | except ManifestInvalidRevisionError: |
| 1964 | diff['unreachable'].append((fromProj, toProj)) |
| 1965 | else: |
| 1966 | if fromRevId != toRevId: |
| 1967 | diff['changed'].append((fromProj, toProj)) |
| 1968 | toKeys.remove(proj) |
| 1969 | |
| 1970 | for proj in toKeys: |
| 1971 | diff['added'].append(toProjects[proj]) |
| 1972 | |
| 1973 | return diff |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1974 | |
| 1975 | |
| 1976 | class GitcManifest(XmlManifest): |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 1977 | """Parser for GitC (git-in-the-cloud) manifests.""" |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1978 | |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 1979 | def _ParseProject(self, node, parent=None): |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1980 | """Override _ParseProject and add support for GITC specific attributes.""" |
Mike Frysinger | 5d9c497 | 2021-02-19 13:34:09 -0500 | [diff] [blame] | 1981 | return super()._ParseProject( |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1982 | node, parent=parent, old_revision=node.getAttribute('old-revision')) |
| 1983 | |
| 1984 | def _output_manifest_project_extras(self, p, e): |
| 1985 | """Output GITC Specific Project attributes""" |
| 1986 | if p.old_revision: |
Stefan Beller | 6685106 | 2016-06-17 16:40:08 -0700 | [diff] [blame] | 1987 | e.setAttribute('old-revision', str(p.old_revision)) |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 1988 | |
| 1989 | |
| 1990 | class RepoClient(XmlManifest): |
| 1991 | """Manages a repo client checkout.""" |
| 1992 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1993 | def __init__(self, repodir, manifest_file=None, submanifest_path='', **kwargs): |
LaMont Jones | ff6b1da | 2022-06-01 21:03:34 +0000 | [diff] [blame] | 1994 | """Initialize. |
| 1995 | |
| 1996 | Args: |
| 1997 | repodir: Path to the .repo/ dir for holding all internal checkout state. |
| 1998 | It must be in the top directory of the repo client checkout. |
| 1999 | manifest_file: Full path to the manifest file to parse. This will usually |
| 2000 | be |repodir|/|MANIFEST_FILE_NAME|. |
| 2001 | submanifest_path: The submanifest root relative to the repo root. |
| 2002 | **kwargs: Additional keyword arguments, passed to XmlManifest. |
| 2003 | """ |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 2004 | self.isGitcClient = False |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 2005 | submanifest_path = submanifest_path or '' |
| 2006 | if submanifest_path: |
| 2007 | self._CheckLocalPath(submanifest_path) |
| 2008 | prefix = os.path.join(repodir, SUBMANIFEST_DIR, submanifest_path) |
| 2009 | else: |
| 2010 | prefix = repodir |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 2011 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 2012 | if os.path.exists(os.path.join(prefix, LOCAL_MANIFEST_NAME)): |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 2013 | print('error: %s is not supported; put local manifests in `%s` instead' % |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 2014 | (LOCAL_MANIFEST_NAME, os.path.join(prefix, LOCAL_MANIFESTS_DIR_NAME)), |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 2015 | file=sys.stderr) |
| 2016 | sys.exit(1) |
| 2017 | |
| 2018 | if manifest_file is None: |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 2019 | manifest_file = os.path.join(prefix, MANIFEST_FILE_NAME) |
| 2020 | local_manifests = os.path.abspath(os.path.join(prefix, LOCAL_MANIFESTS_DIR_NAME)) |
| 2021 | super().__init__(repodir, manifest_file, local_manifests, |
| 2022 | submanifest_path=submanifest_path, **kwargs) |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 2023 | |
| 2024 | # TODO: Completely separate manifest logic out of the client. |
| 2025 | self.manifest = self |
| 2026 | |
| 2027 | |
| 2028 | class GitcClient(RepoClient, GitcManifest): |
| 2029 | """Manages a GitC client checkout.""" |
| 2030 | |
| 2031 | def __init__(self, repodir, gitc_client_name): |
| 2032 | """Initialize the GitcManifest object.""" |
| 2033 | self.gitc_client_name = gitc_client_name |
| 2034 | self.gitc_client_dir = os.path.join(gitc_utils.get_gitc_manifest_dir(), |
| 2035 | gitc_client_name) |
| 2036 | |
Mike Frysinger | 5d9c497 | 2021-02-19 13:34:09 -0500 | [diff] [blame] | 2037 | super().__init__(repodir, os.path.join(self.gitc_client_dir, '.manifest')) |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 2038 | self.isGitcClient = True |