blob: 50417c60883677db5e54ccdb6eb05aace47bef7a [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
Conley Owensdb728cd2011-09-26 16:34:01 -070021import urlparse
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070022import xml.dom.minidom
23
David Pursehousee15c65a2012-08-22 10:46:11 +090024from git_config import GitConfig
David Pursehousee00aa6b2012-09-11 14:33:51 +090025from git_refs import R_HEADS, HEAD
26from project import RemoteSpec, Project, MetaProject
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070027from error import ManifestParseError
28
29MANIFEST_FILE_NAME = 'manifest.xml'
Shawn O. Pearce5cc66792008-10-23 16:19:27 -070030LOCAL_MANIFEST_NAME = 'local_manifest.xml'
David Pursehouse2d5a0df2012-11-13 02:50:36 +090031LOCAL_MANIFESTS_DIR_NAME = 'local_manifests'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070032
Conley Owensdb728cd2011-09-26 16:34:01 -070033urlparse.uses_relative.extend(['ssh', 'git'])
34urlparse.uses_netloc.extend(['ssh', 'git'])
35
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070036class _Default(object):
37 """Project defaults within the manifest."""
38
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -070039 revisionExpr = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070040 remote = None
Shawn O. Pearce6392c872011-09-22 17:44:31 -070041 sync_j = 1
Anatol Pomazau79770d22012-04-20 14:41:59 -070042 sync_c = False
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +080043 sync_s = False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070044
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070045class _XmlRemote(object):
46 def __init__(self,
47 name,
Yestin Sunb292b982012-07-02 07:32:50 -070048 alias=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070049 fetch=None,
Conley Owensdb728cd2011-09-26 16:34:01 -070050 manifestUrl=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070051 review=None):
52 self.name = name
53 self.fetchUrl = fetch
Conley Owensdb728cd2011-09-26 16:34:01 -070054 self.manifestUrl = manifestUrl
Yestin Sunb292b982012-07-02 07:32:50 -070055 self.remoteAlias = alias
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070056 self.reviewUrl = review
Conley Owensceea3682011-10-20 10:45:47 -070057 self.resolvedFetchUrl = self._resolveFetchUrl()
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070058
David Pursehouse717ece92012-11-13 08:49:16 +090059 def __eq__(self, other):
60 return self.__dict__ == other.__dict__
61
62 def __ne__(self, other):
63 return self.__dict__ != other.__dict__
64
Conley Owensceea3682011-10-20 10:45:47 -070065 def _resolveFetchUrl(self):
66 url = self.fetchUrl.rstrip('/')
Conley Owensdb728cd2011-09-26 16:34:01 -070067 manifestUrl = self.manifestUrl.rstrip('/')
Shawn Pearcea9f11b32013-01-02 15:40:48 -080068 p = manifestUrl.startswith('persistent-http')
69 if p:
70 manifestUrl = manifestUrl[len('persistent-'):]
71
Conley Owensdb728cd2011-09-26 16:34:01 -070072 # 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 Pursehousec1b86a22012-11-14 11:36:51 +090075 manifestUrl = 'gopher://' + manifestUrl
Conley Owensdb728cd2011-09-26 16:34:01 -070076 url = urlparse.urljoin(manifestUrl, url)
Shawn Pearcea9f11b32013-01-02 15:40:48 -080077 url = re.sub(r'^gopher://', '', url)
78 if p:
79 url = 'persistent-' + url
80 return url
Conley Owensceea3682011-10-20 10:45:47 -070081
82 def ToRemoteSpec(self, projectName):
Conley Owens9d8f9142011-10-20 14:36:35 -070083 url = self.resolvedFetchUrl.rstrip('/') + '/' + projectName
Yestin Sunb292b982012-07-02 07:32:50 -070084 remoteName = self.name
85 if self.remoteAlias:
86 remoteName = self.remoteAlias
87 return RemoteSpec(remoteName, url, self.reviewUrl)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070088
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070089class XmlManifest(object):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070090 """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 Projectcf31fe92008-10-21 07:00:00 -070096 self.globalConfig = GitConfig.ForUser()
David Pursehouse4eb285c2013-02-14 16:28:44 +090097 self.localManifestWarning = False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070098
99 self.repoProject = MetaProject(self, 'repo',
100 gitdir = os.path.join(repodir, 'repo/.git'),
101 worktree = os.path.join(repodir, 'repo'))
102
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700103 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -0800104 gitdir = os.path.join(repodir, 'manifests.git'),
105 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700106
107 self._Unload()
108
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700109 def Override(self, name):
110 """Use a different manifest, just for the current instantiation.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700111 """
112 path = os.path.join(self.manifestProject.worktree, name)
113 if not os.path.isfile(path):
114 raise ManifestParseError('manifest %s not found' % name)
115
116 old = self.manifestFile
117 try:
118 self.manifestFile = path
119 self._Unload()
120 self._Load()
121 finally:
122 self.manifestFile = old
123
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700124 def Link(self, name):
125 """Update the repo metadata to use a different manifest.
126 """
127 self.Override(name)
128
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700129 try:
Sebastian Frias223bf962012-11-21 19:09:25 +0100130 if os.path.lexists(self.manifestFile):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700131 os.remove(self.manifestFile)
132 os.symlink('manifests/%s' % name, self.manifestFile)
Sebastian Frias223bf962012-11-21 19:09:25 +0100133 except OSError as e:
134 raise ManifestParseError('cannot link manifest %s: %s' % (name, str(e)))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700135
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800136 def _RemoteToXml(self, r, doc, root):
137 e = doc.createElement('remote')
138 root.appendChild(e)
139 e.setAttribute('name', r.name)
140 e.setAttribute('fetch', r.fetchUrl)
141 if r.reviewUrl is not None:
142 e.setAttribute('review', r.reviewUrl)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800143
Brian Harring14a66742012-09-28 20:21:57 -0700144 def Save(self, fd, peg_rev=False, peg_rev_upstream=True):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800145 """Write the current manifest out to the given file descriptor.
146 """
Colin Cross5acde752012-03-28 20:15:45 -0700147 mp = self.manifestProject
148
149 groups = mp.config.GetString('manifest.groups')
Matt Gumbel0c635bb2012-12-21 10:14:53 -0800150 if groups:
151 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Colin Cross5acde752012-03-28 20:15:45 -0700152
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800153 doc = xml.dom.minidom.Document()
154 root = doc.createElement('manifest')
155 doc.appendChild(root)
156
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700157 # Save out the notice. There's a little bit of work here to give it the
158 # right whitespace, which assumes that the notice is automatically indented
159 # by 4 by minidom.
160 if self.notice:
161 notice_element = root.appendChild(doc.createElement('notice'))
162 notice_lines = self.notice.splitlines()
163 indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:]
164 notice_element.appendChild(doc.createTextNode(indented_notice))
165
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800166 d = self.default
167 sort_remotes = list(self.remotes.keys())
168 sort_remotes.sort()
169
170 for r in sort_remotes:
171 self._RemoteToXml(self.remotes[r], doc, root)
172 if self.remotes:
173 root.appendChild(doc.createTextNode(''))
174
175 have_default = False
176 e = doc.createElement('default')
177 if d.remote:
178 have_default = True
179 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700180 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800181 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700182 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700183 if d.sync_j > 1:
184 have_default = True
185 e.setAttribute('sync-j', '%d' % d.sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700186 if d.sync_c:
187 have_default = True
188 e.setAttribute('sync-c', 'true')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800189 if d.sync_s:
190 have_default = True
191 e.setAttribute('sync-s', 'true')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800192 if have_default:
193 root.appendChild(e)
194 root.appendChild(doc.createTextNode(''))
195
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700196 if self._manifest_server:
197 e = doc.createElement('manifest-server')
198 e.setAttribute('url', self._manifest_server)
199 root.appendChild(e)
200 root.appendChild(doc.createTextNode(''))
201
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800202 def output_projects(parent, parent_node, projects):
203 for p in projects:
204 output_project(parent, parent_node, self.projects[p])
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800205
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800206 def output_project(parent, parent_node, p):
Colin Cross5acde752012-03-28 20:15:45 -0700207 if not p.MatchesGroups(groups):
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800208 return
209
210 name = p.name
211 relpath = p.relpath
212 if parent:
213 name = self._UnjoinName(parent.name, name)
214 relpath = self._UnjoinRelpath(parent.relpath, relpath)
Colin Cross5acde752012-03-28 20:15:45 -0700215
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800216 e = doc.createElement('project')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800217 parent_node.appendChild(e)
218 e.setAttribute('name', name)
219 if relpath != name:
220 e.setAttribute('path', relpath)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800221 if not d.remote or p.remote.name != d.remote.name:
222 e.setAttribute('remote', p.remote.name)
223 if peg_rev:
224 if self.IsMirror:
Brian Harring14a66742012-09-28 20:21:57 -0700225 value = p.bare_git.rev_parse(p.revisionExpr + '^0')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800226 else:
Brian Harring14a66742012-09-28 20:21:57 -0700227 value = p.work_git.rev_parse(HEAD + '^0')
228 e.setAttribute('revision', value)
229 if peg_rev_upstream and value != p.revisionExpr:
230 # Only save the origin if the origin is not a sha1, and the default
231 # isn't our value, and the if the default doesn't already have that
232 # covered.
233 e.setAttribute('upstream', p.revisionExpr)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700234 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
235 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800236
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800237 for c in p.copyfiles:
238 ce = doc.createElement('copyfile')
239 ce.setAttribute('src', c.src)
240 ce.setAttribute('dest', c.dest)
241 e.appendChild(ce)
242
Conley Owensbb1b5f52012-08-13 13:11:18 -0700243 default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath]
Dmitry Fink17f85ea2012-08-06 14:52:29 -0700244 egroups = [g for g in p.groups if g not in default_groups]
Conley Owens971de8e2012-04-16 10:36:08 -0700245 if egroups:
246 e.setAttribute('groups', ','.join(egroups))
Colin Cross5acde752012-03-28 20:15:45 -0700247
James W. Mills24c13082012-04-12 15:04:13 -0500248 for a in p.annotations:
249 if a.keep == "true":
250 ae = doc.createElement('annotation')
251 ae.setAttribute('name', a.name)
252 ae.setAttribute('value', a.value)
253 e.appendChild(ae)
254
Anatol Pomazau79770d22012-04-20 14:41:59 -0700255 if p.sync_c:
256 e.setAttribute('sync-c', 'true')
257
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800258 if p.sync_s:
259 e.setAttribute('sync-s', 'true')
260
261 if p.subprojects:
262 sort_projects = [subp.name for subp in p.subprojects]
263 sort_projects.sort()
264 output_projects(p, e, sort_projects)
265
266 sort_projects = [key for key in self.projects.keys()
267 if not self.projects[key].parent]
268 sort_projects.sort()
269 output_projects(None, root, sort_projects)
270
Doug Anderson37282b42011-03-04 11:54:18 -0800271 if self._repo_hooks_project:
272 root.appendChild(doc.createTextNode(''))
273 e = doc.createElement('repo-hooks')
274 e.setAttribute('in-project', self._repo_hooks_project.name)
275 e.setAttribute('enabled-list',
276 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
277 root.appendChild(e)
278
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800279 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
280
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700281 @property
282 def projects(self):
283 self._Load()
284 return self._projects
285
286 @property
287 def remotes(self):
288 self._Load()
289 return self._remotes
290
291 @property
292 def default(self):
293 self._Load()
294 return self._default
295
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800296 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800297 def repo_hooks_project(self):
298 self._Load()
299 return self._repo_hooks_project
300
301 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700302 def notice(self):
303 self._Load()
304 return self._notice
305
306 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700307 def manifest_server(self):
308 self._Load()
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800309 return self._manifest_server
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700310
311 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800312 def IsMirror(self):
313 return self.manifestProject.config.GetBoolean('repo.mirror')
314
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700315 def _Unload(self):
316 self._loaded = False
317 self._projects = {}
318 self._remotes = {}
319 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800320 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700321 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700322 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700323 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700324
325 def _Load(self):
326 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800327 m = self.manifestProject
328 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700329 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800330 b = b[len(R_HEADS):]
331 self.branch = b
332
Colin Cross23acdd32012-04-21 00:33:54 -0700333 nodes = []
Brian Harring475a47d2012-06-07 20:05:35 -0700334 nodes.append(self._ParseManifestXml(self.manifestFile,
335 self.manifestProject.worktree))
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700336
337 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
338 if os.path.exists(local):
David Pursehouse4eb285c2013-02-14 16:28:44 +0900339 if not self.localManifestWarning:
340 self.localManifestWarning = True
341 print('warning: %s is deprecated; put local manifests in `%s` instead'
342 % (LOCAL_MANIFEST_NAME, os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME)),
343 file=sys.stderr)
Brian Harring475a47d2012-06-07 20:05:35 -0700344 nodes.append(self._ParseManifestXml(local, self.repodir))
Colin Cross23acdd32012-04-21 00:33:54 -0700345
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900346 local_dir = os.path.abspath(os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME))
347 try:
David Pursehouse52f1e5d2012-11-14 04:53:24 +0900348 for local_file in sorted(os.listdir(local_dir)):
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900349 if local_file.endswith('.xml'):
David Pursehouse5f434ed2012-11-22 13:48:10 +0900350 local = os.path.join(local_dir, local_file)
351 nodes.append(self._ParseManifestXml(local, self.repodir))
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900352 except OSError:
353 pass
354
Joe Onorato26e24752013-01-11 12:35:53 -0800355 try:
356 self._ParseManifest(nodes)
357 except ManifestParseError as e:
358 # There was a problem parsing, unload ourselves in case they catch
359 # this error and try again later, we will show the correct error
360 self._Unload()
361 raise e
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700362
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800363 if self.IsMirror:
364 self._AddMetaProjectMirror(self.repoProject)
365 self._AddMetaProjectMirror(self.manifestProject)
366
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700367 self._loaded = True
368
Brian Harring475a47d2012-06-07 20:05:35 -0700369 def _ParseManifestXml(self, path, include_root):
David Pursehousef7fc8a92012-11-13 04:00:28 +0900370 try:
371 root = xml.dom.minidom.parse(path)
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900372 except (OSError, xml.parsers.expat.ExpatError) as e:
David Pursehousef7fc8a92012-11-13 04:00:28 +0900373 raise ManifestParseError("error parsing manifest %s: %s" % (path, e))
374
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700375 if not root or not root.childNodes:
Brian Harring26448742011-04-28 05:04:41 -0700376 raise ManifestParseError("no root node in %s" % (path,))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700377
Jooncheol Park34acdd22012-08-27 02:25:59 +0900378 for manifest in root.childNodes:
379 if manifest.nodeName == 'manifest':
380 break
381 else:
Brian Harring26448742011-04-28 05:04:41 -0700382 raise ManifestParseError("no <manifest> in %s" % (path,))
383
Colin Cross23acdd32012-04-21 00:33:54 -0700384 nodes = []
David Pursehouse4f7bdea2012-10-22 12:50:15 +0900385 for node in manifest.childNodes: # pylint:disable=W0631
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900386 # We only get here if manifest is initialised
David Pursehousec1b86a22012-11-14 11:36:51 +0900387 if node.nodeName == 'include':
388 name = self._reqatt(node, 'name')
389 fp = os.path.join(include_root, name)
390 if not os.path.isfile(fp):
391 raise ManifestParseError, \
392 "include %s doesn't exist or isn't a file" % \
393 (name,)
394 try:
395 nodes.extend(self._ParseManifestXml(fp, include_root))
396 # should isolate this to the exact exception, but that's
397 # tricky. actual parsing implementation may vary.
398 except (KeyboardInterrupt, RuntimeError, SystemExit):
399 raise
400 except Exception as e:
401 raise ManifestParseError(
402 "failed parsing included manifest %s: %s", (name, e))
403 else:
404 nodes.append(node)
Colin Cross23acdd32012-04-21 00:33:54 -0700405 return nodes
Brian Harring26448742011-04-28 05:04:41 -0700406
Colin Cross23acdd32012-04-21 00:33:54 -0700407 def _ParseManifest(self, node_list):
408 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700409 if node.nodeName == 'remote':
410 remote = self._ParseRemote(node)
David Pursehouse717ece92012-11-13 08:49:16 +0900411 if remote:
412 if remote.name in self._remotes:
413 if remote != self._remotes[remote.name]:
414 raise ManifestParseError(
415 'remote %s already exists with different attributes' %
416 (remote.name))
417 else:
418 self._remotes[remote.name] = remote
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700419
Colin Cross23acdd32012-04-21 00:33:54 -0700420 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700421 if node.nodeName == 'default':
422 if self._default is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800423 raise ManifestParseError(
424 'duplicate default in %s' %
425 (self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700426 self._default = self._ParseDefault(node)
427 if self._default is None:
428 self._default = _Default()
429
Colin Cross23acdd32012-04-21 00:33:54 -0700430 for node in itertools.chain(*node_list):
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700431 if node.nodeName == 'notice':
432 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800433 raise ManifestParseError(
434 'duplicate notice in %s' %
435 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700436 self._notice = self._ParseNotice(node)
437
Colin Cross23acdd32012-04-21 00:33:54 -0700438 for node in itertools.chain(*node_list):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700439 if node.nodeName == 'manifest-server':
440 url = self._reqatt(node, 'url')
441 if self._manifest_server is not None:
David Pursehousec1b86a22012-11-14 11:36:51 +0900442 raise ManifestParseError(
443 'duplicate manifest-server in %s' %
444 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700445 self._manifest_server = url
446
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800447 def recursively_add_projects(project):
448 if self._projects.get(project.name):
449 raise ManifestParseError(
450 'duplicate project %s in %s' %
451 (project.name, self.manifestFile))
452 self._projects[project.name] = project
453 for subproject in project.subprojects:
454 recursively_add_projects(subproject)
455
Colin Cross23acdd32012-04-21 00:33:54 -0700456 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700457 if node.nodeName == 'project':
458 project = self._ParseProject(node)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800459 recursively_add_projects(project)
Doug Anderson37282b42011-03-04 11:54:18 -0800460 if node.nodeName == 'repo-hooks':
461 # Get the name of the project and the (space-separated) list of enabled.
462 repo_hooks_project = self._reqatt(node, 'in-project')
463 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
464
465 # Only one project can be the hooks project
466 if self._repo_hooks_project is not None:
467 raise ManifestParseError(
468 'duplicate repo-hooks in %s' %
469 (self.manifestFile))
470
471 # Store a reference to the Project.
472 try:
473 self._repo_hooks_project = self._projects[repo_hooks_project]
474 except KeyError:
475 raise ManifestParseError(
476 'project %s not found for repo-hooks' %
477 (repo_hooks_project))
478
479 # Store the enabled hooks in the Project object.
480 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
Colin Cross23acdd32012-04-21 00:33:54 -0700481 if node.nodeName == 'remove-project':
482 name = self._reqatt(node, 'name')
483 try:
484 del self._projects[name]
485 except KeyError:
David Pursehousef9107482012-11-16 19:12:32 +0900486 raise ManifestParseError('remove-project element specifies non-existent '
487 'project: %s' % name)
Colin Cross23acdd32012-04-21 00:33:54 -0700488
489 # If the manifest removes the hooks project, treat it as if it deleted
490 # the repo-hooks element too.
491 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
492 self._repo_hooks_project = None
493
Doug Anderson37282b42011-03-04 11:54:18 -0800494
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800495 def _AddMetaProjectMirror(self, m):
496 name = None
497 m_url = m.GetRemote(m.remote.name).url
498 if m_url.endswith('/.git'):
499 raise ManifestParseError, 'refusing to mirror %s' % m_url
500
501 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700502 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800503 if not url.endswith('/'):
504 url += '/'
505 if m_url.startswith(url):
506 remote = self._default.remote
507 name = m_url[len(url):]
508
509 if name is None:
510 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700511 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Shawn O. Pearcef35b2d92012-08-02 11:46:22 -0700512 remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800513 name = m_url[s:]
514
515 if name.endswith('.git'):
516 name = name[:-4]
517
518 if name not in self._projects:
519 m.PreSync()
520 gitdir = os.path.join(self.topdir, '%s.git' % name)
521 project = Project(manifest = self,
522 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700523 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800524 gitdir = gitdir,
525 worktree = None,
526 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700527 revisionExpr = m.revisionExpr,
528 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800529 self._projects[project.name] = project
530
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700531 def _ParseRemote(self, node):
532 """
533 reads a <remote> element from the manifest file
534 """
535 name = self._reqatt(node, 'name')
Yestin Sunb292b982012-07-02 07:32:50 -0700536 alias = node.getAttribute('alias')
537 if alias == '':
538 alias = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700539 fetch = self._reqatt(node, 'fetch')
540 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800541 if review == '':
542 review = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700543 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Yestin Sunb292b982012-07-02 07:32:50 -0700544 return _XmlRemote(name, alias, fetch, manifestUrl, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700545
546 def _ParseDefault(self, node):
547 """
548 reads a <default> element from the manifest file
549 """
550 d = _Default()
551 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700552 d.revisionExpr = node.getAttribute('revision')
553 if d.revisionExpr == '':
554 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700555
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700556 sync_j = node.getAttribute('sync-j')
557 if sync_j == '' or sync_j is None:
558 d.sync_j = 1
559 else:
560 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700561
562 sync_c = node.getAttribute('sync-c')
563 if not sync_c:
564 d.sync_c = False
565 else:
566 d.sync_c = sync_c.lower() in ("yes", "true", "1")
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800567
568 sync_s = node.getAttribute('sync-s')
569 if not sync_s:
570 d.sync_s = False
571 else:
572 d.sync_s = sync_s.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700573 return d
574
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700575 def _ParseNotice(self, node):
576 """
577 reads a <notice> element from the manifest file
578
579 The <notice> element is distinct from other tags in the XML in that the
580 data is conveyed between the start and end tag (it's not an empty-element
581 tag).
582
583 The white space (carriage returns, indentation) for the notice element is
584 relevant and is parsed in a way that is based on how python docstrings work.
585 In fact, the code is remarkably similar to here:
586 http://www.python.org/dev/peps/pep-0257/
587 """
588 # Get the data out of the node...
589 notice = node.childNodes[0].data
590
591 # Figure out minimum indentation, skipping the first line (the same line
592 # as the <notice> tag)...
593 minIndent = sys.maxint
594 lines = notice.splitlines()
595 for line in lines[1:]:
596 lstrippedLine = line.lstrip()
597 if lstrippedLine:
598 indent = len(line) - len(lstrippedLine)
599 minIndent = min(indent, minIndent)
600
601 # Strip leading / trailing blank lines and also indentation.
602 cleanLines = [lines[0].strip()]
603 for line in lines[1:]:
604 cleanLines.append(line[minIndent:].rstrip())
605
606 # Clear completely blank lines from front and back...
607 while cleanLines and not cleanLines[0]:
608 del cleanLines[0]
609 while cleanLines and not cleanLines[-1]:
610 del cleanLines[-1]
611
612 return '\n'.join(cleanLines)
613
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800614 def _JoinName(self, parent_name, name):
615 return os.path.join(parent_name, name)
616
617 def _UnjoinName(self, parent_name, name):
618 return os.path.relpath(name, parent_name)
619
620 def _ParseProject(self, node, parent = None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700621 """
622 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700623 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700624 name = self._reqatt(node, 'name')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800625 if parent:
626 name = self._JoinName(parent.name, name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700627
628 remote = self._get_remote(node)
629 if remote is None:
630 remote = self._default.remote
631 if remote is None:
632 raise ManifestParseError, \
633 "no remote for project %s within %s" % \
634 (name, self.manifestFile)
635
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700636 revisionExpr = node.getAttribute('revision')
637 if not revisionExpr:
638 revisionExpr = self._default.revisionExpr
639 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700640 raise ManifestParseError, \
641 "no revision for project %s within %s" % \
642 (name, self.manifestFile)
643
644 path = node.getAttribute('path')
645 if not path:
646 path = name
647 if path.startswith('/'):
648 raise ManifestParseError, \
649 "project %s path cannot be absolute in %s" % \
650 (name, self.manifestFile)
651
Mike Pontillod3153822012-02-28 11:53:24 -0800652 rebase = node.getAttribute('rebase')
653 if not rebase:
654 rebase = True
655 else:
656 rebase = rebase.lower() in ("yes", "true", "1")
657
Anatol Pomazau79770d22012-04-20 14:41:59 -0700658 sync_c = node.getAttribute('sync-c')
659 if not sync_c:
660 sync_c = False
661 else:
662 sync_c = sync_c.lower() in ("yes", "true", "1")
663
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800664 sync_s = node.getAttribute('sync-s')
665 if not sync_s:
666 sync_s = self._default.sync_s
667 else:
668 sync_s = sync_s.lower() in ("yes", "true", "1")
669
Brian Harring14a66742012-09-28 20:21:57 -0700670 upstream = node.getAttribute('upstream')
671
Conley Owens971de8e2012-04-16 10:36:08 -0700672 groups = ''
673 if node.hasAttribute('groups'):
674 groups = node.getAttribute('groups')
David Pursehouse1d947b32012-10-25 12:23:11 +0900675 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Brian Harring7da13142012-06-15 02:24:20 -0700676
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800677 if parent is None:
678 relpath, worktree, gitdir = self.GetProjectPaths(name, path)
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700679 else:
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800680 relpath, worktree, gitdir = self.GetSubprojectPaths(parent, path)
681
682 default_groups = ['all', 'name:%s' % name, 'path:%s' % relpath]
683 groups.extend(set(default_groups).difference(groups))
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700684
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700685 project = Project(manifest = self,
686 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700687 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700688 gitdir = gitdir,
689 worktree = worktree,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800690 relpath = relpath,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700691 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800692 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700693 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700694 groups = groups,
Brian Harring14a66742012-09-28 20:21:57 -0700695 sync_c = sync_c,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800696 sync_s = sync_s,
697 upstream = upstream,
698 parent = parent)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700699
700 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700701 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700702 self._ParseCopyFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500703 if n.nodeName == 'annotation':
704 self._ParseAnnotation(project, n)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800705 if n.nodeName == 'project':
706 project.subprojects.append(self._ParseProject(n, parent = project))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700707
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700708 return project
709
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800710 def GetProjectPaths(self, name, path):
711 relpath = path
712 if self.IsMirror:
713 worktree = None
714 gitdir = os.path.join(self.topdir, '%s.git' % name)
715 else:
716 worktree = os.path.join(self.topdir, path).replace('\\', '/')
717 gitdir = os.path.join(self.repodir, 'projects', '%s.git' % path)
718 return relpath, worktree, gitdir
719
720 def GetSubprojectName(self, parent, submodule_path):
721 return os.path.join(parent.name, submodule_path)
722
723 def _JoinRelpath(self, parent_relpath, relpath):
724 return os.path.join(parent_relpath, relpath)
725
726 def _UnjoinRelpath(self, parent_relpath, relpath):
727 return os.path.relpath(relpath, parent_relpath)
728
729 def GetSubprojectPaths(self, parent, path):
730 relpath = self._JoinRelpath(parent.relpath, path)
731 gitdir = os.path.join(parent.gitdir, 'subprojects', '%s.git' % path)
732 if self.IsMirror:
733 worktree = None
734 else:
735 worktree = os.path.join(parent.worktree, path).replace('\\', '/')
736 return relpath, worktree, gitdir
737
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700738 def _ParseCopyFile(self, project, node):
739 src = self._reqatt(node, 'src')
740 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800741 if not self.IsMirror:
742 # src is project relative;
743 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800744 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700745
James W. Mills24c13082012-04-12 15:04:13 -0500746 def _ParseAnnotation(self, project, node):
747 name = self._reqatt(node, 'name')
748 value = self._reqatt(node, 'value')
749 try:
750 keep = self._reqatt(node, 'keep').lower()
751 except ManifestParseError:
752 keep = "true"
753 if keep != "true" and keep != "false":
754 raise ManifestParseError, "optional \"keep\" attribute must be \"true\" or \"false\""
755 project.AddAnnotation(name, value, keep)
756
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700757 def _get_remote(self, node):
758 name = node.getAttribute('remote')
759 if not name:
760 return None
761
762 v = self._remotes.get(name)
763 if not v:
764 raise ManifestParseError, \
765 "remote %s not defined in %s" % \
766 (name, self.manifestFile)
767 return v
768
769 def _reqatt(self, node, attname):
770 """
771 reads a required attribute from the node.
772 """
773 v = node.getAttribute(attname)
774 if not v:
775 raise ManifestParseError, \
776 "no %s in <%s> within %s" % \
777 (attname, node.nodeName, self.manifestFile)
778 return v