blob: 5ffc49e9505b18aded1b612ed918719cb457f4ed [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 """
Colin Cross5acde752012-03-28 20:15:45 -0700122 mp = self.manifestProject
123
124 groups = mp.config.GetString('manifest.groups')
Conley Owens971de8e2012-04-16 10:36:08 -0700125 if groups is None:
126 groups = 'default'
127 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Colin Cross5acde752012-03-28 20:15:45 -0700128
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800129 doc = xml.dom.minidom.Document()
130 root = doc.createElement('manifest')
131 doc.appendChild(root)
132
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700133 # Save out the notice. There's a little bit of work here to give it the
134 # right whitespace, which assumes that the notice is automatically indented
135 # by 4 by minidom.
136 if self.notice:
137 notice_element = root.appendChild(doc.createElement('notice'))
138 notice_lines = self.notice.splitlines()
139 indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:]
140 notice_element.appendChild(doc.createTextNode(indented_notice))
141
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800142 d = self.default
143 sort_remotes = list(self.remotes.keys())
144 sort_remotes.sort()
145
146 for r in sort_remotes:
147 self._RemoteToXml(self.remotes[r], doc, root)
148 if self.remotes:
149 root.appendChild(doc.createTextNode(''))
150
151 have_default = False
152 e = doc.createElement('default')
153 if d.remote:
154 have_default = True
155 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700156 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800157 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700158 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700159 if d.sync_j > 1:
160 have_default = True
161 e.setAttribute('sync-j', '%d' % d.sync_j)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800162 if have_default:
163 root.appendChild(e)
164 root.appendChild(doc.createTextNode(''))
165
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700166 if self._manifest_server:
167 e = doc.createElement('manifest-server')
168 e.setAttribute('url', self._manifest_server)
169 root.appendChild(e)
170 root.appendChild(doc.createTextNode(''))
171
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800172 sort_projects = list(self.projects.keys())
173 sort_projects.sort()
174
175 for p in sort_projects:
176 p = self.projects[p]
Colin Cross5acde752012-03-28 20:15:45 -0700177
178 if not p.MatchesGroups(groups):
179 continue
180
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800181 e = doc.createElement('project')
182 root.appendChild(e)
183 e.setAttribute('name', p.name)
184 if p.relpath != p.name:
185 e.setAttribute('path', p.relpath)
186 if not d.remote or p.remote.name != d.remote.name:
187 e.setAttribute('remote', p.remote.name)
188 if peg_rev:
189 if self.IsMirror:
190 e.setAttribute('revision',
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700191 p.bare_git.rev_parse(p.revisionExpr + '^0'))
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800192 else:
193 e.setAttribute('revision',
194 p.work_git.rev_parse(HEAD + '^0'))
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700195 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
196 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800197
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800198 for c in p.copyfiles:
199 ce = doc.createElement('copyfile')
200 ce.setAttribute('src', c.src)
201 ce.setAttribute('dest', c.dest)
202 e.appendChild(ce)
203
Conley Owens971de8e2012-04-16 10:36:08 -0700204 egroups = [g for g in p.groups if g != 'default']
205 if egroups:
206 e.setAttribute('groups', ','.join(egroups))
Colin Cross5acde752012-03-28 20:15:45 -0700207
James W. Mills24c13082012-04-12 15:04:13 -0500208 for a in p.annotations:
209 if a.keep == "true":
210 ae = doc.createElement('annotation')
211 ae.setAttribute('name', a.name)
212 ae.setAttribute('value', a.value)
213 e.appendChild(ae)
214
Doug Anderson37282b42011-03-04 11:54:18 -0800215 if self._repo_hooks_project:
216 root.appendChild(doc.createTextNode(''))
217 e = doc.createElement('repo-hooks')
218 e.setAttribute('in-project', self._repo_hooks_project.name)
219 e.setAttribute('enabled-list',
220 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
221 root.appendChild(e)
222
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800223 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
224
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700225 @property
226 def projects(self):
227 self._Load()
228 return self._projects
229
230 @property
231 def remotes(self):
232 self._Load()
233 return self._remotes
234
235 @property
236 def default(self):
237 self._Load()
238 return self._default
239
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800240 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800241 def repo_hooks_project(self):
242 self._Load()
243 return self._repo_hooks_project
244
245 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700246 def notice(self):
247 self._Load()
248 return self._notice
249
250 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700251 def manifest_server(self):
252 self._Load()
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800253 return self._manifest_server
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700254
255 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800256 def IsMirror(self):
257 return self.manifestProject.config.GetBoolean('repo.mirror')
258
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700259 def _Unload(self):
260 self._loaded = False
261 self._projects = {}
262 self._remotes = {}
263 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800264 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700265 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700266 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700267 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700268
269 def _Load(self):
270 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800271 m = self.manifestProject
272 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700273 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800274 b = b[len(R_HEADS):]
275 self.branch = b
276
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700277 self._ParseManifest(True)
278
279 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
280 if os.path.exists(local):
281 try:
282 real = self.manifestFile
283 self.manifestFile = local
284 self._ParseManifest(False)
285 finally:
286 self.manifestFile = real
287
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800288 if self.IsMirror:
289 self._AddMetaProjectMirror(self.repoProject)
290 self._AddMetaProjectMirror(self.manifestProject)
291
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700292 self._loaded = True
293
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700294 def _ParseManifest(self, is_root_file):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700295 root = xml.dom.minidom.parse(self.manifestFile)
296 if not root or not root.childNodes:
Doug Anderson37282b42011-03-04 11:54:18 -0800297 raise ManifestParseError(
298 "no root node in %s" %
299 self.manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700300
301 config = root.childNodes[0]
302 if config.nodeName != 'manifest':
Doug Anderson37282b42011-03-04 11:54:18 -0800303 raise ManifestParseError(
304 "no <manifest> in %s" %
305 self.manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700306
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700307 for node in config.childNodes:
Shawn O. Pearce03eaf072008-11-20 11:42:22 -0800308 if node.nodeName == 'remove-project':
309 name = self._reqatt(node, 'name')
310 try:
311 del self._projects[name]
312 except KeyError:
Doug Anderson37282b42011-03-04 11:54:18 -0800313 raise ManifestParseError(
314 'project %s not found' %
315 (name))
316
317 # If the manifest removes the hooks project, treat it as if it deleted
318 # the repo-hooks element too.
319 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
320 self._repo_hooks_project = None
Shawn O. Pearce03eaf072008-11-20 11:42:22 -0800321
322 for node in config.childNodes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700323 if node.nodeName == 'remote':
324 remote = self._ParseRemote(node)
325 if self._remotes.get(remote.name):
Doug Anderson37282b42011-03-04 11:54:18 -0800326 raise ManifestParseError(
327 'duplicate remote %s in %s' %
328 (remote.name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700329 self._remotes[remote.name] = remote
330
331 for node in config.childNodes:
332 if node.nodeName == 'default':
333 if self._default is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800334 raise ManifestParseError(
335 'duplicate default in %s' %
336 (self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700337 self._default = self._ParseDefault(node)
338 if self._default is None:
339 self._default = _Default()
340
341 for node in config.childNodes:
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700342 if node.nodeName == 'notice':
343 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800344 raise ManifestParseError(
345 'duplicate notice in %s' %
346 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700347 self._notice = self._ParseNotice(node)
348
349 for node in config.childNodes:
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700350 if node.nodeName == 'manifest-server':
351 url = self._reqatt(node, 'url')
352 if self._manifest_server is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800353 raise ManifestParseError(
354 'duplicate manifest-server in %s' %
355 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700356 self._manifest_server = url
357
358 for node in config.childNodes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700359 if node.nodeName == 'project':
360 project = self._ParseProject(node)
361 if self._projects.get(project.name):
Doug Anderson37282b42011-03-04 11:54:18 -0800362 raise ManifestParseError(
363 'duplicate project %s in %s' %
364 (project.name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700365 self._projects[project.name] = project
366
Doug Anderson37282b42011-03-04 11:54:18 -0800367 for node in config.childNodes:
368 if node.nodeName == 'repo-hooks':
369 # Get the name of the project and the (space-separated) list of enabled.
370 repo_hooks_project = self._reqatt(node, 'in-project')
371 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
372
373 # Only one project can be the hooks project
374 if self._repo_hooks_project is not None:
375 raise ManifestParseError(
376 'duplicate repo-hooks in %s' %
377 (self.manifestFile))
378
379 # Store a reference to the Project.
380 try:
381 self._repo_hooks_project = self._projects[repo_hooks_project]
382 except KeyError:
383 raise ManifestParseError(
384 'project %s not found for repo-hooks' %
385 (repo_hooks_project))
386
387 # Store the enabled hooks in the Project object.
388 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
389
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800390 def _AddMetaProjectMirror(self, m):
391 name = None
392 m_url = m.GetRemote(m.remote.name).url
393 if m_url.endswith('/.git'):
394 raise ManifestParseError, 'refusing to mirror %s' % m_url
395
396 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700397 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800398 if not url.endswith('/'):
399 url += '/'
400 if m_url.startswith(url):
401 remote = self._default.remote
402 name = m_url[len(url):]
403
404 if name is None:
405 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700406 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
407 remote = _XmlRemote('origin', m_url[:s], manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800408 name = m_url[s:]
409
410 if name.endswith('.git'):
411 name = name[:-4]
412
413 if name not in self._projects:
414 m.PreSync()
415 gitdir = os.path.join(self.topdir, '%s.git' % name)
416 project = Project(manifest = self,
417 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700418 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800419 gitdir = gitdir,
420 worktree = None,
421 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700422 revisionExpr = m.revisionExpr,
423 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800424 self._projects[project.name] = project
425
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700426 def _ParseRemote(self, node):
427 """
428 reads a <remote> element from the manifest file
429 """
430 name = self._reqatt(node, 'name')
431 fetch = self._reqatt(node, 'fetch')
432 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800433 if review == '':
434 review = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700435 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
436 return _XmlRemote(name, fetch, manifestUrl, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700437
438 def _ParseDefault(self, node):
439 """
440 reads a <default> element from the manifest file
441 """
442 d = _Default()
443 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700444 d.revisionExpr = node.getAttribute('revision')
445 if d.revisionExpr == '':
446 d.revisionExpr = None
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700447 sync_j = node.getAttribute('sync-j')
448 if sync_j == '' or sync_j is None:
449 d.sync_j = 1
450 else:
451 d.sync_j = int(sync_j)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700452 return d
453
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700454 def _ParseNotice(self, node):
455 """
456 reads a <notice> element from the manifest file
457
458 The <notice> element is distinct from other tags in the XML in that the
459 data is conveyed between the start and end tag (it's not an empty-element
460 tag).
461
462 The white space (carriage returns, indentation) for the notice element is
463 relevant and is parsed in a way that is based on how python docstrings work.
464 In fact, the code is remarkably similar to here:
465 http://www.python.org/dev/peps/pep-0257/
466 """
467 # Get the data out of the node...
468 notice = node.childNodes[0].data
469
470 # Figure out minimum indentation, skipping the first line (the same line
471 # as the <notice> tag)...
472 minIndent = sys.maxint
473 lines = notice.splitlines()
474 for line in lines[1:]:
475 lstrippedLine = line.lstrip()
476 if lstrippedLine:
477 indent = len(line) - len(lstrippedLine)
478 minIndent = min(indent, minIndent)
479
480 # Strip leading / trailing blank lines and also indentation.
481 cleanLines = [lines[0].strip()]
482 for line in lines[1:]:
483 cleanLines.append(line[minIndent:].rstrip())
484
485 # Clear completely blank lines from front and back...
486 while cleanLines and not cleanLines[0]:
487 del cleanLines[0]
488 while cleanLines and not cleanLines[-1]:
489 del cleanLines[-1]
490
491 return '\n'.join(cleanLines)
492
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700493 def _ParseProject(self, node):
494 """
495 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700496 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700497 name = self._reqatt(node, 'name')
498
499 remote = self._get_remote(node)
500 if remote is None:
501 remote = self._default.remote
502 if remote is None:
503 raise ManifestParseError, \
504 "no remote for project %s within %s" % \
505 (name, self.manifestFile)
506
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700507 revisionExpr = node.getAttribute('revision')
508 if not revisionExpr:
509 revisionExpr = self._default.revisionExpr
510 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700511 raise ManifestParseError, \
512 "no revision for project %s within %s" % \
513 (name, self.manifestFile)
514
515 path = node.getAttribute('path')
516 if not path:
517 path = name
518 if path.startswith('/'):
519 raise ManifestParseError, \
520 "project %s path cannot be absolute in %s" % \
521 (name, self.manifestFile)
522
Mike Pontillod3153822012-02-28 11:53:24 -0800523 rebase = node.getAttribute('rebase')
524 if not rebase:
525 rebase = True
526 else:
527 rebase = rebase.lower() in ("yes", "true", "1")
528
Conley Owens971de8e2012-04-16 10:36:08 -0700529 groups = ''
530 if node.hasAttribute('groups'):
531 groups = node.getAttribute('groups')
532 groups = [x for x in re.split('[,\s]+', groups) if x]
533 if 'default' not in groups:
534 groups.append('default')
Colin Cross5acde752012-03-28 20:15:45 -0700535
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800536 if self.IsMirror:
537 relpath = None
538 worktree = None
539 gitdir = os.path.join(self.topdir, '%s.git' % name)
540 else:
Anthony Newnamdf14a702011-01-09 17:31:57 -0800541 worktree = os.path.join(self.topdir, path).replace('\\', '/')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800542 gitdir = os.path.join(self.repodir, 'projects/%s.git' % path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700543
544 project = Project(manifest = self,
545 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700546 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700547 gitdir = gitdir,
548 worktree = worktree,
549 relpath = path,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700550 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800551 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700552 rebase = rebase,
553 groups = groups)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700554
555 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700556 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700557 self._ParseCopyFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500558 if n.nodeName == 'annotation':
559 self._ParseAnnotation(project, n)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700560
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700561 return project
562
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700563 def _ParseCopyFile(self, project, node):
564 src = self._reqatt(node, 'src')
565 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800566 if not self.IsMirror:
567 # src is project relative;
568 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800569 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700570
James W. Mills24c13082012-04-12 15:04:13 -0500571 def _ParseAnnotation(self, project, node):
572 name = self._reqatt(node, 'name')
573 value = self._reqatt(node, 'value')
574 try:
575 keep = self._reqatt(node, 'keep').lower()
576 except ManifestParseError:
577 keep = "true"
578 if keep != "true" and keep != "false":
579 raise ManifestParseError, "optional \"keep\" attribute must be \"true\" or \"false\""
580 project.AddAnnotation(name, value, keep)
581
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700582 def _get_remote(self, node):
583 name = node.getAttribute('remote')
584 if not name:
585 return None
586
587 v = self._remotes.get(name)
588 if not v:
589 raise ManifestParseError, \
590 "remote %s not defined in %s" % \
591 (name, self.manifestFile)
592 return v
593
594 def _reqatt(self, node, attname):
595 """
596 reads a required attribute from the node.
597 """
598 v = node.getAttribute(attname)
599 if not v:
600 raise ManifestParseError, \
601 "no %s in <%s> within %s" % \
602 (attname, node.nodeName, self.manifestFile)
603 return v