blob: ca65e33fac287af6386bd8a6f558429d2983ae76 [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
Anatol Pomazau79770d22012-04-20 14:41:59 -070038 sync_c = False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070039
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070040class _XmlRemote(object):
41 def __init__(self,
42 name,
43 fetch=None,
Conley Owensdb728cd2011-09-26 16:34:01 -070044 manifestUrl=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070045 review=None):
46 self.name = name
47 self.fetchUrl = fetch
Conley Owensdb728cd2011-09-26 16:34:01 -070048 self.manifestUrl = manifestUrl
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070049 self.reviewUrl = review
Conley Owensceea3682011-10-20 10:45:47 -070050 self.resolvedFetchUrl = self._resolveFetchUrl()
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070051
Conley Owensceea3682011-10-20 10:45:47 -070052 def _resolveFetchUrl(self):
53 url = self.fetchUrl.rstrip('/')
Conley Owensdb728cd2011-09-26 16:34:01 -070054 manifestUrl = self.manifestUrl.rstrip('/')
55 # urljoin will get confused if there is no scheme in the base url
56 # ie, if manifestUrl is of the form <hostname:port>
57 if manifestUrl.find(':') != manifestUrl.find('/') - 1:
58 manifestUrl = 'gopher://' + manifestUrl
59 url = urlparse.urljoin(manifestUrl, url)
Conley Owensceea3682011-10-20 10:45:47 -070060 return re.sub(r'^gopher://', '', url)
61
62 def ToRemoteSpec(self, projectName):
Conley Owens9d8f9142011-10-20 14:36:35 -070063 url = self.resolvedFetchUrl.rstrip('/') + '/' + projectName
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070064 return RemoteSpec(self.name, url, self.reviewUrl)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070065
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070066class XmlManifest(object):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070067 """manages the repo configuration file"""
68
69 def __init__(self, repodir):
70 self.repodir = os.path.abspath(repodir)
71 self.topdir = os.path.dirname(self.repodir)
72 self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070073 self.globalConfig = GitConfig.ForUser()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070074
75 self.repoProject = MetaProject(self, 'repo',
76 gitdir = os.path.join(repodir, 'repo/.git'),
77 worktree = os.path.join(repodir, 'repo'))
78
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070079 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -080080 gitdir = os.path.join(repodir, 'manifests.git'),
81 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070082
83 self._Unload()
84
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -070085 def Override(self, name):
86 """Use a different manifest, just for the current instantiation.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070087 """
88 path = os.path.join(self.manifestProject.worktree, name)
89 if not os.path.isfile(path):
90 raise ManifestParseError('manifest %s not found' % name)
91
92 old = self.manifestFile
93 try:
94 self.manifestFile = path
95 self._Unload()
96 self._Load()
97 finally:
98 self.manifestFile = old
99
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700100 def Link(self, name):
101 """Update the repo metadata to use a different manifest.
102 """
103 self.Override(name)
104
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700105 try:
106 if os.path.exists(self.manifestFile):
107 os.remove(self.manifestFile)
108 os.symlink('manifests/%s' % name, self.manifestFile)
109 except OSError, e:
110 raise ManifestParseError('cannot link manifest %s' % name)
111
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800112 def _RemoteToXml(self, r, doc, root):
113 e = doc.createElement('remote')
114 root.appendChild(e)
115 e.setAttribute('name', r.name)
116 e.setAttribute('fetch', r.fetchUrl)
117 if r.reviewUrl is not None:
118 e.setAttribute('review', r.reviewUrl)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800119
120 def Save(self, fd, peg_rev=False):
121 """Write the current manifest out to the given file descriptor.
122 """
Colin Cross5acde752012-03-28 20:15:45 -0700123 mp = self.manifestProject
124
125 groups = mp.config.GetString('manifest.groups')
Colin Crossc39864f2012-04-23 13:41:58 -0700126 if not groups:
Conley Owens971de8e2012-04-16 10:36:08 -0700127 groups = 'default'
128 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Colin Cross5acde752012-03-28 20:15:45 -0700129
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800130 doc = xml.dom.minidom.Document()
131 root = doc.createElement('manifest')
132 doc.appendChild(root)
133
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700134 # Save out the notice. There's a little bit of work here to give it the
135 # right whitespace, which assumes that the notice is automatically indented
136 # by 4 by minidom.
137 if self.notice:
138 notice_element = root.appendChild(doc.createElement('notice'))
139 notice_lines = self.notice.splitlines()
140 indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:]
141 notice_element.appendChild(doc.createTextNode(indented_notice))
142
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800143 d = self.default
144 sort_remotes = list(self.remotes.keys())
145 sort_remotes.sort()
146
147 for r in sort_remotes:
148 self._RemoteToXml(self.remotes[r], doc, root)
149 if self.remotes:
150 root.appendChild(doc.createTextNode(''))
151
152 have_default = False
153 e = doc.createElement('default')
154 if d.remote:
155 have_default = True
156 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700157 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800158 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700159 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700160 if d.sync_j > 1:
161 have_default = True
162 e.setAttribute('sync-j', '%d' % d.sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700163 if d.sync_c:
164 have_default = True
165 e.setAttribute('sync-c', 'true')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800166 if have_default:
167 root.appendChild(e)
168 root.appendChild(doc.createTextNode(''))
169
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700170 if self._manifest_server:
171 e = doc.createElement('manifest-server')
172 e.setAttribute('url', self._manifest_server)
173 root.appendChild(e)
174 root.appendChild(doc.createTextNode(''))
175
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800176 sort_projects = list(self.projects.keys())
177 sort_projects.sort()
178
179 for p in sort_projects:
180 p = self.projects[p]
Colin Cross5acde752012-03-28 20:15:45 -0700181
182 if not p.MatchesGroups(groups):
183 continue
184
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800185 e = doc.createElement('project')
186 root.appendChild(e)
187 e.setAttribute('name', p.name)
188 if p.relpath != p.name:
189 e.setAttribute('path', p.relpath)
190 if not d.remote or p.remote.name != d.remote.name:
191 e.setAttribute('remote', p.remote.name)
192 if peg_rev:
193 if self.IsMirror:
194 e.setAttribute('revision',
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700195 p.bare_git.rev_parse(p.revisionExpr + '^0'))
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800196 else:
197 e.setAttribute('revision',
198 p.work_git.rev_parse(HEAD + '^0'))
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700199 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
200 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800201
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800202 for c in p.copyfiles:
203 ce = doc.createElement('copyfile')
204 ce.setAttribute('src', c.src)
205 ce.setAttribute('dest', c.dest)
206 e.appendChild(ce)
207
Conley Owens971de8e2012-04-16 10:36:08 -0700208 egroups = [g for g in p.groups if g != 'default']
209 if egroups:
210 e.setAttribute('groups', ','.join(egroups))
Colin Cross5acde752012-03-28 20:15:45 -0700211
James W. Mills24c13082012-04-12 15:04:13 -0500212 for a in p.annotations:
213 if a.keep == "true":
214 ae = doc.createElement('annotation')
215 ae.setAttribute('name', a.name)
216 ae.setAttribute('value', a.value)
217 e.appendChild(ae)
218
Anatol Pomazau79770d22012-04-20 14:41:59 -0700219 if p.sync_c:
220 e.setAttribute('sync-c', 'true')
221
Doug Anderson37282b42011-03-04 11:54:18 -0800222 if self._repo_hooks_project:
223 root.appendChild(doc.createTextNode(''))
224 e = doc.createElement('repo-hooks')
225 e.setAttribute('in-project', self._repo_hooks_project.name)
226 e.setAttribute('enabled-list',
227 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
228 root.appendChild(e)
229
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800230 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
231
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700232 @property
233 def projects(self):
234 self._Load()
235 return self._projects
236
237 @property
238 def remotes(self):
239 self._Load()
240 return self._remotes
241
242 @property
243 def default(self):
244 self._Load()
245 return self._default
246
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800247 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800248 def repo_hooks_project(self):
249 self._Load()
250 return self._repo_hooks_project
251
252 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700253 def notice(self):
254 self._Load()
255 return self._notice
256
257 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700258 def manifest_server(self):
259 self._Load()
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800260 return self._manifest_server
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700261
262 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800263 def IsMirror(self):
264 return self.manifestProject.config.GetBoolean('repo.mirror')
265
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700266 def _Unload(self):
267 self._loaded = False
268 self._projects = {}
269 self._remotes = {}
270 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800271 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700272 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700273 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700274 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700275
276 def _Load(self):
277 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800278 m = self.manifestProject
279 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700280 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800281 b = b[len(R_HEADS):]
282 self.branch = b
283
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700284 self._ParseManifest(True)
285
286 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
287 if os.path.exists(local):
288 try:
289 real = self.manifestFile
290 self.manifestFile = local
291 self._ParseManifest(False)
292 finally:
293 self.manifestFile = real
294
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800295 if self.IsMirror:
296 self._AddMetaProjectMirror(self.repoProject)
297 self._AddMetaProjectMirror(self.manifestProject)
298
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700299 self._loaded = True
300
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700301 def _ParseManifest(self, is_root_file):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700302 root = xml.dom.minidom.parse(self.manifestFile)
303 if not root or not root.childNodes:
Doug Anderson37282b42011-03-04 11:54:18 -0800304 raise ManifestParseError(
305 "no root node in %s" %
306 self.manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700307
308 config = root.childNodes[0]
309 if config.nodeName != 'manifest':
Doug Anderson37282b42011-03-04 11:54:18 -0800310 raise ManifestParseError(
311 "no <manifest> in %s" %
312 self.manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700313
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700314 for node in config.childNodes:
Shawn O. Pearce03eaf072008-11-20 11:42:22 -0800315 if node.nodeName == 'remove-project':
316 name = self._reqatt(node, 'name')
317 try:
318 del self._projects[name]
319 except KeyError:
Doug Anderson37282b42011-03-04 11:54:18 -0800320 raise ManifestParseError(
321 'project %s not found' %
322 (name))
323
324 # If the manifest removes the hooks project, treat it as if it deleted
325 # the repo-hooks element too.
326 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
327 self._repo_hooks_project = None
Shawn O. Pearce03eaf072008-11-20 11:42:22 -0800328
329 for node in config.childNodes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700330 if node.nodeName == 'remote':
331 remote = self._ParseRemote(node)
332 if self._remotes.get(remote.name):
Doug Anderson37282b42011-03-04 11:54:18 -0800333 raise ManifestParseError(
334 'duplicate remote %s in %s' %
335 (remote.name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700336 self._remotes[remote.name] = remote
337
338 for node in config.childNodes:
339 if node.nodeName == 'default':
340 if self._default is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800341 raise ManifestParseError(
342 'duplicate default in %s' %
343 (self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700344 self._default = self._ParseDefault(node)
345 if self._default is None:
346 self._default = _Default()
347
348 for node in config.childNodes:
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700349 if node.nodeName == 'notice':
350 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800351 raise ManifestParseError(
352 'duplicate notice in %s' %
353 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700354 self._notice = self._ParseNotice(node)
355
356 for node in config.childNodes:
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700357 if node.nodeName == 'manifest-server':
358 url = self._reqatt(node, 'url')
359 if self._manifest_server is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800360 raise ManifestParseError(
361 'duplicate manifest-server in %s' %
362 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700363 self._manifest_server = url
364
365 for node in config.childNodes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700366 if node.nodeName == 'project':
367 project = self._ParseProject(node)
368 if self._projects.get(project.name):
Doug Anderson37282b42011-03-04 11:54:18 -0800369 raise ManifestParseError(
370 'duplicate project %s in %s' %
371 (project.name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700372 self._projects[project.name] = project
373
Doug Anderson37282b42011-03-04 11:54:18 -0800374 for node in config.childNodes:
375 if node.nodeName == 'repo-hooks':
376 # Get the name of the project and the (space-separated) list of enabled.
377 repo_hooks_project = self._reqatt(node, 'in-project')
378 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
379
380 # Only one project can be the hooks project
381 if self._repo_hooks_project is not None:
382 raise ManifestParseError(
383 'duplicate repo-hooks in %s' %
384 (self.manifestFile))
385
386 # Store a reference to the Project.
387 try:
388 self._repo_hooks_project = self._projects[repo_hooks_project]
389 except KeyError:
390 raise ManifestParseError(
391 'project %s not found for repo-hooks' %
392 (repo_hooks_project))
393
394 # Store the enabled hooks in the Project object.
395 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
396
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800397 def _AddMetaProjectMirror(self, m):
398 name = None
399 m_url = m.GetRemote(m.remote.name).url
400 if m_url.endswith('/.git'):
401 raise ManifestParseError, 'refusing to mirror %s' % m_url
402
403 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700404 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800405 if not url.endswith('/'):
406 url += '/'
407 if m_url.startswith(url):
408 remote = self._default.remote
409 name = m_url[len(url):]
410
411 if name is None:
412 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700413 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
414 remote = _XmlRemote('origin', m_url[:s], manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800415 name = m_url[s:]
416
417 if name.endswith('.git'):
418 name = name[:-4]
419
420 if name not in self._projects:
421 m.PreSync()
422 gitdir = os.path.join(self.topdir, '%s.git' % name)
423 project = Project(manifest = self,
424 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700425 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800426 gitdir = gitdir,
427 worktree = None,
428 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700429 revisionExpr = m.revisionExpr,
430 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800431 self._projects[project.name] = project
432
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700433 def _ParseRemote(self, node):
434 """
435 reads a <remote> element from the manifest file
436 """
437 name = self._reqatt(node, 'name')
438 fetch = self._reqatt(node, 'fetch')
439 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800440 if review == '':
441 review = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700442 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
443 return _XmlRemote(name, fetch, manifestUrl, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700444
445 def _ParseDefault(self, node):
446 """
447 reads a <default> element from the manifest file
448 """
449 d = _Default()
450 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700451 d.revisionExpr = node.getAttribute('revision')
452 if d.revisionExpr == '':
453 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700454
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700455 sync_j = node.getAttribute('sync-j')
456 if sync_j == '' or sync_j is None:
457 d.sync_j = 1
458 else:
459 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700460
461 sync_c = node.getAttribute('sync-c')
462 if not sync_c:
463 d.sync_c = False
464 else:
465 d.sync_c = sync_c.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700466 return d
467
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700468 def _ParseNotice(self, node):
469 """
470 reads a <notice> element from the manifest file
471
472 The <notice> element is distinct from other tags in the XML in that the
473 data is conveyed between the start and end tag (it's not an empty-element
474 tag).
475
476 The white space (carriage returns, indentation) for the notice element is
477 relevant and is parsed in a way that is based on how python docstrings work.
478 In fact, the code is remarkably similar to here:
479 http://www.python.org/dev/peps/pep-0257/
480 """
481 # Get the data out of the node...
482 notice = node.childNodes[0].data
483
484 # Figure out minimum indentation, skipping the first line (the same line
485 # as the <notice> tag)...
486 minIndent = sys.maxint
487 lines = notice.splitlines()
488 for line in lines[1:]:
489 lstrippedLine = line.lstrip()
490 if lstrippedLine:
491 indent = len(line) - len(lstrippedLine)
492 minIndent = min(indent, minIndent)
493
494 # Strip leading / trailing blank lines and also indentation.
495 cleanLines = [lines[0].strip()]
496 for line in lines[1:]:
497 cleanLines.append(line[minIndent:].rstrip())
498
499 # Clear completely blank lines from front and back...
500 while cleanLines and not cleanLines[0]:
501 del cleanLines[0]
502 while cleanLines and not cleanLines[-1]:
503 del cleanLines[-1]
504
505 return '\n'.join(cleanLines)
506
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700507 def _ParseProject(self, node):
508 """
509 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700510 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700511 name = self._reqatt(node, 'name')
512
513 remote = self._get_remote(node)
514 if remote is None:
515 remote = self._default.remote
516 if remote is None:
517 raise ManifestParseError, \
518 "no remote for project %s within %s" % \
519 (name, self.manifestFile)
520
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700521 revisionExpr = node.getAttribute('revision')
522 if not revisionExpr:
523 revisionExpr = self._default.revisionExpr
524 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700525 raise ManifestParseError, \
526 "no revision for project %s within %s" % \
527 (name, self.manifestFile)
528
529 path = node.getAttribute('path')
530 if not path:
531 path = name
532 if path.startswith('/'):
533 raise ManifestParseError, \
534 "project %s path cannot be absolute in %s" % \
535 (name, self.manifestFile)
536
Mike Pontillod3153822012-02-28 11:53:24 -0800537 rebase = node.getAttribute('rebase')
538 if not rebase:
539 rebase = True
540 else:
541 rebase = rebase.lower() in ("yes", "true", "1")
542
Anatol Pomazau79770d22012-04-20 14:41:59 -0700543 sync_c = node.getAttribute('sync-c')
544 if not sync_c:
545 sync_c = False
546 else:
547 sync_c = sync_c.lower() in ("yes", "true", "1")
548
Conley Owens971de8e2012-04-16 10:36:08 -0700549 groups = ''
550 if node.hasAttribute('groups'):
551 groups = node.getAttribute('groups')
552 groups = [x for x in re.split('[,\s]+', groups) if x]
553 if 'default' not in groups:
554 groups.append('default')
Colin Cross5acde752012-03-28 20:15:45 -0700555
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800556 if self.IsMirror:
557 relpath = None
558 worktree = None
559 gitdir = os.path.join(self.topdir, '%s.git' % name)
560 else:
Anthony Newnamdf14a702011-01-09 17:31:57 -0800561 worktree = os.path.join(self.topdir, path).replace('\\', '/')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800562 gitdir = os.path.join(self.repodir, 'projects/%s.git' % path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700563
564 project = Project(manifest = self,
565 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700566 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700567 gitdir = gitdir,
568 worktree = worktree,
569 relpath = path,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700570 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800571 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700572 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700573 groups = groups,
574 sync_c = sync_c)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700575
576 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700577 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700578 self._ParseCopyFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500579 if n.nodeName == 'annotation':
580 self._ParseAnnotation(project, n)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700581
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700582 return project
583
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700584 def _ParseCopyFile(self, project, node):
585 src = self._reqatt(node, 'src')
586 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800587 if not self.IsMirror:
588 # src is project relative;
589 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800590 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700591
James W. Mills24c13082012-04-12 15:04:13 -0500592 def _ParseAnnotation(self, project, node):
593 name = self._reqatt(node, 'name')
594 value = self._reqatt(node, 'value')
595 try:
596 keep = self._reqatt(node, 'keep').lower()
597 except ManifestParseError:
598 keep = "true"
599 if keep != "true" and keep != "false":
600 raise ManifestParseError, "optional \"keep\" attribute must be \"true\" or \"false\""
601 project.AddAnnotation(name, value, keep)
602
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700603 def _get_remote(self, node):
604 name = node.getAttribute('remote')
605 if not name:
606 return None
607
608 v = self._remotes.get(name)
609 if not v:
610 raise ManifestParseError, \
611 "remote %s not defined in %s" % \
612 (name, self.manifestFile)
613 return v
614
615 def _reqatt(self, node, attname):
616 """
617 reads a required attribute from the node.
618 """
619 v = node.getAttribute(attname)
620 if not v:
621 raise ManifestParseError, \
622 "no %s in <%s> within %s" % \
623 (attname, node.nodeName, self.manifestFile)
624 return v