The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright (C) 2008 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 16 | from __future__ import print_function |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 17 | import itertools |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 18 | import os |
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 |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 21 | import urlparse |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 22 | import xml.dom.minidom |
| 23 | |
David Pursehouse | e15c65a | 2012-08-22 10:46:11 +0900 | [diff] [blame] | 24 | from git_config import GitConfig |
David Pursehouse | e00aa6b | 2012-09-11 14:33:51 +0900 | [diff] [blame] | 25 | from git_refs import R_HEADS, HEAD |
| 26 | from project import RemoteSpec, Project, MetaProject |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 27 | from error import ManifestParseError |
| 28 | |
| 29 | MANIFEST_FILE_NAME = 'manifest.xml' |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 30 | LOCAL_MANIFEST_NAME = 'local_manifest.xml' |
David Pursehouse | 2d5a0df | 2012-11-13 02:50:36 +0900 | [diff] [blame] | 31 | LOCAL_MANIFESTS_DIR_NAME = 'local_manifests' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 32 | |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 33 | urlparse.uses_relative.extend(['ssh', 'git']) |
| 34 | urlparse.uses_netloc.extend(['ssh', 'git']) |
| 35 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 36 | class _Default(object): |
| 37 | """Project defaults within the manifest.""" |
| 38 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 39 | revisionExpr = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 40 | remote = None |
Shawn O. Pearce | 6392c87 | 2011-09-22 17:44:31 -0700 | [diff] [blame] | 41 | sync_j = 1 |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 42 | sync_c = False |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 43 | sync_s = False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 44 | |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 45 | class _XmlRemote(object): |
| 46 | def __init__(self, |
| 47 | name, |
Yestin Sun | b292b98 | 2012-07-02 07:32:50 -0700 | [diff] [blame] | 48 | alias=None, |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 49 | fetch=None, |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 50 | manifestUrl=None, |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 51 | review=None): |
| 52 | self.name = name |
| 53 | self.fetchUrl = fetch |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 54 | self.manifestUrl = manifestUrl |
Yestin Sun | b292b98 | 2012-07-02 07:32:50 -0700 | [diff] [blame] | 55 | self.remoteAlias = alias |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 56 | self.reviewUrl = review |
Conley Owens | ceea368 | 2011-10-20 10:45:47 -0700 | [diff] [blame] | 57 | self.resolvedFetchUrl = self._resolveFetchUrl() |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 58 | |
David Pursehouse | 717ece9 | 2012-11-13 08:49:16 +0900 | [diff] [blame] | 59 | def __eq__(self, other): |
| 60 | return self.__dict__ == other.__dict__ |
| 61 | |
| 62 | def __ne__(self, other): |
| 63 | return self.__dict__ != other.__dict__ |
| 64 | |
Conley Owens | ceea368 | 2011-10-20 10:45:47 -0700 | [diff] [blame] | 65 | def _resolveFetchUrl(self): |
| 66 | url = self.fetchUrl.rstrip('/') |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 67 | manifestUrl = self.manifestUrl.rstrip('/') |
Shawn Pearce | a9f11b3 | 2013-01-02 15:40:48 -0800 | [diff] [blame] | 68 | p = manifestUrl.startswith('persistent-http') |
| 69 | if p: |
| 70 | manifestUrl = manifestUrl[len('persistent-'):] |
| 71 | |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 72 | # urljoin will get confused if there is no scheme in the base url |
| 73 | # ie, if manifestUrl is of the form <hostname:port> |
| 74 | if manifestUrl.find(':') != manifestUrl.find('/') - 1: |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 75 | manifestUrl = 'gopher://' + manifestUrl |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 76 | url = urlparse.urljoin(manifestUrl, url) |
Shawn Pearce | a9f11b3 | 2013-01-02 15:40:48 -0800 | [diff] [blame] | 77 | url = re.sub(r'^gopher://', '', url) |
| 78 | if p: |
| 79 | url = 'persistent-' + url |
| 80 | return url |
Conley Owens | ceea368 | 2011-10-20 10:45:47 -0700 | [diff] [blame] | 81 | |
| 82 | def ToRemoteSpec(self, projectName): |
Conley Owens | 9d8f914 | 2011-10-20 14:36:35 -0700 | [diff] [blame] | 83 | url = self.resolvedFetchUrl.rstrip('/') + '/' + projectName |
Yestin Sun | b292b98 | 2012-07-02 07:32:50 -0700 | [diff] [blame] | 84 | remoteName = self.name |
| 85 | if self.remoteAlias: |
| 86 | remoteName = self.remoteAlias |
| 87 | return RemoteSpec(remoteName, url, self.reviewUrl) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 88 | |
Shawn O. Pearce | c8a300f | 2009-05-18 13:19:57 -0700 | [diff] [blame] | 89 | class XmlManifest(object): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 90 | """manages the repo configuration file""" |
| 91 | |
| 92 | def __init__(self, repodir): |
| 93 | self.repodir = os.path.abspath(repodir) |
| 94 | self.topdir = os.path.dirname(self.repodir) |
| 95 | self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 96 | self.globalConfig = GitConfig.ForUser() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 97 | |
| 98 | self.repoProject = MetaProject(self, 'repo', |
| 99 | gitdir = os.path.join(repodir, 'repo/.git'), |
| 100 | worktree = os.path.join(repodir, 'repo')) |
| 101 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 102 | self.manifestProject = MetaProject(self, 'manifests', |
Shawn O. Pearce | f5c25a6 | 2008-11-04 08:11:53 -0800 | [diff] [blame] | 103 | gitdir = os.path.join(repodir, 'manifests.git'), |
| 104 | worktree = os.path.join(repodir, 'manifests')) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 105 | |
| 106 | self._Unload() |
| 107 | |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 108 | def Override(self, name): |
| 109 | """Use a different manifest, just for the current instantiation. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 110 | """ |
| 111 | path = os.path.join(self.manifestProject.worktree, name) |
| 112 | if not os.path.isfile(path): |
| 113 | raise ManifestParseError('manifest %s not found' % name) |
| 114 | |
| 115 | old = self.manifestFile |
| 116 | try: |
| 117 | self.manifestFile = path |
| 118 | self._Unload() |
| 119 | self._Load() |
| 120 | finally: |
| 121 | self.manifestFile = old |
| 122 | |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 123 | def Link(self, name): |
| 124 | """Update the repo metadata to use a different manifest. |
| 125 | """ |
| 126 | self.Override(name) |
| 127 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 128 | try: |
Sebastian Frias | 223bf96 | 2012-11-21 19:09:25 +0100 | [diff] [blame] | 129 | if os.path.lexists(self.manifestFile): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 130 | os.remove(self.manifestFile) |
| 131 | os.symlink('manifests/%s' % name, self.manifestFile) |
Sebastian Frias | 223bf96 | 2012-11-21 19:09:25 +0100 | [diff] [blame] | 132 | except OSError as e: |
| 133 | raise ManifestParseError('cannot link manifest %s: %s' % (name, str(e))) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 134 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 135 | def _RemoteToXml(self, r, doc, root): |
| 136 | e = doc.createElement('remote') |
| 137 | root.appendChild(e) |
| 138 | e.setAttribute('name', r.name) |
| 139 | e.setAttribute('fetch', r.fetchUrl) |
| 140 | if r.reviewUrl is not None: |
| 141 | e.setAttribute('review', r.reviewUrl) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 142 | |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 143 | def Save(self, fd, peg_rev=False, peg_rev_upstream=True): |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 144 | """Write the current manifest out to the given file descriptor. |
| 145 | """ |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 146 | mp = self.manifestProject |
| 147 | |
| 148 | groups = mp.config.GetString('manifest.groups') |
Matt Gumbel | 0c635bb | 2012-12-21 10:14:53 -0800 | [diff] [blame] | 149 | if groups: |
| 150 | groups = [x for x in re.split(r'[,\s]+', groups) if x] |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 151 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 152 | doc = xml.dom.minidom.Document() |
| 153 | root = doc.createElement('manifest') |
| 154 | doc.appendChild(root) |
| 155 | |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 156 | # Save out the notice. There's a little bit of work here to give it the |
| 157 | # right whitespace, which assumes that the notice is automatically indented |
| 158 | # by 4 by minidom. |
| 159 | if self.notice: |
| 160 | notice_element = root.appendChild(doc.createElement('notice')) |
| 161 | notice_lines = self.notice.splitlines() |
| 162 | indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:] |
| 163 | notice_element.appendChild(doc.createTextNode(indented_notice)) |
| 164 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 165 | d = self.default |
| 166 | sort_remotes = list(self.remotes.keys()) |
| 167 | sort_remotes.sort() |
| 168 | |
| 169 | for r in sort_remotes: |
| 170 | self._RemoteToXml(self.remotes[r], doc, root) |
| 171 | if self.remotes: |
| 172 | root.appendChild(doc.createTextNode('')) |
| 173 | |
| 174 | have_default = False |
| 175 | e = doc.createElement('default') |
| 176 | if d.remote: |
| 177 | have_default = True |
| 178 | e.setAttribute('remote', d.remote.name) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 179 | if d.revisionExpr: |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 180 | have_default = True |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 181 | e.setAttribute('revision', d.revisionExpr) |
Shawn O. Pearce | 6392c87 | 2011-09-22 17:44:31 -0700 | [diff] [blame] | 182 | if d.sync_j > 1: |
| 183 | have_default = True |
| 184 | e.setAttribute('sync-j', '%d' % d.sync_j) |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 185 | if d.sync_c: |
| 186 | have_default = True |
| 187 | e.setAttribute('sync-c', 'true') |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 188 | if d.sync_s: |
| 189 | have_default = True |
| 190 | e.setAttribute('sync-s', 'true') |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 191 | if have_default: |
| 192 | root.appendChild(e) |
| 193 | root.appendChild(doc.createTextNode('')) |
| 194 | |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 195 | if self._manifest_server: |
| 196 | e = doc.createElement('manifest-server') |
| 197 | e.setAttribute('url', self._manifest_server) |
| 198 | root.appendChild(e) |
| 199 | root.appendChild(doc.createTextNode('')) |
| 200 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 201 | def output_projects(parent, parent_node, projects): |
| 202 | for p in projects: |
| 203 | output_project(parent, parent_node, self.projects[p]) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 204 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 205 | def output_project(parent, parent_node, p): |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 206 | if not p.MatchesGroups(groups): |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 207 | return |
| 208 | |
| 209 | name = p.name |
| 210 | relpath = p.relpath |
| 211 | if parent: |
| 212 | name = self._UnjoinName(parent.name, name) |
| 213 | relpath = self._UnjoinRelpath(parent.relpath, relpath) |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 214 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 215 | e = doc.createElement('project') |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 216 | parent_node.appendChild(e) |
| 217 | e.setAttribute('name', name) |
| 218 | if relpath != name: |
| 219 | e.setAttribute('path', relpath) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 220 | if not d.remote or p.remote.name != d.remote.name: |
| 221 | e.setAttribute('remote', p.remote.name) |
| 222 | if peg_rev: |
| 223 | if self.IsMirror: |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 224 | value = p.bare_git.rev_parse(p.revisionExpr + '^0') |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 225 | else: |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 226 | value = p.work_git.rev_parse(HEAD + '^0') |
| 227 | e.setAttribute('revision', value) |
| 228 | if peg_rev_upstream and value != p.revisionExpr: |
| 229 | # Only save the origin if the origin is not a sha1, and the default |
| 230 | # isn't our value, and the if the default doesn't already have that |
| 231 | # covered. |
| 232 | e.setAttribute('upstream', p.revisionExpr) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 233 | elif not d.revisionExpr or p.revisionExpr != d.revisionExpr: |
| 234 | e.setAttribute('revision', p.revisionExpr) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 235 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 236 | for c in p.copyfiles: |
| 237 | ce = doc.createElement('copyfile') |
| 238 | ce.setAttribute('src', c.src) |
| 239 | ce.setAttribute('dest', c.dest) |
| 240 | e.appendChild(ce) |
| 241 | |
Conley Owens | bb1b5f5 | 2012-08-13 13:11:18 -0700 | [diff] [blame] | 242 | default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath] |
Dmitry Fink | 17f85ea | 2012-08-06 14:52:29 -0700 | [diff] [blame] | 243 | 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] | 244 | if egroups: |
| 245 | e.setAttribute('groups', ','.join(egroups)) |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 246 | |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 247 | for a in p.annotations: |
| 248 | if a.keep == "true": |
| 249 | ae = doc.createElement('annotation') |
| 250 | ae.setAttribute('name', a.name) |
| 251 | ae.setAttribute('value', a.value) |
| 252 | e.appendChild(ae) |
| 253 | |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 254 | if p.sync_c: |
| 255 | e.setAttribute('sync-c', 'true') |
| 256 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 257 | if p.sync_s: |
| 258 | e.setAttribute('sync-s', 'true') |
| 259 | |
| 260 | if p.subprojects: |
| 261 | sort_projects = [subp.name for subp in p.subprojects] |
| 262 | sort_projects.sort() |
| 263 | output_projects(p, e, sort_projects) |
| 264 | |
| 265 | sort_projects = [key for key in self.projects.keys() |
| 266 | if not self.projects[key].parent] |
| 267 | sort_projects.sort() |
| 268 | output_projects(None, root, sort_projects) |
| 269 | |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 270 | if self._repo_hooks_project: |
| 271 | root.appendChild(doc.createTextNode('')) |
| 272 | e = doc.createElement('repo-hooks') |
| 273 | e.setAttribute('in-project', self._repo_hooks_project.name) |
| 274 | e.setAttribute('enabled-list', |
| 275 | ' '.join(self._repo_hooks_project.enabled_repo_hooks)) |
| 276 | root.appendChild(e) |
| 277 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 278 | doc.writexml(fd, '', ' ', '\n', 'UTF-8') |
| 279 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 280 | @property |
| 281 | def projects(self): |
| 282 | self._Load() |
| 283 | return self._projects |
| 284 | |
| 285 | @property |
| 286 | def remotes(self): |
| 287 | self._Load() |
| 288 | return self._remotes |
| 289 | |
| 290 | @property |
| 291 | def default(self): |
| 292 | self._Load() |
| 293 | return self._default |
| 294 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 295 | @property |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 296 | def repo_hooks_project(self): |
| 297 | self._Load() |
| 298 | return self._repo_hooks_project |
| 299 | |
| 300 | @property |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 301 | def notice(self): |
| 302 | self._Load() |
| 303 | return self._notice |
| 304 | |
| 305 | @property |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 306 | def manifest_server(self): |
| 307 | self._Load() |
Shawn O. Pearce | 34fb20f | 2011-11-30 13:41:02 -0800 | [diff] [blame] | 308 | return self._manifest_server |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 309 | |
| 310 | @property |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 311 | def IsMirror(self): |
| 312 | return self.manifestProject.config.GetBoolean('repo.mirror') |
| 313 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 314 | def _Unload(self): |
| 315 | self._loaded = False |
| 316 | self._projects = {} |
| 317 | self._remotes = {} |
| 318 | self._default = None |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 319 | self._repo_hooks_project = None |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 320 | self._notice = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 321 | self.branch = None |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 322 | self._manifest_server = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 323 | |
| 324 | def _Load(self): |
| 325 | if not self._loaded: |
Shawn O. Pearce | 2450a29 | 2008-11-04 08:22:07 -0800 | [diff] [blame] | 326 | m = self.manifestProject |
| 327 | b = m.GetBranch(m.CurrentBranch).merge |
Shawn O. Pearce | 21c5c34 | 2009-06-25 16:47:30 -0700 | [diff] [blame] | 328 | if b is not None and b.startswith(R_HEADS): |
Shawn O. Pearce | 2450a29 | 2008-11-04 08:22:07 -0800 | [diff] [blame] | 329 | b = b[len(R_HEADS):] |
| 330 | self.branch = b |
| 331 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 332 | nodes = [] |
Brian Harring | 475a47d | 2012-06-07 20:05:35 -0700 | [diff] [blame] | 333 | nodes.append(self._ParseManifestXml(self.manifestFile, |
| 334 | self.manifestProject.worktree)) |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 335 | |
| 336 | local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME) |
| 337 | if os.path.exists(local): |
David Pursehouse | 606eab8 | 2012-11-22 13:47:16 +0900 | [diff] [blame] | 338 | print('warning: %s is deprecated; put local manifests in `%s` instead' |
| 339 | % (LOCAL_MANIFEST_NAME, os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME)), |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 340 | file=sys.stderr) |
Brian Harring | 475a47d | 2012-06-07 20:05:35 -0700 | [diff] [blame] | 341 | nodes.append(self._ParseManifestXml(local, self.repodir)) |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 342 | |
David Pursehouse | 2d5a0df | 2012-11-13 02:50:36 +0900 | [diff] [blame] | 343 | local_dir = os.path.abspath(os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME)) |
| 344 | try: |
David Pursehouse | 52f1e5d | 2012-11-14 04:53:24 +0900 | [diff] [blame] | 345 | for local_file in sorted(os.listdir(local_dir)): |
David Pursehouse | 2d5a0df | 2012-11-13 02:50:36 +0900 | [diff] [blame] | 346 | if local_file.endswith('.xml'): |
David Pursehouse | 5f434ed | 2012-11-22 13:48:10 +0900 | [diff] [blame] | 347 | local = os.path.join(local_dir, local_file) |
| 348 | nodes.append(self._ParseManifestXml(local, self.repodir)) |
David Pursehouse | 2d5a0df | 2012-11-13 02:50:36 +0900 | [diff] [blame] | 349 | except OSError: |
| 350 | pass |
| 351 | |
Joe Onorato | 26e2475 | 2013-01-11 12:35:53 -0800 | [diff] [blame] | 352 | try: |
| 353 | self._ParseManifest(nodes) |
| 354 | except ManifestParseError as e: |
| 355 | # There was a problem parsing, unload ourselves in case they catch |
| 356 | # this error and try again later, we will show the correct error |
| 357 | self._Unload() |
| 358 | raise e |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 359 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 360 | if self.IsMirror: |
| 361 | self._AddMetaProjectMirror(self.repoProject) |
| 362 | self._AddMetaProjectMirror(self.manifestProject) |
| 363 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 364 | self._loaded = True |
| 365 | |
Brian Harring | 475a47d | 2012-06-07 20:05:35 -0700 | [diff] [blame] | 366 | def _ParseManifestXml(self, path, include_root): |
David Pursehouse | f7fc8a9 | 2012-11-13 04:00:28 +0900 | [diff] [blame] | 367 | try: |
| 368 | root = xml.dom.minidom.parse(path) |
David Pursehouse | 2d5a0df | 2012-11-13 02:50:36 +0900 | [diff] [blame] | 369 | except (OSError, xml.parsers.expat.ExpatError) as e: |
David Pursehouse | f7fc8a9 | 2012-11-13 04:00:28 +0900 | [diff] [blame] | 370 | raise ManifestParseError("error parsing manifest %s: %s" % (path, e)) |
| 371 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 372 | if not root or not root.childNodes: |
Brian Harring | 2644874 | 2011-04-28 05:04:41 -0700 | [diff] [blame] | 373 | raise ManifestParseError("no root node in %s" % (path,)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 374 | |
Jooncheol Park | 34acdd2 | 2012-08-27 02:25:59 +0900 | [diff] [blame] | 375 | for manifest in root.childNodes: |
| 376 | if manifest.nodeName == 'manifest': |
| 377 | break |
| 378 | else: |
Brian Harring | 2644874 | 2011-04-28 05:04:41 -0700 | [diff] [blame] | 379 | raise ManifestParseError("no <manifest> in %s" % (path,)) |
| 380 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 381 | nodes = [] |
David Pursehouse | 4f7bdea | 2012-10-22 12:50:15 +0900 | [diff] [blame] | 382 | for node in manifest.childNodes: # pylint:disable=W0631 |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 383 | # We only get here if manifest is initialised |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 384 | if node.nodeName == 'include': |
| 385 | name = self._reqatt(node, 'name') |
| 386 | fp = os.path.join(include_root, name) |
| 387 | if not os.path.isfile(fp): |
| 388 | raise ManifestParseError, \ |
| 389 | "include %s doesn't exist or isn't a file" % \ |
| 390 | (name,) |
| 391 | try: |
| 392 | nodes.extend(self._ParseManifestXml(fp, include_root)) |
| 393 | # should isolate this to the exact exception, but that's |
| 394 | # tricky. actual parsing implementation may vary. |
| 395 | except (KeyboardInterrupt, RuntimeError, SystemExit): |
| 396 | raise |
| 397 | except Exception as e: |
| 398 | raise ManifestParseError( |
| 399 | "failed parsing included manifest %s: %s", (name, e)) |
| 400 | else: |
| 401 | nodes.append(node) |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 402 | return nodes |
Brian Harring | 2644874 | 2011-04-28 05:04:41 -0700 | [diff] [blame] | 403 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 404 | def _ParseManifest(self, node_list): |
| 405 | for node in itertools.chain(*node_list): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 406 | if node.nodeName == 'remote': |
| 407 | remote = self._ParseRemote(node) |
David Pursehouse | 717ece9 | 2012-11-13 08:49:16 +0900 | [diff] [blame] | 408 | if remote: |
| 409 | if remote.name in self._remotes: |
| 410 | if remote != self._remotes[remote.name]: |
| 411 | raise ManifestParseError( |
| 412 | 'remote %s already exists with different attributes' % |
| 413 | (remote.name)) |
| 414 | else: |
| 415 | self._remotes[remote.name] = remote |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 416 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 417 | for node in itertools.chain(*node_list): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 418 | if node.nodeName == 'default': |
| 419 | if self._default is not None: |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 420 | raise ManifestParseError( |
| 421 | 'duplicate default in %s' % |
| 422 | (self.manifestFile)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 423 | self._default = self._ParseDefault(node) |
| 424 | if self._default is None: |
| 425 | self._default = _Default() |
| 426 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 427 | for node in itertools.chain(*node_list): |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 428 | if node.nodeName == 'notice': |
| 429 | if self._notice is not None: |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 430 | raise ManifestParseError( |
| 431 | 'duplicate notice in %s' % |
| 432 | (self.manifestFile)) |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 433 | self._notice = self._ParseNotice(node) |
| 434 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 435 | for node in itertools.chain(*node_list): |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 436 | if node.nodeName == 'manifest-server': |
| 437 | url = self._reqatt(node, 'url') |
| 438 | if self._manifest_server is not None: |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 439 | raise ManifestParseError( |
| 440 | 'duplicate manifest-server in %s' % |
| 441 | (self.manifestFile)) |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 442 | self._manifest_server = url |
| 443 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 444 | def recursively_add_projects(project): |
| 445 | if self._projects.get(project.name): |
| 446 | raise ManifestParseError( |
| 447 | 'duplicate project %s in %s' % |
| 448 | (project.name, self.manifestFile)) |
| 449 | self._projects[project.name] = project |
| 450 | for subproject in project.subprojects: |
| 451 | recursively_add_projects(subproject) |
| 452 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 453 | for node in itertools.chain(*node_list): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 454 | if node.nodeName == 'project': |
| 455 | project = self._ParseProject(node) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 456 | recursively_add_projects(project) |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 457 | if node.nodeName == 'repo-hooks': |
| 458 | # Get the name of the project and the (space-separated) list of enabled. |
| 459 | repo_hooks_project = self._reqatt(node, 'in-project') |
| 460 | enabled_repo_hooks = self._reqatt(node, 'enabled-list').split() |
| 461 | |
| 462 | # Only one project can be the hooks project |
| 463 | if self._repo_hooks_project is not None: |
| 464 | raise ManifestParseError( |
| 465 | 'duplicate repo-hooks in %s' % |
| 466 | (self.manifestFile)) |
| 467 | |
| 468 | # Store a reference to the Project. |
| 469 | try: |
| 470 | self._repo_hooks_project = self._projects[repo_hooks_project] |
| 471 | except KeyError: |
| 472 | raise ManifestParseError( |
| 473 | 'project %s not found for repo-hooks' % |
| 474 | (repo_hooks_project)) |
| 475 | |
| 476 | # Store the enabled hooks in the Project object. |
| 477 | self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 478 | if node.nodeName == 'remove-project': |
| 479 | name = self._reqatt(node, 'name') |
| 480 | try: |
| 481 | del self._projects[name] |
| 482 | except KeyError: |
David Pursehouse | f910748 | 2012-11-16 19:12:32 +0900 | [diff] [blame] | 483 | raise ManifestParseError('remove-project element specifies non-existent ' |
| 484 | 'project: %s' % name) |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 485 | |
| 486 | # If the manifest removes the hooks project, treat it as if it deleted |
| 487 | # the repo-hooks element too. |
| 488 | if self._repo_hooks_project and (self._repo_hooks_project.name == name): |
| 489 | self._repo_hooks_project = None |
| 490 | |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 491 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 492 | def _AddMetaProjectMirror(self, m): |
| 493 | name = None |
| 494 | m_url = m.GetRemote(m.remote.name).url |
| 495 | if m_url.endswith('/.git'): |
| 496 | raise ManifestParseError, 'refusing to mirror %s' % m_url |
| 497 | |
| 498 | if self._default and self._default.remote: |
Conley Owens | ceea368 | 2011-10-20 10:45:47 -0700 | [diff] [blame] | 499 | url = self._default.remote.resolvedFetchUrl |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 500 | if not url.endswith('/'): |
| 501 | url += '/' |
| 502 | if m_url.startswith(url): |
| 503 | remote = self._default.remote |
| 504 | name = m_url[len(url):] |
| 505 | |
| 506 | if name is None: |
| 507 | s = m_url.rindex('/') + 1 |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 508 | manifestUrl = self.manifestProject.config.GetString('remote.origin.url') |
Shawn O. Pearce | f35b2d9 | 2012-08-02 11:46:22 -0700 | [diff] [blame] | 509 | remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 510 | name = m_url[s:] |
| 511 | |
| 512 | if name.endswith('.git'): |
| 513 | name = name[:-4] |
| 514 | |
| 515 | if name not in self._projects: |
| 516 | m.PreSync() |
| 517 | gitdir = os.path.join(self.topdir, '%s.git' % name) |
| 518 | project = Project(manifest = self, |
| 519 | name = name, |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 520 | remote = remote.ToRemoteSpec(name), |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 521 | gitdir = gitdir, |
| 522 | worktree = None, |
| 523 | relpath = None, |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 524 | revisionExpr = m.revisionExpr, |
| 525 | revisionId = None) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 526 | self._projects[project.name] = project |
| 527 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 528 | def _ParseRemote(self, node): |
| 529 | """ |
| 530 | reads a <remote> element from the manifest file |
| 531 | """ |
| 532 | name = self._reqatt(node, 'name') |
Yestin Sun | b292b98 | 2012-07-02 07:32:50 -0700 | [diff] [blame] | 533 | alias = node.getAttribute('alias') |
| 534 | if alias == '': |
| 535 | alias = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 536 | fetch = self._reqatt(node, 'fetch') |
| 537 | review = node.getAttribute('review') |
Shawn O. Pearce | ae6e094 | 2008-11-06 10:25:35 -0800 | [diff] [blame] | 538 | if review == '': |
| 539 | review = None |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 540 | manifestUrl = self.manifestProject.config.GetString('remote.origin.url') |
Yestin Sun | b292b98 | 2012-07-02 07:32:50 -0700 | [diff] [blame] | 541 | return _XmlRemote(name, alias, fetch, manifestUrl, review) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 542 | |
| 543 | def _ParseDefault(self, node): |
| 544 | """ |
| 545 | reads a <default> element from the manifest file |
| 546 | """ |
| 547 | d = _Default() |
| 548 | d.remote = self._get_remote(node) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 549 | d.revisionExpr = node.getAttribute('revision') |
| 550 | if d.revisionExpr == '': |
| 551 | d.revisionExpr = None |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 552 | |
Shawn O. Pearce | 6392c87 | 2011-09-22 17:44:31 -0700 | [diff] [blame] | 553 | sync_j = node.getAttribute('sync-j') |
| 554 | if sync_j == '' or sync_j is None: |
| 555 | d.sync_j = 1 |
| 556 | else: |
| 557 | d.sync_j = int(sync_j) |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 558 | |
| 559 | sync_c = node.getAttribute('sync-c') |
| 560 | if not sync_c: |
| 561 | d.sync_c = False |
| 562 | else: |
| 563 | d.sync_c = sync_c.lower() in ("yes", "true", "1") |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 564 | |
| 565 | sync_s = node.getAttribute('sync-s') |
| 566 | if not sync_s: |
| 567 | d.sync_s = False |
| 568 | else: |
| 569 | d.sync_s = sync_s.lower() in ("yes", "true", "1") |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 570 | return d |
| 571 | |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 572 | def _ParseNotice(self, node): |
| 573 | """ |
| 574 | reads a <notice> element from the manifest file |
| 575 | |
| 576 | The <notice> element is distinct from other tags in the XML in that the |
| 577 | data is conveyed between the start and end tag (it's not an empty-element |
| 578 | tag). |
| 579 | |
| 580 | The white space (carriage returns, indentation) for the notice element is |
| 581 | relevant and is parsed in a way that is based on how python docstrings work. |
| 582 | In fact, the code is remarkably similar to here: |
| 583 | http://www.python.org/dev/peps/pep-0257/ |
| 584 | """ |
| 585 | # Get the data out of the node... |
| 586 | notice = node.childNodes[0].data |
| 587 | |
| 588 | # Figure out minimum indentation, skipping the first line (the same line |
| 589 | # as the <notice> tag)... |
| 590 | minIndent = sys.maxint |
| 591 | lines = notice.splitlines() |
| 592 | for line in lines[1:]: |
| 593 | lstrippedLine = line.lstrip() |
| 594 | if lstrippedLine: |
| 595 | indent = len(line) - len(lstrippedLine) |
| 596 | minIndent = min(indent, minIndent) |
| 597 | |
| 598 | # Strip leading / trailing blank lines and also indentation. |
| 599 | cleanLines = [lines[0].strip()] |
| 600 | for line in lines[1:]: |
| 601 | cleanLines.append(line[minIndent:].rstrip()) |
| 602 | |
| 603 | # Clear completely blank lines from front and back... |
| 604 | while cleanLines and not cleanLines[0]: |
| 605 | del cleanLines[0] |
| 606 | while cleanLines and not cleanLines[-1]: |
| 607 | del cleanLines[-1] |
| 608 | |
| 609 | return '\n'.join(cleanLines) |
| 610 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 611 | def _JoinName(self, parent_name, name): |
| 612 | return os.path.join(parent_name, name) |
| 613 | |
| 614 | def _UnjoinName(self, parent_name, name): |
| 615 | return os.path.relpath(name, parent_name) |
| 616 | |
| 617 | def _ParseProject(self, node, parent = None): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 618 | """ |
| 619 | reads a <project> element from the manifest file |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 620 | """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 621 | name = self._reqatt(node, 'name') |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 622 | if parent: |
| 623 | name = self._JoinName(parent.name, name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 624 | |
| 625 | remote = self._get_remote(node) |
| 626 | if remote is None: |
| 627 | remote = self._default.remote |
| 628 | if remote is None: |
| 629 | raise ManifestParseError, \ |
| 630 | "no remote for project %s within %s" % \ |
| 631 | (name, self.manifestFile) |
| 632 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 633 | revisionExpr = node.getAttribute('revision') |
| 634 | if not revisionExpr: |
| 635 | revisionExpr = self._default.revisionExpr |
| 636 | if not revisionExpr: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 637 | raise ManifestParseError, \ |
| 638 | "no revision for project %s within %s" % \ |
| 639 | (name, self.manifestFile) |
| 640 | |
| 641 | path = node.getAttribute('path') |
| 642 | if not path: |
| 643 | path = name |
| 644 | if path.startswith('/'): |
| 645 | raise ManifestParseError, \ |
| 646 | "project %s path cannot be absolute in %s" % \ |
| 647 | (name, self.manifestFile) |
| 648 | |
Mike Pontillo | d315382 | 2012-02-28 11:53:24 -0800 | [diff] [blame] | 649 | rebase = node.getAttribute('rebase') |
| 650 | if not rebase: |
| 651 | rebase = True |
| 652 | else: |
| 653 | rebase = rebase.lower() in ("yes", "true", "1") |
| 654 | |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 655 | sync_c = node.getAttribute('sync-c') |
| 656 | if not sync_c: |
| 657 | sync_c = False |
| 658 | else: |
| 659 | sync_c = sync_c.lower() in ("yes", "true", "1") |
| 660 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 661 | sync_s = node.getAttribute('sync-s') |
| 662 | if not sync_s: |
| 663 | sync_s = self._default.sync_s |
| 664 | else: |
| 665 | sync_s = sync_s.lower() in ("yes", "true", "1") |
| 666 | |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 667 | upstream = node.getAttribute('upstream') |
| 668 | |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 669 | groups = '' |
| 670 | if node.hasAttribute('groups'): |
| 671 | groups = node.getAttribute('groups') |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 672 | groups = [x for x in re.split(r'[,\s]+', groups) if x] |
Brian Harring | 7da1314 | 2012-06-15 02:24:20 -0700 | [diff] [blame] | 673 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 674 | if parent is None: |
| 675 | relpath, worktree, gitdir = self.GetProjectPaths(name, path) |
Shawn O. Pearce | cd81dd6 | 2012-10-26 12:18:00 -0700 | [diff] [blame] | 676 | else: |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 677 | relpath, worktree, gitdir = self.GetSubprojectPaths(parent, path) |
| 678 | |
| 679 | default_groups = ['all', 'name:%s' % name, 'path:%s' % relpath] |
| 680 | groups.extend(set(default_groups).difference(groups)) |
Shawn O. Pearce | cd81dd6 | 2012-10-26 12:18:00 -0700 | [diff] [blame] | 681 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 682 | project = Project(manifest = self, |
| 683 | name = name, |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 684 | remote = remote.ToRemoteSpec(name), |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 685 | gitdir = gitdir, |
| 686 | worktree = worktree, |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 687 | relpath = relpath, |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 688 | revisionExpr = revisionExpr, |
Mike Pontillo | d315382 | 2012-02-28 11:53:24 -0800 | [diff] [blame] | 689 | revisionId = None, |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 690 | rebase = rebase, |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 691 | groups = groups, |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 692 | sync_c = sync_c, |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 693 | sync_s = sync_s, |
| 694 | upstream = upstream, |
| 695 | parent = parent) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 696 | |
| 697 | for n in node.childNodes: |
Shawn O. Pearce | 242b526 | 2009-05-19 13:00:29 -0700 | [diff] [blame] | 698 | if n.nodeName == 'copyfile': |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 699 | self._ParseCopyFile(project, n) |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 700 | if n.nodeName == 'annotation': |
| 701 | self._ParseAnnotation(project, n) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 702 | if n.nodeName == 'project': |
| 703 | project.subprojects.append(self._ParseProject(n, parent = project)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 704 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 705 | return project |
| 706 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 707 | def GetProjectPaths(self, name, path): |
| 708 | relpath = path |
| 709 | if self.IsMirror: |
| 710 | worktree = None |
| 711 | gitdir = os.path.join(self.topdir, '%s.git' % name) |
| 712 | else: |
| 713 | worktree = os.path.join(self.topdir, path).replace('\\', '/') |
| 714 | gitdir = os.path.join(self.repodir, 'projects', '%s.git' % path) |
| 715 | return relpath, worktree, gitdir |
| 716 | |
| 717 | def GetSubprojectName(self, parent, submodule_path): |
| 718 | return os.path.join(parent.name, submodule_path) |
| 719 | |
| 720 | def _JoinRelpath(self, parent_relpath, relpath): |
| 721 | return os.path.join(parent_relpath, relpath) |
| 722 | |
| 723 | def _UnjoinRelpath(self, parent_relpath, relpath): |
| 724 | return os.path.relpath(relpath, parent_relpath) |
| 725 | |
| 726 | def GetSubprojectPaths(self, parent, path): |
| 727 | relpath = self._JoinRelpath(parent.relpath, path) |
| 728 | gitdir = os.path.join(parent.gitdir, 'subprojects', '%s.git' % path) |
| 729 | if self.IsMirror: |
| 730 | worktree = None |
| 731 | else: |
| 732 | worktree = os.path.join(parent.worktree, path).replace('\\', '/') |
| 733 | return relpath, worktree, gitdir |
| 734 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 735 | def _ParseCopyFile(self, project, node): |
| 736 | src = self._reqatt(node, 'src') |
| 737 | dest = self._reqatt(node, 'dest') |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 738 | if not self.IsMirror: |
| 739 | # src is project relative; |
| 740 | # dest is relative to the top of the tree |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 741 | project.AddCopyFile(src, dest, os.path.join(self.topdir, dest)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 742 | |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 743 | def _ParseAnnotation(self, project, node): |
| 744 | name = self._reqatt(node, 'name') |
| 745 | value = self._reqatt(node, 'value') |
| 746 | try: |
| 747 | keep = self._reqatt(node, 'keep').lower() |
| 748 | except ManifestParseError: |
| 749 | keep = "true" |
| 750 | if keep != "true" and keep != "false": |
| 751 | raise ManifestParseError, "optional \"keep\" attribute must be \"true\" or \"false\"" |
| 752 | project.AddAnnotation(name, value, keep) |
| 753 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 754 | def _get_remote(self, node): |
| 755 | name = node.getAttribute('remote') |
| 756 | if not name: |
| 757 | return None |
| 758 | |
| 759 | v = self._remotes.get(name) |
| 760 | if not v: |
| 761 | raise ManifestParseError, \ |
| 762 | "remote %s not defined in %s" % \ |
| 763 | (name, self.manifestFile) |
| 764 | return v |
| 765 | |
| 766 | def _reqatt(self, node, attname): |
| 767 | """ |
| 768 | reads a required attribute from the node. |
| 769 | """ |
| 770 | v = node.getAttribute(attname) |
| 771 | if not v: |
| 772 | raise ManifestParseError, \ |
| 773 | "no %s in <%s> within %s" % \ |
| 774 | (attname, node.nodeName, self.manifestFile) |
| 775 | return v |