blob: 9189eec45570d990c3177536dfc8ae5c8551c55d [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()
231 return self._manifest_server
232
233 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800234 def IsMirror(self):
235 return self.manifestProject.config.GetBoolean('repo.mirror')
236
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700237 def _Unload(self):
238 self._loaded = False
239 self._projects = {}
240 self._remotes = {}
241 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800242 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700243 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700244 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700245 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700246
247 def _Load(self):
248 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800249 m = self.manifestProject
250 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700251 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800252 b = b[len(R_HEADS):]
253 self.branch = b
254
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700255 self._ParseManifest(True)
256
257 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
258 if os.path.exists(local):
259 try:
260 real = self.manifestFile
261 self.manifestFile = local
262 self._ParseManifest(False)
263 finally:
264 self.manifestFile = real
265
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800266 if self.IsMirror:
267 self._AddMetaProjectMirror(self.repoProject)
268 self._AddMetaProjectMirror(self.manifestProject)
269
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700270 self._loaded = True
271
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700272 def _ParseManifest(self, is_root_file):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700273 root = xml.dom.minidom.parse(self.manifestFile)
274 if not root or not root.childNodes:
Doug Anderson37282b42011-03-04 11:54:18 -0800275 raise ManifestParseError(
276 "no root node in %s" %
277 self.manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700278
279 config = root.childNodes[0]
280 if config.nodeName != 'manifest':
Doug Anderson37282b42011-03-04 11:54:18 -0800281 raise ManifestParseError(
282 "no <manifest> in %s" %
283 self.manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700284
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700285 for node in config.childNodes:
Shawn O. Pearce03eaf072008-11-20 11:42:22 -0800286 if node.nodeName == 'remove-project':
287 name = self._reqatt(node, 'name')
288 try:
289 del self._projects[name]
290 except KeyError:
Doug Anderson37282b42011-03-04 11:54:18 -0800291 raise ManifestParseError(
292 'project %s not found' %
293 (name))
294
295 # If the manifest removes the hooks project, treat it as if it deleted
296 # the repo-hooks element too.
297 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
298 self._repo_hooks_project = None
Shawn O. Pearce03eaf072008-11-20 11:42:22 -0800299
300 for node in config.childNodes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700301 if node.nodeName == 'remote':
302 remote = self._ParseRemote(node)
303 if self._remotes.get(remote.name):
Doug Anderson37282b42011-03-04 11:54:18 -0800304 raise ManifestParseError(
305 'duplicate remote %s in %s' %
306 (remote.name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700307 self._remotes[remote.name] = remote
308
309 for node in config.childNodes:
310 if node.nodeName == 'default':
311 if self._default is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800312 raise ManifestParseError(
313 'duplicate default in %s' %
314 (self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700315 self._default = self._ParseDefault(node)
316 if self._default is None:
317 self._default = _Default()
318
319 for node in config.childNodes:
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700320 if node.nodeName == 'notice':
321 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800322 raise ManifestParseError(
323 'duplicate notice in %s' %
324 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700325 self._notice = self._ParseNotice(node)
326
327 for node in config.childNodes:
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700328 if node.nodeName == 'manifest-server':
329 url = self._reqatt(node, 'url')
330 if self._manifest_server is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800331 raise ManifestParseError(
332 'duplicate manifest-server in %s' %
333 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700334 self._manifest_server = url
335
336 for node in config.childNodes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700337 if node.nodeName == 'project':
338 project = self._ParseProject(node)
339 if self._projects.get(project.name):
Doug Anderson37282b42011-03-04 11:54:18 -0800340 raise ManifestParseError(
341 'duplicate project %s in %s' %
342 (project.name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700343 self._projects[project.name] = project
344
Doug Anderson37282b42011-03-04 11:54:18 -0800345 for node in config.childNodes:
346 if node.nodeName == 'repo-hooks':
347 # Get the name of the project and the (space-separated) list of enabled.
348 repo_hooks_project = self._reqatt(node, 'in-project')
349 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
350
351 # Only one project can be the hooks project
352 if self._repo_hooks_project is not None:
353 raise ManifestParseError(
354 'duplicate repo-hooks in %s' %
355 (self.manifestFile))
356
357 # Store a reference to the Project.
358 try:
359 self._repo_hooks_project = self._projects[repo_hooks_project]
360 except KeyError:
361 raise ManifestParseError(
362 'project %s not found for repo-hooks' %
363 (repo_hooks_project))
364
365 # Store the enabled hooks in the Project object.
366 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
367
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800368 def _AddMetaProjectMirror(self, m):
369 name = None
370 m_url = m.GetRemote(m.remote.name).url
371 if m_url.endswith('/.git'):
372 raise ManifestParseError, 'refusing to mirror %s' % m_url
373
374 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700375 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800376 if not url.endswith('/'):
377 url += '/'
378 if m_url.startswith(url):
379 remote = self._default.remote
380 name = m_url[len(url):]
381
382 if name is None:
383 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700384 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
385 remote = _XmlRemote('origin', m_url[:s], manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800386 name = m_url[s:]
387
388 if name.endswith('.git'):
389 name = name[:-4]
390
391 if name not in self._projects:
392 m.PreSync()
393 gitdir = os.path.join(self.topdir, '%s.git' % name)
394 project = Project(manifest = self,
395 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700396 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800397 gitdir = gitdir,
398 worktree = None,
399 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700400 revisionExpr = m.revisionExpr,
401 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800402 self._projects[project.name] = project
403
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700404 def _ParseRemote(self, node):
405 """
406 reads a <remote> element from the manifest file
407 """
408 name = self._reqatt(node, 'name')
409 fetch = self._reqatt(node, 'fetch')
410 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800411 if review == '':
412 review = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700413 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
414 return _XmlRemote(name, fetch, manifestUrl, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700415
416 def _ParseDefault(self, node):
417 """
418 reads a <default> element from the manifest file
419 """
420 d = _Default()
421 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700422 d.revisionExpr = node.getAttribute('revision')
423 if d.revisionExpr == '':
424 d.revisionExpr = None
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700425 sync_j = node.getAttribute('sync-j')
426 if sync_j == '' or sync_j is None:
427 d.sync_j = 1
428 else:
429 d.sync_j = int(sync_j)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700430 return d
431
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700432 def _ParseNotice(self, node):
433 """
434 reads a <notice> element from the manifest file
435
436 The <notice> element is distinct from other tags in the XML in that the
437 data is conveyed between the start and end tag (it's not an empty-element
438 tag).
439
440 The white space (carriage returns, indentation) for the notice element is
441 relevant and is parsed in a way that is based on how python docstrings work.
442 In fact, the code is remarkably similar to here:
443 http://www.python.org/dev/peps/pep-0257/
444 """
445 # Get the data out of the node...
446 notice = node.childNodes[0].data
447
448 # Figure out minimum indentation, skipping the first line (the same line
449 # as the <notice> tag)...
450 minIndent = sys.maxint
451 lines = notice.splitlines()
452 for line in lines[1:]:
453 lstrippedLine = line.lstrip()
454 if lstrippedLine:
455 indent = len(line) - len(lstrippedLine)
456 minIndent = min(indent, minIndent)
457
458 # Strip leading / trailing blank lines and also indentation.
459 cleanLines = [lines[0].strip()]
460 for line in lines[1:]:
461 cleanLines.append(line[minIndent:].rstrip())
462
463 # Clear completely blank lines from front and back...
464 while cleanLines and not cleanLines[0]:
465 del cleanLines[0]
466 while cleanLines and not cleanLines[-1]:
467 del cleanLines[-1]
468
469 return '\n'.join(cleanLines)
470
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700471 def _ParseProject(self, node):
472 """
473 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700474 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700475 name = self._reqatt(node, 'name')
476
477 remote = self._get_remote(node)
478 if remote is None:
479 remote = self._default.remote
480 if remote is None:
481 raise ManifestParseError, \
482 "no remote for project %s within %s" % \
483 (name, self.manifestFile)
484
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700485 revisionExpr = node.getAttribute('revision')
486 if not revisionExpr:
487 revisionExpr = self._default.revisionExpr
488 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700489 raise ManifestParseError, \
490 "no revision for project %s within %s" % \
491 (name, self.manifestFile)
492
493 path = node.getAttribute('path')
494 if not path:
495 path = name
496 if path.startswith('/'):
497 raise ManifestParseError, \
498 "project %s path cannot be absolute in %s" % \
499 (name, self.manifestFile)
500
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800501 if self.IsMirror:
502 relpath = None
503 worktree = None
504 gitdir = os.path.join(self.topdir, '%s.git' % name)
505 else:
Anthony Newnamdf14a702011-01-09 17:31:57 -0800506 worktree = os.path.join(self.topdir, path).replace('\\', '/')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800507 gitdir = os.path.join(self.repodir, 'projects/%s.git' % path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700508
509 project = Project(manifest = self,
510 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700511 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700512 gitdir = gitdir,
513 worktree = worktree,
514 relpath = path,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700515 revisionExpr = revisionExpr,
516 revisionId = None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700517
518 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700519 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700520 self._ParseCopyFile(project, n)
521
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700522 return project
523
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700524 def _ParseCopyFile(self, project, node):
525 src = self._reqatt(node, 'src')
526 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800527 if not self.IsMirror:
528 # src is project relative;
529 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800530 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700531
532 def _get_remote(self, node):
533 name = node.getAttribute('remote')
534 if not name:
535 return None
536
537 v = self._remotes.get(name)
538 if not v:
539 raise ManifestParseError, \
540 "remote %s not defined in %s" % \
541 (name, self.manifestFile)
542 return v
543
544 def _reqatt(self, node, attname):
545 """
546 reads a required attribute from the node.
547 """
548 v = node.getAttribute(attname)
549 if not v:
550 raise ManifestParseError, \
551 "no %s in <%s> within %s" % \
552 (attname, node.nodeName, self.manifestFile)
553 return v