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