blob: 45896be93e9361dea43782ee8fd09fdd8bf4c0ad [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
192 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700193 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800194 b = b[len(R_HEADS):]
195 self.branch = b
196
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700197 self._ParseManifest(True)
198
199 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
200 if os.path.exists(local):
201 try:
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700202 real = self._manifestFile
203 self._manifestFile = local
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700204 self._ParseManifest(False)
205 finally:
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700206 self._manifestFile = real
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700207
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800208 if self.IsMirror:
209 self._AddMetaProjectMirror(self.repoProject)
210 self._AddMetaProjectMirror(self.manifestProject)
211
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700212 self._loaded = True
213
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700214 def _ParseManifest(self, is_root_file):
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700215 root = xml.dom.minidom.parse(self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700216 if not root or not root.childNodes:
217 raise ManifestParseError, \
218 "no root node in %s" % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700219 self._manifestFile
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700220
221 config = root.childNodes[0]
222 if config.nodeName != 'manifest':
223 raise ManifestParseError, \
224 "no <manifest> in %s" % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700225 self._manifestFile
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700226
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700227 for node in config.childNodes:
Shawn O. Pearce03eaf072008-11-20 11:42:22 -0800228 if node.nodeName == 'remove-project':
229 name = self._reqatt(node, 'name')
230 try:
231 del self._projects[name]
232 except KeyError:
233 raise ManifestParseError, \
234 'project %s not found' % \
235 (name)
236
237 for node in config.childNodes:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700238 if node.nodeName == 'remote':
239 remote = self._ParseRemote(node)
240 if self._remotes.get(remote.name):
241 raise ManifestParseError, \
242 'duplicate remote %s in %s' % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700243 (remote.name, self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700244 self._remotes[remote.name] = remote
245
246 for node in config.childNodes:
247 if node.nodeName == 'default':
248 if self._default is not None:
249 raise ManifestParseError, \
250 'duplicate default in %s' % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700251 (self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700252 self._default = self._ParseDefault(node)
253 if self._default is None:
254 self._default = _Default()
255
256 for node in config.childNodes:
257 if node.nodeName == 'project':
258 project = self._ParseProject(node)
259 if self._projects.get(project.name):
260 raise ManifestParseError, \
261 'duplicate project %s in %s' % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700262 (project.name, self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700263 self._projects[project.name] = project
264
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800265 def _AddMetaProjectMirror(self, m):
266 name = None
267 m_url = m.GetRemote(m.remote.name).url
268 if m_url.endswith('/.git'):
269 raise ManifestParseError, 'refusing to mirror %s' % m_url
270
271 if self._default and self._default.remote:
272 url = self._default.remote.fetchUrl
273 if not url.endswith('/'):
274 url += '/'
275 if m_url.startswith(url):
276 remote = self._default.remote
277 name = m_url[len(url):]
278
279 if name is None:
280 s = m_url.rindex('/') + 1
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700281 remote = _XmlRemote('origin', m_url[:s])
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800282 name = m_url[s:]
283
284 if name.endswith('.git'):
285 name = name[:-4]
286
287 if name not in self._projects:
288 m.PreSync()
289 gitdir = os.path.join(self.topdir, '%s.git' % name)
290 project = Project(manifest = self,
291 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700292 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800293 gitdir = gitdir,
294 worktree = None,
295 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700296 revisionExpr = m.revisionExpr,
297 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800298 self._projects[project.name] = project
299
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700300 def _ParseRemote(self, node):
301 """
302 reads a <remote> element from the manifest file
303 """
304 name = self._reqatt(node, 'name')
305 fetch = self._reqatt(node, 'fetch')
306 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800307 if review == '':
308 review = None
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700309 return _XmlRemote(name, fetch, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700310
311 def _ParseDefault(self, node):
312 """
313 reads a <default> element from the manifest file
314 """
315 d = _Default()
316 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700317 d.revisionExpr = node.getAttribute('revision')
318 if d.revisionExpr == '':
319 d.revisionExpr = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700320 return d
321
322 def _ParseProject(self, node):
323 """
324 reads a <project> element from the manifest file
325 """
326 name = self._reqatt(node, 'name')
327
328 remote = self._get_remote(node)
329 if remote is None:
330 remote = self._default.remote
331 if remote is None:
332 raise ManifestParseError, \
333 "no remote for project %s within %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. Pearce3c8dea12009-05-29 18:38:17 -0700336 revisionExpr = node.getAttribute('revision')
337 if not revisionExpr:
338 revisionExpr = self._default.revisionExpr
339 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700340 raise ManifestParseError, \
341 "no revision for project %s within %s" % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700342 (name, self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700343
344 path = node.getAttribute('path')
345 if not path:
346 path = name
347 if path.startswith('/'):
348 raise ManifestParseError, \
349 "project %s path cannot be absolute in %s" % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700350 (name, self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700351
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800352 if self.IsMirror:
353 relpath = None
354 worktree = None
355 gitdir = os.path.join(self.topdir, '%s.git' % name)
356 else:
357 worktree = os.path.join(self.topdir, path)
358 gitdir = os.path.join(self.repodir, 'projects/%s.git' % path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700359
360 project = Project(manifest = self,
361 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700362 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700363 gitdir = gitdir,
364 worktree = worktree,
365 relpath = path,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700366 revisionExpr = revisionExpr,
367 revisionId = None)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700368
369 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700370 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700371 self._ParseCopyFile(project, n)
372
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700373 return project
374
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700375 def _ParseCopyFile(self, project, node):
376 src = self._reqatt(node, 'src')
377 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800378 if not self.IsMirror:
379 # src is project relative;
380 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800381 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700382
383 def _get_remote(self, node):
384 name = node.getAttribute('remote')
385 if not name:
386 return None
387
388 v = self._remotes.get(name)
389 if not v:
390 raise ManifestParseError, \
391 "remote %s not defined in %s" % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700392 (name, self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700393 return v
394
395 def _reqatt(self, node, attname):
396 """
397 reads a required attribute from the node.
398 """
399 v = node.getAttribute(attname)
400 if not v:
401 raise ManifestParseError, \
402 "no %s in <%s> within %s" % \
Shawn O. Pearcef1a6b142009-06-03 16:01:11 -0700403 (attname, node.nodeName, self._manifestFile)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700404 return v