blob: d0c9debe26499dc4557ae5cedd58d927dc3108ef [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
110 d = self.default
111 sort_remotes = list(self.remotes.keys())
112 sort_remotes.sort()
113
114 for r in sort_remotes:
115 self._RemoteToXml(self.remotes[r], doc, root)
116 if self.remotes:
117 root.appendChild(doc.createTextNode(''))
118
119 have_default = False
120 e = doc.createElement('default')
121 if d.remote:
122 have_default = True
123 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700124 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800125 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700126 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800127 if have_default:
128 root.appendChild(e)
129 root.appendChild(doc.createTextNode(''))
130
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700131 if self._manifest_server:
132 e = doc.createElement('manifest-server')
133 e.setAttribute('url', self._manifest_server)
134 root.appendChild(e)
135 root.appendChild(doc.createTextNode(''))
136
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800137 sort_projects = list(self.projects.keys())
138 sort_projects.sort()
139
140 for p in sort_projects:
141 p = self.projects[p]
142 e = doc.createElement('project')
143 root.appendChild(e)
144 e.setAttribute('name', p.name)
145 if p.relpath != p.name:
146 e.setAttribute('path', p.relpath)
147 if not d.remote or p.remote.name != d.remote.name:
148 e.setAttribute('remote', p.remote.name)
149 if peg_rev:
150 if self.IsMirror:
151 e.setAttribute('revision',
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700152 p.bare_git.rev_parse(p.revisionExpr + '^0'))
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800153 else:
154 e.setAttribute('revision',
155 p.work_git.rev_parse(HEAD + '^0'))
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700156 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
157 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800158
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800159 for c in p.copyfiles:
160 ce = doc.createElement('copyfile')
161 ce.setAttribute('src', c.src)
162 ce.setAttribute('dest', c.dest)
163 e.appendChild(ce)
164
165 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
166
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700167 @property
168 def projects(self):
169 self._Load()
170 return self._projects
171
172 @property
173 def remotes(self):
174 self._Load()
175 return self._remotes
176
177 @property
178 def default(self):
179 self._Load()
180 return self._default
181
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800182 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700183 def manifest_server(self):
184 self._Load()
185 return self._manifest_server
186
187 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800188 def IsMirror(self):
189 return self.manifestProject.config.GetBoolean('repo.mirror')
190
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700191 def _Unload(self):
192 self._loaded = False
193 self._projects = {}
194 self._remotes = {}
195 self._default = None
196 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700197 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700198
199 def _Load(self):
200 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800201 m = self.manifestProject
202 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700203 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800204 b = b[len(R_HEADS):]
205 self.branch = b
206
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700207 self._ParseManifest(True)
208
209 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
210 if os.path.exists(local):
211 try:
212 real = self.manifestFile
213 self.manifestFile = local
214 self._ParseManifest(False)
215 finally:
216 self.manifestFile = real
217
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800218 if self.IsMirror:
219 self._AddMetaProjectMirror(self.repoProject)
220 self._AddMetaProjectMirror(self.manifestProject)
221
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700222 self._loaded = True
223
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700224 def _ParseManifest(self, is_root_file):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700225 root = xml.dom.minidom.parse(self.manifestFile)
226 if not root or not root.childNodes:
227 raise ManifestParseError, \
228 "no root node in %s" % \
229 self.manifestFile
230
231 config = root.childNodes[0]
232 if config.nodeName != 'manifest':
233 raise ManifestParseError, \
234 "no <manifest> in %s" % \
235 self.manifestFile
236
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700237 for node in config.childNodes:
Shawn O. Pearce03eaf072008-11-20 11:42:22 -0800238 if node.nodeName == 'remove-project':
239 name = self._reqatt(node, 'name')
240 try:
241 del self._projects[name]
242 except KeyError:
243 raise ManifestParseError, \
244 'project %s not found' % \
245 (name)
246
247 for node in config.childNodes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700248 if node.nodeName == 'remote':
249 remote = self._ParseRemote(node)
250 if self._remotes.get(remote.name):
251 raise ManifestParseError, \
252 'duplicate remote %s in %s' % \
253 (remote.name, self.manifestFile)
254 self._remotes[remote.name] = remote
255
256 for node in config.childNodes:
257 if node.nodeName == 'default':
258 if self._default is not None:
259 raise ManifestParseError, \
260 'duplicate default in %s' % \
261 (self.manifestFile)
262 self._default = self._ParseDefault(node)
263 if self._default is None:
264 self._default = _Default()
265
266 for node in config.childNodes:
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700267 if node.nodeName == 'manifest-server':
268 url = self._reqatt(node, 'url')
269 if self._manifest_server is not None:
270 raise ManifestParseError, \
271 'duplicate manifest-server in %s' % \
272 (self.manifestFile)
273 self._manifest_server = url
274
275 for node in config.childNodes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700276 if node.nodeName == 'project':
277 project = self._ParseProject(node)
278 if self._projects.get(project.name):
279 raise ManifestParseError, \
280 'duplicate project %s in %s' % \
281 (project.name, self.manifestFile)
282 self._projects[project.name] = project
283
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800284 def _AddMetaProjectMirror(self, m):
285 name = None
286 m_url = m.GetRemote(m.remote.name).url
287 if m_url.endswith('/.git'):
288 raise ManifestParseError, 'refusing to mirror %s' % m_url
289
290 if self._default and self._default.remote:
291 url = self._default.remote.fetchUrl
292 if not url.endswith('/'):
293 url += '/'
294 if m_url.startswith(url):
295 remote = self._default.remote
296 name = m_url[len(url):]
297
298 if name is None:
299 s = m_url.rindex('/') + 1
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700300 remote = _XmlRemote('origin', m_url[:s])
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800301 name = m_url[s:]
302
303 if name.endswith('.git'):
304 name = name[:-4]
305
306 if name not in self._projects:
307 m.PreSync()
308 gitdir = os.path.join(self.topdir, '%s.git' % name)
309 project = Project(manifest = self,
310 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700311 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800312 gitdir = gitdir,
313 worktree = None,
314 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700315 revisionExpr = m.revisionExpr,
316 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800317 self._projects[project.name] = project
318
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700319 def _ParseRemote(self, node):
320 """
321 reads a <remote> element from the manifest file
322 """
323 name = self._reqatt(node, 'name')
324 fetch = self._reqatt(node, 'fetch')
325 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800326 if review == '':
327 review = None
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700328 return _XmlRemote(name, fetch, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700329
330 def _ParseDefault(self, node):
331 """
332 reads a <default> element from the manifest file
333 """
334 d = _Default()
335 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700336 d.revisionExpr = node.getAttribute('revision')
337 if d.revisionExpr == '':
338 d.revisionExpr = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700339 return d
340
341 def _ParseProject(self, node):
342 """
343 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700344 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700345 name = self._reqatt(node, 'name')
346
347 remote = self._get_remote(node)
348 if remote is None:
349 remote = self._default.remote
350 if remote is None:
351 raise ManifestParseError, \
352 "no remote for project %s within %s" % \
353 (name, self.manifestFile)
354
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700355 revisionExpr = node.getAttribute('revision')
356 if not revisionExpr:
357 revisionExpr = self._default.revisionExpr
358 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700359 raise ManifestParseError, \
360 "no revision for project %s within %s" % \
361 (name, self.manifestFile)
362
363 path = node.getAttribute('path')
364 if not path:
365 path = name
366 if path.startswith('/'):
367 raise ManifestParseError, \
368 "project %s path cannot be absolute in %s" % \
369 (name, self.manifestFile)
370
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800371 if self.IsMirror:
372 relpath = None
373 worktree = None
374 gitdir = os.path.join(self.topdir, '%s.git' % name)
375 else:
376 worktree = os.path.join(self.topdir, path)
377 gitdir = os.path.join(self.repodir, 'projects/%s.git' % path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700378
379 project = Project(manifest = self,
380 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700381 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700382 gitdir = gitdir,
383 worktree = worktree,
384 relpath = path,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700385 revisionExpr = revisionExpr,
386 revisionId = None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700387
388 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700389 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700390 self._ParseCopyFile(project, n)
391
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700392 return project
393
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700394 def _ParseCopyFile(self, project, node):
395 src = self._reqatt(node, 'src')
396 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800397 if not self.IsMirror:
398 # src is project relative;
399 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800400 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700401
402 def _get_remote(self, node):
403 name = node.getAttribute('remote')
404 if not name:
405 return None
406
407 v = self._remotes.get(name)
408 if not v:
409 raise ManifestParseError, \
410 "remote %s not defined in %s" % \
411 (name, self.manifestFile)
412 return v
413
414 def _reqatt(self, node, attname):
415 """
416 reads a required attribute from the node.
417 """
418 v = node.getAttribute(attname)
419 if not v:
420 raise ManifestParseError, \
421 "no %s in <%s> within %s" % \
422 (attname, node.nodeName, self.manifestFile)
423 return v