blob: c9e5c405632da824390b1d2da285e84c68aecaa1 [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
16import os
Conley Owensdb728cd2011-09-26 16:34:01 -070017import re
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070018import sys
Conley Owensdb728cd2011-09-26 16:34:01 -070019import urlparse
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070020import xml.dom.minidom
21
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070022from git_config import GitConfig, IsId
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070023from project import RemoteSpec, Project, MetaProject, R_HEADS, HEAD
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070024from error import ManifestParseError
25
26MANIFEST_FILE_NAME = 'manifest.xml'
Shawn O. Pearce5cc66792008-10-23 16:19:27 -070027LOCAL_MANIFEST_NAME = 'local_manifest.xml'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070028
Conley Owensdb728cd2011-09-26 16:34:01 -070029urlparse.uses_relative.extend(['ssh', 'git'])
30urlparse.uses_netloc.extend(['ssh', 'git'])
31
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070032class _Default(object):
33 """Project defaults within the manifest."""
34
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -070035 revisionExpr = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070036 remote = None
Shawn O. Pearce6392c872011-09-22 17:44:31 -070037 sync_j = 1
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070038
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070039class _XmlRemote(object):
40 def __init__(self,
41 name,
42 fetch=None,
Conley Owensdb728cd2011-09-26 16:34:01 -070043 manifestUrl=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070044 review=None):
45 self.name = name
46 self.fetchUrl = fetch
Conley Owensdb728cd2011-09-26 16:34:01 -070047 self.manifestUrl = manifestUrl
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070048 self.reviewUrl = review
Conley Owensceea3682011-10-20 10:45:47 -070049 self.resolvedFetchUrl = self._resolveFetchUrl()
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070050
Conley Owensceea3682011-10-20 10:45:47 -070051 def _resolveFetchUrl(self):
52 url = self.fetchUrl.rstrip('/')
Conley Owensdb728cd2011-09-26 16:34:01 -070053 manifestUrl = self.manifestUrl.rstrip('/')
54 # urljoin will get confused if there is no scheme in the base url
55 # ie, if manifestUrl is of the form <hostname:port>
56 if manifestUrl.find(':') != manifestUrl.find('/') - 1:
57 manifestUrl = 'gopher://' + manifestUrl
58 url = urlparse.urljoin(manifestUrl, url)
Conley Owensceea3682011-10-20 10:45:47 -070059 return re.sub(r'^gopher://', '', url)
60
61 def ToRemoteSpec(self, projectName):
Conley Owens9d8f9142011-10-20 14:36:35 -070062 url = self.resolvedFetchUrl.rstrip('/') + '/' + projectName
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070063 return RemoteSpec(self.name, url, self.reviewUrl)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070064
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070065class XmlManifest(object):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070066 """manages the repo configuration file"""
67
68 def __init__(self, repodir):
69 self.repodir = os.path.abspath(repodir)
70 self.topdir = os.path.dirname(self.repodir)
71 self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070072 self.globalConfig = GitConfig.ForUser()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070073
74 self.repoProject = MetaProject(self, 'repo',
75 gitdir = os.path.join(repodir, 'repo/.git'),
76 worktree = os.path.join(repodir, 'repo'))
77
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070078 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -080079 gitdir = os.path.join(repodir, 'manifests.git'),
80 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070081
82 self._Unload()
83
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -070084 def Override(self, name):
85 """Use a different manifest, just for the current instantiation.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070086 """
87 path = os.path.join(self.manifestProject.worktree, name)
88 if not os.path.isfile(path):
89 raise ManifestParseError('manifest %s not found' % name)
90
91 old = self.manifestFile
92 try:
93 self.manifestFile = path
94 self._Unload()
95 self._Load()
96 finally:
97 self.manifestFile = old
98
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -070099 def Link(self, name):
100 """Update the repo metadata to use a different manifest.
101 """
102 self.Override(name)
103
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700104 try:
105 if os.path.exists(self.manifestFile):
106 os.remove(self.manifestFile)
107 os.symlink('manifests/%s' % name, self.manifestFile)
108 except OSError, e:
109 raise ManifestParseError('cannot link manifest %s' % name)
110
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800111 def _RemoteToXml(self, r, doc, root):
112 e = doc.createElement('remote')
113 root.appendChild(e)
114 e.setAttribute('name', r.name)
115 e.setAttribute('fetch', r.fetchUrl)
116 if r.reviewUrl is not None:
117 e.setAttribute('review', r.reviewUrl)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800118
119 def Save(self, fd, peg_rev=False):
120 """Write the current manifest out to the given file descriptor.
121 """
122 doc = xml.dom.minidom.Document()
123 root = doc.createElement('manifest')
124 doc.appendChild(root)
125
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700126 # Save out the notice. There's a little bit of work here to give it the
127 # right whitespace, which assumes that the notice is automatically indented
128 # by 4 by minidom.
129 if self.notice:
130 notice_element = root.appendChild(doc.createElement('notice'))
131 notice_lines = self.notice.splitlines()
132 indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:]
133 notice_element.appendChild(doc.createTextNode(indented_notice))
134
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800135 d = self.default
136 sort_remotes = list(self.remotes.keys())
137 sort_remotes.sort()
138
139 for r in sort_remotes:
140 self._RemoteToXml(self.remotes[r], doc, root)
141 if self.remotes:
142 root.appendChild(doc.createTextNode(''))
143
144 have_default = False
145 e = doc.createElement('default')
146 if d.remote:
147 have_default = True
148 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700149 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800150 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700151 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700152 if d.sync_j > 1:
153 have_default = True
154 e.setAttribute('sync-j', '%d' % d.sync_j)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800155 if have_default:
156 root.appendChild(e)
157 root.appendChild(doc.createTextNode(''))
158
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700159 if self._manifest_server:
160 e = doc.createElement('manifest-server')
161 e.setAttribute('url', self._manifest_server)
162 root.appendChild(e)
163 root.appendChild(doc.createTextNode(''))
164
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800165 sort_projects = list(self.projects.keys())
166 sort_projects.sort()
167
168 for p in sort_projects:
169 p = self.projects[p]
170 e = doc.createElement('project')
171 root.appendChild(e)
172 e.setAttribute('name', p.name)
173 if p.relpath != p.name:
174 e.setAttribute('path', p.relpath)
175 if not d.remote or p.remote.name != d.remote.name:
176 e.setAttribute('remote', p.remote.name)
177 if peg_rev:
178 if self.IsMirror:
179 e.setAttribute('revision',
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700180 p.bare_git.rev_parse(p.revisionExpr + '^0'))
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800181 else:
182 e.setAttribute('revision',
183 p.work_git.rev_parse(HEAD + '^0'))
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700184 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
185 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800186
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800187 for c in p.copyfiles:
188 ce = doc.createElement('copyfile')
189 ce.setAttribute('src', c.src)
190 ce.setAttribute('dest', c.dest)
191 e.appendChild(ce)
192
Doug Anderson37282b42011-03-04 11:54:18 -0800193 if self._repo_hooks_project:
194 root.appendChild(doc.createTextNode(''))
195 e = doc.createElement('repo-hooks')
196 e.setAttribute('in-project', self._repo_hooks_project.name)
197 e.setAttribute('enabled-list',
198 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
199 root.appendChild(e)
200
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800201 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
202
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700203 @property
204 def projects(self):
205 self._Load()
206 return self._projects
207
208 @property
209 def remotes(self):
210 self._Load()
211 return self._remotes
212
213 @property
214 def default(self):
215 self._Load()
216 return self._default
217
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800218 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800219 def repo_hooks_project(self):
220 self._Load()
221 return self._repo_hooks_project
222
223 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700224 def notice(self):
225 self._Load()
226 return self._notice
227
228 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700229 def manifest_server(self):
230 self._Load()
Victor Boivieee1c2f52011-04-19 10:50:12 +0200231
232 if self._manifest_server:
233 return self._manifest_server
234
235 return self.manifestProject.config.GetString('repo.manifest-server')
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700236
237 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800238 def IsMirror(self):
239 return self.manifestProject.config.GetBoolean('repo.mirror')
240
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700241 def _Unload(self):
242 self._loaded = False
243 self._projects = {}
244 self._remotes = {}
245 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800246 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700247 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700248 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700249 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700250
251 def _Load(self):
252 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800253 m = self.manifestProject
254 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700255 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800256 b = b[len(R_HEADS):]
257 self.branch = b
258
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700259 self._ParseManifest(True)
260
261 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
262 if os.path.exists(local):
263 try:
264 real = self.manifestFile
265 self.manifestFile = local
266 self._ParseManifest(False)
267 finally:
268 self.manifestFile = real
269
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800270 if self.IsMirror:
271 self._AddMetaProjectMirror(self.repoProject)
272 self._AddMetaProjectMirror(self.manifestProject)
273
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700274 self._loaded = True
275
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700276 def _ParseManifest(self, is_root_file):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700277 root = xml.dom.minidom.parse(self.manifestFile)
278 if not root or not root.childNodes:
Doug Anderson37282b42011-03-04 11:54:18 -0800279 raise ManifestParseError(
280 "no root node in %s" %
281 self.manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700282
283 config = root.childNodes[0]
284 if config.nodeName != 'manifest':
Doug Anderson37282b42011-03-04 11:54:18 -0800285 raise ManifestParseError(
286 "no <manifest> in %s" %
287 self.manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700288
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700289 for node in config.childNodes:
Shawn O. Pearce03eaf072008-11-20 11:42:22 -0800290 if node.nodeName == 'remove-project':
291 name = self._reqatt(node, 'name')
292 try:
293 del self._projects[name]
294 except KeyError:
Doug Anderson37282b42011-03-04 11:54:18 -0800295 raise ManifestParseError(
296 'project %s not found' %
297 (name))
298
299 # If the manifest removes the hooks project, treat it as if it deleted
300 # the repo-hooks element too.
301 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
302 self._repo_hooks_project = None
Shawn O. Pearce03eaf072008-11-20 11:42:22 -0800303
304 for node in config.childNodes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700305 if node.nodeName == 'remote':
306 remote = self._ParseRemote(node)
307 if self._remotes.get(remote.name):
Doug Anderson37282b42011-03-04 11:54:18 -0800308 raise ManifestParseError(
309 'duplicate remote %s in %s' %
310 (remote.name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700311 self._remotes[remote.name] = remote
312
313 for node in config.childNodes:
314 if node.nodeName == 'default':
315 if self._default is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800316 raise ManifestParseError(
317 'duplicate default in %s' %
318 (self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700319 self._default = self._ParseDefault(node)
320 if self._default is None:
321 self._default = _Default()
322
323 for node in config.childNodes:
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700324 if node.nodeName == 'notice':
325 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800326 raise ManifestParseError(
327 'duplicate notice in %s' %
328 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700329 self._notice = self._ParseNotice(node)
330
331 for node in config.childNodes:
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700332 if node.nodeName == 'manifest-server':
333 url = self._reqatt(node, 'url')
334 if self._manifest_server is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800335 raise ManifestParseError(
336 'duplicate manifest-server in %s' %
337 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700338 self._manifest_server = url
339
340 for node in config.childNodes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700341 if node.nodeName == 'project':
342 project = self._ParseProject(node)
343 if self._projects.get(project.name):
Doug Anderson37282b42011-03-04 11:54:18 -0800344 raise ManifestParseError(
345 'duplicate project %s in %s' %
346 (project.name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700347 self._projects[project.name] = project
348
Doug Anderson37282b42011-03-04 11:54:18 -0800349 for node in config.childNodes:
350 if node.nodeName == 'repo-hooks':
351 # Get the name of the project and the (space-separated) list of enabled.
352 repo_hooks_project = self._reqatt(node, 'in-project')
353 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
354
355 # Only one project can be the hooks project
356 if self._repo_hooks_project is not None:
357 raise ManifestParseError(
358 'duplicate repo-hooks in %s' %
359 (self.manifestFile))
360
361 # Store a reference to the Project.
362 try:
363 self._repo_hooks_project = self._projects[repo_hooks_project]
364 except KeyError:
365 raise ManifestParseError(
366 'project %s not found for repo-hooks' %
367 (repo_hooks_project))
368
369 # Store the enabled hooks in the Project object.
370 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
371
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800372 def _AddMetaProjectMirror(self, m):
373 name = None
374 m_url = m.GetRemote(m.remote.name).url
375 if m_url.endswith('/.git'):
376 raise ManifestParseError, 'refusing to mirror %s' % m_url
377
378 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700379 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800380 if not url.endswith('/'):
381 url += '/'
382 if m_url.startswith(url):
383 remote = self._default.remote
384 name = m_url[len(url):]
385
386 if name is None:
387 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700388 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
389 remote = _XmlRemote('origin', m_url[:s], manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800390 name = m_url[s:]
391
392 if name.endswith('.git'):
393 name = name[:-4]
394
395 if name not in self._projects:
396 m.PreSync()
397 gitdir = os.path.join(self.topdir, '%s.git' % name)
398 project = Project(manifest = self,
399 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700400 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800401 gitdir = gitdir,
402 worktree = None,
403 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700404 revisionExpr = m.revisionExpr,
405 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800406 self._projects[project.name] = project
407
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700408 def _ParseRemote(self, node):
409 """
410 reads a <remote> element from the manifest file
411 """
412 name = self._reqatt(node, 'name')
413 fetch = self._reqatt(node, 'fetch')
414 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800415 if review == '':
416 review = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700417 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
418 return _XmlRemote(name, fetch, manifestUrl, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700419
420 def _ParseDefault(self, node):
421 """
422 reads a <default> element from the manifest file
423 """
424 d = _Default()
425 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700426 d.revisionExpr = node.getAttribute('revision')
427 if d.revisionExpr == '':
428 d.revisionExpr = None
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700429 sync_j = node.getAttribute('sync-j')
430 if sync_j == '' or sync_j is None:
431 d.sync_j = 1
432 else:
433 d.sync_j = int(sync_j)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700434 return d
435
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700436 def _ParseNotice(self, node):
437 """
438 reads a <notice> element from the manifest file
439
440 The <notice> element is distinct from other tags in the XML in that the
441 data is conveyed between the start and end tag (it's not an empty-element
442 tag).
443
444 The white space (carriage returns, indentation) for the notice element is
445 relevant and is parsed in a way that is based on how python docstrings work.
446 In fact, the code is remarkably similar to here:
447 http://www.python.org/dev/peps/pep-0257/
448 """
449 # Get the data out of the node...
450 notice = node.childNodes[0].data
451
452 # Figure out minimum indentation, skipping the first line (the same line
453 # as the <notice> tag)...
454 minIndent = sys.maxint
455 lines = notice.splitlines()
456 for line in lines[1:]:
457 lstrippedLine = line.lstrip()
458 if lstrippedLine:
459 indent = len(line) - len(lstrippedLine)
460 minIndent = min(indent, minIndent)
461
462 # Strip leading / trailing blank lines and also indentation.
463 cleanLines = [lines[0].strip()]
464 for line in lines[1:]:
465 cleanLines.append(line[minIndent:].rstrip())
466
467 # Clear completely blank lines from front and back...
468 while cleanLines and not cleanLines[0]:
469 del cleanLines[0]
470 while cleanLines and not cleanLines[-1]:
471 del cleanLines[-1]
472
473 return '\n'.join(cleanLines)
474
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700475 def _ParseProject(self, node):
476 """
477 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700478 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700479 name = self._reqatt(node, 'name')
480
481 remote = self._get_remote(node)
482 if remote is None:
483 remote = self._default.remote
484 if remote is None:
485 raise ManifestParseError, \
486 "no remote for project %s within %s" % \
487 (name, self.manifestFile)
488
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700489 revisionExpr = node.getAttribute('revision')
490 if not revisionExpr:
491 revisionExpr = self._default.revisionExpr
492 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700493 raise ManifestParseError, \
494 "no revision for project %s within %s" % \
495 (name, self.manifestFile)
496
497 path = node.getAttribute('path')
498 if not path:
499 path = name
500 if path.startswith('/'):
501 raise ManifestParseError, \
502 "project %s path cannot be absolute in %s" % \
503 (name, self.manifestFile)
504
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800505 if self.IsMirror:
506 relpath = None
507 worktree = None
508 gitdir = os.path.join(self.topdir, '%s.git' % name)
509 else:
Anthony Newnamdf14a702011-01-09 17:31:57 -0800510 worktree = os.path.join(self.topdir, path).replace('\\', '/')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800511 gitdir = os.path.join(self.repodir, 'projects/%s.git' % path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700512
513 project = Project(manifest = self,
514 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700515 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700516 gitdir = gitdir,
517 worktree = worktree,
518 relpath = path,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700519 revisionExpr = revisionExpr,
520 revisionId = None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700521
522 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700523 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700524 self._ParseCopyFile(project, n)
525
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700526 return project
527
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700528 def _ParseCopyFile(self, project, node):
529 src = self._reqatt(node, 'src')
530 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800531 if not self.IsMirror:
532 # src is project relative;
533 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800534 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700535
536 def _get_remote(self, node):
537 name = node.getAttribute('remote')
538 if not name:
539 return None
540
541 v = self._remotes.get(name)
542 if not v:
543 raise ManifestParseError, \
544 "remote %s not defined in %s" % \
545 (name, self.manifestFile)
546 return v
547
548 def _reqatt(self, node, attname):
549 """
550 reads a required attribute from the node.
551 """
552 v = node.getAttribute(attname)
553 if not v:
554 raise ManifestParseError, \
555 "no %s in <%s> within %s" % \
556 (attname, node.nodeName, self.manifestFile)
557 return v