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 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 15 | import itertools |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 16 | import os |
Raman Tenneti | 080877e | 2021-03-09 15:19:06 -0800 | [diff] [blame] | 17 | import platform |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 18 | import re |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 19 | import sys |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 20 | import xml.dom.minidom |
Mike Frysinger | acf63b2 | 2019-06-13 02:24:21 -0400 | [diff] [blame] | 21 | import urllib.parse |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 22 | |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 23 | import gitc_utils |
Miguel Gaio | 1f20776 | 2020-07-17 14:09:13 +0200 | [diff] [blame] | 24 | from git_config import GitConfig, IsId |
David Pursehouse | e00aa6b | 2012-09-11 14:33:51 +0900 | [diff] [blame] | 25 | from git_refs import R_HEADS, HEAD |
Renaud Paquay | d5cec5e | 2016-11-01 11:24:03 -0700 | [diff] [blame] | 26 | import platform_utils |
David Pursehouse | e00aa6b | 2012-09-11 14:33:51 +0900 | [diff] [blame] | 27 | from project import RemoteSpec, Project, MetaProject |
Mike Frysinger | 04122b7 | 2019-07-31 23:32:58 -0400 | [diff] [blame] | 28 | from error import (ManifestParseError, ManifestInvalidPathError, |
| 29 | ManifestInvalidRevisionError) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 30 | |
| 31 | MANIFEST_FILE_NAME = 'manifest.xml' |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 32 | LOCAL_MANIFEST_NAME = 'local_manifest.xml' |
David Pursehouse | 2d5a0df | 2012-11-13 02:50:36 +0900 | [diff] [blame] | 33 | LOCAL_MANIFESTS_DIR_NAME = 'local_manifests' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 34 | |
Anthony King | cb07ba7 | 2015-03-28 23:26:04 +0000 | [diff] [blame] | 35 | # urljoin gets confused if the scheme is not known. |
Joe Kilner | 6e31079 | 2016-10-27 15:53:53 -0700 | [diff] [blame] | 36 | urllib.parse.uses_relative.extend([ |
| 37 | 'ssh', |
| 38 | 'git', |
| 39 | 'persistent-https', |
| 40 | 'sso', |
| 41 | 'rpc']) |
| 42 | urllib.parse.uses_netloc.extend([ |
| 43 | 'ssh', |
| 44 | 'git', |
| 45 | 'persistent-https', |
| 46 | 'sso', |
| 47 | 'rpc']) |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 48 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 49 | |
Mike Frysinger | bb8ee7f | 2020-02-22 05:30:12 -0500 | [diff] [blame] | 50 | def XmlBool(node, attr, default=None): |
| 51 | """Determine boolean value of |node|'s |attr|. |
| 52 | |
| 53 | Invalid values will issue a non-fatal warning. |
| 54 | |
| 55 | Args: |
| 56 | node: XML node whose attributes we access. |
| 57 | attr: The attribute to access. |
| 58 | default: If the attribute is not set (value is empty), then use this. |
| 59 | |
| 60 | Returns: |
| 61 | True if the attribute is a valid string representing true. |
| 62 | False if the attribute is a valid string representing false. |
| 63 | |default| otherwise. |
| 64 | """ |
| 65 | value = node.getAttribute(attr) |
| 66 | s = value.lower() |
| 67 | if s == '': |
| 68 | return default |
| 69 | elif s in {'yes', 'true', '1'}: |
| 70 | return True |
| 71 | elif s in {'no', 'false', '0'}: |
| 72 | return False |
| 73 | else: |
| 74 | print('warning: manifest: %s="%s": ignoring invalid XML boolean' % |
| 75 | (attr, value), file=sys.stderr) |
| 76 | return default |
| 77 | |
| 78 | |
| 79 | def XmlInt(node, attr, default=None): |
| 80 | """Determine integer value of |node|'s |attr|. |
| 81 | |
| 82 | Args: |
| 83 | node: XML node whose attributes we access. |
| 84 | attr: The attribute to access. |
| 85 | default: If the attribute is not set (value is empty), then use this. |
| 86 | |
| 87 | Returns: |
| 88 | The number if the attribute is a valid number. |
| 89 | |
| 90 | Raises: |
| 91 | ManifestParseError: The number is invalid. |
| 92 | """ |
| 93 | value = node.getAttribute(attr) |
| 94 | if not value: |
| 95 | return default |
| 96 | |
| 97 | try: |
| 98 | return int(value) |
| 99 | except ValueError: |
| 100 | raise ManifestParseError('manifest: invalid %s="%s" integer' % |
| 101 | (attr, value)) |
| 102 | |
| 103 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 104 | class _Default(object): |
| 105 | """Project defaults within the manifest.""" |
| 106 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 107 | revisionExpr = None |
Conley Owens | b6a16e6 | 2013-09-25 15:06:09 -0700 | [diff] [blame] | 108 | destBranchExpr = None |
Nasser Grainawi | da40341 | 2018-05-04 12:53:29 -0600 | [diff] [blame] | 109 | upstreamExpr = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 110 | remote = None |
Shawn O. Pearce | 6392c87 | 2011-09-22 17:44:31 -0700 | [diff] [blame] | 111 | sync_j = 1 |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 112 | sync_c = False |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 113 | sync_s = False |
YOUNG HO CHA | a32c92c | 2018-02-14 16:57:31 +0900 | [diff] [blame] | 114 | sync_tags = True |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 115 | |
Julien Campergue | 7487992 | 2013-10-09 14:38:46 +0200 | [diff] [blame] | 116 | def __eq__(self, other): |
| 117 | return self.__dict__ == other.__dict__ |
| 118 | |
| 119 | def __ne__(self, other): |
| 120 | return self.__dict__ != other.__dict__ |
| 121 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 122 | |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 123 | class _XmlRemote(object): |
| 124 | def __init__(self, |
| 125 | name, |
Yestin Sun | b292b98 | 2012-07-02 07:32:50 -0700 | [diff] [blame] | 126 | alias=None, |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 127 | fetch=None, |
Steve Rae | d648045 | 2016-08-10 15:00:00 -0700 | [diff] [blame] | 128 | pushUrl=None, |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 129 | manifestUrl=None, |
Anthony King | 36ea2fb | 2014-05-06 11:54:01 +0100 | [diff] [blame] | 130 | review=None, |
Jonathan Nieder | 9371979 | 2015-03-17 11:29:58 -0700 | [diff] [blame] | 131 | revision=None): |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 132 | self.name = name |
| 133 | self.fetchUrl = fetch |
Steve Rae | d648045 | 2016-08-10 15:00:00 -0700 | [diff] [blame] | 134 | self.pushUrl = pushUrl |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 135 | self.manifestUrl = manifestUrl |
Yestin Sun | b292b98 | 2012-07-02 07:32:50 -0700 | [diff] [blame] | 136 | self.remoteAlias = alias |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 137 | self.reviewUrl = review |
Anthony King | 36ea2fb | 2014-05-06 11:54:01 +0100 | [diff] [blame] | 138 | self.revision = revision |
Conley Owens | ceea368 | 2011-10-20 10:45:47 -0700 | [diff] [blame] | 139 | self.resolvedFetchUrl = self._resolveFetchUrl() |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 140 | |
David Pursehouse | 717ece9 | 2012-11-13 08:49:16 +0900 | [diff] [blame] | 141 | def __eq__(self, other): |
| 142 | return self.__dict__ == other.__dict__ |
| 143 | |
| 144 | def __ne__(self, other): |
| 145 | return self.__dict__ != other.__dict__ |
| 146 | |
Conley Owens | ceea368 | 2011-10-20 10:45:47 -0700 | [diff] [blame] | 147 | def _resolveFetchUrl(self): |
| 148 | url = self.fetchUrl.rstrip('/') |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 149 | manifestUrl = self.manifestUrl.rstrip('/') |
Conley Owens | 2d0f508 | 2014-01-31 15:03:51 -0800 | [diff] [blame] | 150 | # urljoin will gets confused over quite a few things. The ones we care |
| 151 | # about here are: |
| 152 | # * no scheme in the base url, like <hostname:port> |
Anthony King | cb07ba7 | 2015-03-28 23:26:04 +0000 | [diff] [blame] | 153 | # We handle no scheme by replacing it with an obscure protocol, gopher |
| 154 | # and then replacing it with the original when we are done. |
| 155 | |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 156 | if manifestUrl.find(':') != manifestUrl.find('/') - 1: |
Conley Owens | 4ccad75 | 2015-04-29 10:45:37 -0700 | [diff] [blame] | 157 | url = urllib.parse.urljoin('gopher://' + manifestUrl, url) |
| 158 | url = re.sub(r'^gopher://', '', url) |
Anthony King | cb07ba7 | 2015-03-28 23:26:04 +0000 | [diff] [blame] | 159 | else: |
| 160 | url = urllib.parse.urljoin(manifestUrl, url) |
Shawn Pearce | a9f11b3 | 2013-01-02 15:40:48 -0800 | [diff] [blame] | 161 | return url |
Conley Owens | ceea368 | 2011-10-20 10:45:47 -0700 | [diff] [blame] | 162 | |
| 163 | def ToRemoteSpec(self, projectName): |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 164 | fetchUrl = self.resolvedFetchUrl.rstrip('/') |
| 165 | url = fetchUrl + '/' + projectName |
Yestin Sun | b292b98 | 2012-07-02 07:32:50 -0700 | [diff] [blame] | 166 | remoteName = self.name |
Conley Owens | 1e7ab2a | 2013-10-08 17:26:57 -0700 | [diff] [blame] | 167 | if self.remoteAlias: |
David Pursehouse | 37128b6 | 2013-10-15 10:48:40 +0900 | [diff] [blame] | 168 | remoteName = self.remoteAlias |
Dan Willemsen | 96c2d65 | 2016-04-06 16:03:54 -0700 | [diff] [blame] | 169 | return RemoteSpec(remoteName, |
| 170 | url=url, |
Steve Rae | d648045 | 2016-08-10 15:00:00 -0700 | [diff] [blame] | 171 | pushUrl=self.pushUrl, |
Dan Willemsen | 96c2d65 | 2016-04-06 16:03:54 -0700 | [diff] [blame] | 172 | review=self.reviewUrl, |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 173 | orig_name=self.name, |
| 174 | fetchUrl=self.fetchUrl) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 175 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 176 | |
Shawn O. Pearce | c8a300f | 2009-05-18 13:19:57 -0700 | [diff] [blame] | 177 | class XmlManifest(object): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 178 | """manages the repo configuration file""" |
| 179 | |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 180 | def __init__(self, repodir, manifest_file, local_manifests=None): |
| 181 | """Initialize. |
| 182 | |
| 183 | Args: |
| 184 | repodir: Path to the .repo/ dir for holding all internal checkout state. |
| 185 | It must be in the top directory of the repo client checkout. |
| 186 | manifest_file: Full path to the manifest file to parse. This will usually |
| 187 | be |repodir|/|MANIFEST_FILE_NAME|. |
| 188 | local_manifests: Full path to the directory of local override manifests. |
| 189 | This will usually be |repodir|/|LOCAL_MANIFESTS_DIR_NAME|. |
| 190 | """ |
| 191 | # TODO(vapier): Move this out of this class. |
| 192 | self.globalConfig = GitConfig.ForUser() |
| 193 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 194 | self.repodir = os.path.abspath(repodir) |
| 195 | self.topdir = os.path.dirname(self.repodir) |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 196 | self.manifestFile = manifest_file |
| 197 | self.local_manifests = local_manifests |
Basil Gello | c745350 | 2018-05-25 20:23:52 +0300 | [diff] [blame] | 198 | self._load_local_manifests = True |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 199 | |
| 200 | self.repoProject = MetaProject(self, 'repo', |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 201 | gitdir=os.path.join(repodir, 'repo/.git'), |
| 202 | worktree=os.path.join(repodir, 'repo')) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 203 | |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 204 | mp = MetaProject(self, 'manifests', |
| 205 | gitdir=os.path.join(repodir, 'manifests.git'), |
| 206 | worktree=os.path.join(repodir, 'manifests')) |
| 207 | self.manifestProject = mp |
| 208 | |
| 209 | # This is a bit hacky, but we're in a chicken & egg situation: all the |
| 210 | # normal repo settings live in the manifestProject which we just setup |
| 211 | # above, so we couldn't easily query before that. We assume Project() |
| 212 | # init doesn't care if this changes afterwards. |
Mike Frysinger | d957ec6 | 2020-02-24 14:40:25 -0500 | [diff] [blame] | 213 | if os.path.exists(mp.gitdir) and mp.config.GetBoolean('repo.worktree'): |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 214 | mp.use_git_worktrees = True |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 215 | |
| 216 | self._Unload() |
| 217 | |
Basil Gello | c745350 | 2018-05-25 20:23:52 +0300 | [diff] [blame] | 218 | def Override(self, name, load_local_manifests=True): |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 219 | """Use a different manifest, just for the current instantiation. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 220 | """ |
Basil Gello | c745350 | 2018-05-25 20:23:52 +0300 | [diff] [blame] | 221 | path = None |
| 222 | |
| 223 | # Look for a manifest by path in the filesystem (including the cwd). |
| 224 | if not load_local_manifests: |
| 225 | local_path = os.path.abspath(name) |
| 226 | if os.path.isfile(local_path): |
| 227 | path = local_path |
| 228 | |
| 229 | # Look for manifests by name from the manifests repo. |
| 230 | if path is None: |
| 231 | path = os.path.join(self.manifestProject.worktree, name) |
| 232 | if not os.path.isfile(path): |
| 233 | raise ManifestParseError('manifest %s not found' % name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 234 | |
| 235 | old = self.manifestFile |
| 236 | try: |
Basil Gello | c745350 | 2018-05-25 20:23:52 +0300 | [diff] [blame] | 237 | self._load_local_manifests = load_local_manifests |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 238 | self.manifestFile = path |
| 239 | self._Unload() |
| 240 | self._Load() |
| 241 | finally: |
| 242 | self.manifestFile = old |
| 243 | |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 244 | def Link(self, name): |
| 245 | """Update the repo metadata to use a different manifest. |
| 246 | """ |
| 247 | self.Override(name) |
| 248 | |
Mike Frysinger | a269b1c | 2020-02-21 00:49:41 -0500 | [diff] [blame] | 249 | # Old versions of repo would generate symlinks we need to clean up. |
| 250 | if os.path.lexists(self.manifestFile): |
| 251 | platform_utils.remove(self.manifestFile) |
| 252 | # This file is interpreted as if it existed inside the manifest repo. |
| 253 | # That allows us to use <include> with the relative file name. |
| 254 | with open(self.manifestFile, 'w') as fp: |
| 255 | fp.write("""<?xml version="1.0" encoding="UTF-8"?> |
| 256 | <!-- |
| 257 | DO NOT EDIT THIS FILE! It is generated by repo and changes will be discarded. |
| 258 | If you want to use a different manifest, use `repo init -m <file>` instead. |
| 259 | |
| 260 | If you want to customize your checkout by overriding manifest settings, use |
| 261 | the local_manifests/ directory instead. |
| 262 | |
| 263 | For more information on repo manifests, check out: |
| 264 | https://gerrit.googlesource.com/git-repo/+/HEAD/docs/manifest-format.md |
| 265 | --> |
| 266 | <manifest> |
| 267 | <include name="%s" /> |
| 268 | </manifest> |
| 269 | """ % (name,)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 270 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 271 | def _RemoteToXml(self, r, doc, root): |
| 272 | e = doc.createElement('remote') |
| 273 | root.appendChild(e) |
| 274 | e.setAttribute('name', r.name) |
| 275 | e.setAttribute('fetch', r.fetchUrl) |
Steve Rae | d648045 | 2016-08-10 15:00:00 -0700 | [diff] [blame] | 276 | if r.pushUrl is not None: |
| 277 | e.setAttribute('pushurl', r.pushUrl) |
Conley Owens | 1e7ab2a | 2013-10-08 17:26:57 -0700 | [diff] [blame] | 278 | if r.remoteAlias is not None: |
| 279 | e.setAttribute('alias', r.remoteAlias) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 280 | if r.reviewUrl is not None: |
| 281 | e.setAttribute('review', r.reviewUrl) |
Anthony King | 36ea2fb | 2014-05-06 11:54:01 +0100 | [diff] [blame] | 282 | if r.revision is not None: |
| 283 | e.setAttribute('revision', r.revision) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 284 | |
Mike Frysinger | 51e39d5 | 2020-12-04 05:32:06 -0500 | [diff] [blame] | 285 | def _ParseList(self, field): |
| 286 | """Parse fields that contain flattened lists. |
| 287 | |
| 288 | These are whitespace & comma separated. Empty elements will be discarded. |
| 289 | """ |
| 290 | return [x for x in re.split(r'[,\s]+', field) if x] |
Josh Triplett | 884a387 | 2014-06-12 14:57:29 -0700 | [diff] [blame] | 291 | |
Mike Frysinger | 23411d3 | 2020-09-02 04:31:10 -0400 | [diff] [blame] | 292 | def ToXml(self, peg_rev=False, peg_rev_upstream=True, peg_rev_dest_branch=True, groups=None): |
| 293 | """Return the current manifest XML.""" |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 294 | mp = self.manifestProject |
| 295 | |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 296 | if groups is None: |
| 297 | groups = mp.config.GetString('manifest.groups') |
Matt Gumbel | 0c635bb | 2012-12-21 10:14:53 -0800 | [diff] [blame] | 298 | if groups: |
Mike Frysinger | 51e39d5 | 2020-12-04 05:32:06 -0500 | [diff] [blame] | 299 | groups = self._ParseList(groups) |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 300 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 301 | doc = xml.dom.minidom.Document() |
| 302 | root = doc.createElement('manifest') |
| 303 | doc.appendChild(root) |
| 304 | |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 305 | # Save out the notice. There's a little bit of work here to give it the |
| 306 | # right whitespace, which assumes that the notice is automatically indented |
| 307 | # by 4 by minidom. |
| 308 | if self.notice: |
| 309 | notice_element = root.appendChild(doc.createElement('notice')) |
| 310 | notice_lines = self.notice.splitlines() |
David Pursehouse | 54a4e60 | 2020-02-12 14:31:05 +0900 | [diff] [blame] | 311 | indented_notice = ('\n'.join(" " * 4 + line for line in notice_lines))[4:] |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 312 | notice_element.appendChild(doc.createTextNode(indented_notice)) |
| 313 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 314 | d = self.default |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 315 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 316 | for r in sorted(self.remotes): |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 317 | self._RemoteToXml(self.remotes[r], doc, root) |
| 318 | if self.remotes: |
| 319 | root.appendChild(doc.createTextNode('')) |
| 320 | |
| 321 | have_default = False |
| 322 | e = doc.createElement('default') |
| 323 | if d.remote: |
| 324 | have_default = True |
| 325 | e.setAttribute('remote', d.remote.name) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 326 | if d.revisionExpr: |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 327 | have_default = True |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 328 | e.setAttribute('revision', d.revisionExpr) |
Simon Ruggier | 7e59de2 | 2015-07-24 12:50:06 +0200 | [diff] [blame] | 329 | if d.destBranchExpr: |
| 330 | have_default = True |
| 331 | e.setAttribute('dest-branch', d.destBranchExpr) |
Nasser Grainawi | da40341 | 2018-05-04 12:53:29 -0600 | [diff] [blame] | 332 | if d.upstreamExpr: |
| 333 | have_default = True |
| 334 | e.setAttribute('upstream', d.upstreamExpr) |
Shawn O. Pearce | 6392c87 | 2011-09-22 17:44:31 -0700 | [diff] [blame] | 335 | if d.sync_j > 1: |
| 336 | have_default = True |
| 337 | e.setAttribute('sync-j', '%d' % d.sync_j) |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 338 | if d.sync_c: |
| 339 | have_default = True |
| 340 | e.setAttribute('sync-c', 'true') |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 341 | if d.sync_s: |
| 342 | have_default = True |
| 343 | e.setAttribute('sync-s', 'true') |
YOUNG HO CHA | a32c92c | 2018-02-14 16:57:31 +0900 | [diff] [blame] | 344 | if not d.sync_tags: |
| 345 | have_default = True |
| 346 | e.setAttribute('sync-tags', 'false') |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 347 | if have_default: |
| 348 | root.appendChild(e) |
| 349 | root.appendChild(doc.createTextNode('')) |
| 350 | |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 351 | if self._manifest_server: |
| 352 | e = doc.createElement('manifest-server') |
| 353 | e.setAttribute('url', self._manifest_server) |
| 354 | root.appendChild(e) |
| 355 | root.appendChild(doc.createTextNode('')) |
| 356 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 357 | def output_projects(parent, parent_node, projects): |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 358 | for project_name in projects: |
| 359 | for project in self._projects[project_name]: |
| 360 | output_project(parent, parent_node, project) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 361 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 362 | def output_project(parent, parent_node, p): |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 363 | if not p.MatchesGroups(groups): |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 364 | return |
| 365 | |
| 366 | name = p.name |
| 367 | relpath = p.relpath |
| 368 | if parent: |
| 369 | name = self._UnjoinName(parent.name, name) |
| 370 | relpath = self._UnjoinRelpath(parent.relpath, relpath) |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 371 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 372 | e = doc.createElement('project') |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 373 | parent_node.appendChild(e) |
| 374 | e.setAttribute('name', name) |
| 375 | if relpath != name: |
| 376 | e.setAttribute('path', relpath) |
Conley Owens | a17d7af | 2013-10-16 14:38:09 -0700 | [diff] [blame] | 377 | remoteName = None |
| 378 | if d.remote: |
Dan Willemsen | 96c2d65 | 2016-04-06 16:03:54 -0700 | [diff] [blame] | 379 | remoteName = d.remote.name |
| 380 | if not d.remote or p.remote.orig_name != remoteName: |
| 381 | remoteName = p.remote.orig_name |
Anthony King | 36ea2fb | 2014-05-06 11:54:01 +0100 | [diff] [blame] | 382 | e.setAttribute('remote', remoteName) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 383 | if peg_rev: |
| 384 | if self.IsMirror: |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 385 | value = p.bare_git.rev_parse(p.revisionExpr + '^0') |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 386 | else: |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 387 | value = p.work_git.rev_parse(HEAD + '^0') |
| 388 | e.setAttribute('revision', value) |
Conley Owens | 551dfec | 2015-07-10 14:54:54 -0700 | [diff] [blame] | 389 | if peg_rev_upstream: |
| 390 | if p.upstream: |
| 391 | e.setAttribute('upstream', p.upstream) |
| 392 | elif value != p.revisionExpr: |
| 393 | # Only save the origin if the origin is not a sha1, and the default |
| 394 | # isn't our value |
| 395 | e.setAttribute('upstream', p.revisionExpr) |
Sean McAllister | af908cb | 2020-04-20 08:41:58 -0600 | [diff] [blame] | 396 | |
| 397 | if peg_rev_dest_branch: |
| 398 | if p.dest_branch: |
| 399 | e.setAttribute('dest-branch', p.dest_branch) |
| 400 | elif value != p.revisionExpr: |
| 401 | e.setAttribute('dest-branch', p.revisionExpr) |
| 402 | |
Anthony King | 36ea2fb | 2014-05-06 11:54:01 +0100 | [diff] [blame] | 403 | else: |
Dan Willemsen | 96c2d65 | 2016-04-06 16:03:54 -0700 | [diff] [blame] | 404 | revision = self.remotes[p.remote.orig_name].revision or d.revisionExpr |
Anthony King | 36ea2fb | 2014-05-06 11:54:01 +0100 | [diff] [blame] | 405 | if not revision or revision != p.revisionExpr: |
| 406 | e.setAttribute('revision', p.revisionExpr) |
Raman Tenneti | b5c5a5e | 2021-02-06 09:44:15 -0800 | [diff] [blame] | 407 | elif p.revisionId: |
| 408 | e.setAttribute('revision', p.revisionId) |
Nasser Grainawi | da40341 | 2018-05-04 12:53:29 -0600 | [diff] [blame] | 409 | if (p.upstream and (p.upstream != p.revisionExpr or |
| 410 | p.upstream != d.upstreamExpr)): |
Mani Chandel | 7a91d51 | 2014-07-24 16:27:08 +0530 | [diff] [blame] | 411 | e.setAttribute('upstream', p.upstream) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 412 | |
Simon Ruggier | 7e59de2 | 2015-07-24 12:50:06 +0200 | [diff] [blame] | 413 | if p.dest_branch and p.dest_branch != d.destBranchExpr: |
| 414 | e.setAttribute('dest-branch', p.dest_branch) |
| 415 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 416 | for c in p.copyfiles: |
| 417 | ce = doc.createElement('copyfile') |
| 418 | ce.setAttribute('src', c.src) |
| 419 | ce.setAttribute('dest', c.dest) |
| 420 | e.appendChild(ce) |
| 421 | |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 422 | for l in p.linkfiles: |
| 423 | le = doc.createElement('linkfile') |
| 424 | le.setAttribute('src', l.src) |
| 425 | le.setAttribute('dest', l.dest) |
| 426 | e.appendChild(le) |
| 427 | |
Conley Owens | bb1b5f5 | 2012-08-13 13:11:18 -0700 | [diff] [blame] | 428 | default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath] |
Dmitry Fink | 17f85ea | 2012-08-06 14:52:29 -0700 | [diff] [blame] | 429 | 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] | 430 | if egroups: |
| 431 | e.setAttribute('groups', ','.join(egroups)) |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 432 | |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 433 | for a in p.annotations: |
| 434 | if a.keep == "true": |
| 435 | ae = doc.createElement('annotation') |
| 436 | ae.setAttribute('name', a.name) |
| 437 | ae.setAttribute('value', a.value) |
| 438 | e.appendChild(ae) |
| 439 | |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 440 | if p.sync_c: |
| 441 | e.setAttribute('sync-c', 'true') |
| 442 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 443 | if p.sync_s: |
| 444 | e.setAttribute('sync-s', 'true') |
| 445 | |
YOUNG HO CHA | a32c92c | 2018-02-14 16:57:31 +0900 | [diff] [blame] | 446 | if not p.sync_tags: |
| 447 | e.setAttribute('sync-tags', 'false') |
| 448 | |
Dan Willemsen | 8840922 | 2015-08-17 15:29:10 -0700 | [diff] [blame] | 449 | if p.clone_depth: |
| 450 | e.setAttribute('clone-depth', str(p.clone_depth)) |
| 451 | |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 452 | self._output_manifest_project_extras(p, e) |
| 453 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 454 | if p.subprojects: |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 455 | subprojects = set(subp.name for subp in p.subprojects) |
| 456 | output_projects(p, e, list(sorted(subprojects))) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 457 | |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 458 | projects = set(p.name for p in self._paths.values() if not p.parent) |
| 459 | output_projects(None, root, list(sorted(projects))) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 460 | |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 461 | if self._repo_hooks_project: |
| 462 | root.appendChild(doc.createTextNode('')) |
| 463 | e = doc.createElement('repo-hooks') |
| 464 | e.setAttribute('in-project', self._repo_hooks_project.name) |
| 465 | e.setAttribute('enabled-list', |
| 466 | ' '.join(self._repo_hooks_project.enabled_repo_hooks)) |
| 467 | root.appendChild(e) |
| 468 | |
Raman Tenneti | 1bb4fb2 | 2021-01-07 16:50:45 -0800 | [diff] [blame] | 469 | if self._superproject: |
| 470 | root.appendChild(doc.createTextNode('')) |
| 471 | e = doc.createElement('superproject') |
| 472 | e.setAttribute('name', self._superproject['name']) |
| 473 | remoteName = None |
| 474 | if d.remote: |
| 475 | remoteName = d.remote.name |
| 476 | remote = self._superproject.get('remote') |
| 477 | if not d.remote or remote.orig_name != remoteName: |
| 478 | remoteName = remote.orig_name |
| 479 | e.setAttribute('remote', remoteName) |
| 480 | root.appendChild(e) |
| 481 | |
Mike Frysinger | 23411d3 | 2020-09-02 04:31:10 -0400 | [diff] [blame] | 482 | return doc |
| 483 | |
| 484 | def ToDict(self, **kwargs): |
| 485 | """Return the current manifest as a dictionary.""" |
| 486 | # Elements that may only appear once. |
| 487 | SINGLE_ELEMENTS = { |
| 488 | 'notice', |
| 489 | 'default', |
| 490 | 'manifest-server', |
| 491 | 'repo-hooks', |
Raman Tenneti | 1bb4fb2 | 2021-01-07 16:50:45 -0800 | [diff] [blame] | 492 | 'superproject', |
Mike Frysinger | 23411d3 | 2020-09-02 04:31:10 -0400 | [diff] [blame] | 493 | } |
| 494 | # Elements that may be repeated. |
| 495 | MULTI_ELEMENTS = { |
| 496 | 'remote', |
| 497 | 'remove-project', |
| 498 | 'project', |
| 499 | 'extend-project', |
| 500 | 'include', |
| 501 | # These are children of 'project' nodes. |
| 502 | 'annotation', |
| 503 | 'project', |
| 504 | 'copyfile', |
| 505 | 'linkfile', |
| 506 | } |
| 507 | |
| 508 | doc = self.ToXml(**kwargs) |
| 509 | ret = {} |
| 510 | |
| 511 | def append_children(ret, node): |
| 512 | for child in node.childNodes: |
| 513 | if child.nodeType == xml.dom.Node.ELEMENT_NODE: |
| 514 | attrs = child.attributes |
| 515 | element = dict((attrs.item(i).localName, attrs.item(i).value) |
| 516 | for i in range(attrs.length)) |
| 517 | if child.nodeName in SINGLE_ELEMENTS: |
| 518 | ret[child.nodeName] = element |
| 519 | elif child.nodeName in MULTI_ELEMENTS: |
| 520 | ret.setdefault(child.nodeName, []).append(element) |
| 521 | else: |
| 522 | raise ManifestParseError('Unhandled element "%s"' % (child.nodeName,)) |
| 523 | |
| 524 | append_children(element, child) |
| 525 | |
| 526 | append_children(ret, doc.firstChild) |
| 527 | |
| 528 | return ret |
| 529 | |
| 530 | def Save(self, fd, **kwargs): |
| 531 | """Write the current manifest out to the given file descriptor.""" |
| 532 | doc = self.ToXml(**kwargs) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 533 | doc.writexml(fd, '', ' ', '\n', 'UTF-8') |
| 534 | |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 535 | def _output_manifest_project_extras(self, p, e): |
| 536 | """Manifests can modify e if they support extra project attributes.""" |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 537 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 538 | @property |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 539 | def paths(self): |
| 540 | self._Load() |
| 541 | return self._paths |
| 542 | |
| 543 | @property |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 544 | def projects(self): |
| 545 | self._Load() |
Anthony King | d58bfe5 | 2014-05-05 23:30:49 +0100 | [diff] [blame] | 546 | return list(self._paths.values()) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 547 | |
| 548 | @property |
| 549 | def remotes(self): |
| 550 | self._Load() |
| 551 | return self._remotes |
| 552 | |
| 553 | @property |
| 554 | def default(self): |
| 555 | self._Load() |
| 556 | return self._default |
| 557 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 558 | @property |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 559 | def repo_hooks_project(self): |
| 560 | self._Load() |
| 561 | return self._repo_hooks_project |
| 562 | |
| 563 | @property |
Raman Tenneti | 1bb4fb2 | 2021-01-07 16:50:45 -0800 | [diff] [blame] | 564 | def superproject(self): |
| 565 | self._Load() |
| 566 | return self._superproject |
| 567 | |
| 568 | @property |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 569 | def notice(self): |
| 570 | self._Load() |
| 571 | return self._notice |
| 572 | |
| 573 | @property |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 574 | def manifest_server(self): |
| 575 | self._Load() |
Shawn O. Pearce | 34fb20f | 2011-11-30 13:41:02 -0800 | [diff] [blame] | 576 | return self._manifest_server |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 577 | |
| 578 | @property |
Xin Li | d79a4bc | 2020-05-20 16:03:45 -0700 | [diff] [blame] | 579 | def CloneBundle(self): |
| 580 | clone_bundle = self.manifestProject.config.GetBoolean('repo.clonebundle') |
| 581 | if clone_bundle is None: |
| 582 | return False if self.manifestProject.config.GetBoolean('repo.partialclone') else True |
| 583 | else: |
| 584 | return clone_bundle |
| 585 | |
| 586 | @property |
Xin Li | 745be2e | 2019-06-03 11:24:30 -0700 | [diff] [blame] | 587 | def CloneFilter(self): |
| 588 | if self.manifestProject.config.GetBoolean('repo.partialclone'): |
| 589 | return self.manifestProject.config.GetString('repo.clonefilter') |
| 590 | return None |
| 591 | |
| 592 | @property |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 593 | def IsMirror(self): |
| 594 | return self.manifestProject.config.GetBoolean('repo.mirror') |
| 595 | |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 596 | @property |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 597 | def UseGitWorktrees(self): |
| 598 | return self.manifestProject.config.GetBoolean('repo.worktree') |
| 599 | |
| 600 | @property |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 601 | def IsArchive(self): |
| 602 | return self.manifestProject.config.GetBoolean('repo.archive') |
| 603 | |
Martin Kelly | e4e94d2 | 2017-03-21 16:05:12 -0700 | [diff] [blame] | 604 | @property |
| 605 | def HasSubmodules(self): |
| 606 | return self.manifestProject.config.GetBoolean('repo.submodules') |
| 607 | |
Raman Tenneti | 080877e | 2021-03-09 15:19:06 -0800 | [diff] [blame] | 608 | def GetDefaultGroupsStr(self): |
| 609 | """Returns the default group string for the platform.""" |
| 610 | return 'default,platform-' + platform.system().lower() |
| 611 | |
| 612 | def GetGroupsStr(self): |
| 613 | """Returns the manifest group string that should be synced.""" |
| 614 | groups = self.manifestProject.config.GetString('manifest.groups') |
| 615 | if not groups: |
| 616 | groups = self.GetDefaultGroupsStr() |
| 617 | return groups |
| 618 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 619 | def _Unload(self): |
| 620 | self._loaded = False |
| 621 | self._projects = {} |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 622 | self._paths = {} |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 623 | self._remotes = {} |
| 624 | self._default = None |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 625 | self._repo_hooks_project = None |
Raman Tenneti | 1bb4fb2 | 2021-01-07 16:50:45 -0800 | [diff] [blame] | 626 | self._superproject = {} |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 627 | self._notice = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 628 | self.branch = None |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 629 | self._manifest_server = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 630 | |
| 631 | def _Load(self): |
| 632 | if not self._loaded: |
Shawn O. Pearce | 2450a29 | 2008-11-04 08:22:07 -0800 | [diff] [blame] | 633 | m = self.manifestProject |
| 634 | b = m.GetBranch(m.CurrentBranch).merge |
Shawn O. Pearce | 21c5c34 | 2009-06-25 16:47:30 -0700 | [diff] [blame] | 635 | if b is not None and b.startswith(R_HEADS): |
Shawn O. Pearce | 2450a29 | 2008-11-04 08:22:07 -0800 | [diff] [blame] | 636 | b = b[len(R_HEADS):] |
| 637 | self.branch = b |
| 638 | |
Mike Frysinger | 5413397 | 2021-03-01 21:38:08 -0500 | [diff] [blame] | 639 | # The manifestFile was specified by the user which is why we allow include |
| 640 | # paths to point anywhere. |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 641 | nodes = [] |
Mike Frysinger | 5413397 | 2021-03-01 21:38:08 -0500 | [diff] [blame] | 642 | nodes.append(self._ParseManifestXml( |
| 643 | self.manifestFile, self.manifestProject.worktree, |
| 644 | restrict_includes=False)) |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 645 | |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 646 | if self._load_local_manifests and self.local_manifests: |
Basil Gello | c745350 | 2018-05-25 20:23:52 +0300 | [diff] [blame] | 647 | try: |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 648 | for local_file in sorted(platform_utils.listdir(self.local_manifests)): |
Basil Gello | c745350 | 2018-05-25 20:23:52 +0300 | [diff] [blame] | 649 | if local_file.endswith('.xml'): |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 650 | local = os.path.join(self.local_manifests, local_file) |
Mike Frysinger | 5413397 | 2021-03-01 21:38:08 -0500 | [diff] [blame] | 651 | # Since local manifests are entirely managed by the user, allow |
| 652 | # them to point anywhere the user wants. |
| 653 | nodes.append(self._ParseManifestXml( |
| 654 | local, self.repodir, restrict_includes=False)) |
Basil Gello | c745350 | 2018-05-25 20:23:52 +0300 | [diff] [blame] | 655 | except OSError: |
| 656 | pass |
David Pursehouse | 2d5a0df | 2012-11-13 02:50:36 +0900 | [diff] [blame] | 657 | |
Joe Onorato | 26e2475 | 2013-01-11 12:35:53 -0800 | [diff] [blame] | 658 | try: |
| 659 | self._ParseManifest(nodes) |
| 660 | except ManifestParseError as e: |
| 661 | # There was a problem parsing, unload ourselves in case they catch |
| 662 | # this error and try again later, we will show the correct error |
| 663 | self._Unload() |
| 664 | raise e |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 665 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 666 | if self.IsMirror: |
| 667 | self._AddMetaProjectMirror(self.repoProject) |
| 668 | self._AddMetaProjectMirror(self.manifestProject) |
| 669 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 670 | self._loaded = True |
| 671 | |
Mike Frysinger | 5413397 | 2021-03-01 21:38:08 -0500 | [diff] [blame] | 672 | def _ParseManifestXml(self, path, include_root, parent_groups='', |
| 673 | restrict_includes=True): |
| 674 | """Parse a manifest XML and return the computed nodes. |
| 675 | |
| 676 | Args: |
| 677 | path: The XML file to read & parse. |
| 678 | include_root: The path to interpret include "name"s relative to. |
| 679 | parent_groups: The groups to apply to this projects. |
| 680 | restrict_includes: Whether to constrain the "name" attribute of includes. |
| 681 | |
| 682 | Returns: |
| 683 | List of XML nodes. |
| 684 | """ |
David Pursehouse | f7fc8a9 | 2012-11-13 04:00:28 +0900 | [diff] [blame] | 685 | try: |
| 686 | root = xml.dom.minidom.parse(path) |
David Pursehouse | 2d5a0df | 2012-11-13 02:50:36 +0900 | [diff] [blame] | 687 | except (OSError, xml.parsers.expat.ExpatError) as e: |
David Pursehouse | f7fc8a9 | 2012-11-13 04:00:28 +0900 | [diff] [blame] | 688 | raise ManifestParseError("error parsing manifest %s: %s" % (path, e)) |
| 689 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 690 | if not root or not root.childNodes: |
Brian Harring | 2644874 | 2011-04-28 05:04:41 -0700 | [diff] [blame] | 691 | raise ManifestParseError("no root node in %s" % (path,)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 692 | |
Jooncheol Park | 34acdd2 | 2012-08-27 02:25:59 +0900 | [diff] [blame] | 693 | for manifest in root.childNodes: |
| 694 | if manifest.nodeName == 'manifest': |
| 695 | break |
| 696 | else: |
Brian Harring | 2644874 | 2011-04-28 05:04:41 -0700 | [diff] [blame] | 697 | raise ManifestParseError("no <manifest> in %s" % (path,)) |
| 698 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 699 | nodes = [] |
David Pursehouse | 65b0ba5 | 2018-06-24 16:21:51 +0900 | [diff] [blame] | 700 | for node in manifest.childNodes: |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 701 | if node.nodeName == 'include': |
| 702 | name = self._reqatt(node, 'name') |
Mike Frysinger | 5413397 | 2021-03-01 21:38:08 -0500 | [diff] [blame] | 703 | if restrict_includes: |
| 704 | msg = self._CheckLocalPath(name) |
| 705 | if msg: |
| 706 | raise ManifestInvalidPathError( |
| 707 | '<include> invalid "name": %s: %s' % (name, msg)) |
Fredrik de Groot | 352c93b | 2020-10-06 12:55:14 +0200 | [diff] [blame] | 708 | include_groups = '' |
| 709 | if parent_groups: |
| 710 | include_groups = parent_groups |
| 711 | if node.hasAttribute('groups'): |
| 712 | include_groups = node.getAttribute('groups') + ',' + include_groups |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 713 | fp = os.path.join(include_root, name) |
| 714 | if not os.path.isfile(fp): |
Mike Frysinger | 5413397 | 2021-03-01 21:38:08 -0500 | [diff] [blame] | 715 | raise ManifestParseError("include [%s/]%s doesn't exist or isn't a file" |
| 716 | % (include_root, name)) |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 717 | try: |
Fredrik de Groot | 352c93b | 2020-10-06 12:55:14 +0200 | [diff] [blame] | 718 | nodes.extend(self._ParseManifestXml(fp, include_root, include_groups)) |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 719 | # should isolate this to the exact exception, but that's |
| 720 | # tricky. actual parsing implementation may vary. |
Mike Frysinger | 5413397 | 2021-03-01 21:38:08 -0500 | [diff] [blame] | 721 | except (KeyboardInterrupt, RuntimeError, SystemExit, ManifestParseError): |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 722 | raise |
| 723 | except Exception as e: |
| 724 | raise ManifestParseError( |
Mike Frysinger | ec558df | 2019-07-05 01:38:05 -0400 | [diff] [blame] | 725 | "failed parsing included manifest %s: %s" % (name, e)) |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 726 | else: |
Fredrik de Groot | 352c93b | 2020-10-06 12:55:14 +0200 | [diff] [blame] | 727 | if parent_groups and node.nodeName == 'project': |
| 728 | nodeGroups = parent_groups |
| 729 | if node.hasAttribute('groups'): |
| 730 | nodeGroups = node.getAttribute('groups') + ',' + nodeGroups |
| 731 | node.setAttribute('groups', nodeGroups) |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 732 | nodes.append(node) |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 733 | return nodes |
Brian Harring | 2644874 | 2011-04-28 05:04:41 -0700 | [diff] [blame] | 734 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 735 | def _ParseManifest(self, node_list): |
| 736 | for node in itertools.chain(*node_list): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 737 | if node.nodeName == 'remote': |
| 738 | remote = self._ParseRemote(node) |
David Pursehouse | 717ece9 | 2012-11-13 08:49:16 +0900 | [diff] [blame] | 739 | if remote: |
| 740 | if remote.name in self._remotes: |
| 741 | if remote != self._remotes[remote.name]: |
| 742 | raise ManifestParseError( |
| 743 | 'remote %s already exists with different attributes' % |
| 744 | (remote.name)) |
| 745 | else: |
| 746 | self._remotes[remote.name] = remote |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 747 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 748 | for node in itertools.chain(*node_list): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 749 | if node.nodeName == 'default': |
Julien Campergue | 7487992 | 2013-10-09 14:38:46 +0200 | [diff] [blame] | 750 | new_default = self._ParseDefault(node) |
| 751 | if self._default is None: |
| 752 | self._default = new_default |
| 753 | elif new_default != self._default: |
David Pursehouse | 37128b6 | 2013-10-15 10:48:40 +0900 | [diff] [blame] | 754 | raise ManifestParseError('duplicate default in %s' % |
| 755 | (self.manifestFile)) |
Julien Campergue | 7487992 | 2013-10-09 14:38:46 +0200 | [diff] [blame] | 756 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 757 | if self._default is None: |
| 758 | self._default = _Default() |
| 759 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 760 | for node in itertools.chain(*node_list): |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 761 | if node.nodeName == 'notice': |
| 762 | if self._notice is not None: |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 763 | raise ManifestParseError( |
| 764 | 'duplicate notice in %s' % |
| 765 | (self.manifestFile)) |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 766 | self._notice = self._ParseNotice(node) |
| 767 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 768 | for node in itertools.chain(*node_list): |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 769 | if node.nodeName == 'manifest-server': |
| 770 | url = self._reqatt(node, 'url') |
| 771 | if self._manifest_server is not None: |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 772 | raise ManifestParseError( |
| 773 | 'duplicate manifest-server in %s' % |
| 774 | (self.manifestFile)) |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 775 | self._manifest_server = url |
| 776 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 777 | def recursively_add_projects(project): |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 778 | projects = self._projects.setdefault(project.name, []) |
| 779 | if project.relpath is None: |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 780 | raise ManifestParseError( |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 781 | 'missing path for %s in %s' % |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 782 | (project.name, self.manifestFile)) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 783 | if project.relpath in self._paths: |
| 784 | raise ManifestParseError( |
| 785 | 'duplicate path %s in %s' % |
| 786 | (project.relpath, self.manifestFile)) |
| 787 | self._paths[project.relpath] = project |
| 788 | projects.append(project) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 789 | for subproject in project.subprojects: |
| 790 | recursively_add_projects(subproject) |
| 791 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 792 | for node in itertools.chain(*node_list): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 793 | if node.nodeName == 'project': |
| 794 | project = self._ParseProject(node) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 795 | recursively_add_projects(project) |
Josh Triplett | 884a387 | 2014-06-12 14:57:29 -0700 | [diff] [blame] | 796 | if node.nodeName == 'extend-project': |
| 797 | name = self._reqatt(node, 'name') |
| 798 | |
| 799 | if name not in self._projects: |
| 800 | raise ManifestParseError('extend-project element specifies non-existent ' |
| 801 | 'project: %s' % name) |
| 802 | |
| 803 | path = node.getAttribute('path') |
| 804 | groups = node.getAttribute('groups') |
| 805 | if groups: |
Mike Frysinger | 51e39d5 | 2020-12-04 05:32:06 -0500 | [diff] [blame] | 806 | groups = self._ParseList(groups) |
Luis Hector Chavez | 7d52585 | 2018-03-15 09:54:08 -0700 | [diff] [blame] | 807 | revision = node.getAttribute('revision') |
Kyunam Jo | bd0aae9 | 2020-02-04 11:38:53 +0900 | [diff] [blame] | 808 | remote = node.getAttribute('remote') |
| 809 | if remote: |
| 810 | remote = self._get_remote(node) |
Josh Triplett | 884a387 | 2014-06-12 14:57:29 -0700 | [diff] [blame] | 811 | |
| 812 | for p in self._projects[name]: |
| 813 | if path and p.relpath != path: |
| 814 | continue |
| 815 | if groups: |
| 816 | p.groups.extend(groups) |
Luis Hector Chavez | 7d52585 | 2018-03-15 09:54:08 -0700 | [diff] [blame] | 817 | if revision: |
| 818 | p.revisionExpr = revision |
Miguel Gaio | 1f20776 | 2020-07-17 14:09:13 +0200 | [diff] [blame] | 819 | if IsId(revision): |
| 820 | p.revisionId = revision |
| 821 | else: |
| 822 | p.revisionId = None |
Kyunam Jo | bd0aae9 | 2020-02-04 11:38:53 +0900 | [diff] [blame] | 823 | if remote: |
| 824 | p.remote = remote.ToRemoteSpec(name) |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 825 | if node.nodeName == 'repo-hooks': |
| 826 | # Get the name of the project and the (space-separated) list of enabled. |
| 827 | repo_hooks_project = self._reqatt(node, 'in-project') |
Mike Frysinger | 51e39d5 | 2020-12-04 05:32:06 -0500 | [diff] [blame] | 828 | enabled_repo_hooks = self._ParseList(self._reqatt(node, 'enabled-list')) |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 829 | |
| 830 | # Only one project can be the hooks project |
| 831 | if self._repo_hooks_project is not None: |
| 832 | raise ManifestParseError( |
| 833 | 'duplicate repo-hooks in %s' % |
| 834 | (self.manifestFile)) |
| 835 | |
| 836 | # Store a reference to the Project. |
| 837 | try: |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 838 | repo_hooks_projects = self._projects[repo_hooks_project] |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 839 | except KeyError: |
| 840 | raise ManifestParseError( |
| 841 | 'project %s not found for repo-hooks' % |
| 842 | (repo_hooks_project)) |
| 843 | |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 844 | if len(repo_hooks_projects) != 1: |
| 845 | raise ManifestParseError( |
| 846 | 'internal error parsing repo-hooks in %s' % |
| 847 | (self.manifestFile)) |
| 848 | self._repo_hooks_project = repo_hooks_projects[0] |
| 849 | |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 850 | # Store the enabled hooks in the Project object. |
| 851 | self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks |
Raman Tenneti | 1bb4fb2 | 2021-01-07 16:50:45 -0800 | [diff] [blame] | 852 | if node.nodeName == 'superproject': |
| 853 | name = self._reqatt(node, 'name') |
| 854 | # There can only be one superproject. |
| 855 | if self._superproject.get('name'): |
| 856 | raise ManifestParseError( |
| 857 | 'duplicate superproject in %s' % |
| 858 | (self.manifestFile)) |
| 859 | self._superproject['name'] = name |
| 860 | remote_name = node.getAttribute('remote') |
| 861 | if not remote_name: |
| 862 | remote = self._default.remote |
| 863 | else: |
| 864 | remote = self._get_remote(node) |
| 865 | if remote is None: |
| 866 | raise ManifestParseError("no remote for superproject %s within %s" % |
| 867 | (name, self.manifestFile)) |
| 868 | self._superproject['remote'] = remote.ToRemoteSpec(name) |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 869 | if node.nodeName == 'remove-project': |
| 870 | name = self._reqatt(node, 'name') |
David James | b8433df | 2014-01-30 10:11:17 -0800 | [diff] [blame] | 871 | |
| 872 | if name not in self._projects: |
David Pursehouse | f910748 | 2012-11-16 19:12:32 +0900 | [diff] [blame] | 873 | raise ManifestParseError('remove-project element specifies non-existent ' |
| 874 | 'project: %s' % name) |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 875 | |
David James | b8433df | 2014-01-30 10:11:17 -0800 | [diff] [blame] | 876 | for p in self._projects[name]: |
| 877 | del self._paths[p.relpath] |
| 878 | del self._projects[name] |
| 879 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 880 | # If the manifest removes the hooks project, treat it as if it deleted |
| 881 | # the repo-hooks element too. |
| 882 | if self._repo_hooks_project and (self._repo_hooks_project.name == name): |
| 883 | self._repo_hooks_project = None |
| 884 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 885 | def _AddMetaProjectMirror(self, m): |
| 886 | name = None |
| 887 | m_url = m.GetRemote(m.remote.name).url |
| 888 | if m_url.endswith('/.git'): |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 889 | raise ManifestParseError('refusing to mirror %s' % m_url) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 890 | |
| 891 | if self._default and self._default.remote: |
Conley Owens | ceea368 | 2011-10-20 10:45:47 -0700 | [diff] [blame] | 892 | url = self._default.remote.resolvedFetchUrl |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 893 | if not url.endswith('/'): |
| 894 | url += '/' |
| 895 | if m_url.startswith(url): |
| 896 | remote = self._default.remote |
| 897 | name = m_url[len(url):] |
| 898 | |
| 899 | if name is None: |
| 900 | s = m_url.rindex('/') + 1 |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 901 | manifestUrl = self.manifestProject.config.GetString('remote.origin.url') |
Shawn O. Pearce | f35b2d9 | 2012-08-02 11:46:22 -0700 | [diff] [blame] | 902 | remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 903 | name = m_url[s:] |
| 904 | |
| 905 | if name.endswith('.git'): |
| 906 | name = name[:-4] |
| 907 | |
| 908 | if name not in self._projects: |
| 909 | m.PreSync() |
| 910 | gitdir = os.path.join(self.topdir, '%s.git' % name) |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 911 | project = Project(manifest=self, |
| 912 | name=name, |
| 913 | remote=remote.ToRemoteSpec(name), |
| 914 | gitdir=gitdir, |
| 915 | objdir=gitdir, |
| 916 | worktree=None, |
| 917 | relpath=name or None, |
| 918 | revisionExpr=m.revisionExpr, |
| 919 | revisionId=None) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 920 | self._projects[project.name] = [project] |
Kwanhong Lee | ccd218c | 2014-02-17 13:07:32 +0900 | [diff] [blame] | 921 | self._paths[project.relpath] = project |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 922 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 923 | def _ParseRemote(self, node): |
| 924 | """ |
| 925 | reads a <remote> element from the manifest file |
| 926 | """ |
| 927 | name = self._reqatt(node, 'name') |
Yestin Sun | b292b98 | 2012-07-02 07:32:50 -0700 | [diff] [blame] | 928 | alias = node.getAttribute('alias') |
| 929 | if alias == '': |
| 930 | alias = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 931 | fetch = self._reqatt(node, 'fetch') |
Steve Rae | d648045 | 2016-08-10 15:00:00 -0700 | [diff] [blame] | 932 | pushUrl = node.getAttribute('pushurl') |
| 933 | if pushUrl == '': |
| 934 | pushUrl = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 935 | review = node.getAttribute('review') |
Shawn O. Pearce | ae6e094 | 2008-11-06 10:25:35 -0800 | [diff] [blame] | 936 | if review == '': |
| 937 | review = None |
Anthony King | 36ea2fb | 2014-05-06 11:54:01 +0100 | [diff] [blame] | 938 | revision = node.getAttribute('revision') |
| 939 | if revision == '': |
| 940 | revision = None |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 941 | manifestUrl = self.manifestProject.config.GetString('remote.origin.url') |
Steve Rae | d648045 | 2016-08-10 15:00:00 -0700 | [diff] [blame] | 942 | return _XmlRemote(name, alias, fetch, pushUrl, manifestUrl, review, revision) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 943 | |
| 944 | def _ParseDefault(self, node): |
| 945 | """ |
| 946 | reads a <default> element from the manifest file |
| 947 | """ |
| 948 | d = _Default() |
| 949 | d.remote = self._get_remote(node) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 950 | d.revisionExpr = node.getAttribute('revision') |
| 951 | if d.revisionExpr == '': |
| 952 | d.revisionExpr = None |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 953 | |
Bryan Jacobs | f609f91 | 2013-05-06 13:36:24 -0400 | [diff] [blame] | 954 | d.destBranchExpr = node.getAttribute('dest-branch') or None |
Nasser Grainawi | da40341 | 2018-05-04 12:53:29 -0600 | [diff] [blame] | 955 | d.upstreamExpr = node.getAttribute('upstream') or None |
Bryan Jacobs | f609f91 | 2013-05-06 13:36:24 -0400 | [diff] [blame] | 956 | |
Mike Frysinger | bb8ee7f | 2020-02-22 05:30:12 -0500 | [diff] [blame] | 957 | d.sync_j = XmlInt(node, 'sync-j', 1) |
| 958 | if d.sync_j <= 0: |
| 959 | raise ManifestParseError('%s: sync-j must be greater than 0, not "%s"' % |
| 960 | (self.manifestFile, d.sync_j)) |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 961 | |
Mike Frysinger | bb8ee7f | 2020-02-22 05:30:12 -0500 | [diff] [blame] | 962 | d.sync_c = XmlBool(node, 'sync-c', False) |
| 963 | d.sync_s = XmlBool(node, 'sync-s', False) |
| 964 | d.sync_tags = XmlBool(node, 'sync-tags', True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 965 | return d |
| 966 | |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 967 | def _ParseNotice(self, node): |
| 968 | """ |
| 969 | reads a <notice> element from the manifest file |
| 970 | |
| 971 | The <notice> element is distinct from other tags in the XML in that the |
| 972 | data is conveyed between the start and end tag (it's not an empty-element |
| 973 | tag). |
| 974 | |
| 975 | The white space (carriage returns, indentation) for the notice element is |
| 976 | relevant and is parsed in a way that is based on how python docstrings work. |
| 977 | In fact, the code is remarkably similar to here: |
| 978 | http://www.python.org/dev/peps/pep-0257/ |
| 979 | """ |
| 980 | # Get the data out of the node... |
| 981 | notice = node.childNodes[0].data |
| 982 | |
| 983 | # Figure out minimum indentation, skipping the first line (the same line |
| 984 | # as the <notice> tag)... |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 985 | minIndent = sys.maxsize |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 986 | lines = notice.splitlines() |
| 987 | for line in lines[1:]: |
| 988 | lstrippedLine = line.lstrip() |
| 989 | if lstrippedLine: |
| 990 | indent = len(line) - len(lstrippedLine) |
| 991 | minIndent = min(indent, minIndent) |
| 992 | |
| 993 | # Strip leading / trailing blank lines and also indentation. |
| 994 | cleanLines = [lines[0].strip()] |
| 995 | for line in lines[1:]: |
| 996 | cleanLines.append(line[minIndent:].rstrip()) |
| 997 | |
| 998 | # Clear completely blank lines from front and back... |
| 999 | while cleanLines and not cleanLines[0]: |
| 1000 | del cleanLines[0] |
| 1001 | while cleanLines and not cleanLines[-1]: |
| 1002 | del cleanLines[-1] |
| 1003 | |
| 1004 | return '\n'.join(cleanLines) |
| 1005 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1006 | def _JoinName(self, parent_name, name): |
| 1007 | return os.path.join(parent_name, name) |
| 1008 | |
| 1009 | def _UnjoinName(self, parent_name, name): |
| 1010 | return os.path.relpath(name, parent_name) |
| 1011 | |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 1012 | def _ParseProject(self, node, parent=None, **extra_proj_attrs): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1013 | """ |
| 1014 | reads a <project> element from the manifest file |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 1015 | """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1016 | name = self._reqatt(node, 'name') |
Mike Frysinger | a29424e | 2021-02-25 21:53:49 -0500 | [diff] [blame] | 1017 | msg = self._CheckLocalPath(name, dir_ok=True) |
| 1018 | if msg: |
| 1019 | raise ManifestInvalidPathError( |
| 1020 | '<project> invalid "name": %s: %s' % (name, msg)) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1021 | if parent: |
| 1022 | name = self._JoinName(parent.name, name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1023 | |
| 1024 | remote = self._get_remote(node) |
| 1025 | if remote is None: |
| 1026 | remote = self._default.remote |
| 1027 | if remote is None: |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 1028 | raise ManifestParseError("no remote for project %s within %s" % |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 1029 | (name, self.manifestFile)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1030 | |
Anthony King | 36ea2fb | 2014-05-06 11:54:01 +0100 | [diff] [blame] | 1031 | revisionExpr = node.getAttribute('revision') or remote.revision |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 1032 | if not revisionExpr: |
| 1033 | revisionExpr = self._default.revisionExpr |
| 1034 | if not revisionExpr: |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 1035 | raise ManifestParseError("no revision for project %s within %s" % |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 1036 | (name, self.manifestFile)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1037 | |
| 1038 | path = node.getAttribute('path') |
| 1039 | if not path: |
| 1040 | path = name |
Mike Frysinger | a29424e | 2021-02-25 21:53:49 -0500 | [diff] [blame] | 1041 | else: |
| 1042 | msg = self._CheckLocalPath(path, dir_ok=True) |
| 1043 | if msg: |
| 1044 | raise ManifestInvalidPathError( |
| 1045 | '<project> invalid "path": %s: %s' % (path, msg)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1046 | |
Mike Frysinger | bb8ee7f | 2020-02-22 05:30:12 -0500 | [diff] [blame] | 1047 | rebase = XmlBool(node, 'rebase', True) |
| 1048 | sync_c = XmlBool(node, 'sync-c', False) |
| 1049 | sync_s = XmlBool(node, 'sync-s', self._default.sync_s) |
| 1050 | sync_tags = XmlBool(node, 'sync-tags', self._default.sync_tags) |
Mike Pontillo | d315382 | 2012-02-28 11:53:24 -0800 | [diff] [blame] | 1051 | |
Mike Frysinger | bb8ee7f | 2020-02-22 05:30:12 -0500 | [diff] [blame] | 1052 | clone_depth = XmlInt(node, 'clone-depth') |
| 1053 | if clone_depth is not None and clone_depth <= 0: |
| 1054 | raise ManifestParseError('%s: clone-depth must be greater than 0, not "%s"' % |
| 1055 | (self.manifestFile, clone_depth)) |
David Pursehouse | ede7f12 | 2012-11-27 22:25:30 +0900 | [diff] [blame] | 1056 | |
Bryan Jacobs | f609f91 | 2013-05-06 13:36:24 -0400 | [diff] [blame] | 1057 | dest_branch = node.getAttribute('dest-branch') or self._default.destBranchExpr |
| 1058 | |
Nasser Grainawi | da40341 | 2018-05-04 12:53:29 -0600 | [diff] [blame] | 1059 | upstream = node.getAttribute('upstream') or self._default.upstreamExpr |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 1060 | |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 1061 | groups = '' |
| 1062 | if node.hasAttribute('groups'): |
| 1063 | groups = node.getAttribute('groups') |
Mike Frysinger | 51e39d5 | 2020-12-04 05:32:06 -0500 | [diff] [blame] | 1064 | groups = self._ParseList(groups) |
Brian Harring | 7da1314 | 2012-06-15 02:24:20 -0700 | [diff] [blame] | 1065 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1066 | if parent is None: |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 1067 | relpath, worktree, gitdir, objdir, use_git_worktrees = \ |
| 1068 | self.GetProjectPaths(name, path) |
Shawn O. Pearce | cd81dd6 | 2012-10-26 12:18:00 -0700 | [diff] [blame] | 1069 | else: |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 1070 | use_git_worktrees = False |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1071 | relpath, worktree, gitdir, objdir = \ |
| 1072 | self.GetSubprojectPaths(parent, name, path) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1073 | |
| 1074 | default_groups = ['all', 'name:%s' % name, 'path:%s' % relpath] |
| 1075 | groups.extend(set(default_groups).difference(groups)) |
Shawn O. Pearce | cd81dd6 | 2012-10-26 12:18:00 -0700 | [diff] [blame] | 1076 | |
Scott Fan | db83b1b | 2013-02-28 09:34:14 +0800 | [diff] [blame] | 1077 | if self.IsMirror and node.hasAttribute('force-path'): |
Mike Frysinger | bb8ee7f | 2020-02-22 05:30:12 -0500 | [diff] [blame] | 1078 | if XmlBool(node, 'force-path', False): |
Scott Fan | db83b1b | 2013-02-28 09:34:14 +0800 | [diff] [blame] | 1079 | gitdir = os.path.join(self.topdir, '%s.git' % path) |
| 1080 | |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 1081 | project = Project(manifest=self, |
| 1082 | name=name, |
| 1083 | remote=remote.ToRemoteSpec(name), |
| 1084 | gitdir=gitdir, |
| 1085 | objdir=objdir, |
| 1086 | worktree=worktree, |
| 1087 | relpath=relpath, |
| 1088 | revisionExpr=revisionExpr, |
| 1089 | revisionId=None, |
| 1090 | rebase=rebase, |
| 1091 | groups=groups, |
| 1092 | sync_c=sync_c, |
| 1093 | sync_s=sync_s, |
| 1094 | sync_tags=sync_tags, |
| 1095 | clone_depth=clone_depth, |
| 1096 | upstream=upstream, |
| 1097 | parent=parent, |
| 1098 | dest_branch=dest_branch, |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 1099 | use_git_worktrees=use_git_worktrees, |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1100 | **extra_proj_attrs) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1101 | |
| 1102 | for n in node.childNodes: |
Shawn O. Pearce | 242b526 | 2009-05-19 13:00:29 -0700 | [diff] [blame] | 1103 | if n.nodeName == 'copyfile': |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1104 | self._ParseCopyFile(project, n) |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1105 | if n.nodeName == 'linkfile': |
| 1106 | self._ParseLinkFile(project, n) |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 1107 | if n.nodeName == 'annotation': |
| 1108 | self._ParseAnnotation(project, n) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1109 | if n.nodeName == 'project': |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 1110 | project.subprojects.append(self._ParseProject(n, parent=project)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1111 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1112 | return project |
| 1113 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1114 | def GetProjectPaths(self, name, path): |
Mike Frysinger | cebf227 | 2020-05-26 01:02:29 -0400 | [diff] [blame] | 1115 | # The manifest entries might have trailing slashes. Normalize them to avoid |
| 1116 | # unexpected filesystem behavior since we do string concatenation below. |
| 1117 | path = path.rstrip('/') |
| 1118 | name = name.rstrip('/') |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 1119 | use_git_worktrees = False |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1120 | relpath = path |
| 1121 | if self.IsMirror: |
| 1122 | worktree = None |
| 1123 | gitdir = os.path.join(self.topdir, '%s.git' % name) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1124 | objdir = gitdir |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1125 | else: |
| 1126 | worktree = os.path.join(self.topdir, path).replace('\\', '/') |
| 1127 | gitdir = os.path.join(self.repodir, 'projects', '%s.git' % path) |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 1128 | # We allow people to mix git worktrees & non-git worktrees for now. |
| 1129 | # This allows for in situ migration of repo clients. |
| 1130 | if os.path.exists(gitdir) or not self.UseGitWorktrees: |
| 1131 | objdir = os.path.join(self.repodir, 'project-objects', '%s.git' % name) |
| 1132 | else: |
| 1133 | use_git_worktrees = True |
| 1134 | gitdir = os.path.join(self.repodir, 'worktrees', '%s.git' % name) |
| 1135 | objdir = gitdir |
| 1136 | return relpath, worktree, gitdir, objdir, use_git_worktrees |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1137 | |
| 1138 | def GetProjectsWithName(self, name): |
| 1139 | return self._projects.get(name, []) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1140 | |
| 1141 | def GetSubprojectName(self, parent, submodule_path): |
| 1142 | return os.path.join(parent.name, submodule_path) |
| 1143 | |
| 1144 | def _JoinRelpath(self, parent_relpath, relpath): |
| 1145 | return os.path.join(parent_relpath, relpath) |
| 1146 | |
| 1147 | def _UnjoinRelpath(self, parent_relpath, relpath): |
| 1148 | return os.path.relpath(relpath, parent_relpath) |
| 1149 | |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1150 | def GetSubprojectPaths(self, parent, name, path): |
Mike Frysinger | cebf227 | 2020-05-26 01:02:29 -0400 | [diff] [blame] | 1151 | # The manifest entries might have trailing slashes. Normalize them to avoid |
| 1152 | # unexpected filesystem behavior since we do string concatenation below. |
| 1153 | path = path.rstrip('/') |
| 1154 | name = name.rstrip('/') |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1155 | relpath = self._JoinRelpath(parent.relpath, path) |
| 1156 | gitdir = os.path.join(parent.gitdir, 'subprojects', '%s.git' % path) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1157 | objdir = os.path.join(parent.gitdir, 'subproject-objects', '%s.git' % name) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1158 | if self.IsMirror: |
| 1159 | worktree = None |
| 1160 | else: |
| 1161 | worktree = os.path.join(parent.worktree, path).replace('\\', '/') |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 1162 | return relpath, worktree, gitdir, objdir |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1163 | |
Mike Frysinger | 04122b7 | 2019-07-31 23:32:58 -0400 | [diff] [blame] | 1164 | @staticmethod |
Mike Frysinger | a00c5f4 | 2021-02-25 18:26:31 -0500 | [diff] [blame] | 1165 | def _CheckLocalPath(path, dir_ok=False, cwd_dot_ok=False): |
| 1166 | """Verify |path| is reasonable for use in filesystem paths. |
| 1167 | |
Mike Frysinger | a29424e | 2021-02-25 21:53:49 -0500 | [diff] [blame] | 1168 | Used with <copyfile> & <linkfile> & <project> elements. |
Mike Frysinger | a00c5f4 | 2021-02-25 18:26:31 -0500 | [diff] [blame] | 1169 | |
| 1170 | This only validates the |path| in isolation: it does not check against the |
| 1171 | current filesystem state. Thus it is suitable as a first-past in a parser. |
| 1172 | |
| 1173 | It enforces a number of constraints: |
| 1174 | * No empty paths. |
| 1175 | * No "~" in paths. |
| 1176 | * No Unicode codepoints that filesystems might elide when normalizing. |
| 1177 | * No relative path components like "." or "..". |
| 1178 | * No absolute paths. |
| 1179 | * No ".git" or ".repo*" path components. |
| 1180 | |
| 1181 | Args: |
| 1182 | path: The path name to validate. |
| 1183 | dir_ok: Whether |path| may force a directory (e.g. end in a /). |
| 1184 | cwd_dot_ok: Whether |path| may be just ".". |
| 1185 | |
| 1186 | Returns: |
| 1187 | None if |path| is OK, a failure message otherwise. |
| 1188 | """ |
| 1189 | if not path: |
| 1190 | return 'empty paths not allowed' |
| 1191 | |
Mike Frysinger | 04122b7 | 2019-07-31 23:32:58 -0400 | [diff] [blame] | 1192 | if '~' in path: |
| 1193 | return '~ not allowed (due to 8.3 filenames on Windows filesystems)' |
| 1194 | |
| 1195 | # Some filesystems (like Apple's HFS+) try to normalize Unicode codepoints |
| 1196 | # which means there are alternative names for ".git". Reject paths with |
| 1197 | # these in it as there shouldn't be any reasonable need for them here. |
| 1198 | # The set of codepoints here was cribbed from jgit's implementation: |
| 1199 | # https://eclipse.googlesource.com/jgit/jgit/+/9110037e3e9461ff4dac22fee84ef3694ed57648/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectChecker.java#884 |
| 1200 | BAD_CODEPOINTS = { |
| 1201 | u'\u200C', # ZERO WIDTH NON-JOINER |
| 1202 | u'\u200D', # ZERO WIDTH JOINER |
| 1203 | u'\u200E', # LEFT-TO-RIGHT MARK |
| 1204 | u'\u200F', # RIGHT-TO-LEFT MARK |
| 1205 | u'\u202A', # LEFT-TO-RIGHT EMBEDDING |
| 1206 | u'\u202B', # RIGHT-TO-LEFT EMBEDDING |
| 1207 | u'\u202C', # POP DIRECTIONAL FORMATTING |
| 1208 | u'\u202D', # LEFT-TO-RIGHT OVERRIDE |
| 1209 | u'\u202E', # RIGHT-TO-LEFT OVERRIDE |
| 1210 | u'\u206A', # INHIBIT SYMMETRIC SWAPPING |
| 1211 | u'\u206B', # ACTIVATE SYMMETRIC SWAPPING |
| 1212 | u'\u206C', # INHIBIT ARABIC FORM SHAPING |
| 1213 | u'\u206D', # ACTIVATE ARABIC FORM SHAPING |
| 1214 | u'\u206E', # NATIONAL DIGIT SHAPES |
| 1215 | u'\u206F', # NOMINAL DIGIT SHAPES |
| 1216 | u'\uFEFF', # ZERO WIDTH NO-BREAK SPACE |
| 1217 | } |
| 1218 | if BAD_CODEPOINTS & set(path): |
| 1219 | # This message is more expansive than reality, but should be fine. |
| 1220 | return 'Unicode combining characters not allowed' |
| 1221 | |
| 1222 | # Assume paths might be used on case-insensitive filesystems. |
| 1223 | path = path.lower() |
| 1224 | |
Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 1225 | # Split up the path by its components. We can't use os.path.sep exclusively |
| 1226 | # as some platforms (like Windows) will convert / to \ and that bypasses all |
| 1227 | # our constructed logic here. Especially since manifest authors only use |
| 1228 | # / in their paths. |
| 1229 | resep = re.compile(r'[/%s]' % re.escape(os.path.sep)) |
| 1230 | parts = resep.split(path) |
| 1231 | |
Mike Frysinger | ae62541 | 2020-02-10 17:10:03 -0500 | [diff] [blame] | 1232 | # Some people use src="." to create stable links to projects. Lets allow |
| 1233 | # that but reject all other uses of "." to keep things simple. |
Mike Frysinger | a00c5f4 | 2021-02-25 18:26:31 -0500 | [diff] [blame] | 1234 | if not cwd_dot_ok or parts != ['.']: |
Mike Frysinger | ae62541 | 2020-02-10 17:10:03 -0500 | [diff] [blame] | 1235 | for part in set(parts): |
| 1236 | if part in {'.', '..', '.git'} or part.startswith('.repo'): |
| 1237 | return 'bad component: %s' % (part,) |
Mike Frysinger | 04122b7 | 2019-07-31 23:32:58 -0400 | [diff] [blame] | 1238 | |
Mike Frysinger | a00c5f4 | 2021-02-25 18:26:31 -0500 | [diff] [blame] | 1239 | if not dir_ok and resep.match(path[-1]): |
Mike Frysinger | 04122b7 | 2019-07-31 23:32:58 -0400 | [diff] [blame] | 1240 | return 'dirs not allowed' |
| 1241 | |
Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 1242 | # NB: The two abspath checks here are to handle platforms with multiple |
| 1243 | # filesystem path styles (e.g. Windows). |
Mike Frysinger | 04122b7 | 2019-07-31 23:32:58 -0400 | [diff] [blame] | 1244 | norm = os.path.normpath(path) |
Mike Frysinger | d925459 | 2020-02-19 22:36:26 -0500 | [diff] [blame] | 1245 | if (norm == '..' or |
| 1246 | (len(norm) >= 3 and norm.startswith('..') and resep.match(norm[0])) or |
| 1247 | os.path.isabs(norm) or |
| 1248 | norm.startswith('/')): |
Mike Frysinger | 04122b7 | 2019-07-31 23:32:58 -0400 | [diff] [blame] | 1249 | return 'path cannot be outside' |
| 1250 | |
| 1251 | @classmethod |
| 1252 | def _ValidateFilePaths(cls, element, src, dest): |
| 1253 | """Verify |src| & |dest| are reasonable for <copyfile> & <linkfile>. |
| 1254 | |
| 1255 | We verify the path independent of any filesystem state as we won't have a |
| 1256 | checkout available to compare to. i.e. This is for parsing validation |
| 1257 | purposes only. |
| 1258 | |
| 1259 | We'll do full/live sanity checking before we do the actual filesystem |
| 1260 | modifications in _CopyFile/_LinkFile/etc... |
| 1261 | """ |
| 1262 | # |dest| is the file we write to or symlink we create. |
| 1263 | # It is relative to the top of the repo client checkout. |
| 1264 | msg = cls._CheckLocalPath(dest) |
| 1265 | if msg: |
| 1266 | raise ManifestInvalidPathError( |
| 1267 | '<%s> invalid "dest": %s: %s' % (element, dest, msg)) |
| 1268 | |
| 1269 | # |src| is the file we read from or path we point to for symlinks. |
| 1270 | # It is relative to the top of the git project checkout. |
Mike Frysinger | a00c5f4 | 2021-02-25 18:26:31 -0500 | [diff] [blame] | 1271 | is_linkfile = element == 'linkfile' |
| 1272 | 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] | 1273 | if msg: |
| 1274 | raise ManifestInvalidPathError( |
| 1275 | '<%s> invalid "src": %s: %s' % (element, src, msg)) |
| 1276 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1277 | def _ParseCopyFile(self, project, node): |
| 1278 | src = self._reqatt(node, 'src') |
| 1279 | dest = self._reqatt(node, 'dest') |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 1280 | if not self.IsMirror: |
| 1281 | # src is project relative; |
Mike Frysinger | 04122b7 | 2019-07-31 23:32:58 -0400 | [diff] [blame] | 1282 | # dest is relative to the top of the tree. |
| 1283 | # We only validate paths if we actually plan to process them. |
| 1284 | self._ValidateFilePaths('copyfile', src, dest) |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 1285 | project.AddCopyFile(src, dest, self.topdir) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1286 | |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1287 | def _ParseLinkFile(self, project, node): |
| 1288 | src = self._reqatt(node, 'src') |
| 1289 | dest = self._reqatt(node, 'dest') |
| 1290 | if not self.IsMirror: |
| 1291 | # src is project relative; |
Mike Frysinger | 04122b7 | 2019-07-31 23:32:58 -0400 | [diff] [blame] | 1292 | # dest is relative to the top of the tree. |
| 1293 | # We only validate paths if we actually plan to process them. |
| 1294 | self._ValidateFilePaths('linkfile', src, dest) |
Mike Frysinger | e6a202f | 2019-08-02 15:57:57 -0400 | [diff] [blame] | 1295 | project.AddLinkFile(src, dest, self.topdir) |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1296 | |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 1297 | def _ParseAnnotation(self, project, node): |
| 1298 | name = self._reqatt(node, 'name') |
| 1299 | value = self._reqatt(node, 'value') |
| 1300 | try: |
| 1301 | keep = self._reqatt(node, 'keep').lower() |
| 1302 | except ManifestParseError: |
| 1303 | keep = "true" |
| 1304 | if keep != "true" and keep != "false": |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 1305 | raise ManifestParseError('optional "keep" attribute must be ' |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 1306 | '"true" or "false"') |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 1307 | project.AddAnnotation(name, value, keep) |
| 1308 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1309 | def _get_remote(self, node): |
| 1310 | name = node.getAttribute('remote') |
| 1311 | if not name: |
| 1312 | return None |
| 1313 | |
| 1314 | v = self._remotes.get(name) |
| 1315 | if not v: |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 1316 | raise ManifestParseError("remote %s not defined in %s" % |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 1317 | (name, self.manifestFile)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1318 | return v |
| 1319 | |
| 1320 | def _reqatt(self, node, attname): |
| 1321 | """ |
| 1322 | reads a required attribute from the node. |
| 1323 | """ |
| 1324 | v = node.getAttribute(attname) |
| 1325 | if not v: |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 1326 | raise ManifestParseError("no %s in <%s> within %s" % |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 1327 | (attname, node.nodeName, self.manifestFile)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1328 | return v |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame] | 1329 | |
| 1330 | def projectsDiff(self, manifest): |
| 1331 | """return the projects differences between two manifests. |
| 1332 | |
| 1333 | The diff will be from self to given manifest. |
| 1334 | |
| 1335 | """ |
| 1336 | fromProjects = self.paths |
| 1337 | toProjects = manifest.paths |
| 1338 | |
Anthony King | 7446c59 | 2014-05-06 09:19:39 +0100 | [diff] [blame] | 1339 | fromKeys = sorted(fromProjects.keys()) |
| 1340 | toKeys = sorted(toProjects.keys()) |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame] | 1341 | |
| 1342 | diff = {'added': [], 'removed': [], 'changed': [], 'unreachable': []} |
| 1343 | |
| 1344 | for proj in fromKeys: |
David Pursehouse | eeff353 | 2020-02-12 11:24:10 +0900 | [diff] [blame] | 1345 | if proj not in toKeys: |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame] | 1346 | diff['removed'].append(fromProjects[proj]) |
| 1347 | else: |
| 1348 | fromProj = fromProjects[proj] |
| 1349 | toProj = toProjects[proj] |
| 1350 | try: |
| 1351 | fromRevId = fromProj.GetCommitRevisionId() |
| 1352 | toRevId = toProj.GetCommitRevisionId() |
| 1353 | except ManifestInvalidRevisionError: |
| 1354 | diff['unreachable'].append((fromProj, toProj)) |
| 1355 | else: |
| 1356 | if fromRevId != toRevId: |
| 1357 | diff['changed'].append((fromProj, toProj)) |
| 1358 | toKeys.remove(proj) |
| 1359 | |
| 1360 | for proj in toKeys: |
| 1361 | diff['added'].append(toProjects[proj]) |
| 1362 | |
| 1363 | return diff |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1364 | |
| 1365 | |
| 1366 | class GitcManifest(XmlManifest): |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 1367 | """Parser for GitC (git-in-the-cloud) manifests.""" |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1368 | |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 1369 | def _ParseProject(self, node, parent=None): |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1370 | """Override _ParseProject and add support for GITC specific attributes.""" |
Mike Frysinger | 5d9c497 | 2021-02-19 13:34:09 -0500 | [diff] [blame] | 1371 | return super()._ParseProject( |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1372 | node, parent=parent, old_revision=node.getAttribute('old-revision')) |
| 1373 | |
| 1374 | def _output_manifest_project_extras(self, p, e): |
| 1375 | """Output GITC Specific Project attributes""" |
| 1376 | if p.old_revision: |
Stefan Beller | 6685106 | 2016-06-17 16:40:08 -0700 | [diff] [blame] | 1377 | e.setAttribute('old-revision', str(p.old_revision)) |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 1378 | |
| 1379 | |
| 1380 | class RepoClient(XmlManifest): |
| 1381 | """Manages a repo client checkout.""" |
| 1382 | |
| 1383 | def __init__(self, repodir, manifest_file=None): |
| 1384 | self.isGitcClient = False |
| 1385 | |
| 1386 | if os.path.exists(os.path.join(repodir, LOCAL_MANIFEST_NAME)): |
| 1387 | print('error: %s is not supported; put local manifests in `%s` instead' % |
| 1388 | (LOCAL_MANIFEST_NAME, os.path.join(repodir, LOCAL_MANIFESTS_DIR_NAME)), |
| 1389 | file=sys.stderr) |
| 1390 | sys.exit(1) |
| 1391 | |
| 1392 | if manifest_file is None: |
| 1393 | manifest_file = os.path.join(repodir, MANIFEST_FILE_NAME) |
| 1394 | local_manifests = os.path.abspath(os.path.join(repodir, LOCAL_MANIFESTS_DIR_NAME)) |
Mike Frysinger | 5d9c497 | 2021-02-19 13:34:09 -0500 | [diff] [blame] | 1395 | super().__init__(repodir, manifest_file, local_manifests) |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 1396 | |
| 1397 | # TODO: Completely separate manifest logic out of the client. |
| 1398 | self.manifest = self |
| 1399 | |
| 1400 | |
| 1401 | class GitcClient(RepoClient, GitcManifest): |
| 1402 | """Manages a GitC client checkout.""" |
| 1403 | |
| 1404 | def __init__(self, repodir, gitc_client_name): |
| 1405 | """Initialize the GitcManifest object.""" |
| 1406 | self.gitc_client_name = gitc_client_name |
| 1407 | self.gitc_client_dir = os.path.join(gitc_utils.get_gitc_manifest_dir(), |
| 1408 | gitc_client_name) |
| 1409 | |
Mike Frysinger | 5d9c497 | 2021-02-19 13:34:09 -0500 | [diff] [blame] | 1410 | super().__init__(repodir, os.path.join(self.gitc_client_dir, '.manifest')) |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 1411 | self.isGitcClient = True |