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 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 16 | import itertools |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 17 | import os |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 18 | import re |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 19 | import sys |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 20 | import urlparse |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 21 | import xml.dom.minidom |
| 22 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 23 | from git_config import GitConfig, IsId |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 24 | from project import RemoteSpec, Project, MetaProject, R_HEADS, HEAD |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 25 | from error import ManifestParseError |
| 26 | |
| 27 | MANIFEST_FILE_NAME = 'manifest.xml' |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 28 | LOCAL_MANIFEST_NAME = 'local_manifest.xml' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 29 | |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 30 | urlparse.uses_relative.extend(['ssh', 'git']) |
| 31 | urlparse.uses_netloc.extend(['ssh', 'git']) |
| 32 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | class _Default(object): |
| 34 | """Project defaults within the manifest.""" |
| 35 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 36 | revisionExpr = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 37 | remote = None |
Shawn O. Pearce | 6392c87 | 2011-09-22 17:44:31 -0700 | [diff] [blame] | 38 | sync_j = 1 |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 39 | sync_c = False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 40 | |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 41 | class _XmlRemote(object): |
| 42 | def __init__(self, |
| 43 | name, |
| 44 | fetch=None, |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 45 | manifestUrl=None, |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 46 | review=None): |
| 47 | self.name = name |
| 48 | self.fetchUrl = fetch |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 49 | self.manifestUrl = manifestUrl |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 50 | self.reviewUrl = review |
Conley Owens | ceea368 | 2011-10-20 10:45:47 -0700 | [diff] [blame] | 51 | self.resolvedFetchUrl = self._resolveFetchUrl() |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 52 | |
Conley Owens | ceea368 | 2011-10-20 10:45:47 -0700 | [diff] [blame] | 53 | def _resolveFetchUrl(self): |
| 54 | url = self.fetchUrl.rstrip('/') |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 55 | manifestUrl = self.manifestUrl.rstrip('/') |
| 56 | # urljoin will get confused if there is no scheme in the base url |
| 57 | # ie, if manifestUrl is of the form <hostname:port> |
| 58 | if manifestUrl.find(':') != manifestUrl.find('/') - 1: |
| 59 | manifestUrl = 'gopher://' + manifestUrl |
| 60 | url = urlparse.urljoin(manifestUrl, url) |
Conley Owens | ceea368 | 2011-10-20 10:45:47 -0700 | [diff] [blame] | 61 | return re.sub(r'^gopher://', '', url) |
| 62 | |
| 63 | def ToRemoteSpec(self, projectName): |
Conley Owens | 9d8f914 | 2011-10-20 14:36:35 -0700 | [diff] [blame] | 64 | url = self.resolvedFetchUrl.rstrip('/') + '/' + projectName |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 65 | return RemoteSpec(self.name, url, self.reviewUrl) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 66 | |
Shawn O. Pearce | c8a300f | 2009-05-18 13:19:57 -0700 | [diff] [blame] | 67 | class XmlManifest(object): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 68 | """manages the repo configuration file""" |
| 69 | |
| 70 | def __init__(self, repodir): |
| 71 | self.repodir = os.path.abspath(repodir) |
| 72 | self.topdir = os.path.dirname(self.repodir) |
| 73 | 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] | 74 | self.globalConfig = GitConfig.ForUser() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 75 | |
| 76 | self.repoProject = MetaProject(self, 'repo', |
| 77 | gitdir = os.path.join(repodir, 'repo/.git'), |
| 78 | worktree = os.path.join(repodir, 'repo')) |
| 79 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 80 | self.manifestProject = MetaProject(self, 'manifests', |
Shawn O. Pearce | f5c25a6 | 2008-11-04 08:11:53 -0800 | [diff] [blame] | 81 | gitdir = os.path.join(repodir, 'manifests.git'), |
| 82 | worktree = os.path.join(repodir, 'manifests')) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 83 | |
| 84 | self._Unload() |
| 85 | |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 86 | def Override(self, name): |
| 87 | """Use a different manifest, just for the current instantiation. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 88 | """ |
| 89 | path = os.path.join(self.manifestProject.worktree, name) |
| 90 | if not os.path.isfile(path): |
| 91 | raise ManifestParseError('manifest %s not found' % name) |
| 92 | |
| 93 | old = self.manifestFile |
| 94 | try: |
| 95 | self.manifestFile = path |
| 96 | self._Unload() |
| 97 | self._Load() |
| 98 | finally: |
| 99 | self.manifestFile = old |
| 100 | |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 101 | def Link(self, name): |
| 102 | """Update the repo metadata to use a different manifest. |
| 103 | """ |
| 104 | self.Override(name) |
| 105 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 106 | try: |
| 107 | if os.path.exists(self.manifestFile): |
| 108 | os.remove(self.manifestFile) |
| 109 | os.symlink('manifests/%s' % name, self.manifestFile) |
| 110 | except OSError, e: |
| 111 | raise ManifestParseError('cannot link manifest %s' % name) |
| 112 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 113 | def _RemoteToXml(self, r, doc, root): |
| 114 | e = doc.createElement('remote') |
| 115 | root.appendChild(e) |
| 116 | e.setAttribute('name', r.name) |
| 117 | e.setAttribute('fetch', r.fetchUrl) |
| 118 | if r.reviewUrl is not None: |
| 119 | e.setAttribute('review', r.reviewUrl) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 120 | |
| 121 | def Save(self, fd, peg_rev=False): |
| 122 | """Write the current manifest out to the given file descriptor. |
| 123 | """ |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 124 | mp = self.manifestProject |
| 125 | |
| 126 | groups = mp.config.GetString('manifest.groups') |
Colin Cross | c39864f | 2012-04-23 13:41:58 -0700 | [diff] [blame] | 127 | if not groups: |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 128 | groups = 'default' |
| 129 | groups = [x for x in re.split(r'[,\s]+', groups) if x] |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 130 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 131 | doc = xml.dom.minidom.Document() |
| 132 | root = doc.createElement('manifest') |
| 133 | doc.appendChild(root) |
| 134 | |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 135 | # Save out the notice. There's a little bit of work here to give it the |
| 136 | # right whitespace, which assumes that the notice is automatically indented |
| 137 | # by 4 by minidom. |
| 138 | if self.notice: |
| 139 | notice_element = root.appendChild(doc.createElement('notice')) |
| 140 | notice_lines = self.notice.splitlines() |
| 141 | indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:] |
| 142 | notice_element.appendChild(doc.createTextNode(indented_notice)) |
| 143 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 144 | d = self.default |
| 145 | sort_remotes = list(self.remotes.keys()) |
| 146 | sort_remotes.sort() |
| 147 | |
| 148 | for r in sort_remotes: |
| 149 | self._RemoteToXml(self.remotes[r], doc, root) |
| 150 | if self.remotes: |
| 151 | root.appendChild(doc.createTextNode('')) |
| 152 | |
| 153 | have_default = False |
| 154 | e = doc.createElement('default') |
| 155 | if d.remote: |
| 156 | have_default = True |
| 157 | e.setAttribute('remote', d.remote.name) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 158 | if d.revisionExpr: |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 159 | have_default = True |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 160 | e.setAttribute('revision', d.revisionExpr) |
Shawn O. Pearce | 6392c87 | 2011-09-22 17:44:31 -0700 | [diff] [blame] | 161 | if d.sync_j > 1: |
| 162 | have_default = True |
| 163 | e.setAttribute('sync-j', '%d' % d.sync_j) |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 164 | if d.sync_c: |
| 165 | have_default = True |
| 166 | e.setAttribute('sync-c', 'true') |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 167 | if have_default: |
| 168 | root.appendChild(e) |
| 169 | root.appendChild(doc.createTextNode('')) |
| 170 | |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 171 | if self._manifest_server: |
| 172 | e = doc.createElement('manifest-server') |
| 173 | e.setAttribute('url', self._manifest_server) |
| 174 | root.appendChild(e) |
| 175 | root.appendChild(doc.createTextNode('')) |
| 176 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 177 | sort_projects = list(self.projects.keys()) |
| 178 | sort_projects.sort() |
| 179 | |
| 180 | for p in sort_projects: |
| 181 | p = self.projects[p] |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 182 | |
| 183 | if not p.MatchesGroups(groups): |
| 184 | continue |
| 185 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 186 | e = doc.createElement('project') |
| 187 | root.appendChild(e) |
| 188 | e.setAttribute('name', p.name) |
| 189 | if p.relpath != p.name: |
| 190 | e.setAttribute('path', p.relpath) |
| 191 | if not d.remote or p.remote.name != d.remote.name: |
| 192 | e.setAttribute('remote', p.remote.name) |
| 193 | if peg_rev: |
| 194 | if self.IsMirror: |
| 195 | e.setAttribute('revision', |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 196 | p.bare_git.rev_parse(p.revisionExpr + '^0')) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 197 | else: |
| 198 | e.setAttribute('revision', |
| 199 | p.work_git.rev_parse(HEAD + '^0')) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 200 | elif not d.revisionExpr or p.revisionExpr != d.revisionExpr: |
| 201 | e.setAttribute('revision', p.revisionExpr) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 202 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 203 | for c in p.copyfiles: |
| 204 | ce = doc.createElement('copyfile') |
| 205 | ce.setAttribute('src', c.src) |
| 206 | ce.setAttribute('dest', c.dest) |
| 207 | e.appendChild(ce) |
| 208 | |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 209 | egroups = [g for g in p.groups if g != 'default'] |
| 210 | if egroups: |
| 211 | e.setAttribute('groups', ','.join(egroups)) |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 212 | |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 213 | for a in p.annotations: |
| 214 | if a.keep == "true": |
| 215 | ae = doc.createElement('annotation') |
| 216 | ae.setAttribute('name', a.name) |
| 217 | ae.setAttribute('value', a.value) |
| 218 | e.appendChild(ae) |
| 219 | |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 220 | if p.sync_c: |
| 221 | e.setAttribute('sync-c', 'true') |
| 222 | |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 223 | if self._repo_hooks_project: |
| 224 | root.appendChild(doc.createTextNode('')) |
| 225 | e = doc.createElement('repo-hooks') |
| 226 | e.setAttribute('in-project', self._repo_hooks_project.name) |
| 227 | e.setAttribute('enabled-list', |
| 228 | ' '.join(self._repo_hooks_project.enabled_repo_hooks)) |
| 229 | root.appendChild(e) |
| 230 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 231 | doc.writexml(fd, '', ' ', '\n', 'UTF-8') |
| 232 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 233 | @property |
| 234 | def projects(self): |
| 235 | self._Load() |
| 236 | return self._projects |
| 237 | |
| 238 | @property |
| 239 | def remotes(self): |
| 240 | self._Load() |
| 241 | return self._remotes |
| 242 | |
| 243 | @property |
| 244 | def default(self): |
| 245 | self._Load() |
| 246 | return self._default |
| 247 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 248 | @property |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 249 | def repo_hooks_project(self): |
| 250 | self._Load() |
| 251 | return self._repo_hooks_project |
| 252 | |
| 253 | @property |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 254 | def notice(self): |
| 255 | self._Load() |
| 256 | return self._notice |
| 257 | |
| 258 | @property |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 259 | def manifest_server(self): |
| 260 | self._Load() |
Shawn O. Pearce | 34fb20f | 2011-11-30 13:41:02 -0800 | [diff] [blame] | 261 | return self._manifest_server |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 262 | |
| 263 | @property |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 264 | def IsMirror(self): |
| 265 | return self.manifestProject.config.GetBoolean('repo.mirror') |
| 266 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 267 | def _Unload(self): |
| 268 | self._loaded = False |
| 269 | self._projects = {} |
| 270 | self._remotes = {} |
| 271 | self._default = None |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 272 | self._repo_hooks_project = None |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 273 | self._notice = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 274 | self.branch = None |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 275 | self._manifest_server = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 276 | |
| 277 | def _Load(self): |
| 278 | if not self._loaded: |
Shawn O. Pearce | 2450a29 | 2008-11-04 08:22:07 -0800 | [diff] [blame] | 279 | m = self.manifestProject |
| 280 | b = m.GetBranch(m.CurrentBranch).merge |
Shawn O. Pearce | 21c5c34 | 2009-06-25 16:47:30 -0700 | [diff] [blame] | 281 | if b is not None and b.startswith(R_HEADS): |
Shawn O. Pearce | 2450a29 | 2008-11-04 08:22:07 -0800 | [diff] [blame] | 282 | b = b[len(R_HEADS):] |
| 283 | self.branch = b |
| 284 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 285 | nodes = [] |
| 286 | nodes.append(self._ParseManifestXml(self.manifestFile)) |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 287 | |
| 288 | local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME) |
| 289 | if os.path.exists(local): |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 290 | nodes.append(self._ParseManifestXml(local)) |
| 291 | |
| 292 | self._ParseManifest(nodes) |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 293 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 294 | if self.IsMirror: |
| 295 | self._AddMetaProjectMirror(self.repoProject) |
| 296 | self._AddMetaProjectMirror(self.manifestProject) |
| 297 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 298 | self._loaded = True |
| 299 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 300 | def _ParseManifestXml(self, path): |
Brian Harring | 2644874 | 2011-04-28 05:04:41 -0700 | [diff] [blame] | 301 | root = xml.dom.minidom.parse(path) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 302 | if not root or not root.childNodes: |
Brian Harring | 2644874 | 2011-04-28 05:04:41 -0700 | [diff] [blame] | 303 | raise ManifestParseError("no root node in %s" % (path,)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 304 | |
| 305 | config = root.childNodes[0] |
| 306 | if config.nodeName != 'manifest': |
Brian Harring | 2644874 | 2011-04-28 05:04:41 -0700 | [diff] [blame] | 307 | raise ManifestParseError("no <manifest> in %s" % (path,)) |
| 308 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 309 | nodes = [] |
Brian Harring | 2644874 | 2011-04-28 05:04:41 -0700 | [diff] [blame] | 310 | for node in config.childNodes: |
| 311 | if node.nodeName == 'include': |
| 312 | name = self._reqatt(node, 'name') |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 313 | fp = os.path.join(os.path.dirname(path), name) |
Brian Harring | 2644874 | 2011-04-28 05:04:41 -0700 | [diff] [blame] | 314 | if not os.path.isfile(fp): |
| 315 | raise ManifestParseError, \ |
| 316 | "include %s doesn't exist or isn't a file" % \ |
| 317 | (name,) |
| 318 | try: |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 319 | nodes.extend(self._ParseManifestXml(fp)) |
Brian Harring | 2644874 | 2011-04-28 05:04:41 -0700 | [diff] [blame] | 320 | # should isolate this to the exact exception, but that's |
| 321 | # tricky. actual parsing implementation may vary. |
| 322 | except (KeyboardInterrupt, RuntimeError, SystemExit): |
| 323 | raise |
| 324 | except Exception, e: |
| 325 | raise ManifestParseError( |
| 326 | "failed parsing included manifest %s: %s", (name, e)) |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 327 | else: |
| 328 | nodes.append(node) |
| 329 | return nodes |
Brian Harring | 2644874 | 2011-04-28 05:04:41 -0700 | [diff] [blame] | 330 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 331 | def _ParseManifest(self, node_list): |
| 332 | for node in itertools.chain(*node_list): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 333 | if node.nodeName == 'remote': |
| 334 | remote = self._ParseRemote(node) |
| 335 | if self._remotes.get(remote.name): |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 336 | raise ManifestParseError( |
| 337 | 'duplicate remote %s in %s' % |
| 338 | (remote.name, self.manifestFile)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 339 | self._remotes[remote.name] = remote |
| 340 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 341 | for node in itertools.chain(*node_list): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 342 | if node.nodeName == 'default': |
| 343 | if self._default is not None: |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 344 | raise ManifestParseError( |
| 345 | 'duplicate default in %s' % |
| 346 | (self.manifestFile)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 347 | self._default = self._ParseDefault(node) |
| 348 | if self._default is None: |
| 349 | self._default = _Default() |
| 350 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 351 | for node in itertools.chain(*node_list): |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 352 | if node.nodeName == 'notice': |
| 353 | if self._notice is not None: |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 354 | raise ManifestParseError( |
| 355 | 'duplicate notice in %s' % |
| 356 | (self.manifestFile)) |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 357 | self._notice = self._ParseNotice(node) |
| 358 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 359 | for node in itertools.chain(*node_list): |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 360 | if node.nodeName == 'manifest-server': |
| 361 | url = self._reqatt(node, 'url') |
| 362 | if self._manifest_server is not None: |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 363 | raise ManifestParseError( |
| 364 | 'duplicate manifest-server in %s' % |
| 365 | (self.manifestFile)) |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 366 | self._manifest_server = url |
| 367 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 368 | for node in itertools.chain(*node_list): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 369 | if node.nodeName == 'project': |
| 370 | project = self._ParseProject(node) |
| 371 | if self._projects.get(project.name): |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 372 | raise ManifestParseError( |
| 373 | 'duplicate project %s in %s' % |
| 374 | (project.name, self.manifestFile)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 375 | self._projects[project.name] = project |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 376 | if node.nodeName == 'repo-hooks': |
| 377 | # Get the name of the project and the (space-separated) list of enabled. |
| 378 | repo_hooks_project = self._reqatt(node, 'in-project') |
| 379 | enabled_repo_hooks = self._reqatt(node, 'enabled-list').split() |
| 380 | |
| 381 | # Only one project can be the hooks project |
| 382 | if self._repo_hooks_project is not None: |
| 383 | raise ManifestParseError( |
| 384 | 'duplicate repo-hooks in %s' % |
| 385 | (self.manifestFile)) |
| 386 | |
| 387 | # Store a reference to the Project. |
| 388 | try: |
| 389 | self._repo_hooks_project = self._projects[repo_hooks_project] |
| 390 | except KeyError: |
| 391 | raise ManifestParseError( |
| 392 | 'project %s not found for repo-hooks' % |
| 393 | (repo_hooks_project)) |
| 394 | |
| 395 | # Store the enabled hooks in the Project object. |
| 396 | self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 397 | if node.nodeName == 'remove-project': |
| 398 | name = self._reqatt(node, 'name') |
| 399 | try: |
| 400 | del self._projects[name] |
| 401 | except KeyError: |
| 402 | raise ManifestParseError( |
| 403 | 'project %s not found' % |
| 404 | (name)) |
| 405 | |
| 406 | # If the manifest removes the hooks project, treat it as if it deleted |
| 407 | # the repo-hooks element too. |
| 408 | if self._repo_hooks_project and (self._repo_hooks_project.name == name): |
| 409 | self._repo_hooks_project = None |
| 410 | |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 411 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 412 | def _AddMetaProjectMirror(self, m): |
| 413 | name = None |
| 414 | m_url = m.GetRemote(m.remote.name).url |
| 415 | if m_url.endswith('/.git'): |
| 416 | raise ManifestParseError, 'refusing to mirror %s' % m_url |
| 417 | |
| 418 | if self._default and self._default.remote: |
Conley Owens | ceea368 | 2011-10-20 10:45:47 -0700 | [diff] [blame] | 419 | url = self._default.remote.resolvedFetchUrl |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 420 | if not url.endswith('/'): |
| 421 | url += '/' |
| 422 | if m_url.startswith(url): |
| 423 | remote = self._default.remote |
| 424 | name = m_url[len(url):] |
| 425 | |
| 426 | if name is None: |
| 427 | s = m_url.rindex('/') + 1 |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 428 | manifestUrl = self.manifestProject.config.GetString('remote.origin.url') |
| 429 | remote = _XmlRemote('origin', m_url[:s], manifestUrl) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 430 | name = m_url[s:] |
| 431 | |
| 432 | if name.endswith('.git'): |
| 433 | name = name[:-4] |
| 434 | |
| 435 | if name not in self._projects: |
| 436 | m.PreSync() |
| 437 | gitdir = os.path.join(self.topdir, '%s.git' % name) |
| 438 | project = Project(manifest = self, |
| 439 | name = name, |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 440 | remote = remote.ToRemoteSpec(name), |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 441 | gitdir = gitdir, |
| 442 | worktree = None, |
| 443 | relpath = None, |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 444 | revisionExpr = m.revisionExpr, |
| 445 | revisionId = None) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 446 | self._projects[project.name] = project |
| 447 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 448 | def _ParseRemote(self, node): |
| 449 | """ |
| 450 | reads a <remote> element from the manifest file |
| 451 | """ |
| 452 | name = self._reqatt(node, 'name') |
| 453 | fetch = self._reqatt(node, 'fetch') |
| 454 | review = node.getAttribute('review') |
Shawn O. Pearce | ae6e094 | 2008-11-06 10:25:35 -0800 | [diff] [blame] | 455 | if review == '': |
| 456 | review = None |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 457 | manifestUrl = self.manifestProject.config.GetString('remote.origin.url') |
| 458 | return _XmlRemote(name, fetch, manifestUrl, review) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 459 | |
| 460 | def _ParseDefault(self, node): |
| 461 | """ |
| 462 | reads a <default> element from the manifest file |
| 463 | """ |
| 464 | d = _Default() |
| 465 | d.remote = self._get_remote(node) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 466 | d.revisionExpr = node.getAttribute('revision') |
| 467 | if d.revisionExpr == '': |
| 468 | d.revisionExpr = None |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 469 | |
Shawn O. Pearce | 6392c87 | 2011-09-22 17:44:31 -0700 | [diff] [blame] | 470 | sync_j = node.getAttribute('sync-j') |
| 471 | if sync_j == '' or sync_j is None: |
| 472 | d.sync_j = 1 |
| 473 | else: |
| 474 | d.sync_j = int(sync_j) |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 475 | |
| 476 | sync_c = node.getAttribute('sync-c') |
| 477 | if not sync_c: |
| 478 | d.sync_c = False |
| 479 | else: |
| 480 | d.sync_c = sync_c.lower() in ("yes", "true", "1") |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 481 | return d |
| 482 | |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 483 | def _ParseNotice(self, node): |
| 484 | """ |
| 485 | reads a <notice> element from the manifest file |
| 486 | |
| 487 | The <notice> element is distinct from other tags in the XML in that the |
| 488 | data is conveyed between the start and end tag (it's not an empty-element |
| 489 | tag). |
| 490 | |
| 491 | The white space (carriage returns, indentation) for the notice element is |
| 492 | relevant and is parsed in a way that is based on how python docstrings work. |
| 493 | In fact, the code is remarkably similar to here: |
| 494 | http://www.python.org/dev/peps/pep-0257/ |
| 495 | """ |
| 496 | # Get the data out of the node... |
| 497 | notice = node.childNodes[0].data |
| 498 | |
| 499 | # Figure out minimum indentation, skipping the first line (the same line |
| 500 | # as the <notice> tag)... |
| 501 | minIndent = sys.maxint |
| 502 | lines = notice.splitlines() |
| 503 | for line in lines[1:]: |
| 504 | lstrippedLine = line.lstrip() |
| 505 | if lstrippedLine: |
| 506 | indent = len(line) - len(lstrippedLine) |
| 507 | minIndent = min(indent, minIndent) |
| 508 | |
| 509 | # Strip leading / trailing blank lines and also indentation. |
| 510 | cleanLines = [lines[0].strip()] |
| 511 | for line in lines[1:]: |
| 512 | cleanLines.append(line[minIndent:].rstrip()) |
| 513 | |
| 514 | # Clear completely blank lines from front and back... |
| 515 | while cleanLines and not cleanLines[0]: |
| 516 | del cleanLines[0] |
| 517 | while cleanLines and not cleanLines[-1]: |
| 518 | del cleanLines[-1] |
| 519 | |
| 520 | return '\n'.join(cleanLines) |
| 521 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 522 | def _ParseProject(self, node): |
| 523 | """ |
| 524 | reads a <project> element from the manifest file |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 525 | """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 526 | name = self._reqatt(node, 'name') |
| 527 | |
| 528 | remote = self._get_remote(node) |
| 529 | if remote is None: |
| 530 | remote = self._default.remote |
| 531 | if remote is None: |
| 532 | raise ManifestParseError, \ |
| 533 | "no remote for project %s within %s" % \ |
| 534 | (name, self.manifestFile) |
| 535 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 536 | revisionExpr = node.getAttribute('revision') |
| 537 | if not revisionExpr: |
| 538 | revisionExpr = self._default.revisionExpr |
| 539 | if not revisionExpr: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 540 | raise ManifestParseError, \ |
| 541 | "no revision for project %s within %s" % \ |
| 542 | (name, self.manifestFile) |
| 543 | |
| 544 | path = node.getAttribute('path') |
| 545 | if not path: |
| 546 | path = name |
| 547 | if path.startswith('/'): |
| 548 | raise ManifestParseError, \ |
| 549 | "project %s path cannot be absolute in %s" % \ |
| 550 | (name, self.manifestFile) |
| 551 | |
Mike Pontillo | d315382 | 2012-02-28 11:53:24 -0800 | [diff] [blame] | 552 | rebase = node.getAttribute('rebase') |
| 553 | if not rebase: |
| 554 | rebase = True |
| 555 | else: |
| 556 | rebase = rebase.lower() in ("yes", "true", "1") |
| 557 | |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 558 | sync_c = node.getAttribute('sync-c') |
| 559 | if not sync_c: |
| 560 | sync_c = False |
| 561 | else: |
| 562 | sync_c = sync_c.lower() in ("yes", "true", "1") |
| 563 | |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 564 | groups = '' |
| 565 | if node.hasAttribute('groups'): |
| 566 | groups = node.getAttribute('groups') |
| 567 | groups = [x for x in re.split('[,\s]+', groups) if x] |
| 568 | if 'default' not in groups: |
| 569 | groups.append('default') |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 570 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 571 | if self.IsMirror: |
| 572 | relpath = None |
| 573 | worktree = None |
| 574 | gitdir = os.path.join(self.topdir, '%s.git' % name) |
| 575 | else: |
Anthony Newnam | df14a70 | 2011-01-09 17:31:57 -0800 | [diff] [blame] | 576 | worktree = os.path.join(self.topdir, path).replace('\\', '/') |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 577 | gitdir = os.path.join(self.repodir, 'projects/%s.git' % path) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 578 | |
| 579 | project = Project(manifest = self, |
| 580 | name = name, |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 581 | remote = remote.ToRemoteSpec(name), |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 582 | gitdir = gitdir, |
| 583 | worktree = worktree, |
| 584 | relpath = path, |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 585 | revisionExpr = revisionExpr, |
Mike Pontillo | d315382 | 2012-02-28 11:53:24 -0800 | [diff] [blame] | 586 | revisionId = None, |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 587 | rebase = rebase, |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 588 | groups = groups, |
| 589 | sync_c = sync_c) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 590 | |
| 591 | for n in node.childNodes: |
Shawn O. Pearce | 242b526 | 2009-05-19 13:00:29 -0700 | [diff] [blame] | 592 | if n.nodeName == 'copyfile': |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 593 | self._ParseCopyFile(project, n) |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 594 | if n.nodeName == 'annotation': |
| 595 | self._ParseAnnotation(project, n) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 596 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 597 | return project |
| 598 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 599 | def _ParseCopyFile(self, project, node): |
| 600 | src = self._reqatt(node, 'src') |
| 601 | dest = self._reqatt(node, 'dest') |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 602 | if not self.IsMirror: |
| 603 | # src is project relative; |
| 604 | # dest is relative to the top of the tree |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 605 | 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] | 606 | |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 607 | def _ParseAnnotation(self, project, node): |
| 608 | name = self._reqatt(node, 'name') |
| 609 | value = self._reqatt(node, 'value') |
| 610 | try: |
| 611 | keep = self._reqatt(node, 'keep').lower() |
| 612 | except ManifestParseError: |
| 613 | keep = "true" |
| 614 | if keep != "true" and keep != "false": |
| 615 | raise ManifestParseError, "optional \"keep\" attribute must be \"true\" or \"false\"" |
| 616 | project.AddAnnotation(name, value, keep) |
| 617 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 618 | def _get_remote(self, node): |
| 619 | name = node.getAttribute('remote') |
| 620 | if not name: |
| 621 | return None |
| 622 | |
| 623 | v = self._remotes.get(name) |
| 624 | if not v: |
| 625 | raise ManifestParseError, \ |
| 626 | "remote %s not defined in %s" % \ |
| 627 | (name, self.manifestFile) |
| 628 | return v |
| 629 | |
| 630 | def _reqatt(self, node, attname): |
| 631 | """ |
| 632 | reads a required attribute from the node. |
| 633 | """ |
| 634 | v = node.getAttribute(attname) |
| 635 | if not v: |
| 636 | raise ManifestParseError, \ |
| 637 | "no %s in <%s> within %s" % \ |
| 638 | (attname, node.nodeName, self.manifestFile) |
| 639 | return v |