blob: ed521992507c2701cfbfded09aac3c1d91ec8415 [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()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070097
98 self.repoProject = MetaProject(self, 'repo',
99 gitdir = os.path.join(repodir, 'repo/.git'),
100 worktree = os.path.join(repodir, 'repo'))
101
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700102 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -0800103 gitdir = os.path.join(repodir, 'manifests.git'),
104 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700105
106 self._Unload()
107
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700108 def Override(self, name):
109 """Use a different manifest, just for the current instantiation.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700110 """
111 path = os.path.join(self.manifestProject.worktree, name)
112 if not os.path.isfile(path):
113 raise ManifestParseError('manifest %s not found' % name)
114
115 old = self.manifestFile
116 try:
117 self.manifestFile = path
118 self._Unload()
119 self._Load()
120 finally:
121 self.manifestFile = old
122
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700123 def Link(self, name):
124 """Update the repo metadata to use a different manifest.
125 """
126 self.Override(name)
127
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700128 try:
129 if os.path.exists(self.manifestFile):
130 os.remove(self.manifestFile)
131 os.symlink('manifests/%s' % name, self.manifestFile)
David Pursehouse8a68ff92012-09-24 12:15:13 +0900132 except OSError:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700133 raise ManifestParseError('cannot link manifest %s' % name)
134
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800135 def _RemoteToXml(self, r, doc, root):
136 e = doc.createElement('remote')
137 root.appendChild(e)
138 e.setAttribute('name', r.name)
139 e.setAttribute('fetch', r.fetchUrl)
140 if r.reviewUrl is not None:
141 e.setAttribute('review', r.reviewUrl)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800142
Brian Harring14a66742012-09-28 20:21:57 -0700143 def Save(self, fd, peg_rev=False, peg_rev_upstream=True):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800144 """Write the current manifest out to the given file descriptor.
145 """
Colin Cross5acde752012-03-28 20:15:45 -0700146 mp = self.manifestProject
147
148 groups = mp.config.GetString('manifest.groups')
Colin Crossc39864f2012-04-23 13:41:58 -0700149 if not groups:
Conley Owensbb1b5f52012-08-13 13:11:18 -0700150 groups = 'all'
Conley Owens971de8e2012-04-16 10:36:08 -0700151 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):
Sarah Owenscecd1d82012-11-01 22:59:27 -0700339 print('warning: %s is deprecated; put local manifests in %s instead'
340 % (LOCAL_MANIFEST_NAME, LOCAL_MANIFESTS_DIR_NAME),
341 file=sys.stderr)
Brian Harring475a47d2012-06-07 20:05:35 -0700342 nodes.append(self._ParseManifestXml(local, self.repodir))
Colin Cross23acdd32012-04-21 00:33:54 -0700343
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900344 local_dir = os.path.abspath(os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME))
345 try:
David Pursehouse52f1e5d2012-11-14 04:53:24 +0900346 for local_file in sorted(os.listdir(local_dir)):
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900347 if local_file.endswith('.xml'):
348 try:
Tobias Droste1a5c7742013-01-03 18:27:45 +0100349 local = os.path.join(local_dir, local_file)
350 nodes.append(self._ParseManifestXml(local, self.repodir))
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900351 except ManifestParseError as e:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700352 print('%s' % str(e), file=sys.stderr)
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900353 except OSError:
354 pass
355
Colin Cross23acdd32012-04-21 00:33:54 -0700356 self._ParseManifest(nodes)
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700357
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800358 if self.IsMirror:
359 self._AddMetaProjectMirror(self.repoProject)
360 self._AddMetaProjectMirror(self.manifestProject)
361
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700362 self._loaded = True
363
Brian Harring475a47d2012-06-07 20:05:35 -0700364 def _ParseManifestXml(self, path, include_root):
David Pursehousef7fc8a92012-11-13 04:00:28 +0900365 try:
366 root = xml.dom.minidom.parse(path)
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900367 except (OSError, xml.parsers.expat.ExpatError) as e:
David Pursehousef7fc8a92012-11-13 04:00:28 +0900368 raise ManifestParseError("error parsing manifest %s: %s" % (path, e))
369
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700370 if not root or not root.childNodes:
Brian Harring26448742011-04-28 05:04:41 -0700371 raise ManifestParseError("no root node in %s" % (path,))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700372
Jooncheol Park34acdd22012-08-27 02:25:59 +0900373 for manifest in root.childNodes:
374 if manifest.nodeName == 'manifest':
375 break
376 else:
Brian Harring26448742011-04-28 05:04:41 -0700377 raise ManifestParseError("no <manifest> in %s" % (path,))
378
Colin Cross23acdd32012-04-21 00:33:54 -0700379 nodes = []
David Pursehouse4f7bdea2012-10-22 12:50:15 +0900380 for node in manifest.childNodes: # pylint:disable=W0631
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900381 # We only get here if manifest is initialised
David Pursehousec1b86a22012-11-14 11:36:51 +0900382 if node.nodeName == 'include':
383 name = self._reqatt(node, 'name')
384 fp = os.path.join(include_root, name)
385 if not os.path.isfile(fp):
386 raise ManifestParseError, \
387 "include %s doesn't exist or isn't a file" % \
388 (name,)
389 try:
390 nodes.extend(self._ParseManifestXml(fp, include_root))
391 # should isolate this to the exact exception, but that's
392 # tricky. actual parsing implementation may vary.
393 except (KeyboardInterrupt, RuntimeError, SystemExit):
394 raise
395 except Exception as e:
396 raise ManifestParseError(
397 "failed parsing included manifest %s: %s", (name, e))
398 else:
399 nodes.append(node)
Colin Cross23acdd32012-04-21 00:33:54 -0700400 return nodes
Brian Harring26448742011-04-28 05:04:41 -0700401
Colin Cross23acdd32012-04-21 00:33:54 -0700402 def _ParseManifest(self, node_list):
403 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700404 if node.nodeName == 'remote':
405 remote = self._ParseRemote(node)
David Pursehouse717ece92012-11-13 08:49:16 +0900406 if remote:
407 if remote.name in self._remotes:
408 if remote != self._remotes[remote.name]:
409 raise ManifestParseError(
410 'remote %s already exists with different attributes' %
411 (remote.name))
412 else:
413 self._remotes[remote.name] = remote
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700414
Colin Cross23acdd32012-04-21 00:33:54 -0700415 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700416 if node.nodeName == 'default':
417 if self._default is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800418 raise ManifestParseError(
419 'duplicate default in %s' %
420 (self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700421 self._default = self._ParseDefault(node)
422 if self._default is None:
423 self._default = _Default()
424
Colin Cross23acdd32012-04-21 00:33:54 -0700425 for node in itertools.chain(*node_list):
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700426 if node.nodeName == 'notice':
427 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800428 raise ManifestParseError(
429 'duplicate notice in %s' %
430 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700431 self._notice = self._ParseNotice(node)
432
Colin Cross23acdd32012-04-21 00:33:54 -0700433 for node in itertools.chain(*node_list):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700434 if node.nodeName == 'manifest-server':
435 url = self._reqatt(node, 'url')
436 if self._manifest_server is not None:
David Pursehousec1b86a22012-11-14 11:36:51 +0900437 raise ManifestParseError(
438 'duplicate manifest-server in %s' %
439 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700440 self._manifest_server = url
441
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800442 def recursively_add_projects(project):
443 if self._projects.get(project.name):
444 raise ManifestParseError(
445 'duplicate project %s in %s' %
446 (project.name, self.manifestFile))
447 self._projects[project.name] = project
448 for subproject in project.subprojects:
449 recursively_add_projects(subproject)
450
Colin Cross23acdd32012-04-21 00:33:54 -0700451 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700452 if node.nodeName == 'project':
453 project = self._ParseProject(node)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800454 recursively_add_projects(project)
Doug Anderson37282b42011-03-04 11:54:18 -0800455 if node.nodeName == 'repo-hooks':
456 # Get the name of the project and the (space-separated) list of enabled.
457 repo_hooks_project = self._reqatt(node, 'in-project')
458 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
459
460 # Only one project can be the hooks project
461 if self._repo_hooks_project is not None:
462 raise ManifestParseError(
463 'duplicate repo-hooks in %s' %
464 (self.manifestFile))
465
466 # Store a reference to the Project.
467 try:
468 self._repo_hooks_project = self._projects[repo_hooks_project]
469 except KeyError:
470 raise ManifestParseError(
471 'project %s not found for repo-hooks' %
472 (repo_hooks_project))
473
474 # Store the enabled hooks in the Project object.
475 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
Colin Cross23acdd32012-04-21 00:33:54 -0700476 if node.nodeName == 'remove-project':
477 name = self._reqatt(node, 'name')
478 try:
479 del self._projects[name]
480 except KeyError:
David Pursehousef9107482012-11-16 19:12:32 +0900481 raise ManifestParseError('remove-project element specifies non-existent '
482 'project: %s' % name)
Colin Cross23acdd32012-04-21 00:33:54 -0700483
484 # If the manifest removes the hooks project, treat it as if it deleted
485 # the repo-hooks element too.
486 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
487 self._repo_hooks_project = None
488
Doug Anderson37282b42011-03-04 11:54:18 -0800489
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800490 def _AddMetaProjectMirror(self, m):
491 name = None
492 m_url = m.GetRemote(m.remote.name).url
493 if m_url.endswith('/.git'):
494 raise ManifestParseError, 'refusing to mirror %s' % m_url
495
496 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700497 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800498 if not url.endswith('/'):
499 url += '/'
500 if m_url.startswith(url):
501 remote = self._default.remote
502 name = m_url[len(url):]
503
504 if name is None:
505 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700506 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Shawn O. Pearcef35b2d92012-08-02 11:46:22 -0700507 remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800508 name = m_url[s:]
509
510 if name.endswith('.git'):
511 name = name[:-4]
512
513 if name not in self._projects:
514 m.PreSync()
515 gitdir = os.path.join(self.topdir, '%s.git' % name)
516 project = Project(manifest = self,
517 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700518 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800519 gitdir = gitdir,
520 worktree = None,
521 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700522 revisionExpr = m.revisionExpr,
523 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800524 self._projects[project.name] = project
525
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700526 def _ParseRemote(self, node):
527 """
528 reads a <remote> element from the manifest file
529 """
530 name = self._reqatt(node, 'name')
Yestin Sunb292b982012-07-02 07:32:50 -0700531 alias = node.getAttribute('alias')
532 if alias == '':
533 alias = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700534 fetch = self._reqatt(node, 'fetch')
535 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800536 if review == '':
537 review = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700538 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Yestin Sunb292b982012-07-02 07:32:50 -0700539 return _XmlRemote(name, alias, fetch, manifestUrl, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700540
541 def _ParseDefault(self, node):
542 """
543 reads a <default> element from the manifest file
544 """
545 d = _Default()
546 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700547 d.revisionExpr = node.getAttribute('revision')
548 if d.revisionExpr == '':
549 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700550
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700551 sync_j = node.getAttribute('sync-j')
552 if sync_j == '' or sync_j is None:
553 d.sync_j = 1
554 else:
555 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700556
557 sync_c = node.getAttribute('sync-c')
558 if not sync_c:
559 d.sync_c = False
560 else:
561 d.sync_c = sync_c.lower() in ("yes", "true", "1")
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800562
563 sync_s = node.getAttribute('sync-s')
564 if not sync_s:
565 d.sync_s = False
566 else:
567 d.sync_s = sync_s.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700568 return d
569
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700570 def _ParseNotice(self, node):
571 """
572 reads a <notice> element from the manifest file
573
574 The <notice> element is distinct from other tags in the XML in that the
575 data is conveyed between the start and end tag (it's not an empty-element
576 tag).
577
578 The white space (carriage returns, indentation) for the notice element is
579 relevant and is parsed in a way that is based on how python docstrings work.
580 In fact, the code is remarkably similar to here:
581 http://www.python.org/dev/peps/pep-0257/
582 """
583 # Get the data out of the node...
584 notice = node.childNodes[0].data
585
586 # Figure out minimum indentation, skipping the first line (the same line
587 # as the <notice> tag)...
588 minIndent = sys.maxint
589 lines = notice.splitlines()
590 for line in lines[1:]:
591 lstrippedLine = line.lstrip()
592 if lstrippedLine:
593 indent = len(line) - len(lstrippedLine)
594 minIndent = min(indent, minIndent)
595
596 # Strip leading / trailing blank lines and also indentation.
597 cleanLines = [lines[0].strip()]
598 for line in lines[1:]:
599 cleanLines.append(line[minIndent:].rstrip())
600
601 # Clear completely blank lines from front and back...
602 while cleanLines and not cleanLines[0]:
603 del cleanLines[0]
604 while cleanLines and not cleanLines[-1]:
605 del cleanLines[-1]
606
607 return '\n'.join(cleanLines)
608
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800609 def _JoinName(self, parent_name, name):
610 return os.path.join(parent_name, name)
611
612 def _UnjoinName(self, parent_name, name):
613 return os.path.relpath(name, parent_name)
614
615 def _ParseProject(self, node, parent = None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700616 """
617 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700618 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700619 name = self._reqatt(node, 'name')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800620 if parent:
621 name = self._JoinName(parent.name, name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700622
623 remote = self._get_remote(node)
624 if remote is None:
625 remote = self._default.remote
626 if remote is None:
627 raise ManifestParseError, \
628 "no remote for project %s within %s" % \
629 (name, self.manifestFile)
630
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700631 revisionExpr = node.getAttribute('revision')
632 if not revisionExpr:
633 revisionExpr = self._default.revisionExpr
634 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700635 raise ManifestParseError, \
636 "no revision for project %s within %s" % \
637 (name, self.manifestFile)
638
639 path = node.getAttribute('path')
640 if not path:
641 path = name
642 if path.startswith('/'):
643 raise ManifestParseError, \
644 "project %s path cannot be absolute in %s" % \
645 (name, self.manifestFile)
646
Mike Pontillod3153822012-02-28 11:53:24 -0800647 rebase = node.getAttribute('rebase')
648 if not rebase:
649 rebase = True
650 else:
651 rebase = rebase.lower() in ("yes", "true", "1")
652
Anatol Pomazau79770d22012-04-20 14:41:59 -0700653 sync_c = node.getAttribute('sync-c')
654 if not sync_c:
655 sync_c = False
656 else:
657 sync_c = sync_c.lower() in ("yes", "true", "1")
658
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800659 sync_s = node.getAttribute('sync-s')
660 if not sync_s:
661 sync_s = self._default.sync_s
662 else:
663 sync_s = sync_s.lower() in ("yes", "true", "1")
664
Brian Harring14a66742012-09-28 20:21:57 -0700665 upstream = node.getAttribute('upstream')
666
Conley Owens971de8e2012-04-16 10:36:08 -0700667 groups = ''
668 if node.hasAttribute('groups'):
669 groups = node.getAttribute('groups')
David Pursehouse1d947b32012-10-25 12:23:11 +0900670 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Brian Harring7da13142012-06-15 02:24:20 -0700671
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800672 if parent is None:
673 relpath, worktree, gitdir = self.GetProjectPaths(name, path)
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700674 else:
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800675 relpath, worktree, gitdir = self.GetSubprojectPaths(parent, path)
676
677 default_groups = ['all', 'name:%s' % name, 'path:%s' % relpath]
678 groups.extend(set(default_groups).difference(groups))
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700679
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700680 project = Project(manifest = self,
681 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700682 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700683 gitdir = gitdir,
684 worktree = worktree,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800685 relpath = relpath,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700686 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800687 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700688 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700689 groups = groups,
Brian Harring14a66742012-09-28 20:21:57 -0700690 sync_c = sync_c,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800691 sync_s = sync_s,
692 upstream = upstream,
693 parent = parent)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700694
695 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700696 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700697 self._ParseCopyFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500698 if n.nodeName == 'annotation':
699 self._ParseAnnotation(project, n)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800700 if n.nodeName == 'project':
701 project.subprojects.append(self._ParseProject(n, parent = project))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700702
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700703 return project
704
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800705 def GetProjectPaths(self, name, path):
706 relpath = path
707 if self.IsMirror:
708 worktree = None
709 gitdir = os.path.join(self.topdir, '%s.git' % name)
710 else:
711 worktree = os.path.join(self.topdir, path).replace('\\', '/')
712 gitdir = os.path.join(self.repodir, 'projects', '%s.git' % path)
713 return relpath, worktree, gitdir
714
715 def GetSubprojectName(self, parent, submodule_path):
716 return os.path.join(parent.name, submodule_path)
717
718 def _JoinRelpath(self, parent_relpath, relpath):
719 return os.path.join(parent_relpath, relpath)
720
721 def _UnjoinRelpath(self, parent_relpath, relpath):
722 return os.path.relpath(relpath, parent_relpath)
723
724 def GetSubprojectPaths(self, parent, path):
725 relpath = self._JoinRelpath(parent.relpath, path)
726 gitdir = os.path.join(parent.gitdir, 'subprojects', '%s.git' % path)
727 if self.IsMirror:
728 worktree = None
729 else:
730 worktree = os.path.join(parent.worktree, path).replace('\\', '/')
731 return relpath, worktree, gitdir
732
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700733 def _ParseCopyFile(self, project, node):
734 src = self._reqatt(node, 'src')
735 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800736 if not self.IsMirror:
737 # src is project relative;
738 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800739 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700740
James W. Mills24c13082012-04-12 15:04:13 -0500741 def _ParseAnnotation(self, project, node):
742 name = self._reqatt(node, 'name')
743 value = self._reqatt(node, 'value')
744 try:
745 keep = self._reqatt(node, 'keep').lower()
746 except ManifestParseError:
747 keep = "true"
748 if keep != "true" and keep != "false":
749 raise ManifestParseError, "optional \"keep\" attribute must be \"true\" or \"false\""
750 project.AddAnnotation(name, value, keep)
751
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700752 def _get_remote(self, node):
753 name = node.getAttribute('remote')
754 if not name:
755 return None
756
757 v = self._remotes.get(name)
758 if not v:
759 raise ManifestParseError, \
760 "remote %s not defined in %s" % \
761 (name, self.manifestFile)
762 return v
763
764 def _reqatt(self, node, attname):
765 """
766 reads a required attribute from the node.
767 """
768 v = node.getAttribute(attname)
769 if not v:
770 raise ManifestParseError, \
771 "no %s in <%s> within %s" % \
772 (attname, node.nodeName, self.manifestFile)
773 return v