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