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 | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 21 | from project import RemoteSpec, Project, MetaProject, R_HEADS, HEAD |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 22 | from error import ManifestParseError |
| 23 | |
| 24 | MANIFEST_FILE_NAME = 'manifest.xml' |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 25 | LOCAL_MANIFEST_NAME = 'local_manifest.xml' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 26 | |
| 27 | class _Default(object): |
| 28 | """Project defaults within the manifest.""" |
| 29 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 30 | revisionExpr = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 31 | remote = None |
| 32 | |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 33 | class _XmlRemote(object): |
| 34 | def __init__(self, |
| 35 | name, |
| 36 | fetch=None, |
| 37 | review=None): |
| 38 | self.name = name |
| 39 | self.fetchUrl = fetch |
| 40 | self.reviewUrl = review |
| 41 | |
| 42 | def ToRemoteSpec(self, projectName): |
| 43 | url = self.fetchUrl |
| 44 | while url.endswith('/'): |
| 45 | url = url[:-1] |
| 46 | url += '/%s.git' % projectName |
| 47 | return RemoteSpec(self.name, url, self.reviewUrl) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 48 | |
Shawn O. Pearce | c8a300f | 2009-05-18 13:19:57 -0700 | [diff] [blame] | 49 | class XmlManifest(object): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 50 | """manages the repo configuration file""" |
| 51 | |
| 52 | def __init__(self, repodir): |
| 53 | self.repodir = os.path.abspath(repodir) |
| 54 | self.topdir = os.path.dirname(self.repodir) |
| 55 | 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] | 56 | self.globalConfig = GitConfig.ForUser() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 57 | |
| 58 | self.repoProject = MetaProject(self, 'repo', |
| 59 | gitdir = os.path.join(repodir, 'repo/.git'), |
| 60 | worktree = os.path.join(repodir, 'repo')) |
| 61 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 62 | self.manifestProject = MetaProject(self, 'manifests', |
Shawn O. Pearce | f5c25a6 | 2008-11-04 08:11:53 -0800 | [diff] [blame] | 63 | gitdir = os.path.join(repodir, 'manifests.git'), |
| 64 | worktree = os.path.join(repodir, 'manifests')) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 65 | |
| 66 | self._Unload() |
| 67 | |
| 68 | def Link(self, name): |
| 69 | """Update the repo metadata to use a different manifest. |
| 70 | """ |
| 71 | path = os.path.join(self.manifestProject.worktree, name) |
| 72 | if not os.path.isfile(path): |
| 73 | raise ManifestParseError('manifest %s not found' % name) |
| 74 | |
| 75 | old = self.manifestFile |
| 76 | try: |
| 77 | self.manifestFile = path |
| 78 | self._Unload() |
| 79 | self._Load() |
| 80 | finally: |
| 81 | self.manifestFile = old |
| 82 | |
| 83 | try: |
| 84 | if os.path.exists(self.manifestFile): |
| 85 | os.remove(self.manifestFile) |
| 86 | os.symlink('manifests/%s' % name, self.manifestFile) |
| 87 | except OSError, e: |
| 88 | raise ManifestParseError('cannot link manifest %s' % name) |
| 89 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 90 | def _RemoteToXml(self, r, doc, root): |
| 91 | e = doc.createElement('remote') |
| 92 | root.appendChild(e) |
| 93 | e.setAttribute('name', r.name) |
| 94 | e.setAttribute('fetch', r.fetchUrl) |
| 95 | if r.reviewUrl is not None: |
| 96 | e.setAttribute('review', r.reviewUrl) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 97 | |
| 98 | def Save(self, fd, peg_rev=False): |
| 99 | """Write the current manifest out to the given file descriptor. |
| 100 | """ |
| 101 | doc = xml.dom.minidom.Document() |
| 102 | root = doc.createElement('manifest') |
| 103 | doc.appendChild(root) |
| 104 | |
| 105 | d = self.default |
| 106 | sort_remotes = list(self.remotes.keys()) |
| 107 | sort_remotes.sort() |
| 108 | |
| 109 | for r in sort_remotes: |
| 110 | self._RemoteToXml(self.remotes[r], doc, root) |
| 111 | if self.remotes: |
| 112 | root.appendChild(doc.createTextNode('')) |
| 113 | |
| 114 | have_default = False |
| 115 | e = doc.createElement('default') |
| 116 | if d.remote: |
| 117 | have_default = True |
| 118 | e.setAttribute('remote', d.remote.name) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 119 | if d.revisionExpr: |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 120 | have_default = True |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 121 | e.setAttribute('revision', d.revisionExpr) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 122 | if have_default: |
| 123 | root.appendChild(e) |
| 124 | root.appendChild(doc.createTextNode('')) |
| 125 | |
| 126 | sort_projects = list(self.projects.keys()) |
| 127 | sort_projects.sort() |
| 128 | |
| 129 | for p in sort_projects: |
| 130 | p = self.projects[p] |
| 131 | e = doc.createElement('project') |
| 132 | root.appendChild(e) |
| 133 | e.setAttribute('name', p.name) |
| 134 | if p.relpath != p.name: |
| 135 | e.setAttribute('path', p.relpath) |
| 136 | if not d.remote or p.remote.name != d.remote.name: |
| 137 | e.setAttribute('remote', p.remote.name) |
| 138 | if peg_rev: |
| 139 | if self.IsMirror: |
| 140 | e.setAttribute('revision', |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 141 | p.bare_git.rev_parse(p.revisionExpr + '^0')) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 142 | else: |
| 143 | e.setAttribute('revision', |
| 144 | p.work_git.rev_parse(HEAD + '^0')) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 145 | elif not d.revisionExpr or p.revisionExpr != d.revisionExpr: |
| 146 | e.setAttribute('revision', p.revisionExpr) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 147 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 148 | for c in p.copyfiles: |
| 149 | ce = doc.createElement('copyfile') |
| 150 | ce.setAttribute('src', c.src) |
| 151 | ce.setAttribute('dest', c.dest) |
| 152 | e.appendChild(ce) |
| 153 | |
| 154 | doc.writexml(fd, '', ' ', '\n', 'UTF-8') |
| 155 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 156 | @property |
| 157 | def projects(self): |
| 158 | self._Load() |
| 159 | return self._projects |
| 160 | |
| 161 | @property |
| 162 | def remotes(self): |
| 163 | self._Load() |
| 164 | return self._remotes |
| 165 | |
| 166 | @property |
| 167 | def default(self): |
| 168 | self._Load() |
| 169 | return self._default |
| 170 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 171 | @property |
| 172 | def IsMirror(self): |
| 173 | return self.manifestProject.config.GetBoolean('repo.mirror') |
| 174 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 175 | def _Unload(self): |
| 176 | self._loaded = False |
| 177 | self._projects = {} |
| 178 | self._remotes = {} |
| 179 | self._default = None |
| 180 | self.branch = None |
| 181 | |
| 182 | def _Load(self): |
| 183 | if not self._loaded: |
Shawn O. Pearce | 2450a29 | 2008-11-04 08:22:07 -0800 | [diff] [blame] | 184 | m = self.manifestProject |
| 185 | b = m.GetBranch(m.CurrentBranch).merge |
| 186 | if b.startswith(R_HEADS): |
| 187 | b = b[len(R_HEADS):] |
| 188 | self.branch = b |
| 189 | |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 190 | self._ParseManifest(True) |
| 191 | |
| 192 | local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME) |
| 193 | if os.path.exists(local): |
| 194 | try: |
| 195 | real = self.manifestFile |
| 196 | self.manifestFile = local |
| 197 | self._ParseManifest(False) |
| 198 | finally: |
| 199 | self.manifestFile = real |
| 200 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 201 | if self.IsMirror: |
| 202 | self._AddMetaProjectMirror(self.repoProject) |
| 203 | self._AddMetaProjectMirror(self.manifestProject) |
| 204 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 205 | self._loaded = True |
| 206 | |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 207 | def _ParseManifest(self, is_root_file): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 208 | root = xml.dom.minidom.parse(self.manifestFile) |
| 209 | if not root or not root.childNodes: |
| 210 | raise ManifestParseError, \ |
| 211 | "no root node in %s" % \ |
| 212 | self.manifestFile |
| 213 | |
| 214 | config = root.childNodes[0] |
| 215 | if config.nodeName != 'manifest': |
| 216 | raise ManifestParseError, \ |
| 217 | "no <manifest> in %s" % \ |
| 218 | self.manifestFile |
| 219 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 220 | for node in config.childNodes: |
Shawn O. Pearce | 03eaf07 | 2008-11-20 11:42:22 -0800 | [diff] [blame] | 221 | if node.nodeName == 'remove-project': |
| 222 | name = self._reqatt(node, 'name') |
| 223 | try: |
| 224 | del self._projects[name] |
| 225 | except KeyError: |
| 226 | raise ManifestParseError, \ |
| 227 | 'project %s not found' % \ |
| 228 | (name) |
| 229 | |
| 230 | for node in config.childNodes: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 231 | if node.nodeName == 'remote': |
| 232 | remote = self._ParseRemote(node) |
| 233 | if self._remotes.get(remote.name): |
| 234 | raise ManifestParseError, \ |
| 235 | 'duplicate remote %s in %s' % \ |
| 236 | (remote.name, self.manifestFile) |
| 237 | self._remotes[remote.name] = remote |
| 238 | |
| 239 | for node in config.childNodes: |
| 240 | if node.nodeName == 'default': |
| 241 | if self._default is not None: |
| 242 | raise ManifestParseError, \ |
| 243 | 'duplicate default in %s' % \ |
| 244 | (self.manifestFile) |
| 245 | self._default = self._ParseDefault(node) |
| 246 | if self._default is None: |
| 247 | self._default = _Default() |
| 248 | |
| 249 | for node in config.childNodes: |
| 250 | if node.nodeName == 'project': |
| 251 | project = self._ParseProject(node) |
| 252 | if self._projects.get(project.name): |
| 253 | raise ManifestParseError, \ |
| 254 | 'duplicate project %s in %s' % \ |
| 255 | (project.name, self.manifestFile) |
| 256 | self._projects[project.name] = project |
| 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 |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 274 | remote = _XmlRemote('origin', m_url[:s]) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 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, |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 285 | remote = remote.ToRemoteSpec(name), |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 286 | gitdir = gitdir, |
| 287 | worktree = None, |
| 288 | relpath = None, |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 289 | revisionExpr = m.revisionExpr, |
| 290 | revisionId = None) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 291 | self._projects[project.name] = project |
| 292 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 293 | def _ParseRemote(self, node): |
| 294 | """ |
| 295 | reads a <remote> element from the manifest file |
| 296 | """ |
| 297 | name = self._reqatt(node, 'name') |
| 298 | fetch = self._reqatt(node, 'fetch') |
| 299 | review = node.getAttribute('review') |
Shawn O. Pearce | ae6e094 | 2008-11-06 10:25:35 -0800 | [diff] [blame] | 300 | if review == '': |
| 301 | review = None |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 302 | return _XmlRemote(name, fetch, review) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 303 | |
| 304 | def _ParseDefault(self, node): |
| 305 | """ |
| 306 | reads a <default> element from the manifest file |
| 307 | """ |
| 308 | d = _Default() |
| 309 | d.remote = self._get_remote(node) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 310 | d.revisionExpr = node.getAttribute('revision') |
| 311 | if d.revisionExpr == '': |
| 312 | d.revisionExpr = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 313 | return d |
| 314 | |
| 315 | def _ParseProject(self, node): |
| 316 | """ |
| 317 | reads a <project> element from the manifest file |
| 318 | """ |
| 319 | name = self._reqatt(node, 'name') |
| 320 | |
| 321 | remote = self._get_remote(node) |
| 322 | if remote is None: |
| 323 | remote = self._default.remote |
| 324 | if remote is None: |
| 325 | raise ManifestParseError, \ |
| 326 | "no remote for project %s within %s" % \ |
| 327 | (name, self.manifestFile) |
| 328 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 329 | revisionExpr = node.getAttribute('revision') |
| 330 | if not revisionExpr: |
| 331 | revisionExpr = self._default.revisionExpr |
| 332 | if not revisionExpr: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 333 | raise ManifestParseError, \ |
| 334 | "no revision for project %s within %s" % \ |
| 335 | (name, self.manifestFile) |
| 336 | |
| 337 | path = node.getAttribute('path') |
| 338 | if not path: |
| 339 | path = name |
| 340 | if path.startswith('/'): |
| 341 | raise ManifestParseError, \ |
| 342 | "project %s path cannot be absolute in %s" % \ |
| 343 | (name, self.manifestFile) |
| 344 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 345 | if self.IsMirror: |
| 346 | relpath = None |
| 347 | worktree = None |
| 348 | gitdir = os.path.join(self.topdir, '%s.git' % name) |
| 349 | else: |
| 350 | worktree = os.path.join(self.topdir, path) |
| 351 | 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] | 352 | |
| 353 | project = Project(manifest = self, |
| 354 | name = name, |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 355 | remote = remote.ToRemoteSpec(name), |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 356 | gitdir = gitdir, |
| 357 | worktree = worktree, |
| 358 | relpath = path, |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 359 | revisionExpr = revisionExpr, |
| 360 | revisionId = None) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 361 | |
| 362 | for n in node.childNodes: |
Shawn O. Pearce | 242b526 | 2009-05-19 13:00:29 -0700 | [diff] [blame] | 363 | if n.nodeName == 'copyfile': |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 364 | self._ParseCopyFile(project, n) |
| 365 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 366 | return project |
| 367 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 368 | def _ParseCopyFile(self, project, node): |
| 369 | src = self._reqatt(node, 'src') |
| 370 | dest = self._reqatt(node, 'dest') |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 371 | if not self.IsMirror: |
| 372 | # src is project relative; |
| 373 | # dest is relative to the top of the tree |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 374 | 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] | 375 | |
| 376 | def _get_remote(self, node): |
| 377 | name = node.getAttribute('remote') |
| 378 | if not name: |
| 379 | return None |
| 380 | |
| 381 | v = self._remotes.get(name) |
| 382 | if not v: |
| 383 | raise ManifestParseError, \ |
| 384 | "remote %s not defined in %s" % \ |
| 385 | (name, self.manifestFile) |
| 386 | return v |
| 387 | |
| 388 | def _reqatt(self, node, attname): |
| 389 | """ |
| 390 | reads a required attribute from the node. |
| 391 | """ |
| 392 | v = node.getAttribute(attname) |
| 393 | if not v: |
| 394 | raise ManifestParseError, \ |
| 395 | "no %s in <%s> within %s" % \ |
| 396 | (attname, node.nodeName, self.manifestFile) |
| 397 | return v |