blob: 51e653ede0fb2bb796b4bb7d86486663fd4e5ebb [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
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)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700119 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800120 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700121 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800122 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',
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700141 p.bare_git.rev_parse(p.revisionExpr + '^0'))
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800142 else:
143 e.setAttribute('revision',
144 p.work_git.rev_parse(HEAD + '^0'))
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700145 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
146 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800147
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,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700289 revisionExpr = m.revisionExpr,
290 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800291 self._projects[project.name] = project
292
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700293 def _ParseRemote(self, node):
294 """
295 reads a <remote> element from the manifest file
296 """
297 name = self._reqatt(node, 'name')
298 fetch = self._reqatt(node, 'fetch')
299 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800300 if review == '':
301 review = None
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700302 return _XmlRemote(name, fetch, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700303
304 def _ParseDefault(self, node):
305 """
306 reads a <default> element from the manifest file
307 """
308 d = _Default()
309 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700310 d.revisionExpr = node.getAttribute('revision')
311 if d.revisionExpr == '':
312 d.revisionExpr = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700313 return d
314
315 def _ParseProject(self, node):
316 """
317 reads a <project> element from the manifest file
318 """
319 name = self._reqatt(node, 'name')
320
321 remote = self._get_remote(node)
322 if remote is None:
323 remote = self._default.remote
324 if remote is None:
325 raise ManifestParseError, \
326 "no remote for project %s within %s" % \
327 (name, self.manifestFile)
328
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700329 revisionExpr = node.getAttribute('revision')
330 if not revisionExpr:
331 revisionExpr = self._default.revisionExpr
332 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700333 raise ManifestParseError, \
334 "no revision for project %s within %s" % \
335 (name, self.manifestFile)
336
337 path = node.getAttribute('path')
338 if not path:
339 path = name
340 if path.startswith('/'):
341 raise ManifestParseError, \
342 "project %s path cannot be absolute in %s" % \
343 (name, self.manifestFile)
344
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800345 if self.IsMirror:
346 relpath = None
347 worktree = None
348 gitdir = os.path.join(self.topdir, '%s.git' % name)
349 else:
350 worktree = os.path.join(self.topdir, path)
351 gitdir = os.path.join(self.repodir, 'projects/%s.git' % path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700352
353 project = Project(manifest = self,
354 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700355 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700356 gitdir = gitdir,
357 worktree = worktree,
358 relpath = path,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700359 revisionExpr = revisionExpr,
360 revisionId = None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700361
362 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700363 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700364 self._ParseCopyFile(project, n)
365
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700366 return project
367
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700368 def _ParseCopyFile(self, project, node):
369 src = self._reqatt(node, 'src')
370 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800371 if not self.IsMirror:
372 # src is project relative;
373 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800374 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700375
376 def _get_remote(self, node):
377 name = node.getAttribute('remote')
378 if not name:
379 return None
380
381 v = self._remotes.get(name)
382 if not v:
383 raise ManifestParseError, \
384 "remote %s not defined in %s" % \
385 (name, self.manifestFile)
386 return v
387
388 def _reqatt(self, node, attname):
389 """
390 reads a required attribute from the node.
391 """
392 v = node.getAttribute(attname)
393 if not v:
394 raise ManifestParseError, \
395 "no %s in <%s> within %s" % \
396 (attname, node.nodeName, self.manifestFile)
397 return v