blob: d888653d915523fb25cad6d43dbcee9b85e65219 [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
Shawn O. Pearce20951792009-07-03 15:26:17 -070020from git_config import GitConfig
21from git_config import IsId
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -070022from manifest import Manifest
Shawn O. Pearce20951792009-07-03 15:26:17 -070023from project import RemoteSpec
24from project import Project
25from project import MetaProject
26from project import R_HEADS
27from project import HEAD
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070028from error import ManifestParseError
29
30MANIFEST_FILE_NAME = 'manifest.xml'
Shawn O. Pearce5cc66792008-10-23 16:19:27 -070031LOCAL_MANIFEST_NAME = 'local_manifest.xml'
Shawn O. Pearcecc6c7962009-07-03 15:29:02 -070032R_M = 'refs/remotes/m/'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070033
34class _Default(object):
35 """Project defaults within the manifest."""
36
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -070037 revisionExpr = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070038 remote = None
39
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070040class _XmlRemote(object):
41 def __init__(self,
42 name,
43 fetch=None,
44 review=None):
45 self.name = name
46 self.fetchUrl = fetch
47 self.reviewUrl = review
48
49 def ToRemoteSpec(self, projectName):
50 url = self.fetchUrl
51 while url.endswith('/'):
52 url = url[:-1]
53 url += '/%s.git' % projectName
54 return RemoteSpec(self.name, url, self.reviewUrl)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070055
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -070056class XmlManifest(Manifest):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070057 """manages the repo configuration file"""
58
59 def __init__(self, repodir):
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -070060 Manifest.__init__(self, repodir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070061
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -070062 self._manifestFile = os.path.join(repodir, MANIFEST_FILE_NAME)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070063 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -080064 gitdir = os.path.join(repodir, 'manifests.git'),
65 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070066
67 self._Unload()
68
69 def Link(self, name):
70 """Update the repo metadata to use a different manifest.
71 """
72 path = os.path.join(self.manifestProject.worktree, name)
73 if not os.path.isfile(path):
74 raise ManifestParseError('manifest %s not found' % name)
75
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -070076 old = self._manifestFile
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070077 try:
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -070078 self._manifestFile = path
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070079 self._Unload()
80 self._Load()
81 finally:
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -070082 self._manifestFile = old
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070083
84 try:
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -070085 if os.path.exists(self._manifestFile):
86 os.remove(self._manifestFile)
87 os.symlink('manifests/%s' % name, self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070088 except OSError, e:
89 raise ManifestParseError('cannot link manifest %s' % name)
90
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080091 def _RemoteToXml(self, r, doc, root):
92 e = doc.createElement('remote')
93 root.appendChild(e)
94 e.setAttribute('name', r.name)
95 e.setAttribute('fetch', r.fetchUrl)
96 if r.reviewUrl is not None:
97 e.setAttribute('review', r.reviewUrl)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -080098
99 def Save(self, fd, peg_rev=False):
100 """Write the current manifest out to the given file descriptor.
101 """
102 doc = xml.dom.minidom.Document()
103 root = doc.createElement('manifest')
104 doc.appendChild(root)
105
106 d = self.default
107 sort_remotes = list(self.remotes.keys())
108 sort_remotes.sort()
109
110 for r in sort_remotes:
111 self._RemoteToXml(self.remotes[r], doc, root)
112 if self.remotes:
113 root.appendChild(doc.createTextNode(''))
114
115 have_default = False
116 e = doc.createElement('default')
117 if d.remote:
118 have_default = True
119 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700120 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800121 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700122 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800123 if have_default:
124 root.appendChild(e)
125 root.appendChild(doc.createTextNode(''))
126
127 sort_projects = list(self.projects.keys())
128 sort_projects.sort()
129
130 for p in sort_projects:
131 p = self.projects[p]
132 e = doc.createElement('project')
133 root.appendChild(e)
134 e.setAttribute('name', p.name)
135 if p.relpath != p.name:
136 e.setAttribute('path', p.relpath)
137 if not d.remote or p.remote.name != d.remote.name:
138 e.setAttribute('remote', p.remote.name)
139 if peg_rev:
140 if self.IsMirror:
141 e.setAttribute('revision',
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700142 p.bare_git.rev_parse(p.revisionExpr + '^0'))
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800143 else:
144 e.setAttribute('revision',
145 p.work_git.rev_parse(HEAD + '^0'))
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700146 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
147 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800148
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800149 for c in p.copyfiles:
150 ce = doc.createElement('copyfile')
151 ce.setAttribute('src', c.src)
152 ce.setAttribute('dest', c.dest)
153 e.appendChild(ce)
154
155 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
156
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700157 @property
158 def projects(self):
159 self._Load()
160 return self._projects
161
162 @property
163 def remotes(self):
164 self._Load()
165 return self._remotes
166
167 @property
168 def default(self):
169 self._Load()
170 return self._default
171
Shawn O. Pearce75b87c82009-07-03 16:24:57 -0700172 def InitBranch(self):
173 m = self.manifestProject
174 if m.CurrentBranch is None:
175 return m.StartBranch('default')
176 return True
177
Shawn O. Pearcecc6c7962009-07-03 15:29:02 -0700178 def SetMRefs(self, project):
179 if self.branch:
180 project._InitAnyMRef(R_M + self.branch)
181
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700182 def _Unload(self):
183 self._loaded = False
184 self._projects = {}
185 self._remotes = {}
186 self._default = None
187 self.branch = None
188
189 def _Load(self):
190 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800191 m = self.manifestProject
Shawn O. Pearce5f947bb2009-07-03 17:24:17 -0700192 b = m.GetBranch(m.CurrentBranch)
193 if b.remote and b.remote.name:
194 m.remote.name = b.remote.name
195 b = b.merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700196 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800197 b = b[len(R_HEADS):]
198 self.branch = b
199
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700200 self._ParseManifest(True)
201
202 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
203 if os.path.exists(local):
204 try:
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700205 real = self._manifestFile
206 self._manifestFile = local
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700207 self._ParseManifest(False)
208 finally:
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700209 self._manifestFile = real
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700210
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800211 if self.IsMirror:
212 self._AddMetaProjectMirror(self.repoProject)
213 self._AddMetaProjectMirror(self.manifestProject)
214
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700215 self._loaded = True
216
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700217 def _ParseManifest(self, is_root_file):
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700218 root = xml.dom.minidom.parse(self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700219 if not root or not root.childNodes:
220 raise ManifestParseError, \
221 "no root node in %s" % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700222 self._manifestFile
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700223
224 config = root.childNodes[0]
225 if config.nodeName != 'manifest':
226 raise ManifestParseError, \
227 "no <manifest> in %s" % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700228 self._manifestFile
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700229
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700230 for node in config.childNodes:
Shawn O. Pearce03eaf072008-11-20 11:42:22 -0800231 if node.nodeName == 'remove-project':
232 name = self._reqatt(node, 'name')
233 try:
234 del self._projects[name]
235 except KeyError:
236 raise ManifestParseError, \
237 'project %s not found' % \
238 (name)
239
240 for node in config.childNodes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700241 if node.nodeName == 'remote':
242 remote = self._ParseRemote(node)
243 if self._remotes.get(remote.name):
244 raise ManifestParseError, \
245 'duplicate remote %s in %s' % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700246 (remote.name, self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700247 self._remotes[remote.name] = remote
248
249 for node in config.childNodes:
250 if node.nodeName == 'default':
251 if self._default is not None:
252 raise ManifestParseError, \
253 'duplicate default in %s' % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700254 (self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700255 self._default = self._ParseDefault(node)
256 if self._default is None:
257 self._default = _Default()
258
259 for node in config.childNodes:
260 if node.nodeName == 'project':
261 project = self._ParseProject(node)
262 if self._projects.get(project.name):
263 raise ManifestParseError, \
264 'duplicate project %s in %s' % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700265 (project.name, self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700266 self._projects[project.name] = project
267
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800268 def _AddMetaProjectMirror(self, m):
269 name = None
270 m_url = m.GetRemote(m.remote.name).url
271 if m_url.endswith('/.git'):
272 raise ManifestParseError, 'refusing to mirror %s' % m_url
273
274 if self._default and self._default.remote:
275 url = self._default.remote.fetchUrl
276 if not url.endswith('/'):
277 url += '/'
278 if m_url.startswith(url):
279 remote = self._default.remote
280 name = m_url[len(url):]
281
282 if name is None:
283 s = m_url.rindex('/') + 1
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700284 remote = _XmlRemote('origin', m_url[:s])
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800285 name = m_url[s:]
286
287 if name.endswith('.git'):
288 name = name[:-4]
289
290 if name not in self._projects:
291 m.PreSync()
292 gitdir = os.path.join(self.topdir, '%s.git' % name)
293 project = Project(manifest = self,
294 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700295 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800296 gitdir = gitdir,
297 worktree = None,
298 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700299 revisionExpr = m.revisionExpr,
300 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800301 self._projects[project.name] = project
302
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700303 def _ParseRemote(self, node):
304 """
305 reads a <remote> element from the manifest file
306 """
307 name = self._reqatt(node, 'name')
308 fetch = self._reqatt(node, 'fetch')
309 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800310 if review == '':
311 review = None
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700312 return _XmlRemote(name, fetch, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700313
314 def _ParseDefault(self, node):
315 """
316 reads a <default> element from the manifest file
317 """
318 d = _Default()
319 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700320 d.revisionExpr = node.getAttribute('revision')
321 if d.revisionExpr == '':
322 d.revisionExpr = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700323 return d
324
325 def _ParseProject(self, node):
326 """
327 reads a <project> element from the manifest file
328 """
329 name = self._reqatt(node, 'name')
330
331 remote = self._get_remote(node)
332 if remote is None:
333 remote = self._default.remote
334 if remote is None:
335 raise ManifestParseError, \
336 "no remote for project %s within %s" % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700337 (name, self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700338
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700339 revisionExpr = node.getAttribute('revision')
340 if not revisionExpr:
341 revisionExpr = self._default.revisionExpr
342 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700343 raise ManifestParseError, \
344 "no revision for project %s within %s" % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700345 (name, self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700346
347 path = node.getAttribute('path')
348 if not path:
349 path = name
350 if path.startswith('/'):
351 raise ManifestParseError, \
352 "project %s path cannot be absolute in %s" % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700353 (name, self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700354
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800355 if self.IsMirror:
356 relpath = None
357 worktree = None
358 gitdir = os.path.join(self.topdir, '%s.git' % name)
359 else:
360 worktree = os.path.join(self.topdir, path)
361 gitdir = os.path.join(self.repodir, 'projects/%s.git' % path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700362
363 project = Project(manifest = self,
364 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700365 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700366 gitdir = gitdir,
367 worktree = worktree,
368 relpath = path,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700369 revisionExpr = revisionExpr,
370 revisionId = None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700371
372 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700373 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700374 self._ParseCopyFile(project, n)
375
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700376 return project
377
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700378 def _ParseCopyFile(self, project, node):
379 src = self._reqatt(node, 'src')
380 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800381 if not self.IsMirror:
382 # src is project relative;
383 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800384 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700385
386 def _get_remote(self, node):
387 name = node.getAttribute('remote')
388 if not name:
389 return None
390
391 v = self._remotes.get(name)
392 if not v:
393 raise ManifestParseError, \
394 "remote %s not defined in %s" % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700395 (name, self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700396 return v
397
398 def _reqatt(self, node, attname):
399 """
400 reads a required attribute from the node.
401 """
402 v = node.getAttribute(attname)
403 if not v:
404 raise ManifestParseError, \
405 "no %s in <%s> within %s" % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700406 (attname, node.nodeName, self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700407 return v