blob: daf5740bc8f1cbe490c364b28571e08bee8d07bc [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
Colin Cross23acdd32012-04-21 00:33:54 -070016import itertools
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070017import os
Conley Owensdb728cd2011-09-26 16:34:01 -070018import re
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070019import sys
Conley Owensdb728cd2011-09-26 16:34:01 -070020import urlparse
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070021import xml.dom.minidom
22
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070023from git_config import GitConfig, IsId
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070024from project import RemoteSpec, Project, MetaProject, R_HEADS, HEAD
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070025from error import ManifestParseError
26
27MANIFEST_FILE_NAME = 'manifest.xml'
Shawn O. Pearce5cc66792008-10-23 16:19:27 -070028LOCAL_MANIFEST_NAME = 'local_manifest.xml'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070029
Conley Owensdb728cd2011-09-26 16:34:01 -070030urlparse.uses_relative.extend(['ssh', 'git'])
31urlparse.uses_netloc.extend(['ssh', 'git'])
32
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070033class _Default(object):
34 """Project defaults within the manifest."""
35
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -070036 revisionExpr = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070037 remote = None
Shawn O. Pearce6392c872011-09-22 17:44:31 -070038 sync_j = 1
Anatol Pomazau79770d22012-04-20 14:41:59 -070039 sync_c = False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070040
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070041class _XmlRemote(object):
42 def __init__(self,
43 name,
44 fetch=None,
Conley Owensdb728cd2011-09-26 16:34:01 -070045 manifestUrl=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070046 review=None):
47 self.name = name
48 self.fetchUrl = fetch
Conley Owensdb728cd2011-09-26 16:34:01 -070049 self.manifestUrl = manifestUrl
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070050 self.reviewUrl = review
Conley Owensceea3682011-10-20 10:45:47 -070051 self.resolvedFetchUrl = self._resolveFetchUrl()
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070052
Conley Owensceea3682011-10-20 10:45:47 -070053 def _resolveFetchUrl(self):
54 url = self.fetchUrl.rstrip('/')
Conley Owensdb728cd2011-09-26 16:34:01 -070055 manifestUrl = self.manifestUrl.rstrip('/')
56 # urljoin will get confused if there is no scheme in the base url
57 # ie, if manifestUrl is of the form <hostname:port>
58 if manifestUrl.find(':') != manifestUrl.find('/') - 1:
59 manifestUrl = 'gopher://' + manifestUrl
60 url = urlparse.urljoin(manifestUrl, url)
Conley Owensceea3682011-10-20 10:45:47 -070061 return re.sub(r'^gopher://', '', url)
62
63 def ToRemoteSpec(self, projectName):
Conley Owens9d8f9142011-10-20 14:36:35 -070064 url = self.resolvedFetchUrl.rstrip('/') + '/' + projectName
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070065 return RemoteSpec(self.name, url, self.reviewUrl)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070066
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070067class XmlManifest(object):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070068 """manages the repo configuration file"""
69
70 def __init__(self, repodir):
71 self.repodir = os.path.abspath(repodir)
72 self.topdir = os.path.dirname(self.repodir)
73 self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070074 self.globalConfig = GitConfig.ForUser()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070075
76 self.repoProject = MetaProject(self, 'repo',
77 gitdir = os.path.join(repodir, 'repo/.git'),
78 worktree = os.path.join(repodir, 'repo'))
79
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070080 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -080081 gitdir = os.path.join(repodir, 'manifests.git'),
82 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070083
84 self._Unload()
85
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -070086 def Override(self, name):
87 """Use a different manifest, just for the current instantiation.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070088 """
89 path = os.path.join(self.manifestProject.worktree, name)
90 if not os.path.isfile(path):
91 raise ManifestParseError('manifest %s not found' % name)
92
93 old = self.manifestFile
94 try:
95 self.manifestFile = path
96 self._Unload()
97 self._Load()
98 finally:
99 self.manifestFile = old
100
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700101 def Link(self, name):
102 """Update the repo metadata to use a different manifest.
103 """
104 self.Override(name)
105
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700106 try:
107 if os.path.exists(self.manifestFile):
108 os.remove(self.manifestFile)
109 os.symlink('manifests/%s' % name, self.manifestFile)
110 except OSError, e:
111 raise ManifestParseError('cannot link manifest %s' % name)
112
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800113 def _RemoteToXml(self, r, doc, root):
114 e = doc.createElement('remote')
115 root.appendChild(e)
116 e.setAttribute('name', r.name)
117 e.setAttribute('fetch', r.fetchUrl)
118 if r.reviewUrl is not None:
119 e.setAttribute('review', r.reviewUrl)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800120
121 def Save(self, fd, peg_rev=False):
122 """Write the current manifest out to the given file descriptor.
123 """
Colin Cross5acde752012-03-28 20:15:45 -0700124 mp = self.manifestProject
125
126 groups = mp.config.GetString('manifest.groups')
Colin Crossc39864f2012-04-23 13:41:58 -0700127 if not groups:
Conley Owens971de8e2012-04-16 10:36:08 -0700128 groups = 'default'
129 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Colin Cross5acde752012-03-28 20:15:45 -0700130
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800131 doc = xml.dom.minidom.Document()
132 root = doc.createElement('manifest')
133 doc.appendChild(root)
134
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700135 # Save out the notice. There's a little bit of work here to give it the
136 # right whitespace, which assumes that the notice is automatically indented
137 # by 4 by minidom.
138 if self.notice:
139 notice_element = root.appendChild(doc.createElement('notice'))
140 notice_lines = self.notice.splitlines()
141 indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:]
142 notice_element.appendChild(doc.createTextNode(indented_notice))
143
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800144 d = self.default
145 sort_remotes = list(self.remotes.keys())
146 sort_remotes.sort()
147
148 for r in sort_remotes:
149 self._RemoteToXml(self.remotes[r], doc, root)
150 if self.remotes:
151 root.appendChild(doc.createTextNode(''))
152
153 have_default = False
154 e = doc.createElement('default')
155 if d.remote:
156 have_default = True
157 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700158 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800159 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700160 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700161 if d.sync_j > 1:
162 have_default = True
163 e.setAttribute('sync-j', '%d' % d.sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700164 if d.sync_c:
165 have_default = True
166 e.setAttribute('sync-c', 'true')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800167 if have_default:
168 root.appendChild(e)
169 root.appendChild(doc.createTextNode(''))
170
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700171 if self._manifest_server:
172 e = doc.createElement('manifest-server')
173 e.setAttribute('url', self._manifest_server)
174 root.appendChild(e)
175 root.appendChild(doc.createTextNode(''))
176
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800177 sort_projects = list(self.projects.keys())
178 sort_projects.sort()
179
180 for p in sort_projects:
181 p = self.projects[p]
Colin Cross5acde752012-03-28 20:15:45 -0700182
183 if not p.MatchesGroups(groups):
184 continue
185
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800186 e = doc.createElement('project')
187 root.appendChild(e)
188 e.setAttribute('name', p.name)
189 if p.relpath != p.name:
190 e.setAttribute('path', p.relpath)
191 if not d.remote or p.remote.name != d.remote.name:
192 e.setAttribute('remote', p.remote.name)
193 if peg_rev:
194 if self.IsMirror:
195 e.setAttribute('revision',
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700196 p.bare_git.rev_parse(p.revisionExpr + '^0'))
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800197 else:
198 e.setAttribute('revision',
199 p.work_git.rev_parse(HEAD + '^0'))
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700200 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
201 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800202
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800203 for c in p.copyfiles:
204 ce = doc.createElement('copyfile')
205 ce.setAttribute('src', c.src)
206 ce.setAttribute('dest', c.dest)
207 e.appendChild(ce)
208
Conley Owens971de8e2012-04-16 10:36:08 -0700209 egroups = [g for g in p.groups if g != 'default']
210 if egroups:
211 e.setAttribute('groups', ','.join(egroups))
Colin Cross5acde752012-03-28 20:15:45 -0700212
James W. Mills24c13082012-04-12 15:04:13 -0500213 for a in p.annotations:
214 if a.keep == "true":
215 ae = doc.createElement('annotation')
216 ae.setAttribute('name', a.name)
217 ae.setAttribute('value', a.value)
218 e.appendChild(ae)
219
Anatol Pomazau79770d22012-04-20 14:41:59 -0700220 if p.sync_c:
221 e.setAttribute('sync-c', 'true')
222
Doug Anderson37282b42011-03-04 11:54:18 -0800223 if self._repo_hooks_project:
224 root.appendChild(doc.createTextNode(''))
225 e = doc.createElement('repo-hooks')
226 e.setAttribute('in-project', self._repo_hooks_project.name)
227 e.setAttribute('enabled-list',
228 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
229 root.appendChild(e)
230
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800231 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
232
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700233 @property
234 def projects(self):
235 self._Load()
236 return self._projects
237
238 @property
239 def remotes(self):
240 self._Load()
241 return self._remotes
242
243 @property
244 def default(self):
245 self._Load()
246 return self._default
247
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800248 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800249 def repo_hooks_project(self):
250 self._Load()
251 return self._repo_hooks_project
252
253 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700254 def notice(self):
255 self._Load()
256 return self._notice
257
258 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700259 def manifest_server(self):
260 self._Load()
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800261 return self._manifest_server
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700262
263 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800264 def IsMirror(self):
265 return self.manifestProject.config.GetBoolean('repo.mirror')
266
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700267 def _Unload(self):
268 self._loaded = False
269 self._projects = {}
270 self._remotes = {}
271 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800272 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700273 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700274 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700275 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700276
277 def _Load(self):
278 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800279 m = self.manifestProject
280 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700281 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800282 b = b[len(R_HEADS):]
283 self.branch = b
284
Colin Cross23acdd32012-04-21 00:33:54 -0700285 nodes = []
286 nodes.append(self._ParseManifestXml(self.manifestFile))
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700287
288 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
289 if os.path.exists(local):
Colin Cross23acdd32012-04-21 00:33:54 -0700290 nodes.append(self._ParseManifestXml(local))
291
292 self._ParseManifest(nodes)
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700293
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800294 if self.IsMirror:
295 self._AddMetaProjectMirror(self.repoProject)
296 self._AddMetaProjectMirror(self.manifestProject)
297
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700298 self._loaded = True
299
Colin Cross23acdd32012-04-21 00:33:54 -0700300 def _ParseManifestXml(self, path):
Brian Harring26448742011-04-28 05:04:41 -0700301 root = xml.dom.minidom.parse(path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700302 if not root or not root.childNodes:
Brian Harring26448742011-04-28 05:04:41 -0700303 raise ManifestParseError("no root node in %s" % (path,))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700304
305 config = root.childNodes[0]
306 if config.nodeName != 'manifest':
Brian Harring26448742011-04-28 05:04:41 -0700307 raise ManifestParseError("no <manifest> in %s" % (path,))
308
Colin Cross23acdd32012-04-21 00:33:54 -0700309 nodes = []
Brian Harring26448742011-04-28 05:04:41 -0700310 for node in config.childNodes:
311 if node.nodeName == 'include':
312 name = self._reqatt(node, 'name')
Colin Cross23acdd32012-04-21 00:33:54 -0700313 fp = os.path.join(os.path.dirname(path), name)
Brian Harring26448742011-04-28 05:04:41 -0700314 if not os.path.isfile(fp):
315 raise ManifestParseError, \
316 "include %s doesn't exist or isn't a file" % \
317 (name,)
318 try:
Colin Cross23acdd32012-04-21 00:33:54 -0700319 nodes.extend(self._ParseManifestXml(fp))
Brian Harring26448742011-04-28 05:04:41 -0700320 # should isolate this to the exact exception, but that's
321 # tricky. actual parsing implementation may vary.
322 except (KeyboardInterrupt, RuntimeError, SystemExit):
323 raise
324 except Exception, e:
325 raise ManifestParseError(
326 "failed parsing included manifest %s: %s", (name, e))
Colin Cross23acdd32012-04-21 00:33:54 -0700327 else:
328 nodes.append(node)
329 return nodes
Brian Harring26448742011-04-28 05:04:41 -0700330
Colin Cross23acdd32012-04-21 00:33:54 -0700331 def _ParseManifest(self, node_list):
332 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700333 if node.nodeName == 'remote':
334 remote = self._ParseRemote(node)
335 if self._remotes.get(remote.name):
Doug Anderson37282b42011-03-04 11:54:18 -0800336 raise ManifestParseError(
337 'duplicate remote %s in %s' %
338 (remote.name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700339 self._remotes[remote.name] = remote
340
Colin Cross23acdd32012-04-21 00:33:54 -0700341 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700342 if node.nodeName == 'default':
343 if self._default is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800344 raise ManifestParseError(
345 'duplicate default in %s' %
346 (self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700347 self._default = self._ParseDefault(node)
348 if self._default is None:
349 self._default = _Default()
350
Colin Cross23acdd32012-04-21 00:33:54 -0700351 for node in itertools.chain(*node_list):
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700352 if node.nodeName == 'notice':
353 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800354 raise ManifestParseError(
355 'duplicate notice in %s' %
356 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700357 self._notice = self._ParseNotice(node)
358
Colin Cross23acdd32012-04-21 00:33:54 -0700359 for node in itertools.chain(*node_list):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700360 if node.nodeName == 'manifest-server':
361 url = self._reqatt(node, 'url')
362 if self._manifest_server is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800363 raise ManifestParseError(
364 'duplicate manifest-server in %s' %
365 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700366 self._manifest_server = url
367
Colin Cross23acdd32012-04-21 00:33:54 -0700368 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700369 if node.nodeName == 'project':
370 project = self._ParseProject(node)
371 if self._projects.get(project.name):
Doug Anderson37282b42011-03-04 11:54:18 -0800372 raise ManifestParseError(
373 'duplicate project %s in %s' %
374 (project.name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700375 self._projects[project.name] = project
Doug Anderson37282b42011-03-04 11:54:18 -0800376 if node.nodeName == 'repo-hooks':
377 # Get the name of the project and the (space-separated) list of enabled.
378 repo_hooks_project = self._reqatt(node, 'in-project')
379 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
380
381 # Only one project can be the hooks project
382 if self._repo_hooks_project is not None:
383 raise ManifestParseError(
384 'duplicate repo-hooks in %s' %
385 (self.manifestFile))
386
387 # Store a reference to the Project.
388 try:
389 self._repo_hooks_project = self._projects[repo_hooks_project]
390 except KeyError:
391 raise ManifestParseError(
392 'project %s not found for repo-hooks' %
393 (repo_hooks_project))
394
395 # Store the enabled hooks in the Project object.
396 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
Colin Cross23acdd32012-04-21 00:33:54 -0700397 if node.nodeName == 'remove-project':
398 name = self._reqatt(node, 'name')
399 try:
400 del self._projects[name]
401 except KeyError:
402 raise ManifestParseError(
403 'project %s not found' %
404 (name))
405
406 # If the manifest removes the hooks project, treat it as if it deleted
407 # the repo-hooks element too.
408 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
409 self._repo_hooks_project = None
410
Doug Anderson37282b42011-03-04 11:54:18 -0800411
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800412 def _AddMetaProjectMirror(self, m):
413 name = None
414 m_url = m.GetRemote(m.remote.name).url
415 if m_url.endswith('/.git'):
416 raise ManifestParseError, 'refusing to mirror %s' % m_url
417
418 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700419 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800420 if not url.endswith('/'):
421 url += '/'
422 if m_url.startswith(url):
423 remote = self._default.remote
424 name = m_url[len(url):]
425
426 if name is None:
427 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700428 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
429 remote = _XmlRemote('origin', m_url[:s], manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800430 name = m_url[s:]
431
432 if name.endswith('.git'):
433 name = name[:-4]
434
435 if name not in self._projects:
436 m.PreSync()
437 gitdir = os.path.join(self.topdir, '%s.git' % name)
438 project = Project(manifest = self,
439 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700440 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800441 gitdir = gitdir,
442 worktree = None,
443 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700444 revisionExpr = m.revisionExpr,
445 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800446 self._projects[project.name] = project
447
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700448 def _ParseRemote(self, node):
449 """
450 reads a <remote> element from the manifest file
451 """
452 name = self._reqatt(node, 'name')
453 fetch = self._reqatt(node, 'fetch')
454 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800455 if review == '':
456 review = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700457 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
458 return _XmlRemote(name, fetch, manifestUrl, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700459
460 def _ParseDefault(self, node):
461 """
462 reads a <default> element from the manifest file
463 """
464 d = _Default()
465 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700466 d.revisionExpr = node.getAttribute('revision')
467 if d.revisionExpr == '':
468 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700469
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700470 sync_j = node.getAttribute('sync-j')
471 if sync_j == '' or sync_j is None:
472 d.sync_j = 1
473 else:
474 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700475
476 sync_c = node.getAttribute('sync-c')
477 if not sync_c:
478 d.sync_c = False
479 else:
480 d.sync_c = sync_c.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700481 return d
482
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700483 def _ParseNotice(self, node):
484 """
485 reads a <notice> element from the manifest file
486
487 The <notice> element is distinct from other tags in the XML in that the
488 data is conveyed between the start and end tag (it's not an empty-element
489 tag).
490
491 The white space (carriage returns, indentation) for the notice element is
492 relevant and is parsed in a way that is based on how python docstrings work.
493 In fact, the code is remarkably similar to here:
494 http://www.python.org/dev/peps/pep-0257/
495 """
496 # Get the data out of the node...
497 notice = node.childNodes[0].data
498
499 # Figure out minimum indentation, skipping the first line (the same line
500 # as the <notice> tag)...
501 minIndent = sys.maxint
502 lines = notice.splitlines()
503 for line in lines[1:]:
504 lstrippedLine = line.lstrip()
505 if lstrippedLine:
506 indent = len(line) - len(lstrippedLine)
507 minIndent = min(indent, minIndent)
508
509 # Strip leading / trailing blank lines and also indentation.
510 cleanLines = [lines[0].strip()]
511 for line in lines[1:]:
512 cleanLines.append(line[minIndent:].rstrip())
513
514 # Clear completely blank lines from front and back...
515 while cleanLines and not cleanLines[0]:
516 del cleanLines[0]
517 while cleanLines and not cleanLines[-1]:
518 del cleanLines[-1]
519
520 return '\n'.join(cleanLines)
521
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700522 def _ParseProject(self, node):
523 """
524 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700525 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700526 name = self._reqatt(node, 'name')
527
528 remote = self._get_remote(node)
529 if remote is None:
530 remote = self._default.remote
531 if remote is None:
532 raise ManifestParseError, \
533 "no remote for project %s within %s" % \
534 (name, self.manifestFile)
535
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700536 revisionExpr = node.getAttribute('revision')
537 if not revisionExpr:
538 revisionExpr = self._default.revisionExpr
539 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700540 raise ManifestParseError, \
541 "no revision for project %s within %s" % \
542 (name, self.manifestFile)
543
544 path = node.getAttribute('path')
545 if not path:
546 path = name
547 if path.startswith('/'):
548 raise ManifestParseError, \
549 "project %s path cannot be absolute in %s" % \
550 (name, self.manifestFile)
551
Mike Pontillod3153822012-02-28 11:53:24 -0800552 rebase = node.getAttribute('rebase')
553 if not rebase:
554 rebase = True
555 else:
556 rebase = rebase.lower() in ("yes", "true", "1")
557
Anatol Pomazau79770d22012-04-20 14:41:59 -0700558 sync_c = node.getAttribute('sync-c')
559 if not sync_c:
560 sync_c = False
561 else:
562 sync_c = sync_c.lower() in ("yes", "true", "1")
563
Conley Owens971de8e2012-04-16 10:36:08 -0700564 groups = ''
565 if node.hasAttribute('groups'):
566 groups = node.getAttribute('groups')
567 groups = [x for x in re.split('[,\s]+', groups) if x]
568 if 'default' not in groups:
569 groups.append('default')
Colin Cross5acde752012-03-28 20:15:45 -0700570
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800571 if self.IsMirror:
572 relpath = None
573 worktree = None
574 gitdir = os.path.join(self.topdir, '%s.git' % name)
575 else:
Anthony Newnamdf14a702011-01-09 17:31:57 -0800576 worktree = os.path.join(self.topdir, path).replace('\\', '/')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800577 gitdir = os.path.join(self.repodir, 'projects/%s.git' % path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700578
579 project = Project(manifest = self,
580 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700581 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700582 gitdir = gitdir,
583 worktree = worktree,
584 relpath = path,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700585 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800586 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700587 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700588 groups = groups,
589 sync_c = sync_c)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700590
591 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700592 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700593 self._ParseCopyFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500594 if n.nodeName == 'annotation':
595 self._ParseAnnotation(project, n)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700596
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700597 return project
598
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700599 def _ParseCopyFile(self, project, node):
600 src = self._reqatt(node, 'src')
601 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800602 if not self.IsMirror:
603 # src is project relative;
604 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800605 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700606
James W. Mills24c13082012-04-12 15:04:13 -0500607 def _ParseAnnotation(self, project, node):
608 name = self._reqatt(node, 'name')
609 value = self._reqatt(node, 'value')
610 try:
611 keep = self._reqatt(node, 'keep').lower()
612 except ManifestParseError:
613 keep = "true"
614 if keep != "true" and keep != "false":
615 raise ManifestParseError, "optional \"keep\" attribute must be \"true\" or \"false\""
616 project.AddAnnotation(name, value, keep)
617
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700618 def _get_remote(self, node):
619 name = node.getAttribute('remote')
620 if not name:
621 return None
622
623 v = self._remotes.get(name)
624 if not v:
625 raise ManifestParseError, \
626 "remote %s not defined in %s" % \
627 (name, self.manifestFile)
628 return v
629
630 def _reqatt(self, node, attname):
631 """
632 reads a required attribute from the node.
633 """
634 v = node.getAttribute(attname)
635 if not v:
636 raise ManifestParseError, \
637 "no %s in <%s> within %s" % \
638 (attname, node.nodeName, self.manifestFile)
639 return v