blob: a46cf24e99262fecbc78ea329307e24cdc2f6e22 [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
Colin Cross23acdd32012-04-21 00:33:54 -070016import itertools
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070017import os
Conley Owensdb728cd2011-09-26 16:34:01 -070018import re
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070019import sys
Conley Owensdb728cd2011-09-26 16:34:01 -070020import urlparse
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070021import xml.dom.minidom
22
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070023from git_config import GitConfig, IsId
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070024from project import RemoteSpec, Project, MetaProject, R_HEADS, HEAD
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070025from error import ManifestParseError
26
27MANIFEST_FILE_NAME = 'manifest.xml'
Shawn O. Pearce5cc66792008-10-23 16:19:27 -070028LOCAL_MANIFEST_NAME = 'local_manifest.xml'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070029
Conley Owensdb728cd2011-09-26 16:34:01 -070030urlparse.uses_relative.extend(['ssh', 'git'])
31urlparse.uses_netloc.extend(['ssh', 'git'])
32
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070033class _Default(object):
34 """Project defaults within the manifest."""
35
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -070036 revisionExpr = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070037 remote = None
Shawn O. Pearce6392c872011-09-22 17:44:31 -070038 sync_j = 1
Anatol Pomazau79770d22012-04-20 14:41:59 -070039 sync_c = False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070040
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070041class _XmlRemote(object):
42 def __init__(self,
43 name,
44 fetch=None,
Conley Owensdb728cd2011-09-26 16:34:01 -070045 manifestUrl=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070046 review=None):
47 self.name = name
48 self.fetchUrl = fetch
Conley Owensdb728cd2011-09-26 16:34:01 -070049 self.manifestUrl = manifestUrl
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070050 self.reviewUrl = review
Conley Owensceea3682011-10-20 10:45:47 -070051 self.resolvedFetchUrl = self._resolveFetchUrl()
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070052
Conley Owensceea3682011-10-20 10:45:47 -070053 def _resolveFetchUrl(self):
54 url = self.fetchUrl.rstrip('/')
Conley Owensdb728cd2011-09-26 16:34:01 -070055 manifestUrl = self.manifestUrl.rstrip('/')
56 # urljoin will get confused if there is no scheme in the base url
57 # ie, if manifestUrl is of the form <hostname:port>
58 if manifestUrl.find(':') != manifestUrl.find('/') - 1:
59 manifestUrl = 'gopher://' + manifestUrl
60 url = urlparse.urljoin(manifestUrl, url)
Conley Owensceea3682011-10-20 10:45:47 -070061 return re.sub(r'^gopher://', '', url)
62
63 def ToRemoteSpec(self, projectName):
Conley Owens9d8f9142011-10-20 14:36:35 -070064 url = self.resolvedFetchUrl.rstrip('/') + '/' + projectName
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070065 return RemoteSpec(self.name, url, self.reviewUrl)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070066
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070067class XmlManifest(object):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070068 """manages the repo configuration file"""
69
70 def __init__(self, repodir):
71 self.repodir = os.path.abspath(repodir)
72 self.topdir = os.path.dirname(self.repodir)
73 self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070074 self.globalConfig = GitConfig.ForUser()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070075
76 self.repoProject = MetaProject(self, 'repo',
77 gitdir = os.path.join(repodir, 'repo/.git'),
78 worktree = os.path.join(repodir, 'repo'))
79
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070080 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -080081 gitdir = os.path.join(repodir, 'manifests.git'),
82 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070083
84 self._Unload()
85
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -070086 def Override(self, name):
87 """Use a different manifest, just for the current instantiation.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070088 """
89 path = os.path.join(self.manifestProject.worktree, name)
90 if not os.path.isfile(path):
91 raise ManifestParseError('manifest %s not found' % name)
92
93 old = self.manifestFile
94 try:
95 self.manifestFile = path
96 self._Unload()
97 self._Load()
98 finally:
99 self.manifestFile = old
100
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700101 def Link(self, name):
102 """Update the repo metadata to use a different manifest.
103 """
104 self.Override(name)
105
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700106 try:
107 if os.path.exists(self.manifestFile):
108 os.remove(self.manifestFile)
109 os.symlink('manifests/%s' % name, self.manifestFile)
110 except OSError, e:
111 raise ManifestParseError('cannot link manifest %s' % name)
112
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800113 def _RemoteToXml(self, r, doc, root):
114 e = doc.createElement('remote')
115 root.appendChild(e)
116 e.setAttribute('name', r.name)
117 e.setAttribute('fetch', r.fetchUrl)
118 if r.reviewUrl is not None:
119 e.setAttribute('review', r.reviewUrl)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800120
121 def Save(self, fd, peg_rev=False):
122 """Write the current manifest out to the given file descriptor.
123 """
Colin Cross5acde752012-03-28 20:15:45 -0700124 mp = self.manifestProject
125
126 groups = mp.config.GetString('manifest.groups')
Colin Crossc39864f2012-04-23 13:41:58 -0700127 if not groups:
Conley Owens971de8e2012-04-16 10:36:08 -0700128 groups = 'default'
129 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Colin Cross5acde752012-03-28 20:15:45 -0700130
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800131 doc = xml.dom.minidom.Document()
132 root = doc.createElement('manifest')
133 doc.appendChild(root)
134
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700135 # Save out the notice. There's a little bit of work here to give it the
136 # right whitespace, which assumes that the notice is automatically indented
137 # by 4 by minidom.
138 if self.notice:
139 notice_element = root.appendChild(doc.createElement('notice'))
140 notice_lines = self.notice.splitlines()
141 indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:]
142 notice_element.appendChild(doc.createTextNode(indented_notice))
143
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800144 d = self.default
145 sort_remotes = list(self.remotes.keys())
146 sort_remotes.sort()
147
148 for r in sort_remotes:
149 self._RemoteToXml(self.remotes[r], doc, root)
150 if self.remotes:
151 root.appendChild(doc.createTextNode(''))
152
153 have_default = False
154 e = doc.createElement('default')
155 if d.remote:
156 have_default = True
157 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700158 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800159 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700160 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700161 if d.sync_j > 1:
162 have_default = True
163 e.setAttribute('sync-j', '%d' % d.sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700164 if d.sync_c:
165 have_default = True
166 e.setAttribute('sync-c', 'true')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800167 if have_default:
168 root.appendChild(e)
169 root.appendChild(doc.createTextNode(''))
170
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700171 if self._manifest_server:
172 e = doc.createElement('manifest-server')
173 e.setAttribute('url', self._manifest_server)
174 root.appendChild(e)
175 root.appendChild(doc.createTextNode(''))
176
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800177 sort_projects = list(self.projects.keys())
178 sort_projects.sort()
179
180 for p in sort_projects:
181 p = self.projects[p]
Colin Cross5acde752012-03-28 20:15:45 -0700182
183 if not p.MatchesGroups(groups):
184 continue
185
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800186 e = doc.createElement('project')
187 root.appendChild(e)
188 e.setAttribute('name', p.name)
189 if p.relpath != p.name:
190 e.setAttribute('path', p.relpath)
191 if not d.remote or p.remote.name != d.remote.name:
192 e.setAttribute('remote', p.remote.name)
193 if peg_rev:
194 if self.IsMirror:
195 e.setAttribute('revision',
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700196 p.bare_git.rev_parse(p.revisionExpr + '^0'))
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800197 else:
198 e.setAttribute('revision',
199 p.work_git.rev_parse(HEAD + '^0'))
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700200 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
201 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800202
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800203 for c in p.copyfiles:
204 ce = doc.createElement('copyfile')
205 ce.setAttribute('src', c.src)
206 ce.setAttribute('dest', c.dest)
207 e.appendChild(ce)
208
Conley Owens971de8e2012-04-16 10:36:08 -0700209 egroups = [g for g in p.groups if g != 'default']
210 if egroups:
211 e.setAttribute('groups', ','.join(egroups))
Colin Cross5acde752012-03-28 20:15:45 -0700212
James W. Mills24c13082012-04-12 15:04:13 -0500213 for a in p.annotations:
214 if a.keep == "true":
215 ae = doc.createElement('annotation')
216 ae.setAttribute('name', a.name)
217 ae.setAttribute('value', a.value)
218 e.appendChild(ae)
219
Anatol Pomazau79770d22012-04-20 14:41:59 -0700220 if p.sync_c:
221 e.setAttribute('sync-c', 'true')
222
Doug Anderson37282b42011-03-04 11:54:18 -0800223 if self._repo_hooks_project:
224 root.appendChild(doc.createTextNode(''))
225 e = doc.createElement('repo-hooks')
226 e.setAttribute('in-project', self._repo_hooks_project.name)
227 e.setAttribute('enabled-list',
228 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
229 root.appendChild(e)
230
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800231 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
232
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700233 @property
234 def projects(self):
235 self._Load()
236 return self._projects
237
238 @property
239 def remotes(self):
240 self._Load()
241 return self._remotes
242
243 @property
244 def default(self):
245 self._Load()
246 return self._default
247
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800248 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800249 def repo_hooks_project(self):
250 self._Load()
251 return self._repo_hooks_project
252
253 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700254 def notice(self):
255 self._Load()
256 return self._notice
257
258 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700259 def manifest_server(self):
260 self._Load()
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800261 return self._manifest_server
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700262
263 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800264 def IsMirror(self):
265 return self.manifestProject.config.GetBoolean('repo.mirror')
266
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700267 def _Unload(self):
268 self._loaded = False
269 self._projects = {}
270 self._remotes = {}
271 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800272 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700273 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700274 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700275 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700276
277 def _Load(self):
278 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800279 m = self.manifestProject
280 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700281 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800282 b = b[len(R_HEADS):]
283 self.branch = b
284
Colin Cross23acdd32012-04-21 00:33:54 -0700285 nodes = []
Brian Harring475a47d2012-06-07 20:05:35 -0700286 nodes.append(self._ParseManifestXml(self.manifestFile,
287 self.manifestProject.worktree))
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700288
289 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
290 if os.path.exists(local):
Brian Harring475a47d2012-06-07 20:05:35 -0700291 nodes.append(self._ParseManifestXml(local, self.repodir))
Colin Cross23acdd32012-04-21 00:33:54 -0700292
293 self._ParseManifest(nodes)
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700294
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 Harring475a47d2012-06-07 20:05:35 -0700301 def _ParseManifestXml(self, path, include_root):
Brian Harring26448742011-04-28 05:04:41 -0700302 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
Colin Cross23acdd32012-04-21 00:33:54 -0700310 nodes = []
Brian Harring26448742011-04-28 05:04:41 -0700311 for node in config.childNodes:
312 if node.nodeName == 'include':
313 name = self._reqatt(node, 'name')
Brian Harring475a47d2012-06-07 20:05:35 -0700314 fp = os.path.join(include_root, name)
Brian Harring26448742011-04-28 05:04:41 -0700315 if not os.path.isfile(fp):
316 raise ManifestParseError, \
317 "include %s doesn't exist or isn't a file" % \
318 (name,)
319 try:
Brian Harring475a47d2012-06-07 20:05:35 -0700320 nodes.extend(self._ParseManifestXml(fp, include_root))
Brian Harring26448742011-04-28 05:04:41 -0700321 # should isolate this to the exact exception, but that's
322 # tricky. actual parsing implementation may vary.
323 except (KeyboardInterrupt, RuntimeError, SystemExit):
324 raise
325 except Exception, e:
326 raise ManifestParseError(
327 "failed parsing included manifest %s: %s", (name, e))
Colin Cross23acdd32012-04-21 00:33:54 -0700328 else:
329 nodes.append(node)
330 return nodes
Shawn O. Pearce03eaf072008-11-20 11:42:22 -0800331
Colin Cross23acdd32012-04-21 00:33:54 -0700332 def _ParseManifest(self, node_list):
333 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700334 if node.nodeName == 'remote':
335 remote = self._ParseRemote(node)
336 if self._remotes.get(remote.name):
Doug Anderson37282b42011-03-04 11:54:18 -0800337 raise ManifestParseError(
338 'duplicate remote %s in %s' %
339 (remote.name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700340 self._remotes[remote.name] = remote
341
Colin Cross23acdd32012-04-21 00:33:54 -0700342 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700343 if node.nodeName == 'default':
344 if self._default is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800345 raise ManifestParseError(
346 'duplicate default in %s' %
347 (self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700348 self._default = self._ParseDefault(node)
349 if self._default is None:
350 self._default = _Default()
351
Colin Cross23acdd32012-04-21 00:33:54 -0700352 for node in itertools.chain(*node_list):
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700353 if node.nodeName == 'notice':
354 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800355 raise ManifestParseError(
356 'duplicate notice in %s' %
357 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700358 self._notice = self._ParseNotice(node)
359
Colin Cross23acdd32012-04-21 00:33:54 -0700360 for node in itertools.chain(*node_list):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700361 if node.nodeName == 'manifest-server':
362 url = self._reqatt(node, 'url')
363 if self._manifest_server is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800364 raise ManifestParseError(
365 'duplicate manifest-server in %s' %
366 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700367 self._manifest_server = url
368
Colin Cross23acdd32012-04-21 00:33:54 -0700369 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700370 if node.nodeName == 'project':
371 project = self._ParseProject(node)
372 if self._projects.get(project.name):
Doug Anderson37282b42011-03-04 11:54:18 -0800373 raise ManifestParseError(
374 'duplicate project %s in %s' %
375 (project.name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700376 self._projects[project.name] = project
Doug Anderson37282b42011-03-04 11:54:18 -0800377 if node.nodeName == 'repo-hooks':
378 # Get the name of the project and the (space-separated) list of enabled.
379 repo_hooks_project = self._reqatt(node, 'in-project')
380 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
381
382 # Only one project can be the hooks project
383 if self._repo_hooks_project is not None:
384 raise ManifestParseError(
385 'duplicate repo-hooks in %s' %
386 (self.manifestFile))
387
388 # Store a reference to the Project.
389 try:
390 self._repo_hooks_project = self._projects[repo_hooks_project]
391 except KeyError:
392 raise ManifestParseError(
393 'project %s not found for repo-hooks' %
394 (repo_hooks_project))
395
396 # Store the enabled hooks in the Project object.
397 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
Colin Cross23acdd32012-04-21 00:33:54 -0700398 if node.nodeName == 'remove-project':
399 name = self._reqatt(node, 'name')
400 try:
401 del self._projects[name]
402 except KeyError:
403 raise ManifestParseError(
404 'project %s not found' %
405 (name))
406
407 # If the manifest removes the hooks project, treat it as if it deleted
408 # the repo-hooks element too.
409 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
410 self._repo_hooks_project = None
411
Doug Anderson37282b42011-03-04 11:54:18 -0800412
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800413 def _AddMetaProjectMirror(self, m):
414 name = None
415 m_url = m.GetRemote(m.remote.name).url
416 if m_url.endswith('/.git'):
417 raise ManifestParseError, 'refusing to mirror %s' % m_url
418
419 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700420 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800421 if not url.endswith('/'):
422 url += '/'
423 if m_url.startswith(url):
424 remote = self._default.remote
425 name = m_url[len(url):]
426
427 if name is None:
428 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700429 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
430 remote = _XmlRemote('origin', m_url[:s], manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800431 name = m_url[s:]
432
433 if name.endswith('.git'):
434 name = name[:-4]
435
436 if name not in self._projects:
437 m.PreSync()
438 gitdir = os.path.join(self.topdir, '%s.git' % name)
439 project = Project(manifest = self,
440 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700441 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800442 gitdir = gitdir,
443 worktree = None,
444 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700445 revisionExpr = m.revisionExpr,
446 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800447 self._projects[project.name] = project
448
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700449 def _ParseRemote(self, node):
450 """
451 reads a <remote> element from the manifest file
452 """
453 name = self._reqatt(node, 'name')
454 fetch = self._reqatt(node, 'fetch')
455 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800456 if review == '':
457 review = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700458 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
459 return _XmlRemote(name, fetch, manifestUrl, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700460
461 def _ParseDefault(self, node):
462 """
463 reads a <default> element from the manifest file
464 """
465 d = _Default()
466 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700467 d.revisionExpr = node.getAttribute('revision')
468 if d.revisionExpr == '':
469 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700470
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700471 sync_j = node.getAttribute('sync-j')
472 if sync_j == '' or sync_j is None:
473 d.sync_j = 1
474 else:
475 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700476
477 sync_c = node.getAttribute('sync-c')
478 if not sync_c:
479 d.sync_c = False
480 else:
481 d.sync_c = sync_c.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700482 return d
483
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700484 def _ParseNotice(self, node):
485 """
486 reads a <notice> element from the manifest file
487
488 The <notice> element is distinct from other tags in the XML in that the
489 data is conveyed between the start and end tag (it's not an empty-element
490 tag).
491
492 The white space (carriage returns, indentation) for the notice element is
493 relevant and is parsed in a way that is based on how python docstrings work.
494 In fact, the code is remarkably similar to here:
495 http://www.python.org/dev/peps/pep-0257/
496 """
497 # Get the data out of the node...
498 notice = node.childNodes[0].data
499
500 # Figure out minimum indentation, skipping the first line (the same line
501 # as the <notice> tag)...
502 minIndent = sys.maxint
503 lines = notice.splitlines()
504 for line in lines[1:]:
505 lstrippedLine = line.lstrip()
506 if lstrippedLine:
507 indent = len(line) - len(lstrippedLine)
508 minIndent = min(indent, minIndent)
509
510 # Strip leading / trailing blank lines and also indentation.
511 cleanLines = [lines[0].strip()]
512 for line in lines[1:]:
513 cleanLines.append(line[minIndent:].rstrip())
514
515 # Clear completely blank lines from front and back...
516 while cleanLines and not cleanLines[0]:
517 del cleanLines[0]
518 while cleanLines and not cleanLines[-1]:
519 del cleanLines[-1]
520
521 return '\n'.join(cleanLines)
522
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700523 def _ParseProject(self, node):
524 """
525 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700526 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700527 name = self._reqatt(node, 'name')
528
529 remote = self._get_remote(node)
530 if remote is None:
531 remote = self._default.remote
532 if remote is None:
533 raise ManifestParseError, \
534 "no remote for project %s within %s" % \
535 (name, self.manifestFile)
536
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700537 revisionExpr = node.getAttribute('revision')
538 if not revisionExpr:
539 revisionExpr = self._default.revisionExpr
540 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700541 raise ManifestParseError, \
542 "no revision for project %s within %s" % \
543 (name, self.manifestFile)
544
545 path = node.getAttribute('path')
546 if not path:
547 path = name
548 if path.startswith('/'):
549 raise ManifestParseError, \
550 "project %s path cannot be absolute in %s" % \
551 (name, self.manifestFile)
552
Mike Pontillod3153822012-02-28 11:53:24 -0800553 rebase = node.getAttribute('rebase')
554 if not rebase:
555 rebase = True
556 else:
557 rebase = rebase.lower() in ("yes", "true", "1")
558
Anatol Pomazau79770d22012-04-20 14:41:59 -0700559 sync_c = node.getAttribute('sync-c')
560 if not sync_c:
561 sync_c = False
562 else:
563 sync_c = sync_c.lower() in ("yes", "true", "1")
564
Conley Owens971de8e2012-04-16 10:36:08 -0700565 groups = ''
566 if node.hasAttribute('groups'):
567 groups = node.getAttribute('groups')
568 groups = [x for x in re.split('[,\s]+', groups) if x]
569 if 'default' not in groups:
570 groups.append('default')
Colin Cross5acde752012-03-28 20:15:45 -0700571
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800572 if self.IsMirror:
573 relpath = None
574 worktree = None
575 gitdir = os.path.join(self.topdir, '%s.git' % name)
576 else:
Anthony Newnamdf14a702011-01-09 17:31:57 -0800577 worktree = os.path.join(self.topdir, path).replace('\\', '/')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800578 gitdir = os.path.join(self.repodir, 'projects/%s.git' % path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700579
580 project = Project(manifest = self,
581 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700582 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700583 gitdir = gitdir,
584 worktree = worktree,
585 relpath = path,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700586 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800587 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700588 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700589 groups = groups,
590 sync_c = sync_c)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700591
592 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700593 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700594 self._ParseCopyFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500595 if n.nodeName == 'annotation':
596 self._ParseAnnotation(project, n)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700597
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700598 return project
599
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700600 def _ParseCopyFile(self, project, node):
601 src = self._reqatt(node, 'src')
602 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800603 if not self.IsMirror:
604 # src is project relative;
605 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800606 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700607
James W. Mills24c13082012-04-12 15:04:13 -0500608 def _ParseAnnotation(self, project, node):
609 name = self._reqatt(node, 'name')
610 value = self._reqatt(node, 'value')
611 try:
612 keep = self._reqatt(node, 'keep').lower()
613 except ManifestParseError:
614 keep = "true"
615 if keep != "true" and keep != "false":
616 raise ManifestParseError, "optional \"keep\" attribute must be \"true\" or \"false\""
617 project.AddAnnotation(name, value, keep)
618
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700619 def _get_remote(self, node):
620 name = node.getAttribute('remote')
621 if not name:
622 return None
623
624 v = self._remotes.get(name)
625 if not v:
626 raise ManifestParseError, \
627 "remote %s not defined in %s" % \
628 (name, self.manifestFile)
629 return v
630
631 def _reqatt(self, node, attname):
632 """
633 reads a required attribute from the node.
634 """
635 v = node.getAttribute(attname)
636 if not v:
637 raise ManifestParseError, \
638 "no %s in <%s> within %s" % \
639 (attname, node.nodeName, self.manifestFile)
640 return v