blob: 2927fd1c00948992096b1351d10a0099b3a195e9 [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
Brian Harring26448742011-04-28 05:04:41 -0700301 def _ParseManifestObject(self, path):
302 root = xml.dom.minidom.parse(path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700303 if not root or not root.childNodes:
Brian Harring26448742011-04-28 05:04:41 -0700304 raise ManifestParseError("no root node in %s" % (path,))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700305
306 config = root.childNodes[0]
307 if config.nodeName != 'manifest':
Brian Harring26448742011-04-28 05:04:41 -0700308 raise ManifestParseError("no <manifest> in %s" % (path,))
309
310 return config
311
312 def _ParseManifest(self, is_root_file):
313 config = self._ParseManifestObject(self.manifestFile)
314
315 for node in config.childNodes:
316 if node.nodeName == 'include':
317 name = self._reqatt(node, 'name')
318 fp = os.path.join(self.manifestProject.worktree, name)
319 if not os.path.isfile(fp):
320 raise ManifestParseError, \
321 "include %s doesn't exist or isn't a file" % \
322 (name,)
323 try:
324 subconfig = self._ParseManifestObject(fp)
325 # should isolate this to the exact exception, but that's
326 # tricky. actual parsing implementation may vary.
327 except (KeyboardInterrupt, RuntimeError, SystemExit):
328 raise
329 except Exception, e:
330 raise ManifestParseError(
331 "failed parsing included manifest %s: %s", (name, e))
332
333 for sub_node in subconfig.childNodes:
334 config.appendChild(sub_node.cloneNode(True))
335
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700336
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700337 for node in config.childNodes:
Shawn O. Pearce03eaf072008-11-20 11:42:22 -0800338 if node.nodeName == 'remove-project':
339 name = self._reqatt(node, 'name')
340 try:
341 del self._projects[name]
342 except KeyError:
Doug Anderson37282b42011-03-04 11:54:18 -0800343 raise ManifestParseError(
344 'project %s not found' %
345 (name))
346
347 # If the manifest removes the hooks project, treat it as if it deleted
348 # the repo-hooks element too.
349 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
350 self._repo_hooks_project = None
Shawn O. Pearce03eaf072008-11-20 11:42:22 -0800351
352 for node in config.childNodes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700353 if node.nodeName == 'remote':
354 remote = self._ParseRemote(node)
355 if self._remotes.get(remote.name):
Doug Anderson37282b42011-03-04 11:54:18 -0800356 raise ManifestParseError(
357 'duplicate remote %s in %s' %
358 (remote.name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700359 self._remotes[remote.name] = remote
360
361 for node in config.childNodes:
362 if node.nodeName == 'default':
363 if self._default is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800364 raise ManifestParseError(
365 'duplicate default in %s' %
366 (self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700367 self._default = self._ParseDefault(node)
368 if self._default is None:
369 self._default = _Default()
370
371 for node in config.childNodes:
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700372 if node.nodeName == 'notice':
373 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800374 raise ManifestParseError(
375 'duplicate notice in %s' %
376 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700377 self._notice = self._ParseNotice(node)
378
379 for node in config.childNodes:
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700380 if node.nodeName == 'manifest-server':
381 url = self._reqatt(node, 'url')
382 if self._manifest_server is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800383 raise ManifestParseError(
384 'duplicate manifest-server in %s' %
385 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700386 self._manifest_server = url
387
388 for node in config.childNodes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700389 if node.nodeName == 'project':
390 project = self._ParseProject(node)
391 if self._projects.get(project.name):
Doug Anderson37282b42011-03-04 11:54:18 -0800392 raise ManifestParseError(
393 'duplicate project %s in %s' %
394 (project.name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700395 self._projects[project.name] = project
396
Doug Anderson37282b42011-03-04 11:54:18 -0800397 for node in config.childNodes:
398 if node.nodeName == 'repo-hooks':
399 # Get the name of the project and the (space-separated) list of enabled.
400 repo_hooks_project = self._reqatt(node, 'in-project')
401 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
402
403 # Only one project can be the hooks project
404 if self._repo_hooks_project is not None:
405 raise ManifestParseError(
406 'duplicate repo-hooks in %s' %
407 (self.manifestFile))
408
409 # Store a reference to the Project.
410 try:
411 self._repo_hooks_project = self._projects[repo_hooks_project]
412 except KeyError:
413 raise ManifestParseError(
414 'project %s not found for repo-hooks' %
415 (repo_hooks_project))
416
417 # Store the enabled hooks in the Project object.
418 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
419
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800420 def _AddMetaProjectMirror(self, m):
421 name = None
422 m_url = m.GetRemote(m.remote.name).url
423 if m_url.endswith('/.git'):
424 raise ManifestParseError, 'refusing to mirror %s' % m_url
425
426 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700427 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800428 if not url.endswith('/'):
429 url += '/'
430 if m_url.startswith(url):
431 remote = self._default.remote
432 name = m_url[len(url):]
433
434 if name is None:
435 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700436 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
437 remote = _XmlRemote('origin', m_url[:s], manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800438 name = m_url[s:]
439
440 if name.endswith('.git'):
441 name = name[:-4]
442
443 if name not in self._projects:
444 m.PreSync()
445 gitdir = os.path.join(self.topdir, '%s.git' % name)
446 project = Project(manifest = self,
447 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700448 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800449 gitdir = gitdir,
450 worktree = None,
451 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700452 revisionExpr = m.revisionExpr,
453 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800454 self._projects[project.name] = project
455
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700456 def _ParseRemote(self, node):
457 """
458 reads a <remote> element from the manifest file
459 """
460 name = self._reqatt(node, 'name')
461 fetch = self._reqatt(node, 'fetch')
462 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800463 if review == '':
464 review = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700465 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
466 return _XmlRemote(name, fetch, manifestUrl, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700467
468 def _ParseDefault(self, node):
469 """
470 reads a <default> element from the manifest file
471 """
472 d = _Default()
473 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700474 d.revisionExpr = node.getAttribute('revision')
475 if d.revisionExpr == '':
476 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700477
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700478 sync_j = node.getAttribute('sync-j')
479 if sync_j == '' or sync_j is None:
480 d.sync_j = 1
481 else:
482 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700483
484 sync_c = node.getAttribute('sync-c')
485 if not sync_c:
486 d.sync_c = False
487 else:
488 d.sync_c = sync_c.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700489 return d
490
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700491 def _ParseNotice(self, node):
492 """
493 reads a <notice> element from the manifest file
494
495 The <notice> element is distinct from other tags in the XML in that the
496 data is conveyed between the start and end tag (it's not an empty-element
497 tag).
498
499 The white space (carriage returns, indentation) for the notice element is
500 relevant and is parsed in a way that is based on how python docstrings work.
501 In fact, the code is remarkably similar to here:
502 http://www.python.org/dev/peps/pep-0257/
503 """
504 # Get the data out of the node...
505 notice = node.childNodes[0].data
506
507 # Figure out minimum indentation, skipping the first line (the same line
508 # as the <notice> tag)...
509 minIndent = sys.maxint
510 lines = notice.splitlines()
511 for line in lines[1:]:
512 lstrippedLine = line.lstrip()
513 if lstrippedLine:
514 indent = len(line) - len(lstrippedLine)
515 minIndent = min(indent, minIndent)
516
517 # Strip leading / trailing blank lines and also indentation.
518 cleanLines = [lines[0].strip()]
519 for line in lines[1:]:
520 cleanLines.append(line[minIndent:].rstrip())
521
522 # Clear completely blank lines from front and back...
523 while cleanLines and not cleanLines[0]:
524 del cleanLines[0]
525 while cleanLines and not cleanLines[-1]:
526 del cleanLines[-1]
527
528 return '\n'.join(cleanLines)
529
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700530 def _ParseProject(self, node):
531 """
532 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700533 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700534 name = self._reqatt(node, 'name')
535
536 remote = self._get_remote(node)
537 if remote is None:
538 remote = self._default.remote
539 if remote is None:
540 raise ManifestParseError, \
541 "no remote for project %s within %s" % \
542 (name, self.manifestFile)
543
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700544 revisionExpr = node.getAttribute('revision')
545 if not revisionExpr:
546 revisionExpr = self._default.revisionExpr
547 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700548 raise ManifestParseError, \
549 "no revision for project %s within %s" % \
550 (name, self.manifestFile)
551
552 path = node.getAttribute('path')
553 if not path:
554 path = name
555 if path.startswith('/'):
556 raise ManifestParseError, \
557 "project %s path cannot be absolute in %s" % \
558 (name, self.manifestFile)
559
Mike Pontillod3153822012-02-28 11:53:24 -0800560 rebase = node.getAttribute('rebase')
561 if not rebase:
562 rebase = True
563 else:
564 rebase = rebase.lower() in ("yes", "true", "1")
565
Anatol Pomazau79770d22012-04-20 14:41:59 -0700566 sync_c = node.getAttribute('sync-c')
567 if not sync_c:
568 sync_c = False
569 else:
570 sync_c = sync_c.lower() in ("yes", "true", "1")
571
Conley Owens971de8e2012-04-16 10:36:08 -0700572 groups = ''
573 if node.hasAttribute('groups'):
574 groups = node.getAttribute('groups')
575 groups = [x for x in re.split('[,\s]+', groups) if x]
576 if 'default' not in groups:
577 groups.append('default')
Colin Cross5acde752012-03-28 20:15:45 -0700578
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800579 if self.IsMirror:
580 relpath = None
581 worktree = None
582 gitdir = os.path.join(self.topdir, '%s.git' % name)
583 else:
Anthony Newnamdf14a702011-01-09 17:31:57 -0800584 worktree = os.path.join(self.topdir, path).replace('\\', '/')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800585 gitdir = os.path.join(self.repodir, 'projects/%s.git' % path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700586
587 project = Project(manifest = self,
588 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700589 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700590 gitdir = gitdir,
591 worktree = worktree,
592 relpath = path,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700593 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800594 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700595 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700596 groups = groups,
597 sync_c = sync_c)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700598
599 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700600 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700601 self._ParseCopyFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500602 if n.nodeName == 'annotation':
603 self._ParseAnnotation(project, n)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700604
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700605 return project
606
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700607 def _ParseCopyFile(self, project, node):
608 src = self._reqatt(node, 'src')
609 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800610 if not self.IsMirror:
611 # src is project relative;
612 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800613 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700614
James W. Mills24c13082012-04-12 15:04:13 -0500615 def _ParseAnnotation(self, project, node):
616 name = self._reqatt(node, 'name')
617 value = self._reqatt(node, 'value')
618 try:
619 keep = self._reqatt(node, 'keep').lower()
620 except ManifestParseError:
621 keep = "true"
622 if keep != "true" and keep != "false":
623 raise ManifestParseError, "optional \"keep\" attribute must be \"true\" or \"false\""
624 project.AddAnnotation(name, value, keep)
625
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700626 def _get_remote(self, node):
627 name = node.getAttribute('remote')
628 if not name:
629 return None
630
631 v = self._remotes.get(name)
632 if not v:
633 raise ManifestParseError, \
634 "remote %s not defined in %s" % \
635 (name, self.manifestFile)
636 return v
637
638 def _reqatt(self, node, attname):
639 """
640 reads a required attribute from the node.
641 """
642 v = node.getAttribute(attname)
643 if not v:
644 raise ManifestParseError, \
645 "no %s in <%s> within %s" % \
646 (attname, node.nodeName, self.manifestFile)
647 return v