blob: 971cf21252a7094e94ae35bfae87a76d91c838fd [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. Pearcef1a6b142009-06-03 16:01:11 -070021from manifest import Manifest
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070022from project import RemoteSpec, Project, MetaProject, R_HEADS, HEAD
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070023from 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
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -070031 revisionExpr = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070032 remote = None
33
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070034class _XmlRemote(object):
35 def __init__(self,
36 name,
37 fetch=None,
38 review=None):
39 self.name = name
40 self.fetchUrl = fetch
41 self.reviewUrl = review
42
43 def ToRemoteSpec(self, projectName):
44 url = self.fetchUrl
45 while url.endswith('/'):
46 url = url[:-1]
47 url += '/%s.git' % projectName
48 return RemoteSpec(self.name, url, self.reviewUrl)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070049
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -070050class XmlManifest(Manifest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070051 """manages the repo configuration file"""
52
53 def __init__(self, repodir):
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -070054 Manifest.__init__(self, repodir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070055
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -070056 self._manifestFile = os.path.join(repodir, MANIFEST_FILE_NAME)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070057 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -080058 gitdir = os.path.join(repodir, 'manifests.git'),
59 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070060
61 self._Unload()
62
63 def Link(self, name):
64 """Update the repo metadata to use a different manifest.
65 """
66 path = os.path.join(self.manifestProject.worktree, name)
67 if not os.path.isfile(path):
68 raise ManifestParseError('manifest %s not found' % name)
69
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -070070 old = self._manifestFile
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070071 try:
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -070072 self._manifestFile = path
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070073 self._Unload()
74 self._Load()
75 finally:
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -070076 self._manifestFile = old
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070077
78 try:
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -070079 if os.path.exists(self._manifestFile):
80 os.remove(self._manifestFile)
81 os.symlink('manifests/%s' % name, self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070082 except OSError, e:
83 raise ManifestParseError('cannot link manifest %s' % name)
84
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080085 def _RemoteToXml(self, r, doc, root):
86 e = doc.createElement('remote')
87 root.appendChild(e)
88 e.setAttribute('name', r.name)
89 e.setAttribute('fetch', r.fetchUrl)
90 if r.reviewUrl is not None:
91 e.setAttribute('review', r.reviewUrl)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080092
93 def Save(self, fd, peg_rev=False):
94 """Write the current manifest out to the given file descriptor.
95 """
96 doc = xml.dom.minidom.Document()
97 root = doc.createElement('manifest')
98 doc.appendChild(root)
99
100 d = self.default
101 sort_remotes = list(self.remotes.keys())
102 sort_remotes.sort()
103
104 for r in sort_remotes:
105 self._RemoteToXml(self.remotes[r], doc, root)
106 if self.remotes:
107 root.appendChild(doc.createTextNode(''))
108
109 have_default = False
110 e = doc.createElement('default')
111 if d.remote:
112 have_default = True
113 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700114 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800115 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700116 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800117 if have_default:
118 root.appendChild(e)
119 root.appendChild(doc.createTextNode(''))
120
121 sort_projects = list(self.projects.keys())
122 sort_projects.sort()
123
124 for p in sort_projects:
125 p = self.projects[p]
126 e = doc.createElement('project')
127 root.appendChild(e)
128 e.setAttribute('name', p.name)
129 if p.relpath != p.name:
130 e.setAttribute('path', p.relpath)
131 if not d.remote or p.remote.name != d.remote.name:
132 e.setAttribute('remote', p.remote.name)
133 if peg_rev:
134 if self.IsMirror:
135 e.setAttribute('revision',
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700136 p.bare_git.rev_parse(p.revisionExpr + '^0'))
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800137 else:
138 e.setAttribute('revision',
139 p.work_git.rev_parse(HEAD + '^0'))
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700140 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
141 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800142
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800143 for c in p.copyfiles:
144 ce = doc.createElement('copyfile')
145 ce.setAttribute('src', c.src)
146 ce.setAttribute('dest', c.dest)
147 e.appendChild(ce)
148
149 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
150
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700151 @property
152 def projects(self):
153 self._Load()
154 return self._projects
155
156 @property
157 def remotes(self):
158 self._Load()
159 return self._remotes
160
161 @property
162 def default(self):
163 self._Load()
164 return self._default
165
166 def _Unload(self):
167 self._loaded = False
168 self._projects = {}
169 self._remotes = {}
170 self._default = None
171 self.branch = None
172
173 def _Load(self):
174 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800175 m = self.manifestProject
176 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700177 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800178 b = b[len(R_HEADS):]
179 self.branch = b
180
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700181 self._ParseManifest(True)
182
183 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
184 if os.path.exists(local):
185 try:
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700186 real = self._manifestFile
187 self._manifestFile = local
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700188 self._ParseManifest(False)
189 finally:
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700190 self._manifestFile = real
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700191
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800192 if self.IsMirror:
193 self._AddMetaProjectMirror(self.repoProject)
194 self._AddMetaProjectMirror(self.manifestProject)
195
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700196 self._loaded = True
197
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700198 def _ParseManifest(self, is_root_file):
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700199 root = xml.dom.minidom.parse(self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700200 if not root or not root.childNodes:
201 raise ManifestParseError, \
202 "no root node in %s" % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700203 self._manifestFile
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700204
205 config = root.childNodes[0]
206 if config.nodeName != 'manifest':
207 raise ManifestParseError, \
208 "no <manifest> in %s" % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700209 self._manifestFile
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700210
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700211 for node in config.childNodes:
Shawn O. Pearce03eaf072008-11-20 11:42:22 -0800212 if node.nodeName == 'remove-project':
213 name = self._reqatt(node, 'name')
214 try:
215 del self._projects[name]
216 except KeyError:
217 raise ManifestParseError, \
218 'project %s not found' % \
219 (name)
220
221 for node in config.childNodes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700222 if node.nodeName == 'remote':
223 remote = self._ParseRemote(node)
224 if self._remotes.get(remote.name):
225 raise ManifestParseError, \
226 'duplicate remote %s in %s' % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700227 (remote.name, self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700228 self._remotes[remote.name] = remote
229
230 for node in config.childNodes:
231 if node.nodeName == 'default':
232 if self._default is not None:
233 raise ManifestParseError, \
234 'duplicate default in %s' % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700235 (self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700236 self._default = self._ParseDefault(node)
237 if self._default is None:
238 self._default = _Default()
239
240 for node in config.childNodes:
241 if node.nodeName == 'project':
242 project = self._ParseProject(node)
243 if self._projects.get(project.name):
244 raise ManifestParseError, \
245 'duplicate project %s in %s' % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700246 (project.name, self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700247 self._projects[project.name] = project
248
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800249 def _AddMetaProjectMirror(self, m):
250 name = None
251 m_url = m.GetRemote(m.remote.name).url
252 if m_url.endswith('/.git'):
253 raise ManifestParseError, 'refusing to mirror %s' % m_url
254
255 if self._default and self._default.remote:
256 url = self._default.remote.fetchUrl
257 if not url.endswith('/'):
258 url += '/'
259 if m_url.startswith(url):
260 remote = self._default.remote
261 name = m_url[len(url):]
262
263 if name is None:
264 s = m_url.rindex('/') + 1
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700265 remote = _XmlRemote('origin', m_url[:s])
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800266 name = m_url[s:]
267
268 if name.endswith('.git'):
269 name = name[:-4]
270
271 if name not in self._projects:
272 m.PreSync()
273 gitdir = os.path.join(self.topdir, '%s.git' % name)
274 project = Project(manifest = self,
275 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700276 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800277 gitdir = gitdir,
278 worktree = None,
279 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700280 revisionExpr = m.revisionExpr,
281 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800282 self._projects[project.name] = project
283
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700284 def _ParseRemote(self, node):
285 """
286 reads a <remote> element from the manifest file
287 """
288 name = self._reqatt(node, 'name')
289 fetch = self._reqatt(node, 'fetch')
290 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800291 if review == '':
292 review = None
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700293 return _XmlRemote(name, fetch, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700294
295 def _ParseDefault(self, node):
296 """
297 reads a <default> element from the manifest file
298 """
299 d = _Default()
300 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700301 d.revisionExpr = node.getAttribute('revision')
302 if d.revisionExpr == '':
303 d.revisionExpr = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700304 return d
305
306 def _ParseProject(self, node):
307 """
308 reads a <project> element from the manifest file
309 """
310 name = self._reqatt(node, 'name')
311
312 remote = self._get_remote(node)
313 if remote is None:
314 remote = self._default.remote
315 if remote is None:
316 raise ManifestParseError, \
317 "no remote for project %s within %s" % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700318 (name, self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700319
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700320 revisionExpr = node.getAttribute('revision')
321 if not revisionExpr:
322 revisionExpr = self._default.revisionExpr
323 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700324 raise ManifestParseError, \
325 "no revision for project %s within %s" % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700326 (name, self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700327
328 path = node.getAttribute('path')
329 if not path:
330 path = name
331 if path.startswith('/'):
332 raise ManifestParseError, \
333 "project %s path cannot be absolute in %s" % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700334 (name, self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700335
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800336 if self.IsMirror:
337 relpath = None
338 worktree = None
339 gitdir = os.path.join(self.topdir, '%s.git' % name)
340 else:
341 worktree = os.path.join(self.topdir, path)
342 gitdir = os.path.join(self.repodir, 'projects/%s.git' % path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700343
344 project = Project(manifest = self,
345 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700346 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700347 gitdir = gitdir,
348 worktree = worktree,
349 relpath = path,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700350 revisionExpr = revisionExpr,
351 revisionId = None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700352
353 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700354 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700355 self._ParseCopyFile(project, n)
356
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700357 return project
358
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700359 def _ParseCopyFile(self, project, node):
360 src = self._reqatt(node, 'src')
361 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800362 if not self.IsMirror:
363 # src is project relative;
364 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800365 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700366
367 def _get_remote(self, node):
368 name = node.getAttribute('remote')
369 if not name:
370 return None
371
372 v = self._remotes.get(name)
373 if not v:
374 raise ManifestParseError, \
375 "remote %s not defined in %s" % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700376 (name, self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700377 return v
378
379 def _reqatt(self, node, attname):
380 """
381 reads a required attribute from the node.
382 """
383 v = node.getAttribute(attname)
384 if not v:
385 raise ManifestParseError, \
386 "no %s in <%s> within %s" % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700387 (attname, node.nodeName, self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700388 return v