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 |
Shawn O. Pearce | 6392c87 | 2011-09-22 17:44:31 -0700 | [diff] [blame] | 32 | sync_j = 1 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 34 | class _XmlRemote(object): |
| 35 | def __init__(self, |
| 36 | name, |
| 37 | fetch=None, |
| 38 | review=None): |
| 39 | self.name = name |
| 40 | self.fetchUrl = fetch |
| 41 | self.reviewUrl = review |
| 42 | |
| 43 | def ToRemoteSpec(self, projectName): |
| 44 | url = self.fetchUrl |
| 45 | while url.endswith('/'): |
| 46 | url = url[:-1] |
| 47 | url += '/%s.git' % projectName |
| 48 | return RemoteSpec(self.name, url, self.reviewUrl) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 49 | |
Shawn O. Pearce | c8a300f | 2009-05-18 13:19:57 -0700 | [diff] [blame] | 50 | class XmlManifest(object): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 51 | """manages the repo configuration file""" |
| 52 | |
| 53 | def __init__(self, repodir): |
| 54 | self.repodir = os.path.abspath(repodir) |
| 55 | self.topdir = os.path.dirname(self.repodir) |
| 56 | 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] | 57 | self.globalConfig = GitConfig.ForUser() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 58 | |
| 59 | self.repoProject = MetaProject(self, 'repo', |
| 60 | gitdir = os.path.join(repodir, 'repo/.git'), |
| 61 | worktree = os.path.join(repodir, 'repo')) |
| 62 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 63 | self.manifestProject = MetaProject(self, 'manifests', |
Shawn O. Pearce | f5c25a6 | 2008-11-04 08:11:53 -0800 | [diff] [blame] | 64 | gitdir = os.path.join(repodir, 'manifests.git'), |
| 65 | worktree = os.path.join(repodir, 'manifests')) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 66 | |
| 67 | self._Unload() |
| 68 | |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 69 | def Override(self, name): |
| 70 | """Use a different manifest, just for the current instantiation. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | |
| 76 | old = self.manifestFile |
| 77 | try: |
| 78 | self.manifestFile = path |
| 79 | self._Unload() |
| 80 | self._Load() |
| 81 | finally: |
| 82 | self.manifestFile = old |
| 83 | |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 84 | def Link(self, name): |
| 85 | """Update the repo metadata to use a different manifest. |
| 86 | """ |
| 87 | self.Override(name) |
| 88 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 89 | try: |
| 90 | if os.path.exists(self.manifestFile): |
| 91 | os.remove(self.manifestFile) |
| 92 | os.symlink('manifests/%s' % name, self.manifestFile) |
| 93 | except OSError, e: |
| 94 | raise ManifestParseError('cannot link manifest %s' % name) |
| 95 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 96 | def _RemoteToXml(self, r, doc, root): |
| 97 | e = doc.createElement('remote') |
| 98 | root.appendChild(e) |
| 99 | e.setAttribute('name', r.name) |
| 100 | e.setAttribute('fetch', r.fetchUrl) |
| 101 | if r.reviewUrl is not None: |
| 102 | e.setAttribute('review', r.reviewUrl) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 103 | |
| 104 | def Save(self, fd, peg_rev=False): |
| 105 | """Write the current manifest out to the given file descriptor. |
| 106 | """ |
| 107 | doc = xml.dom.minidom.Document() |
| 108 | root = doc.createElement('manifest') |
| 109 | doc.appendChild(root) |
| 110 | |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 111 | # Save out the notice. There's a little bit of work here to give it the |
| 112 | # right whitespace, which assumes that the notice is automatically indented |
| 113 | # by 4 by minidom. |
| 114 | if self.notice: |
| 115 | notice_element = root.appendChild(doc.createElement('notice')) |
| 116 | notice_lines = self.notice.splitlines() |
| 117 | indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:] |
| 118 | notice_element.appendChild(doc.createTextNode(indented_notice)) |
| 119 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 120 | d = self.default |
| 121 | sort_remotes = list(self.remotes.keys()) |
| 122 | sort_remotes.sort() |
| 123 | |
| 124 | for r in sort_remotes: |
| 125 | self._RemoteToXml(self.remotes[r], doc, root) |
| 126 | if self.remotes: |
| 127 | root.appendChild(doc.createTextNode('')) |
| 128 | |
| 129 | have_default = False |
| 130 | e = doc.createElement('default') |
| 131 | if d.remote: |
| 132 | have_default = True |
| 133 | e.setAttribute('remote', d.remote.name) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 134 | if d.revisionExpr: |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 135 | have_default = True |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 136 | e.setAttribute('revision', d.revisionExpr) |
Shawn O. Pearce | 6392c87 | 2011-09-22 17:44:31 -0700 | [diff] [blame] | 137 | if d.sync_j > 1: |
| 138 | have_default = True |
| 139 | e.setAttribute('sync-j', '%d' % d.sync_j) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 140 | if have_default: |
| 141 | root.appendChild(e) |
| 142 | root.appendChild(doc.createTextNode('')) |
| 143 | |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 144 | if self._manifest_server: |
| 145 | e = doc.createElement('manifest-server') |
| 146 | e.setAttribute('url', self._manifest_server) |
| 147 | root.appendChild(e) |
| 148 | root.appendChild(doc.createTextNode('')) |
| 149 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 150 | sort_projects = list(self.projects.keys()) |
| 151 | sort_projects.sort() |
| 152 | |
| 153 | for p in sort_projects: |
| 154 | p = self.projects[p] |
| 155 | e = doc.createElement('project') |
| 156 | root.appendChild(e) |
| 157 | e.setAttribute('name', p.name) |
| 158 | if p.relpath != p.name: |
| 159 | e.setAttribute('path', p.relpath) |
| 160 | if not d.remote or p.remote.name != d.remote.name: |
| 161 | e.setAttribute('remote', p.remote.name) |
| 162 | if peg_rev: |
| 163 | if self.IsMirror: |
| 164 | e.setAttribute('revision', |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 165 | p.bare_git.rev_parse(p.revisionExpr + '^0')) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 166 | else: |
| 167 | e.setAttribute('revision', |
| 168 | p.work_git.rev_parse(HEAD + '^0')) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 169 | elif not d.revisionExpr or p.revisionExpr != d.revisionExpr: |
| 170 | e.setAttribute('revision', p.revisionExpr) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 171 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 172 | for c in p.copyfiles: |
| 173 | ce = doc.createElement('copyfile') |
| 174 | ce.setAttribute('src', c.src) |
| 175 | ce.setAttribute('dest', c.dest) |
| 176 | e.appendChild(ce) |
| 177 | |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 178 | if self._repo_hooks_project: |
| 179 | root.appendChild(doc.createTextNode('')) |
| 180 | e = doc.createElement('repo-hooks') |
| 181 | e.setAttribute('in-project', self._repo_hooks_project.name) |
| 182 | e.setAttribute('enabled-list', |
| 183 | ' '.join(self._repo_hooks_project.enabled_repo_hooks)) |
| 184 | root.appendChild(e) |
| 185 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 186 | doc.writexml(fd, '', ' ', '\n', 'UTF-8') |
| 187 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 188 | @property |
| 189 | def projects(self): |
| 190 | self._Load() |
| 191 | return self._projects |
| 192 | |
| 193 | @property |
| 194 | def remotes(self): |
| 195 | self._Load() |
| 196 | return self._remotes |
| 197 | |
| 198 | @property |
| 199 | def default(self): |
| 200 | self._Load() |
| 201 | return self._default |
| 202 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 203 | @property |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 204 | def repo_hooks_project(self): |
| 205 | self._Load() |
| 206 | return self._repo_hooks_project |
| 207 | |
| 208 | @property |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 209 | def notice(self): |
| 210 | self._Load() |
| 211 | return self._notice |
| 212 | |
| 213 | @property |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 214 | def manifest_server(self): |
| 215 | self._Load() |
| 216 | return self._manifest_server |
| 217 | |
| 218 | @property |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 219 | def IsMirror(self): |
| 220 | return self.manifestProject.config.GetBoolean('repo.mirror') |
| 221 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 222 | def _Unload(self): |
| 223 | self._loaded = False |
| 224 | self._projects = {} |
| 225 | self._remotes = {} |
| 226 | self._default = None |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 227 | self._repo_hooks_project = None |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 228 | self._notice = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 229 | self.branch = None |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 230 | self._manifest_server = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 231 | |
| 232 | def _Load(self): |
| 233 | if not self._loaded: |
Shawn O. Pearce | 2450a29 | 2008-11-04 08:22:07 -0800 | [diff] [blame] | 234 | m = self.manifestProject |
| 235 | b = m.GetBranch(m.CurrentBranch).merge |
Shawn O. Pearce | 21c5c34 | 2009-06-25 16:47:30 -0700 | [diff] [blame] | 236 | if b is not None and b.startswith(R_HEADS): |
Shawn O. Pearce | 2450a29 | 2008-11-04 08:22:07 -0800 | [diff] [blame] | 237 | b = b[len(R_HEADS):] |
| 238 | self.branch = b |
| 239 | |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 240 | self._ParseManifest(True) |
| 241 | |
| 242 | local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME) |
| 243 | if os.path.exists(local): |
| 244 | try: |
| 245 | real = self.manifestFile |
| 246 | self.manifestFile = local |
| 247 | self._ParseManifest(False) |
| 248 | finally: |
| 249 | self.manifestFile = real |
| 250 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 251 | if self.IsMirror: |
| 252 | self._AddMetaProjectMirror(self.repoProject) |
| 253 | self._AddMetaProjectMirror(self.manifestProject) |
| 254 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 255 | self._loaded = True |
| 256 | |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 257 | def _ParseManifest(self, is_root_file): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 258 | root = xml.dom.minidom.parse(self.manifestFile) |
| 259 | if not root or not root.childNodes: |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 260 | raise ManifestParseError( |
| 261 | "no root node in %s" % |
| 262 | self.manifestFile) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 263 | |
| 264 | config = root.childNodes[0] |
| 265 | if config.nodeName != 'manifest': |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 266 | raise ManifestParseError( |
| 267 | "no <manifest> in %s" % |
| 268 | self.manifestFile) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 269 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 270 | for node in config.childNodes: |
Shawn O. Pearce | 03eaf07 | 2008-11-20 11:42:22 -0800 | [diff] [blame] | 271 | if node.nodeName == 'remove-project': |
| 272 | name = self._reqatt(node, 'name') |
| 273 | try: |
| 274 | del self._projects[name] |
| 275 | except KeyError: |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 276 | raise ManifestParseError( |
| 277 | 'project %s not found' % |
| 278 | (name)) |
| 279 | |
| 280 | # If the manifest removes the hooks project, treat it as if it deleted |
| 281 | # the repo-hooks element too. |
| 282 | if self._repo_hooks_project and (self._repo_hooks_project.name == name): |
| 283 | self._repo_hooks_project = None |
Shawn O. Pearce | 03eaf07 | 2008-11-20 11:42:22 -0800 | [diff] [blame] | 284 | |
| 285 | for node in config.childNodes: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 286 | if node.nodeName == 'remote': |
| 287 | remote = self._ParseRemote(node) |
| 288 | if self._remotes.get(remote.name): |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 289 | raise ManifestParseError( |
| 290 | 'duplicate remote %s in %s' % |
| 291 | (remote.name, self.manifestFile)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 292 | self._remotes[remote.name] = remote |
| 293 | |
| 294 | for node in config.childNodes: |
| 295 | if node.nodeName == 'default': |
| 296 | if self._default is not None: |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 297 | raise ManifestParseError( |
| 298 | 'duplicate default in %s' % |
| 299 | (self.manifestFile)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 300 | self._default = self._ParseDefault(node) |
| 301 | if self._default is None: |
| 302 | self._default = _Default() |
| 303 | |
| 304 | for node in config.childNodes: |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 305 | if node.nodeName == 'notice': |
| 306 | if self._notice is not None: |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 307 | raise ManifestParseError( |
| 308 | 'duplicate notice in %s' % |
| 309 | (self.manifestFile)) |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 310 | self._notice = self._ParseNotice(node) |
| 311 | |
| 312 | for node in config.childNodes: |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 313 | if node.nodeName == 'manifest-server': |
| 314 | url = self._reqatt(node, 'url') |
| 315 | if self._manifest_server is not None: |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 316 | raise ManifestParseError( |
| 317 | 'duplicate manifest-server in %s' % |
| 318 | (self.manifestFile)) |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 319 | self._manifest_server = url |
| 320 | |
| 321 | for node in config.childNodes: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 322 | if node.nodeName == 'project': |
| 323 | project = self._ParseProject(node) |
| 324 | if self._projects.get(project.name): |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 325 | raise ManifestParseError( |
| 326 | 'duplicate project %s in %s' % |
| 327 | (project.name, self.manifestFile)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 328 | self._projects[project.name] = project |
| 329 | |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 330 | for node in config.childNodes: |
| 331 | if node.nodeName == 'repo-hooks': |
| 332 | # Get the name of the project and the (space-separated) list of enabled. |
| 333 | repo_hooks_project = self._reqatt(node, 'in-project') |
| 334 | enabled_repo_hooks = self._reqatt(node, 'enabled-list').split() |
| 335 | |
| 336 | # Only one project can be the hooks project |
| 337 | if self._repo_hooks_project is not None: |
| 338 | raise ManifestParseError( |
| 339 | 'duplicate repo-hooks in %s' % |
| 340 | (self.manifestFile)) |
| 341 | |
| 342 | # Store a reference to the Project. |
| 343 | try: |
| 344 | self._repo_hooks_project = self._projects[repo_hooks_project] |
| 345 | except KeyError: |
| 346 | raise ManifestParseError( |
| 347 | 'project %s not found for repo-hooks' % |
| 348 | (repo_hooks_project)) |
| 349 | |
| 350 | # Store the enabled hooks in the Project object. |
| 351 | self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks |
| 352 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 353 | def _AddMetaProjectMirror(self, m): |
| 354 | name = None |
| 355 | m_url = m.GetRemote(m.remote.name).url |
| 356 | if m_url.endswith('/.git'): |
| 357 | raise ManifestParseError, 'refusing to mirror %s' % m_url |
| 358 | |
| 359 | if self._default and self._default.remote: |
| 360 | url = self._default.remote.fetchUrl |
| 361 | if not url.endswith('/'): |
| 362 | url += '/' |
| 363 | if m_url.startswith(url): |
| 364 | remote = self._default.remote |
| 365 | name = m_url[len(url):] |
| 366 | |
| 367 | if name is None: |
| 368 | s = m_url.rindex('/') + 1 |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 369 | remote = _XmlRemote('origin', m_url[:s]) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 370 | name = m_url[s:] |
| 371 | |
| 372 | if name.endswith('.git'): |
| 373 | name = name[:-4] |
| 374 | |
| 375 | if name not in self._projects: |
| 376 | m.PreSync() |
| 377 | gitdir = os.path.join(self.topdir, '%s.git' % name) |
| 378 | project = Project(manifest = self, |
| 379 | name = name, |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 380 | remote = remote.ToRemoteSpec(name), |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 381 | gitdir = gitdir, |
| 382 | worktree = None, |
| 383 | relpath = None, |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 384 | revisionExpr = m.revisionExpr, |
| 385 | revisionId = None) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 386 | self._projects[project.name] = project |
| 387 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 388 | def _ParseRemote(self, node): |
| 389 | """ |
| 390 | reads a <remote> element from the manifest file |
| 391 | """ |
| 392 | name = self._reqatt(node, 'name') |
| 393 | fetch = self._reqatt(node, 'fetch') |
| 394 | review = node.getAttribute('review') |
Shawn O. Pearce | ae6e094 | 2008-11-06 10:25:35 -0800 | [diff] [blame] | 395 | if review == '': |
| 396 | review = None |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 397 | return _XmlRemote(name, fetch, review) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 398 | |
| 399 | def _ParseDefault(self, node): |
| 400 | """ |
| 401 | reads a <default> element from the manifest file |
| 402 | """ |
| 403 | d = _Default() |
| 404 | d.remote = self._get_remote(node) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 405 | d.revisionExpr = node.getAttribute('revision') |
| 406 | if d.revisionExpr == '': |
| 407 | d.revisionExpr = None |
Shawn O. Pearce | 6392c87 | 2011-09-22 17:44:31 -0700 | [diff] [blame] | 408 | sync_j = node.getAttribute('sync-j') |
| 409 | if sync_j == '' or sync_j is None: |
| 410 | d.sync_j = 1 |
| 411 | else: |
| 412 | d.sync_j = int(sync_j) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 413 | return d |
| 414 | |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 415 | def _ParseNotice(self, node): |
| 416 | """ |
| 417 | reads a <notice> element from the manifest file |
| 418 | |
| 419 | The <notice> element is distinct from other tags in the XML in that the |
| 420 | data is conveyed between the start and end tag (it's not an empty-element |
| 421 | tag). |
| 422 | |
| 423 | The white space (carriage returns, indentation) for the notice element is |
| 424 | relevant and is parsed in a way that is based on how python docstrings work. |
| 425 | In fact, the code is remarkably similar to here: |
| 426 | http://www.python.org/dev/peps/pep-0257/ |
| 427 | """ |
| 428 | # Get the data out of the node... |
| 429 | notice = node.childNodes[0].data |
| 430 | |
| 431 | # Figure out minimum indentation, skipping the first line (the same line |
| 432 | # as the <notice> tag)... |
| 433 | minIndent = sys.maxint |
| 434 | lines = notice.splitlines() |
| 435 | for line in lines[1:]: |
| 436 | lstrippedLine = line.lstrip() |
| 437 | if lstrippedLine: |
| 438 | indent = len(line) - len(lstrippedLine) |
| 439 | minIndent = min(indent, minIndent) |
| 440 | |
| 441 | # Strip leading / trailing blank lines and also indentation. |
| 442 | cleanLines = [lines[0].strip()] |
| 443 | for line in lines[1:]: |
| 444 | cleanLines.append(line[minIndent:].rstrip()) |
| 445 | |
| 446 | # Clear completely blank lines from front and back... |
| 447 | while cleanLines and not cleanLines[0]: |
| 448 | del cleanLines[0] |
| 449 | while cleanLines and not cleanLines[-1]: |
| 450 | del cleanLines[-1] |
| 451 | |
| 452 | return '\n'.join(cleanLines) |
| 453 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 454 | def _ParseProject(self, node): |
| 455 | """ |
| 456 | reads a <project> element from the manifest file |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 457 | """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 458 | name = self._reqatt(node, 'name') |
| 459 | |
| 460 | remote = self._get_remote(node) |
| 461 | if remote is None: |
| 462 | remote = self._default.remote |
| 463 | if remote is None: |
| 464 | raise ManifestParseError, \ |
| 465 | "no remote for project %s within %s" % \ |
| 466 | (name, self.manifestFile) |
| 467 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 468 | revisionExpr = node.getAttribute('revision') |
| 469 | if not revisionExpr: |
| 470 | revisionExpr = self._default.revisionExpr |
| 471 | if not revisionExpr: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 472 | raise ManifestParseError, \ |
| 473 | "no revision for project %s within %s" % \ |
| 474 | (name, self.manifestFile) |
| 475 | |
| 476 | path = node.getAttribute('path') |
| 477 | if not path: |
| 478 | path = name |
| 479 | if path.startswith('/'): |
| 480 | raise ManifestParseError, \ |
| 481 | "project %s path cannot be absolute in %s" % \ |
| 482 | (name, self.manifestFile) |
| 483 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 484 | if self.IsMirror: |
| 485 | relpath = None |
| 486 | worktree = None |
| 487 | gitdir = os.path.join(self.topdir, '%s.git' % name) |
| 488 | else: |
Anthony Newnam | df14a70 | 2011-01-09 17:31:57 -0800 | [diff] [blame] | 489 | worktree = os.path.join(self.topdir, path).replace('\\', '/') |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 490 | 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] | 491 | |
| 492 | project = Project(manifest = self, |
| 493 | name = name, |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 494 | remote = remote.ToRemoteSpec(name), |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 495 | gitdir = gitdir, |
| 496 | worktree = worktree, |
| 497 | relpath = path, |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 498 | revisionExpr = revisionExpr, |
| 499 | revisionId = None) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 500 | |
| 501 | for n in node.childNodes: |
Shawn O. Pearce | 242b526 | 2009-05-19 13:00:29 -0700 | [diff] [blame] | 502 | if n.nodeName == 'copyfile': |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 503 | self._ParseCopyFile(project, n) |
| 504 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 505 | return project |
| 506 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 507 | def _ParseCopyFile(self, project, node): |
| 508 | src = self._reqatt(node, 'src') |
| 509 | dest = self._reqatt(node, 'dest') |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 510 | if not self.IsMirror: |
| 511 | # src is project relative; |
| 512 | # dest is relative to the top of the tree |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 513 | 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] | 514 | |
| 515 | def _get_remote(self, node): |
| 516 | name = node.getAttribute('remote') |
| 517 | if not name: |
| 518 | return None |
| 519 | |
| 520 | v = self._remotes.get(name) |
| 521 | if not v: |
| 522 | raise ManifestParseError, \ |
| 523 | "remote %s not defined in %s" % \ |
| 524 | (name, self.manifestFile) |
| 525 | return v |
| 526 | |
| 527 | def _reqatt(self, node, attname): |
| 528 | """ |
| 529 | reads a required attribute from the node. |
| 530 | """ |
| 531 | v = node.getAttribute(attname) |
| 532 | if not v: |
| 533 | raise ManifestParseError, \ |
| 534 | "no %s in <%s> within %s" % \ |
| 535 | (attname, node.nodeName, self.manifestFile) |
| 536 | return v |