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 | 2450a29 | 2008-11-04 08:22:07 -0800 | [diff] [blame] | 21 | from project import Project, MetaProject, R_HEADS |
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 | |
| 76 | @property |
| 77 | def projects(self): |
| 78 | self._Load() |
| 79 | return self._projects |
| 80 | |
| 81 | @property |
| 82 | def remotes(self): |
| 83 | self._Load() |
| 84 | return self._remotes |
| 85 | |
| 86 | @property |
| 87 | def default(self): |
| 88 | self._Load() |
| 89 | return self._default |
| 90 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 91 | @property |
| 92 | def IsMirror(self): |
| 93 | return self.manifestProject.config.GetBoolean('repo.mirror') |
| 94 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 95 | def _Unload(self): |
| 96 | self._loaded = False |
| 97 | self._projects = {} |
| 98 | self._remotes = {} |
| 99 | self._default = None |
| 100 | self.branch = None |
| 101 | |
| 102 | def _Load(self): |
| 103 | if not self._loaded: |
Shawn O. Pearce | 2450a29 | 2008-11-04 08:22:07 -0800 | [diff] [blame] | 104 | m = self.manifestProject |
| 105 | b = m.GetBranch(m.CurrentBranch).merge |
| 106 | if b.startswith(R_HEADS): |
| 107 | b = b[len(R_HEADS):] |
| 108 | self.branch = b |
| 109 | |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 110 | self._ParseManifest(True) |
| 111 | |
| 112 | local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME) |
| 113 | if os.path.exists(local): |
| 114 | try: |
| 115 | real = self.manifestFile |
| 116 | self.manifestFile = local |
| 117 | self._ParseManifest(False) |
| 118 | finally: |
| 119 | self.manifestFile = real |
| 120 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 121 | if self.IsMirror: |
| 122 | self._AddMetaProjectMirror(self.repoProject) |
| 123 | self._AddMetaProjectMirror(self.manifestProject) |
| 124 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 125 | self._loaded = True |
| 126 | |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 127 | def _ParseManifest(self, is_root_file): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 128 | root = xml.dom.minidom.parse(self.manifestFile) |
| 129 | if not root or not root.childNodes: |
| 130 | raise ManifestParseError, \ |
| 131 | "no root node in %s" % \ |
| 132 | self.manifestFile |
| 133 | |
| 134 | config = root.childNodes[0] |
| 135 | if config.nodeName != 'manifest': |
| 136 | raise ManifestParseError, \ |
| 137 | "no <manifest> in %s" % \ |
| 138 | self.manifestFile |
| 139 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 140 | for node in config.childNodes: |
| 141 | if node.nodeName == 'remote': |
| 142 | remote = self._ParseRemote(node) |
| 143 | if self._remotes.get(remote.name): |
| 144 | raise ManifestParseError, \ |
| 145 | 'duplicate remote %s in %s' % \ |
| 146 | (remote.name, self.manifestFile) |
| 147 | self._remotes[remote.name] = remote |
| 148 | |
| 149 | for node in config.childNodes: |
| 150 | if node.nodeName == 'default': |
| 151 | if self._default is not None: |
| 152 | raise ManifestParseError, \ |
| 153 | 'duplicate default in %s' % \ |
| 154 | (self.manifestFile) |
| 155 | self._default = self._ParseDefault(node) |
| 156 | if self._default is None: |
| 157 | self._default = _Default() |
| 158 | |
| 159 | for node in config.childNodes: |
| 160 | if node.nodeName == 'project': |
| 161 | project = self._ParseProject(node) |
| 162 | if self._projects.get(project.name): |
| 163 | raise ManifestParseError, \ |
| 164 | 'duplicate project %s in %s' % \ |
| 165 | (project.name, self.manifestFile) |
| 166 | self._projects[project.name] = project |
| 167 | |
Shawn O. Pearce | 70939e2 | 2008-11-06 11:07:14 -0800 | [diff] [blame] | 168 | for node in config.childNodes: |
| 169 | if node.nodeName == 'add-remote': |
| 170 | pn = self._reqatt(node, 'to-project') |
| 171 | project = self._projects.get(pn) |
| 172 | if not project: |
| 173 | raise ManifestParseError, \ |
| 174 | 'project %s not defined in %s' % \ |
| 175 | (pn, self.manifestFile) |
| 176 | self._ParseProjectExtraRemote(project, node) |
| 177 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 178 | def _AddMetaProjectMirror(self, m): |
| 179 | name = None |
| 180 | m_url = m.GetRemote(m.remote.name).url |
| 181 | if m_url.endswith('/.git'): |
| 182 | raise ManifestParseError, 'refusing to mirror %s' % m_url |
| 183 | |
| 184 | if self._default and self._default.remote: |
| 185 | url = self._default.remote.fetchUrl |
| 186 | if not url.endswith('/'): |
| 187 | url += '/' |
| 188 | if m_url.startswith(url): |
| 189 | remote = self._default.remote |
| 190 | name = m_url[len(url):] |
| 191 | |
| 192 | if name is None: |
| 193 | s = m_url.rindex('/') + 1 |
| 194 | remote = Remote('origin', fetch = m_url[:s]) |
| 195 | name = m_url[s:] |
| 196 | |
| 197 | if name.endswith('.git'): |
| 198 | name = name[:-4] |
| 199 | |
| 200 | if name not in self._projects: |
| 201 | m.PreSync() |
| 202 | gitdir = os.path.join(self.topdir, '%s.git' % name) |
| 203 | project = Project(manifest = self, |
| 204 | name = name, |
| 205 | remote = remote, |
| 206 | gitdir = gitdir, |
| 207 | worktree = None, |
| 208 | relpath = None, |
| 209 | revision = m.revision) |
| 210 | self._projects[project.name] = project |
| 211 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 212 | def _ParseRemote(self, node): |
| 213 | """ |
| 214 | reads a <remote> element from the manifest file |
| 215 | """ |
| 216 | name = self._reqatt(node, 'name') |
| 217 | fetch = self._reqatt(node, 'fetch') |
| 218 | review = node.getAttribute('review') |
Shawn O. Pearce | ae6e094 | 2008-11-06 10:25:35 -0800 | [diff] [blame] | 219 | if review == '': |
| 220 | review = None |
| 221 | |
| 222 | projectName = node.getAttribute('project-name') |
| 223 | if projectName == '': |
| 224 | projectName = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 225 | |
| 226 | r = Remote(name=name, |
| 227 | fetch=fetch, |
Shawn O. Pearce | ae6e094 | 2008-11-06 10:25:35 -0800 | [diff] [blame] | 228 | review=review, |
| 229 | projectName=projectName) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 230 | |
| 231 | for n in node.childNodes: |
| 232 | if n.nodeName == 'require': |
| 233 | r.requiredCommits.append(self._reqatt(n, 'commit')) |
| 234 | |
| 235 | return r |
| 236 | |
| 237 | def _ParseDefault(self, node): |
| 238 | """ |
| 239 | reads a <default> element from the manifest file |
| 240 | """ |
| 241 | d = _Default() |
| 242 | d.remote = self._get_remote(node) |
| 243 | d.revision = node.getAttribute('revision') |
Shawn O. Pearce | 5d40e26 | 2008-11-06 11:07:42 -0800 | [diff] [blame] | 244 | if d.revision == '': |
| 245 | d.revision = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 246 | return d |
| 247 | |
| 248 | def _ParseProject(self, node): |
| 249 | """ |
| 250 | reads a <project> element from the manifest file |
| 251 | """ |
| 252 | name = self._reqatt(node, 'name') |
| 253 | |
| 254 | remote = self._get_remote(node) |
| 255 | if remote is None: |
| 256 | remote = self._default.remote |
| 257 | if remote is None: |
| 258 | raise ManifestParseError, \ |
| 259 | "no remote for project %s within %s" % \ |
| 260 | (name, self.manifestFile) |
| 261 | |
| 262 | revision = node.getAttribute('revision') |
| 263 | if not revision: |
| 264 | revision = self._default.revision |
| 265 | if not revision: |
| 266 | raise ManifestParseError, \ |
| 267 | "no revision for project %s within %s" % \ |
| 268 | (name, self.manifestFile) |
| 269 | |
| 270 | path = node.getAttribute('path') |
| 271 | if not path: |
| 272 | path = name |
| 273 | if path.startswith('/'): |
| 274 | raise ManifestParseError, \ |
| 275 | "project %s path cannot be absolute in %s" % \ |
| 276 | (name, self.manifestFile) |
| 277 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 278 | if self.IsMirror: |
| 279 | relpath = None |
| 280 | worktree = None |
| 281 | gitdir = os.path.join(self.topdir, '%s.git' % name) |
| 282 | else: |
| 283 | worktree = os.path.join(self.topdir, path) |
| 284 | 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] | 285 | |
| 286 | project = Project(manifest = self, |
| 287 | name = name, |
| 288 | remote = remote, |
| 289 | gitdir = gitdir, |
| 290 | worktree = worktree, |
| 291 | relpath = path, |
| 292 | revision = revision) |
| 293 | |
| 294 | for n in node.childNodes: |
| 295 | if n.nodeName == 'remote': |
Shawn O. Pearce | 70939e2 | 2008-11-06 11:07:14 -0800 | [diff] [blame] | 296 | self._ParseProjectExtraRemote(project, n) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 297 | elif n.nodeName == 'copyfile': |
| 298 | self._ParseCopyFile(project, n) |
| 299 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 300 | return project |
| 301 | |
Shawn O. Pearce | 70939e2 | 2008-11-06 11:07:14 -0800 | [diff] [blame] | 302 | def _ParseProjectExtraRemote(self, project, n): |
| 303 | r = self._ParseRemote(n) |
| 304 | if project.extraRemotes.get(r.name) \ |
| 305 | or project.remote.name == r.name: |
| 306 | raise ManifestParseError, \ |
| 307 | 'duplicate remote %s in project %s in %s' % \ |
| 308 | (r.name, project.name, self.manifestFile) |
| 309 | project.extraRemotes[r.name] = r |
| 310 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 311 | def _ParseCopyFile(self, project, node): |
| 312 | src = self._reqatt(node, 'src') |
| 313 | dest = self._reqatt(node, 'dest') |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 314 | if not self.IsMirror: |
| 315 | # src is project relative; |
| 316 | # dest is relative to the top of the tree |
| 317 | project.AddCopyFile(src, os.path.join(self.topdir, dest)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 318 | |
| 319 | def _get_remote(self, node): |
| 320 | name = node.getAttribute('remote') |
| 321 | if not name: |
| 322 | return None |
| 323 | |
| 324 | v = self._remotes.get(name) |
| 325 | if not v: |
| 326 | raise ManifestParseError, \ |
| 327 | "remote %s not defined in %s" % \ |
| 328 | (name, self.manifestFile) |
| 329 | return v |
| 330 | |
| 331 | def _reqatt(self, node, attname): |
| 332 | """ |
| 333 | reads a required attribute from the node. |
| 334 | """ |
| 335 | v = node.getAttribute(attname) |
| 336 | if not v: |
| 337 | raise ManifestParseError, \ |
| 338 | "no %s in <%s> within %s" % \ |
| 339 | (attname, node.nodeName, self.manifestFile) |
| 340 | return v |