blob: 4b740e5a386f53ab8973b6b0884ef238764c05e8 [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. Pearcec7a4eef2009-03-05 10:32:38 -080021from project import Project, MetaProject, R_HEADS, HEAD
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070022from remote import Remote
23from error import ManifestParseError
24
25MANIFEST_FILE_NAME = 'manifest.xml'
Shawn O. Pearce5cc66792008-10-23 16:19:27 -070026LOCAL_MANIFEST_NAME = 'local_manifest.xml'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070027
28class _Default(object):
29 """Project defaults within the manifest."""
30
31 revision = None
32 remote = None
33
34
35class Manifest(object):
36 """manages the repo configuration file"""
37
38 def __init__(self, repodir):
39 self.repodir = os.path.abspath(repodir)
40 self.topdir = os.path.dirname(self.repodir)
41 self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070042 self.globalConfig = GitConfig.ForUser()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070043
44 self.repoProject = MetaProject(self, 'repo',
45 gitdir = os.path.join(repodir, 'repo/.git'),
46 worktree = os.path.join(repodir, 'repo'))
47
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070048 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -080049 gitdir = os.path.join(repodir, 'manifests.git'),
50 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070051
52 self._Unload()
53
54 def Link(self, name):
55 """Update the repo metadata to use a different manifest.
56 """
57 path = os.path.join(self.manifestProject.worktree, name)
58 if not os.path.isfile(path):
59 raise ManifestParseError('manifest %s not found' % name)
60
61 old = self.manifestFile
62 try:
63 self.manifestFile = path
64 self._Unload()
65 self._Load()
66 finally:
67 self.manifestFile = old
68
69 try:
70 if os.path.exists(self.manifestFile):
71 os.remove(self.manifestFile)
72 os.symlink('manifests/%s' % name, self.manifestFile)
73 except OSError, e:
74 raise ManifestParseError('cannot link manifest %s' % name)
75
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080076 def _RemoteToXml(self, r, doc, root):
77 e = doc.createElement('remote')
78 root.appendChild(e)
79 e.setAttribute('name', r.name)
80 e.setAttribute('fetch', r.fetchUrl)
81 if r.reviewUrl is not None:
82 e.setAttribute('review', r.reviewUrl)
83 if r.projectName is not None:
84 e.setAttribute('project-name', r.projectName)
85
86 def Save(self, fd, peg_rev=False):
87 """Write the current manifest out to the given file descriptor.
88 """
89 doc = xml.dom.minidom.Document()
90 root = doc.createElement('manifest')
91 doc.appendChild(root)
92
93 d = self.default
94 sort_remotes = list(self.remotes.keys())
95 sort_remotes.sort()
96
97 for r in sort_remotes:
98 self._RemoteToXml(self.remotes[r], doc, root)
99 if self.remotes:
100 root.appendChild(doc.createTextNode(''))
101
102 have_default = False
103 e = doc.createElement('default')
104 if d.remote:
105 have_default = True
106 e.setAttribute('remote', d.remote.name)
107 if d.revision:
108 have_default = True
109 e.setAttribute('revision', d.revision)
110 if have_default:
111 root.appendChild(e)
112 root.appendChild(doc.createTextNode(''))
113
114 sort_projects = list(self.projects.keys())
115 sort_projects.sort()
116
117 for p in sort_projects:
118 p = self.projects[p]
119 e = doc.createElement('project')
120 root.appendChild(e)
121 e.setAttribute('name', p.name)
122 if p.relpath != p.name:
123 e.setAttribute('path', p.relpath)
124 if not d.remote or p.remote.name != d.remote.name:
125 e.setAttribute('remote', p.remote.name)
126 if peg_rev:
127 if self.IsMirror:
128 e.setAttribute('revision',
129 p.bare_git.rev_parse(p.revision + '^0'))
130 else:
131 e.setAttribute('revision',
132 p.work_git.rev_parse(HEAD + '^0'))
133 elif not d.revision or p.revision != d.revision:
134 e.setAttribute('revision', p.revision)
135
136 for r in p.extraRemotes:
137 self._RemoteToXml(p.extraRemotes[r], doc, e)
138 for c in p.copyfiles:
139 ce = doc.createElement('copyfile')
140 ce.setAttribute('src', c.src)
141 ce.setAttribute('dest', c.dest)
142 e.appendChild(ce)
143
144 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
145
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700146 @property
147 def projects(self):
148 self._Load()
149 return self._projects
150
151 @property
152 def remotes(self):
153 self._Load()
154 return self._remotes
155
156 @property
157 def default(self):
158 self._Load()
159 return self._default
160
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800161 @property
162 def IsMirror(self):
163 return self.manifestProject.config.GetBoolean('repo.mirror')
164
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700165 def _Unload(self):
166 self._loaded = False
167 self._projects = {}
168 self._remotes = {}
169 self._default = None
170 self.branch = None
171
172 def _Load(self):
173 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800174 m = self.manifestProject
175 b = m.GetBranch(m.CurrentBranch).merge
176 if b.startswith(R_HEADS):
177 b = b[len(R_HEADS):]
178 self.branch = b
179
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700180 self._ParseManifest(True)
181
182 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
183 if os.path.exists(local):
184 try:
185 real = self.manifestFile
186 self.manifestFile = local
187 self._ParseManifest(False)
188 finally:
189 self.manifestFile = real
190
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800191 if self.IsMirror:
192 self._AddMetaProjectMirror(self.repoProject)
193 self._AddMetaProjectMirror(self.manifestProject)
194
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700195 self._loaded = True
196
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700197 def _ParseManifest(self, is_root_file):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700198 root = xml.dom.minidom.parse(self.manifestFile)
199 if not root or not root.childNodes:
200 raise ManifestParseError, \
201 "no root node in %s" % \
202 self.manifestFile
203
204 config = root.childNodes[0]
205 if config.nodeName != 'manifest':
206 raise ManifestParseError, \
207 "no <manifest> in %s" % \
208 self.manifestFile
209
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700210 for node in config.childNodes:
Shawn O. Pearce03eaf072008-11-20 11:42:22 -0800211 if node.nodeName == 'remove-project':
212 name = self._reqatt(node, 'name')
213 try:
214 del self._projects[name]
215 except KeyError:
216 raise ManifestParseError, \
217 'project %s not found' % \
218 (name)
219
220 for node in config.childNodes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700221 if node.nodeName == 'remote':
222 remote = self._ParseRemote(node)
223 if self._remotes.get(remote.name):
224 raise ManifestParseError, \
225 'duplicate remote %s in %s' % \
226 (remote.name, self.manifestFile)
227 self._remotes[remote.name] = remote
228
229 for node in config.childNodes:
230 if node.nodeName == 'default':
231 if self._default is not None:
232 raise ManifestParseError, \
233 'duplicate default in %s' % \
234 (self.manifestFile)
235 self._default = self._ParseDefault(node)
236 if self._default is None:
237 self._default = _Default()
238
239 for node in config.childNodes:
240 if node.nodeName == 'project':
241 project = self._ParseProject(node)
242 if self._projects.get(project.name):
243 raise ManifestParseError, \
244 'duplicate project %s in %s' % \
245 (project.name, self.manifestFile)
246 self._projects[project.name] = project
247
Shawn O. Pearce70939e22008-11-06 11:07:14 -0800248 for node in config.childNodes:
249 if node.nodeName == 'add-remote':
250 pn = self._reqatt(node, 'to-project')
251 project = self._projects.get(pn)
252 if not project:
253 raise ManifestParseError, \
254 'project %s not defined in %s' % \
255 (pn, self.manifestFile)
256 self._ParseProjectExtraRemote(project, node)
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
274 remote = Remote('origin', fetch = m_url[:s])
275 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,
285 remote = remote,
286 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
301
302 projectName = node.getAttribute('project-name')
303 if projectName == '':
304 projectName = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700305
306 r = Remote(name=name,
307 fetch=fetch,
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800308 review=review,
309 projectName=projectName)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700310 return r
311
312 def _ParseDefault(self, node):
313 """
314 reads a <default> element from the manifest file
315 """
316 d = _Default()
317 d.remote = self._get_remote(node)
318 d.revision = node.getAttribute('revision')
Shawn O. Pearce5d40e262008-11-06 11:07:42 -0800319 if d.revision == '':
320 d.revision = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700321 return d
322
323 def _ParseProject(self, node):
324 """
325 reads a <project> element from the manifest file
326 """
327 name = self._reqatt(node, 'name')
328
329 remote = self._get_remote(node)
330 if remote is None:
331 remote = self._default.remote
332 if remote is None:
333 raise ManifestParseError, \
334 "no remote for project %s within %s" % \
335 (name, self.manifestFile)
336
337 revision = node.getAttribute('revision')
338 if not revision:
339 revision = self._default.revision
340 if not revision:
341 raise ManifestParseError, \
342 "no revision for project %s within %s" % \
343 (name, self.manifestFile)
344
345 path = node.getAttribute('path')
346 if not path:
347 path = name
348 if path.startswith('/'):
349 raise ManifestParseError, \
350 "project %s path cannot be absolute in %s" % \
351 (name, self.manifestFile)
352
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800353 if self.IsMirror:
354 relpath = None
355 worktree = None
356 gitdir = os.path.join(self.topdir, '%s.git' % name)
357 else:
358 worktree = os.path.join(self.topdir, path)
359 gitdir = os.path.join(self.repodir, 'projects/%s.git' % path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700360
361 project = Project(manifest = self,
362 name = name,
363 remote = remote,
364 gitdir = gitdir,
365 worktree = worktree,
366 relpath = path,
367 revision = revision)
368
369 for n in node.childNodes:
370 if n.nodeName == 'remote':
Shawn O. Pearce70939e22008-11-06 11:07:14 -0800371 self._ParseProjectExtraRemote(project, n)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700372 elif n.nodeName == 'copyfile':
373 self._ParseCopyFile(project, n)
374
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700375 return project
376
Shawn O. Pearce70939e22008-11-06 11:07:14 -0800377 def _ParseProjectExtraRemote(self, project, n):
378 r = self._ParseRemote(n)
379 if project.extraRemotes.get(r.name) \
380 or project.remote.name == r.name:
381 raise ManifestParseError, \
382 'duplicate remote %s in project %s in %s' % \
383 (r.name, project.name, self.manifestFile)
384 project.extraRemotes[r.name] = r
385
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700386 def _ParseCopyFile(self, project, node):
387 src = self._reqatt(node, 'src')
388 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800389 if not self.IsMirror:
390 # src is project relative;
391 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800392 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700393
394 def _get_remote(self, node):
395 name = node.getAttribute('remote')
396 if not name:
397 return None
398
399 v = self._remotes.get(name)
400 if not v:
401 raise ManifestParseError, \
402 "remote %s not defined in %s" % \
403 (name, self.manifestFile)
404 return v
405
406 def _reqatt(self, node, attname):
407 """
408 reads a required attribute from the node.
409 """
410 v = node.getAttribute(attname)
411 if not v:
412 raise ManifestParseError, \
413 "no %s in <%s> within %s" % \
414 (attname, node.nodeName, self.manifestFile)
415 return v