The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # |
| 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 | |
| 16 | import os |
| 17 | import sys |
| 18 | import xml.dom.minidom |
| 19 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | from git_config import GitConfig, IsId |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame^] | 21 | from project import Project, MetaProject, R_HEADS, HEAD |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 22 | from remote import Remote |
| 23 | from error import ManifestParseError |
| 24 | |
| 25 | MANIFEST_FILE_NAME = 'manifest.xml' |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 26 | LOCAL_MANIFEST_NAME = 'local_manifest.xml' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 27 | |
| 28 | class _Default(object): |
| 29 | """Project defaults within the manifest.""" |
| 30 | |
| 31 | revision = None |
| 32 | remote = None |
| 33 | |
| 34 | |
| 35 | class 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 Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 42 | self.globalConfig = GitConfig.ForUser() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 43 | |
| 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 Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 48 | self.manifestProject = MetaProject(self, 'manifests', |
Shawn O. Pearce | f5c25a6 | 2008-11-04 08:11:53 -0800 | [diff] [blame] | 49 | gitdir = os.path.join(repodir, 'manifests.git'), |
| 50 | worktree = os.path.join(repodir, 'manifests')) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 51 | |
| 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. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame^] | 76 | 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 Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 146 | @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. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 161 | @property |
| 162 | def IsMirror(self): |
| 163 | return self.manifestProject.config.GetBoolean('repo.mirror') |
| 164 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 165 | 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. Pearce | 2450a29 | 2008-11-04 08:22:07 -0800 | [diff] [blame] | 174 | 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. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 180 | 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. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 191 | if self.IsMirror: |
| 192 | self._AddMetaProjectMirror(self.repoProject) |
| 193 | self._AddMetaProjectMirror(self.manifestProject) |
| 194 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 195 | self._loaded = True |
| 196 | |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 197 | def _ParseManifest(self, is_root_file): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 198 | 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 Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 210 | for node in config.childNodes: |
Shawn O. Pearce | 03eaf07 | 2008-11-20 11:42:22 -0800 | [diff] [blame] | 211 | 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 Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 221 | 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. Pearce | 70939e2 | 2008-11-06 11:07:14 -0800 | [diff] [blame] | 248 | 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. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 258 | 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 Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 292 | 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. Pearce | ae6e094 | 2008-11-06 10:25:35 -0800 | [diff] [blame] | 299 | if review == '': |
| 300 | review = None |
| 301 | |
| 302 | projectName = node.getAttribute('project-name') |
| 303 | if projectName == '': |
| 304 | projectName = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 305 | |
| 306 | r = Remote(name=name, |
| 307 | fetch=fetch, |
Shawn O. Pearce | ae6e094 | 2008-11-06 10:25:35 -0800 | [diff] [blame] | 308 | review=review, |
| 309 | projectName=projectName) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 310 | |
| 311 | for n in node.childNodes: |
| 312 | if n.nodeName == 'require': |
| 313 | r.requiredCommits.append(self._reqatt(n, 'commit')) |
| 314 | |
| 315 | return r |
| 316 | |
| 317 | def _ParseDefault(self, node): |
| 318 | """ |
| 319 | reads a <default> element from the manifest file |
| 320 | """ |
| 321 | d = _Default() |
| 322 | d.remote = self._get_remote(node) |
| 323 | d.revision = node.getAttribute('revision') |
Shawn O. Pearce | 5d40e26 | 2008-11-06 11:07:42 -0800 | [diff] [blame] | 324 | if d.revision == '': |
| 325 | d.revision = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 326 | return d |
| 327 | |
| 328 | def _ParseProject(self, node): |
| 329 | """ |
| 330 | reads a <project> element from the manifest file |
| 331 | """ |
| 332 | name = self._reqatt(node, 'name') |
| 333 | |
| 334 | remote = self._get_remote(node) |
| 335 | if remote is None: |
| 336 | remote = self._default.remote |
| 337 | if remote is None: |
| 338 | raise ManifestParseError, \ |
| 339 | "no remote for project %s within %s" % \ |
| 340 | (name, self.manifestFile) |
| 341 | |
| 342 | revision = node.getAttribute('revision') |
| 343 | if not revision: |
| 344 | revision = self._default.revision |
| 345 | if not revision: |
| 346 | raise ManifestParseError, \ |
| 347 | "no revision for project %s within %s" % \ |
| 348 | (name, self.manifestFile) |
| 349 | |
| 350 | path = node.getAttribute('path') |
| 351 | if not path: |
| 352 | path = name |
| 353 | if path.startswith('/'): |
| 354 | raise ManifestParseError, \ |
| 355 | "project %s path cannot be absolute in %s" % \ |
| 356 | (name, self.manifestFile) |
| 357 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 358 | if self.IsMirror: |
| 359 | relpath = None |
| 360 | worktree = None |
| 361 | gitdir = os.path.join(self.topdir, '%s.git' % name) |
| 362 | else: |
| 363 | worktree = os.path.join(self.topdir, path) |
| 364 | gitdir = os.path.join(self.repodir, 'projects/%s.git' % path) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 365 | |
| 366 | project = Project(manifest = self, |
| 367 | name = name, |
| 368 | remote = remote, |
| 369 | gitdir = gitdir, |
| 370 | worktree = worktree, |
| 371 | relpath = path, |
| 372 | revision = revision) |
| 373 | |
| 374 | for n in node.childNodes: |
| 375 | if n.nodeName == 'remote': |
Shawn O. Pearce | 70939e2 | 2008-11-06 11:07:14 -0800 | [diff] [blame] | 376 | self._ParseProjectExtraRemote(project, n) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 377 | elif n.nodeName == 'copyfile': |
| 378 | self._ParseCopyFile(project, n) |
| 379 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 380 | return project |
| 381 | |
Shawn O. Pearce | 70939e2 | 2008-11-06 11:07:14 -0800 | [diff] [blame] | 382 | def _ParseProjectExtraRemote(self, project, n): |
| 383 | r = self._ParseRemote(n) |
| 384 | if project.extraRemotes.get(r.name) \ |
| 385 | or project.remote.name == r.name: |
| 386 | raise ManifestParseError, \ |
| 387 | 'duplicate remote %s in project %s in %s' % \ |
| 388 | (r.name, project.name, self.manifestFile) |
| 389 | project.extraRemotes[r.name] = r |
| 390 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 391 | def _ParseCopyFile(self, project, node): |
| 392 | src = self._reqatt(node, 'src') |
| 393 | dest = self._reqatt(node, 'dest') |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 394 | if not self.IsMirror: |
| 395 | # src is project relative; |
| 396 | # dest is relative to the top of the tree |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame^] | 397 | project.AddCopyFile(src, dest, os.path.join(self.topdir, dest)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 398 | |
| 399 | def _get_remote(self, node): |
| 400 | name = node.getAttribute('remote') |
| 401 | if not name: |
| 402 | return None |
| 403 | |
| 404 | v = self._remotes.get(name) |
| 405 | if not v: |
| 406 | raise ManifestParseError, \ |
| 407 | "remote %s not defined in %s" % \ |
| 408 | (name, self.manifestFile) |
| 409 | return v |
| 410 | |
| 411 | def _reqatt(self, node, attname): |
| 412 | """ |
| 413 | reads a required attribute from the node. |
| 414 | """ |
| 415 | v = node.getAttribute(attname) |
| 416 | if not v: |
| 417 | raise ManifestParseError, \ |
| 418 | "no %s in <%s> within %s" % \ |
| 419 | (attname, node.nodeName, self.manifestFile) |
| 420 | return v |