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