blob: 5c5617ac917dc1a55a997078eaa5f167da04d9d2 [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
30 revision = None
31 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
68 def Link(self, name):
69 """Update the repo metadata to use a different manifest.
70 """
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
83 try:
84 if os.path.exists(self.manifestFile):
85 os.remove(self.manifestFile)
86 os.symlink('manifests/%s' % name, self.manifestFile)
87 except OSError, e:
88 raise ManifestParseError('cannot link manifest %s' % name)
89
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080090 def _RemoteToXml(self, r, doc, root):
91 e = doc.createElement('remote')
92 root.appendChild(e)
93 e.setAttribute('name', r.name)
94 e.setAttribute('fetch', r.fetchUrl)
95 if r.reviewUrl is not None:
96 e.setAttribute('review', r.reviewUrl)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080097
98 def Save(self, fd, peg_rev=False):
99 """Write the current manifest out to the given file descriptor.
100 """
101 doc = xml.dom.minidom.Document()
102 root = doc.createElement('manifest')
103 doc.appendChild(root)
104
105 d = self.default
106 sort_remotes = list(self.remotes.keys())
107 sort_remotes.sort()
108
109 for r in sort_remotes:
110 self._RemoteToXml(self.remotes[r], doc, root)
111 if self.remotes:
112 root.appendChild(doc.createTextNode(''))
113
114 have_default = False
115 e = doc.createElement('default')
116 if d.remote:
117 have_default = True
118 e.setAttribute('remote', d.remote.name)
119 if d.revision:
120 have_default = True
121 e.setAttribute('revision', d.revision)
122 if have_default:
123 root.appendChild(e)
124 root.appendChild(doc.createTextNode(''))
125
126 sort_projects = list(self.projects.keys())
127 sort_projects.sort()
128
129 for p in sort_projects:
130 p = self.projects[p]
131 e = doc.createElement('project')
132 root.appendChild(e)
133 e.setAttribute('name', p.name)
134 if p.relpath != p.name:
135 e.setAttribute('path', p.relpath)
136 if not d.remote or p.remote.name != d.remote.name:
137 e.setAttribute('remote', p.remote.name)
138 if peg_rev:
139 if self.IsMirror:
140 e.setAttribute('revision',
141 p.bare_git.rev_parse(p.revision + '^0'))
142 else:
143 e.setAttribute('revision',
144 p.work_git.rev_parse(HEAD + '^0'))
145 elif not d.revision or p.revision != d.revision:
146 e.setAttribute('revision', p.revision)
147
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800148 for c in p.copyfiles:
149 ce = doc.createElement('copyfile')
150 ce.setAttribute('src', c.src)
151 ce.setAttribute('dest', c.dest)
152 e.appendChild(ce)
153
154 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
155
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700156 @property
157 def projects(self):
158 self._Load()
159 return self._projects
160
161 @property
162 def remotes(self):
163 self._Load()
164 return self._remotes
165
166 @property
167 def default(self):
168 self._Load()
169 return self._default
170
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800171 @property
172 def IsMirror(self):
173 return self.manifestProject.config.GetBoolean('repo.mirror')
174
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700175 def _Unload(self):
176 self._loaded = False
177 self._projects = {}
178 self._remotes = {}
179 self._default = None
180 self.branch = None
181
182 def _Load(self):
183 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800184 m = self.manifestProject
185 b = m.GetBranch(m.CurrentBranch).merge
186 if b.startswith(R_HEADS):
187 b = b[len(R_HEADS):]
188 self.branch = b
189
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700190 self._ParseManifest(True)
191
192 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
193 if os.path.exists(local):
194 try:
195 real = self.manifestFile
196 self.manifestFile = local
197 self._ParseManifest(False)
198 finally:
199 self.manifestFile = real
200
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800201 if self.IsMirror:
202 self._AddMetaProjectMirror(self.repoProject)
203 self._AddMetaProjectMirror(self.manifestProject)
204
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700205 self._loaded = True
206
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700207 def _ParseManifest(self, is_root_file):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700208 root = xml.dom.minidom.parse(self.manifestFile)
209 if not root or not root.childNodes:
210 raise ManifestParseError, \
211 "no root node in %s" % \
212 self.manifestFile
213
214 config = root.childNodes[0]
215 if config.nodeName != 'manifest':
216 raise ManifestParseError, \
217 "no <manifest> in %s" % \
218 self.manifestFile
219
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700220 for node in config.childNodes:
Shawn O. Pearce03eaf072008-11-20 11:42:22 -0800221 if node.nodeName == 'remove-project':
222 name = self._reqatt(node, 'name')
223 try:
224 del self._projects[name]
225 except KeyError:
226 raise ManifestParseError, \
227 'project %s not found' % \
228 (name)
229
230 for node in config.childNodes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700231 if node.nodeName == 'remote':
232 remote = self._ParseRemote(node)
233 if self._remotes.get(remote.name):
234 raise ManifestParseError, \
235 'duplicate remote %s in %s' % \
236 (remote.name, self.manifestFile)
237 self._remotes[remote.name] = remote
238
239 for node in config.childNodes:
240 if node.nodeName == 'default':
241 if self._default is not None:
242 raise ManifestParseError, \
243 'duplicate default in %s' % \
244 (self.manifestFile)
245 self._default = self._ParseDefault(node)
246 if self._default is None:
247 self._default = _Default()
248
249 for node in config.childNodes:
250 if node.nodeName == 'project':
251 project = self._ParseProject(node)
252 if self._projects.get(project.name):
253 raise ManifestParseError, \
254 'duplicate project %s in %s' % \
255 (project.name, self.manifestFile)
256 self._projects[project.name] = project
257
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800258 def _AddMetaProjectMirror(self, m):
259 name = None
260 m_url = m.GetRemote(m.remote.name).url
261 if m_url.endswith('/.git'):
262 raise ManifestParseError, 'refusing to mirror %s' % m_url
263
264 if self._default and self._default.remote:
265 url = self._default.remote.fetchUrl
266 if not url.endswith('/'):
267 url += '/'
268 if m_url.startswith(url):
269 remote = self._default.remote
270 name = m_url[len(url):]
271
272 if name is None:
273 s = m_url.rindex('/') + 1
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700274 remote = _XmlRemote('origin', m_url[:s])
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800275 name = m_url[s:]
276
277 if name.endswith('.git'):
278 name = name[:-4]
279
280 if name not in self._projects:
281 m.PreSync()
282 gitdir = os.path.join(self.topdir, '%s.git' % name)
283 project = Project(manifest = self,
284 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700285 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800286 gitdir = gitdir,
287 worktree = None,
288 relpath = None,
289 revision = m.revision)
290 self._projects[project.name] = project
291
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700292 def _ParseRemote(self, node):
293 """
294 reads a <remote> element from the manifest file
295 """
296 name = self._reqatt(node, 'name')
297 fetch = self._reqatt(node, 'fetch')
298 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800299 if review == '':
300 review = None
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700301 return _XmlRemote(name, fetch, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700302
303 def _ParseDefault(self, node):
304 """
305 reads a <default> element from the manifest file
306 """
307 d = _Default()
308 d.remote = self._get_remote(node)
309 d.revision = node.getAttribute('revision')
Shawn O. Pearce5d40e262008-11-06 11:07:42 -0800310 if d.revision == '':
311 d.revision = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700312 return d
313
314 def _ParseProject(self, node):
315 """
316 reads a <project> element from the manifest file
317 """
318 name = self._reqatt(node, 'name')
319
320 remote = self._get_remote(node)
321 if remote is None:
322 remote = self._default.remote
323 if remote is None:
324 raise ManifestParseError, \
325 "no remote for project %s within %s" % \
326 (name, self.manifestFile)
327
328 revision = node.getAttribute('revision')
329 if not revision:
330 revision = self._default.revision
331 if not revision:
332 raise ManifestParseError, \
333 "no revision for project %s within %s" % \
334 (name, self.manifestFile)
335
336 path = node.getAttribute('path')
337 if not path:
338 path = name
339 if path.startswith('/'):
340 raise ManifestParseError, \
341 "project %s path cannot be absolute in %s" % \
342 (name, self.manifestFile)
343
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800344 if self.IsMirror:
345 relpath = None
346 worktree = None
347 gitdir = os.path.join(self.topdir, '%s.git' % name)
348 else:
349 worktree = os.path.join(self.topdir, path)
350 gitdir = os.path.join(self.repodir, 'projects/%s.git' % path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700351
352 project = Project(manifest = self,
353 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700354 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700355 gitdir = gitdir,
356 worktree = worktree,
357 relpath = path,
358 revision = revision)
359
360 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700361 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700362 self._ParseCopyFile(project, n)
363
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700364 return project
365
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700366 def _ParseCopyFile(self, project, node):
367 src = self._reqatt(node, 'src')
368 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800369 if not self.IsMirror:
370 # src is project relative;
371 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800372 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700373
374 def _get_remote(self, node):
375 name = node.getAttribute('remote')
376 if not name:
377 return None
378
379 v = self._remotes.get(name)
380 if not v:
381 raise ManifestParseError, \
382 "remote %s not defined in %s" % \
383 (name, self.manifestFile)
384 return v
385
386 def _reqatt(self, node, attname):
387 """
388 reads a required attribute from the node.
389 """
390 v = node.getAttribute(attname)
391 if not v:
392 raise ManifestParseError, \
393 "no %s in <%s> within %s" % \
394 (attname, node.nodeName, self.manifestFile)
395 return v