blob: 9d68f09f63f59033e39da5edb9166dadf336ca9f [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
17import sys
18import xml.dom.minidom
19
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070020from git_config import GitConfig, IsId
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070021from project import RemoteSpec, Project, MetaProject, R_HEADS, HEAD
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070022from error import ManifestParseError
23
24MANIFEST_FILE_NAME = 'manifest.xml'
Shawn O. Pearce5cc66792008-10-23 16:19:27 -070025LOCAL_MANIFEST_NAME = 'local_manifest.xml'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070026
27class _Default(object):
28 """Project defaults within the manifest."""
29
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -070030 revisionExpr = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070031 remote = None
32
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070033class _XmlRemote(object):
34 def __init__(self,
35 name,
36 fetch=None,
37 review=None):
38 self.name = name
39 self.fetchUrl = fetch
40 self.reviewUrl = review
41
42 def ToRemoteSpec(self, projectName):
43 url = self.fetchUrl
44 while url.endswith('/'):
45 url = url[:-1]
46 url += '/%s.git' % projectName
47 return RemoteSpec(self.name, url, self.reviewUrl)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070048
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070049class XmlManifest(object):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070050 """manages the repo configuration file"""
51
52 def __init__(self, repodir):
53 self.repodir = os.path.abspath(repodir)
54 self.topdir = os.path.dirname(self.repodir)
55 self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070056 self.globalConfig = GitConfig.ForUser()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070057
58 self.repoProject = MetaProject(self, 'repo',
59 gitdir = os.path.join(repodir, 'repo/.git'),
60 worktree = os.path.join(repodir, 'repo'))
61
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070062 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -080063 gitdir = os.path.join(repodir, 'manifests.git'),
64 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070065
66 self._Unload()
67
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -070068 def Override(self, name):
69 """Use a different manifest, just for the current instantiation.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070070 """
71 path = os.path.join(self.manifestProject.worktree, name)
72 if not os.path.isfile(path):
73 raise ManifestParseError('manifest %s not found' % name)
74
75 old = self.manifestFile
76 try:
77 self.manifestFile = path
78 self._Unload()
79 self._Load()
80 finally:
81 self.manifestFile = old
82
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -070083 def Link(self, name):
84 """Update the repo metadata to use a different manifest.
85 """
86 self.Override(name)
87
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070088 try:
89 if os.path.exists(self.manifestFile):
90 os.remove(self.manifestFile)
91 os.symlink('manifests/%s' % name, self.manifestFile)
92 except OSError, e:
93 raise ManifestParseError('cannot link manifest %s' % name)
94
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080095 def _RemoteToXml(self, r, doc, root):
96 e = doc.createElement('remote')
97 root.appendChild(e)
98 e.setAttribute('name', r.name)
99 e.setAttribute('fetch', r.fetchUrl)
100 if r.reviewUrl is not None:
101 e.setAttribute('review', r.reviewUrl)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800102
103 def Save(self, fd, peg_rev=False):
104 """Write the current manifest out to the given file descriptor.
105 """
106 doc = xml.dom.minidom.Document()
107 root = doc.createElement('manifest')
108 doc.appendChild(root)
109
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700110 # Save out the notice. There's a little bit of work here to give it the
111 # right whitespace, which assumes that the notice is automatically indented
112 # by 4 by minidom.
113 if self.notice:
114 notice_element = root.appendChild(doc.createElement('notice'))
115 notice_lines = self.notice.splitlines()
116 indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:]
117 notice_element.appendChild(doc.createTextNode(indented_notice))
118
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800119 d = self.default
120 sort_remotes = list(self.remotes.keys())
121 sort_remotes.sort()
122
123 for r in sort_remotes:
124 self._RemoteToXml(self.remotes[r], doc, root)
125 if self.remotes:
126 root.appendChild(doc.createTextNode(''))
127
128 have_default = False
129 e = doc.createElement('default')
130 if d.remote:
131 have_default = True
132 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700133 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800134 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700135 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800136 if have_default:
137 root.appendChild(e)
138 root.appendChild(doc.createTextNode(''))
139
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700140 if self._manifest_server:
141 e = doc.createElement('manifest-server')
142 e.setAttribute('url', self._manifest_server)
143 root.appendChild(e)
144 root.appendChild(doc.createTextNode(''))
145
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800146 sort_projects = list(self.projects.keys())
147 sort_projects.sort()
148
149 for p in sort_projects:
150 p = self.projects[p]
151 e = doc.createElement('project')
152 root.appendChild(e)
153 e.setAttribute('name', p.name)
154 if p.relpath != p.name:
155 e.setAttribute('path', p.relpath)
156 if not d.remote or p.remote.name != d.remote.name:
157 e.setAttribute('remote', p.remote.name)
158 if peg_rev:
159 if self.IsMirror:
160 e.setAttribute('revision',
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700161 p.bare_git.rev_parse(p.revisionExpr + '^0'))
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800162 else:
163 e.setAttribute('revision',
164 p.work_git.rev_parse(HEAD + '^0'))
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700165 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
166 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800167
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800168 for c in p.copyfiles:
169 ce = doc.createElement('copyfile')
170 ce.setAttribute('src', c.src)
171 ce.setAttribute('dest', c.dest)
172 e.appendChild(ce)
173
174 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
175
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700176 @property
177 def projects(self):
178 self._Load()
179 return self._projects
180
181 @property
182 def remotes(self):
183 self._Load()
184 return self._remotes
185
186 @property
187 def default(self):
188 self._Load()
189 return self._default
190
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800191 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700192 def notice(self):
193 self._Load()
194 return self._notice
195
196 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700197 def manifest_server(self):
198 self._Load()
199 return self._manifest_server
200
201 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800202 def IsMirror(self):
203 return self.manifestProject.config.GetBoolean('repo.mirror')
204
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700205 def _Unload(self):
206 self._loaded = False
207 self._projects = {}
208 self._remotes = {}
209 self._default = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700210 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700211 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700212 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700213
214 def _Load(self):
215 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800216 m = self.manifestProject
217 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700218 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800219 b = b[len(R_HEADS):]
220 self.branch = b
221
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700222 self._ParseManifest(True)
223
224 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
225 if os.path.exists(local):
226 try:
227 real = self.manifestFile
228 self.manifestFile = local
229 self._ParseManifest(False)
230 finally:
231 self.manifestFile = real
232
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800233 if self.IsMirror:
234 self._AddMetaProjectMirror(self.repoProject)
235 self._AddMetaProjectMirror(self.manifestProject)
236
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700237 self._loaded = True
238
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700239 def _ParseManifest(self, is_root_file):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700240 root = xml.dom.minidom.parse(self.manifestFile)
241 if not root or not root.childNodes:
242 raise ManifestParseError, \
243 "no root node in %s" % \
244 self.manifestFile
245
246 config = root.childNodes[0]
247 if config.nodeName != 'manifest':
248 raise ManifestParseError, \
249 "no <manifest> in %s" % \
250 self.manifestFile
251
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700252 for node in config.childNodes:
Shawn O. Pearce03eaf072008-11-20 11:42:22 -0800253 if node.nodeName == 'remove-project':
254 name = self._reqatt(node, 'name')
255 try:
256 del self._projects[name]
257 except KeyError:
258 raise ManifestParseError, \
259 'project %s not found' % \
260 (name)
261
262 for node in config.childNodes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700263 if node.nodeName == 'remote':
264 remote = self._ParseRemote(node)
265 if self._remotes.get(remote.name):
266 raise ManifestParseError, \
267 'duplicate remote %s in %s' % \
268 (remote.name, self.manifestFile)
269 self._remotes[remote.name] = remote
270
271 for node in config.childNodes:
272 if node.nodeName == 'default':
273 if self._default is not None:
274 raise ManifestParseError, \
275 'duplicate default in %s' % \
276 (self.manifestFile)
277 self._default = self._ParseDefault(node)
278 if self._default is None:
279 self._default = _Default()
280
281 for node in config.childNodes:
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700282 if node.nodeName == 'notice':
283 if self._notice is not None:
284 raise ManifestParseError, \
285 'duplicate notice in %s' % \
286 (self.manifestFile)
287 self._notice = self._ParseNotice(node)
288
289 for node in config.childNodes:
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700290 if node.nodeName == 'manifest-server':
291 url = self._reqatt(node, 'url')
292 if self._manifest_server is not None:
293 raise ManifestParseError, \
294 'duplicate manifest-server in %s' % \
295 (self.manifestFile)
296 self._manifest_server = url
297
298 for node in config.childNodes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700299 if node.nodeName == 'project':
300 project = self._ParseProject(node)
301 if self._projects.get(project.name):
302 raise ManifestParseError, \
303 'duplicate project %s in %s' % \
304 (project.name, self.manifestFile)
305 self._projects[project.name] = project
306
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800307 def _AddMetaProjectMirror(self, m):
308 name = None
309 m_url = m.GetRemote(m.remote.name).url
310 if m_url.endswith('/.git'):
311 raise ManifestParseError, 'refusing to mirror %s' % m_url
312
313 if self._default and self._default.remote:
314 url = self._default.remote.fetchUrl
315 if not url.endswith('/'):
316 url += '/'
317 if m_url.startswith(url):
318 remote = self._default.remote
319 name = m_url[len(url):]
320
321 if name is None:
322 s = m_url.rindex('/') + 1
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700323 remote = _XmlRemote('origin', m_url[:s])
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800324 name = m_url[s:]
325
326 if name.endswith('.git'):
327 name = name[:-4]
328
329 if name not in self._projects:
330 m.PreSync()
331 gitdir = os.path.join(self.topdir, '%s.git' % name)
332 project = Project(manifest = self,
333 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700334 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800335 gitdir = gitdir,
336 worktree = None,
337 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700338 revisionExpr = m.revisionExpr,
339 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800340 self._projects[project.name] = project
341
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700342 def _ParseRemote(self, node):
343 """
344 reads a <remote> element from the manifest file
345 """
346 name = self._reqatt(node, 'name')
347 fetch = self._reqatt(node, 'fetch')
348 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800349 if review == '':
350 review = None
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700351 return _XmlRemote(name, fetch, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700352
353 def _ParseDefault(self, node):
354 """
355 reads a <default> element from the manifest file
356 """
357 d = _Default()
358 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700359 d.revisionExpr = node.getAttribute('revision')
360 if d.revisionExpr == '':
361 d.revisionExpr = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700362 return d
363
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700364 def _ParseNotice(self, node):
365 """
366 reads a <notice> element from the manifest file
367
368 The <notice> element is distinct from other tags in the XML in that the
369 data is conveyed between the start and end tag (it's not an empty-element
370 tag).
371
372 The white space (carriage returns, indentation) for the notice element is
373 relevant and is parsed in a way that is based on how python docstrings work.
374 In fact, the code is remarkably similar to here:
375 http://www.python.org/dev/peps/pep-0257/
376 """
377 # Get the data out of the node...
378 notice = node.childNodes[0].data
379
380 # Figure out minimum indentation, skipping the first line (the same line
381 # as the <notice> tag)...
382 minIndent = sys.maxint
383 lines = notice.splitlines()
384 for line in lines[1:]:
385 lstrippedLine = line.lstrip()
386 if lstrippedLine:
387 indent = len(line) - len(lstrippedLine)
388 minIndent = min(indent, minIndent)
389
390 # Strip leading / trailing blank lines and also indentation.
391 cleanLines = [lines[0].strip()]
392 for line in lines[1:]:
393 cleanLines.append(line[minIndent:].rstrip())
394
395 # Clear completely blank lines from front and back...
396 while cleanLines and not cleanLines[0]:
397 del cleanLines[0]
398 while cleanLines and not cleanLines[-1]:
399 del cleanLines[-1]
400
401 return '\n'.join(cleanLines)
402
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700403 def _ParseProject(self, node):
404 """
405 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700406 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700407 name = self._reqatt(node, 'name')
408
409 remote = self._get_remote(node)
410 if remote is None:
411 remote = self._default.remote
412 if remote is None:
413 raise ManifestParseError, \
414 "no remote for project %s within %s" % \
415 (name, self.manifestFile)
416
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700417 revisionExpr = node.getAttribute('revision')
418 if not revisionExpr:
419 revisionExpr = self._default.revisionExpr
420 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700421 raise ManifestParseError, \
422 "no revision for project %s within %s" % \
423 (name, self.manifestFile)
424
425 path = node.getAttribute('path')
426 if not path:
427 path = name
428 if path.startswith('/'):
429 raise ManifestParseError, \
430 "project %s path cannot be absolute in %s" % \
431 (name, self.manifestFile)
432
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800433 if self.IsMirror:
434 relpath = None
435 worktree = None
436 gitdir = os.path.join(self.topdir, '%s.git' % name)
437 else:
438 worktree = os.path.join(self.topdir, path)
439 gitdir = os.path.join(self.repodir, 'projects/%s.git' % path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700440
441 project = Project(manifest = self,
442 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700443 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700444 gitdir = gitdir,
445 worktree = worktree,
446 relpath = path,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700447 revisionExpr = revisionExpr,
448 revisionId = None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700449
450 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700451 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700452 self._ParseCopyFile(project, n)
453
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700454 return project
455
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700456 def _ParseCopyFile(self, project, node):
457 src = self._reqatt(node, 'src')
458 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800459 if not self.IsMirror:
460 # src is project relative;
461 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800462 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700463
464 def _get_remote(self, node):
465 name = node.getAttribute('remote')
466 if not name:
467 return None
468
469 v = self._remotes.get(name)
470 if not v:
471 raise ManifestParseError, \
472 "remote %s not defined in %s" % \
473 (name, self.manifestFile)
474 return v
475
476 def _reqatt(self, node, attname):
477 """
478 reads a required attribute from the node.
479 """
480 v = node.getAttribute(attname)
481 if not v:
482 raise ManifestParseError, \
483 "no %s in <%s> within %s" % \
484 (attname, node.nodeName, self.manifestFile)
485 return v