blob: e40e6fac0a711803717d76945b175b7503f87685 [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001#
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 Owenscecd1d82012-11-01 22:59:27 -070016from __future__ import print_function
Colin Cross23acdd32012-04-21 00:33:54 -070017import itertools
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070018import os
Conley Owensdb728cd2011-09-26 16:34:01 -070019import re
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070020import sys
David Pursehouse59bbb582013-05-17 10:49:33 +090021import xml.dom.minidom
22
23from pyversion import is_python3
24if is_python3():
Chirayu Desai217ea7d2013-03-01 19:14:38 +053025 import urllib.parse
David Pursehouse59bbb582013-05-17 10:49:33 +090026else:
Chirayu Desai217ea7d2013-03-01 19:14:38 +053027 import imp
28 import urlparse
29 urllib = imp.new_module('urllib')
Chirayu Desaidb2ad9d2013-06-11 13:42:25 +053030 urllib.parse = urlparse
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070031
David Pursehousee15c65a2012-08-22 10:46:11 +090032from git_config import GitConfig
David Pursehousee00aa6b2012-09-11 14:33:51 +090033from git_refs import R_HEADS, HEAD
34from project import RemoteSpec, Project, MetaProject
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070035from error import ManifestParseError
36
37MANIFEST_FILE_NAME = 'manifest.xml'
Shawn O. Pearce5cc66792008-10-23 16:19:27 -070038LOCAL_MANIFEST_NAME = 'local_manifest.xml'
David Pursehouse2d5a0df2012-11-13 02:50:36 +090039LOCAL_MANIFESTS_DIR_NAME = 'local_manifests'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070040
Chirayu Desai217ea7d2013-03-01 19:14:38 +053041urllib.parse.uses_relative.extend(['ssh', 'git'])
42urllib.parse.uses_netloc.extend(['ssh', 'git'])
Conley Owensdb728cd2011-09-26 16:34:01 -070043
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070044class _Default(object):
45 """Project defaults within the manifest."""
46
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -070047 revisionExpr = None
Conley Owensb6a16e62013-09-25 15:06:09 -070048 destBranchExpr = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070049 remote = None
Shawn O. Pearce6392c872011-09-22 17:44:31 -070050 sync_j = 1
Anatol Pomazau79770d22012-04-20 14:41:59 -070051 sync_c = False
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +080052 sync_s = False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070053
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070054class _XmlRemote(object):
55 def __init__(self,
56 name,
Yestin Sunb292b982012-07-02 07:32:50 -070057 alias=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070058 fetch=None,
Conley Owensdb728cd2011-09-26 16:34:01 -070059 manifestUrl=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070060 review=None):
61 self.name = name
62 self.fetchUrl = fetch
Conley Owensdb728cd2011-09-26 16:34:01 -070063 self.manifestUrl = manifestUrl
Yestin Sunb292b982012-07-02 07:32:50 -070064 self.remoteAlias = alias
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070065 self.reviewUrl = review
Conley Owensceea3682011-10-20 10:45:47 -070066 self.resolvedFetchUrl = self._resolveFetchUrl()
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070067
David Pursehouse717ece92012-11-13 08:49:16 +090068 def __eq__(self, other):
69 return self.__dict__ == other.__dict__
70
71 def __ne__(self, other):
72 return self.__dict__ != other.__dict__
73
Conley Owensceea3682011-10-20 10:45:47 -070074 def _resolveFetchUrl(self):
75 url = self.fetchUrl.rstrip('/')
Conley Owensdb728cd2011-09-26 16:34:01 -070076 manifestUrl = self.manifestUrl.rstrip('/')
Shawn Pearcea9f11b32013-01-02 15:40:48 -080077 p = manifestUrl.startswith('persistent-http')
78 if p:
79 manifestUrl = manifestUrl[len('persistent-'):]
80
Conley Owensdb728cd2011-09-26 16:34:01 -070081 # urljoin will get confused if there is no scheme in the base url
82 # ie, if manifestUrl is of the form <hostname:port>
83 if manifestUrl.find(':') != manifestUrl.find('/') - 1:
David Pursehousec1b86a22012-11-14 11:36:51 +090084 manifestUrl = 'gopher://' + manifestUrl
Chirayu Desai217ea7d2013-03-01 19:14:38 +053085 url = urllib.parse.urljoin(manifestUrl, url)
Shawn Pearcea9f11b32013-01-02 15:40:48 -080086 url = re.sub(r'^gopher://', '', url)
87 if p:
88 url = 'persistent-' + url
89 return url
Conley Owensceea3682011-10-20 10:45:47 -070090
91 def ToRemoteSpec(self, projectName):
Conley Owens9d8f9142011-10-20 14:36:35 -070092 url = self.resolvedFetchUrl.rstrip('/') + '/' + projectName
Yestin Sunb292b982012-07-02 07:32:50 -070093 remoteName = self.name
Yestin Sunb292b982012-07-02 07:32:50 -070094 return RemoteSpec(remoteName, url, self.reviewUrl)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070095
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070096class XmlManifest(object):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070097 """manages the repo configuration file"""
98
99 def __init__(self, repodir):
100 self.repodir = os.path.abspath(repodir)
101 self.topdir = os.path.dirname(self.repodir)
102 self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700103 self.globalConfig = GitConfig.ForUser()
David Pursehouse4eb285c2013-02-14 16:28:44 +0900104 self.localManifestWarning = False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700105
106 self.repoProject = MetaProject(self, 'repo',
107 gitdir = os.path.join(repodir, 'repo/.git'),
108 worktree = os.path.join(repodir, 'repo'))
109
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700110 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -0800111 gitdir = os.path.join(repodir, 'manifests.git'),
112 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700113
114 self._Unload()
115
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700116 def Override(self, name):
117 """Use a different manifest, just for the current instantiation.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700118 """
119 path = os.path.join(self.manifestProject.worktree, name)
120 if not os.path.isfile(path):
121 raise ManifestParseError('manifest %s not found' % name)
122
123 old = self.manifestFile
124 try:
125 self.manifestFile = path
126 self._Unload()
127 self._Load()
128 finally:
129 self.manifestFile = old
130
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700131 def Link(self, name):
132 """Update the repo metadata to use a different manifest.
133 """
134 self.Override(name)
135
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700136 try:
Sebastian Frias223bf962012-11-21 19:09:25 +0100137 if os.path.lexists(self.manifestFile):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700138 os.remove(self.manifestFile)
139 os.symlink('manifests/%s' % name, self.manifestFile)
Sebastian Frias223bf962012-11-21 19:09:25 +0100140 except OSError as e:
141 raise ManifestParseError('cannot link manifest %s: %s' % (name, str(e)))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700142
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800143 def _RemoteToXml(self, r, doc, root):
144 e = doc.createElement('remote')
145 root.appendChild(e)
146 e.setAttribute('name', r.name)
147 e.setAttribute('fetch', r.fetchUrl)
148 if r.reviewUrl is not None:
149 e.setAttribute('review', r.reviewUrl)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800150
Brian Harring14a66742012-09-28 20:21:57 -0700151 def Save(self, fd, peg_rev=False, peg_rev_upstream=True):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800152 """Write the current manifest out to the given file descriptor.
153 """
Colin Cross5acde752012-03-28 20:15:45 -0700154 mp = self.manifestProject
155
156 groups = mp.config.GetString('manifest.groups')
Matt Gumbel0c635bb2012-12-21 10:14:53 -0800157 if groups:
158 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Colin Cross5acde752012-03-28 20:15:45 -0700159
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800160 doc = xml.dom.minidom.Document()
161 root = doc.createElement('manifest')
162 doc.appendChild(root)
163
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700164 # Save out the notice. There's a little bit of work here to give it the
165 # right whitespace, which assumes that the notice is automatically indented
166 # by 4 by minidom.
167 if self.notice:
168 notice_element = root.appendChild(doc.createElement('notice'))
169 notice_lines = self.notice.splitlines()
170 indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:]
171 notice_element.appendChild(doc.createTextNode(indented_notice))
172
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800173 d = self.default
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800174
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530175 for r in sorted(self.remotes):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800176 self._RemoteToXml(self.remotes[r], doc, root)
177 if self.remotes:
178 root.appendChild(doc.createTextNode(''))
179
180 have_default = False
181 e = doc.createElement('default')
182 if d.remote:
183 have_default = True
184 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700185 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800186 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700187 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700188 if d.sync_j > 1:
189 have_default = True
190 e.setAttribute('sync-j', '%d' % d.sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700191 if d.sync_c:
192 have_default = True
193 e.setAttribute('sync-c', 'true')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800194 if d.sync_s:
195 have_default = True
196 e.setAttribute('sync-s', 'true')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800197 if have_default:
198 root.appendChild(e)
199 root.appendChild(doc.createTextNode(''))
200
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700201 if self._manifest_server:
202 e = doc.createElement('manifest-server')
203 e.setAttribute('url', self._manifest_server)
204 root.appendChild(e)
205 root.appendChild(doc.createTextNode(''))
206
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800207 def output_projects(parent, parent_node, projects):
208 for p in projects:
209 output_project(parent, parent_node, self.projects[p])
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800210
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800211 def output_project(parent, parent_node, p):
Colin Cross5acde752012-03-28 20:15:45 -0700212 if not p.MatchesGroups(groups):
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800213 return
214
215 name = p.name
216 relpath = p.relpath
217 if parent:
218 name = self._UnjoinName(parent.name, name)
219 relpath = self._UnjoinRelpath(parent.relpath, relpath)
Colin Cross5acde752012-03-28 20:15:45 -0700220
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800221 e = doc.createElement('project')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800222 parent_node.appendChild(e)
223 e.setAttribute('name', name)
224 if relpath != name:
225 e.setAttribute('path', relpath)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800226 if not d.remote or p.remote.name != d.remote.name:
227 e.setAttribute('remote', p.remote.name)
228 if peg_rev:
229 if self.IsMirror:
Brian Harring14a66742012-09-28 20:21:57 -0700230 value = p.bare_git.rev_parse(p.revisionExpr + '^0')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800231 else:
Brian Harring14a66742012-09-28 20:21:57 -0700232 value = p.work_git.rev_parse(HEAD + '^0')
233 e.setAttribute('revision', value)
234 if peg_rev_upstream and value != p.revisionExpr:
235 # Only save the origin if the origin is not a sha1, and the default
236 # isn't our value, and the if the default doesn't already have that
237 # covered.
238 e.setAttribute('upstream', p.revisionExpr)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700239 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
240 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800241
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800242 for c in p.copyfiles:
243 ce = doc.createElement('copyfile')
244 ce.setAttribute('src', c.src)
245 ce.setAttribute('dest', c.dest)
246 e.appendChild(ce)
247
Conley Owensbb1b5f52012-08-13 13:11:18 -0700248 default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath]
Dmitry Fink17f85ea2012-08-06 14:52:29 -0700249 egroups = [g for g in p.groups if g not in default_groups]
Conley Owens971de8e2012-04-16 10:36:08 -0700250 if egroups:
251 e.setAttribute('groups', ','.join(egroups))
Colin Cross5acde752012-03-28 20:15:45 -0700252
James W. Mills24c13082012-04-12 15:04:13 -0500253 for a in p.annotations:
254 if a.keep == "true":
255 ae = doc.createElement('annotation')
256 ae.setAttribute('name', a.name)
257 ae.setAttribute('value', a.value)
258 e.appendChild(ae)
259
Anatol Pomazau79770d22012-04-20 14:41:59 -0700260 if p.sync_c:
261 e.setAttribute('sync-c', 'true')
262
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800263 if p.sync_s:
264 e.setAttribute('sync-s', 'true')
265
266 if p.subprojects:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530267 sort_projects = list(sorted([subp.name for subp in p.subprojects]))
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800268 output_projects(p, e, sort_projects)
269
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530270 sort_projects = list(sorted([key for key, value in self.projects.items()
271 if not value.parent]))
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800272 sort_projects.sort()
273 output_projects(None, root, sort_projects)
274
Doug Anderson37282b42011-03-04 11:54:18 -0800275 if self._repo_hooks_project:
276 root.appendChild(doc.createTextNode(''))
277 e = doc.createElement('repo-hooks')
278 e.setAttribute('in-project', self._repo_hooks_project.name)
279 e.setAttribute('enabled-list',
280 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
281 root.appendChild(e)
282
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800283 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
284
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700285 @property
286 def projects(self):
287 self._Load()
288 return self._projects
289
290 @property
291 def remotes(self):
292 self._Load()
293 return self._remotes
294
295 @property
296 def default(self):
297 self._Load()
298 return self._default
299
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800300 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800301 def repo_hooks_project(self):
302 self._Load()
303 return self._repo_hooks_project
304
305 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700306 def notice(self):
307 self._Load()
308 return self._notice
309
310 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700311 def manifest_server(self):
312 self._Load()
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800313 return self._manifest_server
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700314
315 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800316 def IsMirror(self):
317 return self.manifestProject.config.GetBoolean('repo.mirror')
318
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700319 def _Unload(self):
320 self._loaded = False
321 self._projects = {}
322 self._remotes = {}
323 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800324 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700325 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700326 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700327 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700328
329 def _Load(self):
330 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800331 m = self.manifestProject
332 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700333 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800334 b = b[len(R_HEADS):]
335 self.branch = b
336
Colin Cross23acdd32012-04-21 00:33:54 -0700337 nodes = []
Brian Harring475a47d2012-06-07 20:05:35 -0700338 nodes.append(self._ParseManifestXml(self.manifestFile,
339 self.manifestProject.worktree))
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700340
341 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
342 if os.path.exists(local):
David Pursehouse4eb285c2013-02-14 16:28:44 +0900343 if not self.localManifestWarning:
344 self.localManifestWarning = True
345 print('warning: %s is deprecated; put local manifests in `%s` instead'
346 % (LOCAL_MANIFEST_NAME, os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME)),
347 file=sys.stderr)
Brian Harring475a47d2012-06-07 20:05:35 -0700348 nodes.append(self._ParseManifestXml(local, self.repodir))
Colin Cross23acdd32012-04-21 00:33:54 -0700349
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900350 local_dir = os.path.abspath(os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME))
351 try:
David Pursehouse52f1e5d2012-11-14 04:53:24 +0900352 for local_file in sorted(os.listdir(local_dir)):
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900353 if local_file.endswith('.xml'):
David Pursehouse5f434ed2012-11-22 13:48:10 +0900354 local = os.path.join(local_dir, local_file)
355 nodes.append(self._ParseManifestXml(local, self.repodir))
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900356 except OSError:
357 pass
358
Joe Onorato26e24752013-01-11 12:35:53 -0800359 try:
360 self._ParseManifest(nodes)
361 except ManifestParseError as e:
362 # There was a problem parsing, unload ourselves in case they catch
363 # this error and try again later, we will show the correct error
364 self._Unload()
365 raise e
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700366
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800367 if self.IsMirror:
368 self._AddMetaProjectMirror(self.repoProject)
369 self._AddMetaProjectMirror(self.manifestProject)
370
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700371 self._loaded = True
372
Brian Harring475a47d2012-06-07 20:05:35 -0700373 def _ParseManifestXml(self, path, include_root):
David Pursehousef7fc8a92012-11-13 04:00:28 +0900374 try:
375 root = xml.dom.minidom.parse(path)
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900376 except (OSError, xml.parsers.expat.ExpatError) as e:
David Pursehousef7fc8a92012-11-13 04:00:28 +0900377 raise ManifestParseError("error parsing manifest %s: %s" % (path, e))
378
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700379 if not root or not root.childNodes:
Brian Harring26448742011-04-28 05:04:41 -0700380 raise ManifestParseError("no root node in %s" % (path,))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700381
Jooncheol Park34acdd22012-08-27 02:25:59 +0900382 for manifest in root.childNodes:
383 if manifest.nodeName == 'manifest':
384 break
385 else:
Brian Harring26448742011-04-28 05:04:41 -0700386 raise ManifestParseError("no <manifest> in %s" % (path,))
387
Colin Cross23acdd32012-04-21 00:33:54 -0700388 nodes = []
David Pursehouse4f7bdea2012-10-22 12:50:15 +0900389 for node in manifest.childNodes: # pylint:disable=W0631
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900390 # We only get here if manifest is initialised
David Pursehousec1b86a22012-11-14 11:36:51 +0900391 if node.nodeName == 'include':
392 name = self._reqatt(node, 'name')
393 fp = os.path.join(include_root, name)
394 if not os.path.isfile(fp):
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530395 raise ManifestParseError("include %s doesn't exist or isn't a file"
396 % (name,))
David Pursehousec1b86a22012-11-14 11:36:51 +0900397 try:
398 nodes.extend(self._ParseManifestXml(fp, include_root))
399 # should isolate this to the exact exception, but that's
400 # tricky. actual parsing implementation may vary.
401 except (KeyboardInterrupt, RuntimeError, SystemExit):
402 raise
403 except Exception as e:
404 raise ManifestParseError(
405 "failed parsing included manifest %s: %s", (name, e))
406 else:
407 nodes.append(node)
Colin Cross23acdd32012-04-21 00:33:54 -0700408 return nodes
Brian Harring26448742011-04-28 05:04:41 -0700409
Colin Cross23acdd32012-04-21 00:33:54 -0700410 def _ParseManifest(self, node_list):
411 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700412 if node.nodeName == 'remote':
413 remote = self._ParseRemote(node)
David Pursehouse717ece92012-11-13 08:49:16 +0900414 if remote:
415 if remote.name in self._remotes:
416 if remote != self._remotes[remote.name]:
417 raise ManifestParseError(
418 'remote %s already exists with different attributes' %
419 (remote.name))
420 else:
421 self._remotes[remote.name] = remote
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700422
Colin Cross23acdd32012-04-21 00:33:54 -0700423 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700424 if node.nodeName == 'default':
425 if self._default is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800426 raise ManifestParseError(
427 'duplicate default in %s' %
428 (self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700429 self._default = self._ParseDefault(node)
430 if self._default is None:
431 self._default = _Default()
432
Colin Cross23acdd32012-04-21 00:33:54 -0700433 for node in itertools.chain(*node_list):
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700434 if node.nodeName == 'notice':
435 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800436 raise ManifestParseError(
437 'duplicate notice in %s' %
438 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700439 self._notice = self._ParseNotice(node)
440
Colin Cross23acdd32012-04-21 00:33:54 -0700441 for node in itertools.chain(*node_list):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700442 if node.nodeName == 'manifest-server':
443 url = self._reqatt(node, 'url')
444 if self._manifest_server is not None:
David Pursehousec1b86a22012-11-14 11:36:51 +0900445 raise ManifestParseError(
446 'duplicate manifest-server in %s' %
447 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700448 self._manifest_server = url
449
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800450 def recursively_add_projects(project):
451 if self._projects.get(project.name):
452 raise ManifestParseError(
453 'duplicate project %s in %s' %
454 (project.name, self.manifestFile))
455 self._projects[project.name] = project
456 for subproject in project.subprojects:
457 recursively_add_projects(subproject)
458
Colin Cross23acdd32012-04-21 00:33:54 -0700459 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700460 if node.nodeName == 'project':
461 project = self._ParseProject(node)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800462 recursively_add_projects(project)
Doug Anderson37282b42011-03-04 11:54:18 -0800463 if node.nodeName == 'repo-hooks':
464 # Get the name of the project and the (space-separated) list of enabled.
465 repo_hooks_project = self._reqatt(node, 'in-project')
466 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
467
468 # Only one project can be the hooks project
469 if self._repo_hooks_project is not None:
470 raise ManifestParseError(
471 'duplicate repo-hooks in %s' %
472 (self.manifestFile))
473
474 # Store a reference to the Project.
475 try:
476 self._repo_hooks_project = self._projects[repo_hooks_project]
477 except KeyError:
478 raise ManifestParseError(
479 'project %s not found for repo-hooks' %
480 (repo_hooks_project))
481
482 # Store the enabled hooks in the Project object.
483 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
Colin Cross23acdd32012-04-21 00:33:54 -0700484 if node.nodeName == 'remove-project':
485 name = self._reqatt(node, 'name')
486 try:
487 del self._projects[name]
488 except KeyError:
David Pursehousef9107482012-11-16 19:12:32 +0900489 raise ManifestParseError('remove-project element specifies non-existent '
490 'project: %s' % name)
Colin Cross23acdd32012-04-21 00:33:54 -0700491
492 # If the manifest removes the hooks project, treat it as if it deleted
493 # the repo-hooks element too.
494 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
495 self._repo_hooks_project = None
496
Doug Anderson37282b42011-03-04 11:54:18 -0800497
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800498 def _AddMetaProjectMirror(self, m):
499 name = None
500 m_url = m.GetRemote(m.remote.name).url
501 if m_url.endswith('/.git'):
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530502 raise ManifestParseError('refusing to mirror %s' % m_url)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800503
504 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700505 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800506 if not url.endswith('/'):
507 url += '/'
508 if m_url.startswith(url):
509 remote = self._default.remote
510 name = m_url[len(url):]
511
512 if name is None:
513 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700514 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Shawn O. Pearcef35b2d92012-08-02 11:46:22 -0700515 remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800516 name = m_url[s:]
517
518 if name.endswith('.git'):
519 name = name[:-4]
520
521 if name not in self._projects:
522 m.PreSync()
523 gitdir = os.path.join(self.topdir, '%s.git' % name)
524 project = Project(manifest = self,
525 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700526 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800527 gitdir = gitdir,
528 worktree = None,
529 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700530 revisionExpr = m.revisionExpr,
531 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800532 self._projects[project.name] = project
533
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700534 def _ParseRemote(self, node):
535 """
536 reads a <remote> element from the manifest file
537 """
538 name = self._reqatt(node, 'name')
Yestin Sunb292b982012-07-02 07:32:50 -0700539 alias = node.getAttribute('alias')
540 if alias == '':
541 alias = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700542 fetch = self._reqatt(node, 'fetch')
543 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800544 if review == '':
545 review = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700546 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Yestin Sunb292b982012-07-02 07:32:50 -0700547 return _XmlRemote(name, alias, fetch, manifestUrl, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700548
549 def _ParseDefault(self, node):
550 """
551 reads a <default> element from the manifest file
552 """
553 d = _Default()
554 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700555 d.revisionExpr = node.getAttribute('revision')
556 if d.revisionExpr == '':
557 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700558
Bryan Jacobsf609f912013-05-06 13:36:24 -0400559 d.destBranchExpr = node.getAttribute('dest-branch') or None
560
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700561 sync_j = node.getAttribute('sync-j')
562 if sync_j == '' or sync_j is None:
563 d.sync_j = 1
564 else:
565 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700566
567 sync_c = node.getAttribute('sync-c')
568 if not sync_c:
569 d.sync_c = False
570 else:
571 d.sync_c = sync_c.lower() in ("yes", "true", "1")
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800572
573 sync_s = node.getAttribute('sync-s')
574 if not sync_s:
575 d.sync_s = False
576 else:
577 d.sync_s = sync_s.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700578 return d
579
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700580 def _ParseNotice(self, node):
581 """
582 reads a <notice> element from the manifest file
583
584 The <notice> element is distinct from other tags in the XML in that the
585 data is conveyed between the start and end tag (it's not an empty-element
586 tag).
587
588 The white space (carriage returns, indentation) for the notice element is
589 relevant and is parsed in a way that is based on how python docstrings work.
590 In fact, the code is remarkably similar to here:
591 http://www.python.org/dev/peps/pep-0257/
592 """
593 # Get the data out of the node...
594 notice = node.childNodes[0].data
595
596 # Figure out minimum indentation, skipping the first line (the same line
597 # as the <notice> tag)...
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530598 minIndent = sys.maxsize
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700599 lines = notice.splitlines()
600 for line in lines[1:]:
601 lstrippedLine = line.lstrip()
602 if lstrippedLine:
603 indent = len(line) - len(lstrippedLine)
604 minIndent = min(indent, minIndent)
605
606 # Strip leading / trailing blank lines and also indentation.
607 cleanLines = [lines[0].strip()]
608 for line in lines[1:]:
609 cleanLines.append(line[minIndent:].rstrip())
610
611 # Clear completely blank lines from front and back...
612 while cleanLines and not cleanLines[0]:
613 del cleanLines[0]
614 while cleanLines and not cleanLines[-1]:
615 del cleanLines[-1]
616
617 return '\n'.join(cleanLines)
618
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800619 def _JoinName(self, parent_name, name):
620 return os.path.join(parent_name, name)
621
622 def _UnjoinName(self, parent_name, name):
623 return os.path.relpath(name, parent_name)
624
625 def _ParseProject(self, node, parent = None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700626 """
627 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700628 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700629 name = self._reqatt(node, 'name')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800630 if parent:
631 name = self._JoinName(parent.name, name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700632
633 remote = self._get_remote(node)
634 if remote is None:
635 remote = self._default.remote
636 if remote is None:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530637 raise ManifestParseError("no remote for project %s within %s" %
638 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700639
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700640 revisionExpr = node.getAttribute('revision')
641 if not revisionExpr:
642 revisionExpr = self._default.revisionExpr
643 if not revisionExpr:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530644 raise ManifestParseError("no revision for project %s within %s" %
645 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700646
647 path = node.getAttribute('path')
648 if not path:
649 path = name
650 if path.startswith('/'):
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530651 raise ManifestParseError("project %s path cannot be absolute in %s" %
652 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700653
Mike Pontillod3153822012-02-28 11:53:24 -0800654 rebase = node.getAttribute('rebase')
655 if not rebase:
656 rebase = True
657 else:
658 rebase = rebase.lower() in ("yes", "true", "1")
659
Anatol Pomazau79770d22012-04-20 14:41:59 -0700660 sync_c = node.getAttribute('sync-c')
661 if not sync_c:
662 sync_c = False
663 else:
664 sync_c = sync_c.lower() in ("yes", "true", "1")
665
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800666 sync_s = node.getAttribute('sync-s')
667 if not sync_s:
668 sync_s = self._default.sync_s
669 else:
670 sync_s = sync_s.lower() in ("yes", "true", "1")
671
David Pursehouseede7f122012-11-27 22:25:30 +0900672 clone_depth = node.getAttribute('clone-depth')
673 if clone_depth:
674 try:
675 clone_depth = int(clone_depth)
676 if clone_depth <= 0:
677 raise ValueError()
678 except ValueError:
679 raise ManifestParseError('invalid clone-depth %s in %s' %
680 (clone_depth, self.manifestFile))
681
Bryan Jacobsf609f912013-05-06 13:36:24 -0400682 dest_branch = node.getAttribute('dest-branch') or self._default.destBranchExpr
683
Brian Harring14a66742012-09-28 20:21:57 -0700684 upstream = node.getAttribute('upstream')
685
Conley Owens971de8e2012-04-16 10:36:08 -0700686 groups = ''
687 if node.hasAttribute('groups'):
688 groups = node.getAttribute('groups')
David Pursehouse1d947b32012-10-25 12:23:11 +0900689 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Brian Harring7da13142012-06-15 02:24:20 -0700690
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800691 if parent is None:
692 relpath, worktree, gitdir = self.GetProjectPaths(name, path)
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700693 else:
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800694 relpath, worktree, gitdir = self.GetSubprojectPaths(parent, path)
695
696 default_groups = ['all', 'name:%s' % name, 'path:%s' % relpath]
697 groups.extend(set(default_groups).difference(groups))
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700698
Scott Fandb83b1b2013-02-28 09:34:14 +0800699 if self.IsMirror and node.hasAttribute('force-path'):
700 if node.getAttribute('force-path').lower() in ("yes", "true", "1"):
701 gitdir = os.path.join(self.topdir, '%s.git' % path)
702
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700703 project = Project(manifest = self,
704 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700705 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700706 gitdir = gitdir,
707 worktree = worktree,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800708 relpath = relpath,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700709 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800710 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700711 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700712 groups = groups,
Brian Harring14a66742012-09-28 20:21:57 -0700713 sync_c = sync_c,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800714 sync_s = sync_s,
David Pursehouseede7f122012-11-27 22:25:30 +0900715 clone_depth = clone_depth,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800716 upstream = upstream,
Bryan Jacobsf609f912013-05-06 13:36:24 -0400717 parent = parent,
718 dest_branch = dest_branch)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700719
720 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700721 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700722 self._ParseCopyFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500723 if n.nodeName == 'annotation':
724 self._ParseAnnotation(project, n)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800725 if n.nodeName == 'project':
726 project.subprojects.append(self._ParseProject(n, parent = project))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700727
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700728 return project
729
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800730 def GetProjectPaths(self, name, path):
731 relpath = path
732 if self.IsMirror:
733 worktree = None
734 gitdir = os.path.join(self.topdir, '%s.git' % name)
735 else:
736 worktree = os.path.join(self.topdir, path).replace('\\', '/')
737 gitdir = os.path.join(self.repodir, 'projects', '%s.git' % path)
738 return relpath, worktree, gitdir
739
740 def GetSubprojectName(self, parent, submodule_path):
741 return os.path.join(parent.name, submodule_path)
742
743 def _JoinRelpath(self, parent_relpath, relpath):
744 return os.path.join(parent_relpath, relpath)
745
746 def _UnjoinRelpath(self, parent_relpath, relpath):
747 return os.path.relpath(relpath, parent_relpath)
748
749 def GetSubprojectPaths(self, parent, path):
750 relpath = self._JoinRelpath(parent.relpath, path)
751 gitdir = os.path.join(parent.gitdir, 'subprojects', '%s.git' % path)
752 if self.IsMirror:
753 worktree = None
754 else:
755 worktree = os.path.join(parent.worktree, path).replace('\\', '/')
756 return relpath, worktree, gitdir
757
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700758 def _ParseCopyFile(self, project, node):
759 src = self._reqatt(node, 'src')
760 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800761 if not self.IsMirror:
762 # src is project relative;
763 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800764 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700765
James W. Mills24c13082012-04-12 15:04:13 -0500766 def _ParseAnnotation(self, project, node):
767 name = self._reqatt(node, 'name')
768 value = self._reqatt(node, 'value')
769 try:
770 keep = self._reqatt(node, 'keep').lower()
771 except ManifestParseError:
772 keep = "true"
773 if keep != "true" and keep != "false":
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530774 raise ManifestParseError('optional "keep" attribute must be '
775 '"true" or "false"')
James W. Mills24c13082012-04-12 15:04:13 -0500776 project.AddAnnotation(name, value, keep)
777
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700778 def _get_remote(self, node):
779 name = node.getAttribute('remote')
780 if not name:
781 return None
782
783 v = self._remotes.get(name)
784 if not v:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530785 raise ManifestParseError("remote %s not defined in %s" %
786 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700787 return v
788
789 def _reqatt(self, node, attname):
790 """
791 reads a required attribute from the node.
792 """
793 v = node.getAttribute(attname)
794 if not v:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530795 raise ManifestParseError("no %s in <%s> within %s" %
796 (attname, node.nodeName, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700797 return v