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() |
| 231 | return self._manifest_server |
| 232 | |
| 233 | @property |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 234 | def IsMirror(self): |
| 235 | return self.manifestProject.config.GetBoolean('repo.mirror') |
| 236 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 237 | def _Unload(self): |
| 238 | self._loaded = False |
| 239 | self._projects = {} |
| 240 | self._remotes = {} |
| 241 | self._default = None |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 242 | self._repo_hooks_project = None |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 243 | self._notice = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 244 | self.branch = None |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 245 | self._manifest_server = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 246 | |
| 247 | def _Load(self): |
| 248 | if not self._loaded: |
Shawn O. Pearce | 2450a29 | 2008-11-04 08:22:07 -0800 | [diff] [blame] | 249 | m = self.manifestProject |
| 250 | b = m.GetBranch(m.CurrentBranch).merge |
Shawn O. Pearce | 21c5c34 | 2009-06-25 16:47:30 -0700 | [diff] [blame] | 251 | if b is not None and b.startswith(R_HEADS): |
Shawn O. Pearce | 2450a29 | 2008-11-04 08:22:07 -0800 | [diff] [blame] | 252 | b = b[len(R_HEADS):] |
| 253 | self.branch = b |
| 254 | |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 255 | self._ParseManifest(True) |
| 256 | |
| 257 | local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME) |
| 258 | if os.path.exists(local): |
| 259 | try: |
| 260 | real = self.manifestFile |
| 261 | self.manifestFile = local |
| 262 | self._ParseManifest(False) |
| 263 | finally: |
| 264 | self.manifestFile = real |
| 265 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 266 | if self.IsMirror: |
| 267 | self._AddMetaProjectMirror(self.repoProject) |
| 268 | self._AddMetaProjectMirror(self.manifestProject) |
| 269 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 270 | self._loaded = True |
| 271 | |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 272 | def _ParseManifest(self, is_root_file): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 273 | root = xml.dom.minidom.parse(self.manifestFile) |
| 274 | if not root or not root.childNodes: |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 275 | raise ManifestParseError( |
| 276 | "no root node in %s" % |
| 277 | self.manifestFile) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 278 | |
| 279 | config = root.childNodes[0] |
| 280 | if config.nodeName != 'manifest': |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 281 | raise ManifestParseError( |
| 282 | "no <manifest> in %s" % |
| 283 | self.manifestFile) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 284 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 285 | for node in config.childNodes: |
Shawn O. Pearce | 03eaf07 | 2008-11-20 11:42:22 -0800 | [diff] [blame] | 286 | if node.nodeName == 'remove-project': |
| 287 | name = self._reqatt(node, 'name') |
| 288 | try: |
| 289 | del self._projects[name] |
| 290 | except KeyError: |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 291 | raise ManifestParseError( |
| 292 | 'project %s not found' % |
| 293 | (name)) |
| 294 | |
| 295 | # If the manifest removes the hooks project, treat it as if it deleted |
| 296 | # the repo-hooks element too. |
| 297 | if self._repo_hooks_project and (self._repo_hooks_project.name == name): |
| 298 | self._repo_hooks_project = None |
Shawn O. Pearce | 03eaf07 | 2008-11-20 11:42:22 -0800 | [diff] [blame] | 299 | |
| 300 | for node in config.childNodes: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 301 | if node.nodeName == 'remote': |
| 302 | remote = self._ParseRemote(node) |
| 303 | if self._remotes.get(remote.name): |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 304 | raise ManifestParseError( |
| 305 | 'duplicate remote %s in %s' % |
| 306 | (remote.name, self.manifestFile)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 307 | self._remotes[remote.name] = remote |
| 308 | |
| 309 | for node in config.childNodes: |
| 310 | if node.nodeName == 'default': |
| 311 | if self._default is not None: |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 312 | raise ManifestParseError( |
| 313 | 'duplicate default in %s' % |
| 314 | (self.manifestFile)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 315 | self._default = self._ParseDefault(node) |
| 316 | if self._default is None: |
| 317 | self._default = _Default() |
| 318 | |
| 319 | for node in config.childNodes: |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 320 | if node.nodeName == 'notice': |
| 321 | if self._notice is not None: |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 322 | raise ManifestParseError( |
| 323 | 'duplicate notice in %s' % |
| 324 | (self.manifestFile)) |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 325 | self._notice = self._ParseNotice(node) |
| 326 | |
| 327 | for node in config.childNodes: |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 328 | if node.nodeName == 'manifest-server': |
| 329 | url = self._reqatt(node, 'url') |
| 330 | if self._manifest_server is not None: |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 331 | raise ManifestParseError( |
| 332 | 'duplicate manifest-server in %s' % |
| 333 | (self.manifestFile)) |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 334 | self._manifest_server = url |
| 335 | |
| 336 | for node in config.childNodes: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 337 | if node.nodeName == 'project': |
| 338 | project = self._ParseProject(node) |
| 339 | if self._projects.get(project.name): |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 340 | raise ManifestParseError( |
| 341 | 'duplicate project %s in %s' % |
| 342 | (project.name, self.manifestFile)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 343 | self._projects[project.name] = project |
| 344 | |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 345 | for node in config.childNodes: |
| 346 | if node.nodeName == 'repo-hooks': |
| 347 | # Get the name of the project and the (space-separated) list of enabled. |
| 348 | repo_hooks_project = self._reqatt(node, 'in-project') |
| 349 | enabled_repo_hooks = self._reqatt(node, 'enabled-list').split() |
| 350 | |
| 351 | # Only one project can be the hooks project |
| 352 | if self._repo_hooks_project is not None: |
| 353 | raise ManifestParseError( |
| 354 | 'duplicate repo-hooks in %s' % |
| 355 | (self.manifestFile)) |
| 356 | |
| 357 | # Store a reference to the Project. |
| 358 | try: |
| 359 | self._repo_hooks_project = self._projects[repo_hooks_project] |
| 360 | except KeyError: |
| 361 | raise ManifestParseError( |
| 362 | 'project %s not found for repo-hooks' % |
| 363 | (repo_hooks_project)) |
| 364 | |
| 365 | # Store the enabled hooks in the Project object. |
| 366 | self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks |
| 367 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 368 | def _AddMetaProjectMirror(self, m): |
| 369 | name = None |
| 370 | m_url = m.GetRemote(m.remote.name).url |
| 371 | if m_url.endswith('/.git'): |
| 372 | raise ManifestParseError, 'refusing to mirror %s' % m_url |
| 373 | |
| 374 | if self._default and self._default.remote: |
Conley Owens | ceea368 | 2011-10-20 10:45:47 -0700 | [diff] [blame] | 375 | url = self._default.remote.resolvedFetchUrl |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 376 | if not url.endswith('/'): |
| 377 | url += '/' |
| 378 | if m_url.startswith(url): |
| 379 | remote = self._default.remote |
| 380 | name = m_url[len(url):] |
| 381 | |
| 382 | if name is None: |
| 383 | s = m_url.rindex('/') + 1 |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 384 | manifestUrl = self.manifestProject.config.GetString('remote.origin.url') |
| 385 | remote = _XmlRemote('origin', m_url[:s], manifestUrl) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 386 | name = m_url[s:] |
| 387 | |
| 388 | if name.endswith('.git'): |
| 389 | name = name[:-4] |
| 390 | |
| 391 | if name not in self._projects: |
| 392 | m.PreSync() |
| 393 | gitdir = os.path.join(self.topdir, '%s.git' % name) |
| 394 | project = Project(manifest = self, |
| 395 | name = name, |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 396 | remote = remote.ToRemoteSpec(name), |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 397 | gitdir = gitdir, |
| 398 | worktree = None, |
| 399 | relpath = None, |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 400 | revisionExpr = m.revisionExpr, |
| 401 | revisionId = None) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 402 | self._projects[project.name] = project |
| 403 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 404 | def _ParseRemote(self, node): |
| 405 | """ |
| 406 | reads a <remote> element from the manifest file |
| 407 | """ |
| 408 | name = self._reqatt(node, 'name') |
| 409 | fetch = self._reqatt(node, 'fetch') |
| 410 | review = node.getAttribute('review') |
Shawn O. Pearce | ae6e094 | 2008-11-06 10:25:35 -0800 | [diff] [blame] | 411 | if review == '': |
| 412 | review = None |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 413 | manifestUrl = self.manifestProject.config.GetString('remote.origin.url') |
| 414 | return _XmlRemote(name, fetch, manifestUrl, review) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 415 | |
| 416 | def _ParseDefault(self, node): |
| 417 | """ |
| 418 | reads a <default> element from the manifest file |
| 419 | """ |
| 420 | d = _Default() |
| 421 | d.remote = self._get_remote(node) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 422 | d.revisionExpr = node.getAttribute('revision') |
| 423 | if d.revisionExpr == '': |
| 424 | d.revisionExpr = None |
Shawn O. Pearce | 6392c87 | 2011-09-22 17:44:31 -0700 | [diff] [blame] | 425 | sync_j = node.getAttribute('sync-j') |
| 426 | if sync_j == '' or sync_j is None: |
| 427 | d.sync_j = 1 |
| 428 | else: |
| 429 | d.sync_j = int(sync_j) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 430 | return d |
| 431 | |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 432 | def _ParseNotice(self, node): |
| 433 | """ |
| 434 | reads a <notice> element from the manifest file |
| 435 | |
| 436 | The <notice> element is distinct from other tags in the XML in that the |
| 437 | data is conveyed between the start and end tag (it's not an empty-element |
| 438 | tag). |
| 439 | |
| 440 | The white space (carriage returns, indentation) for the notice element is |
| 441 | relevant and is parsed in a way that is based on how python docstrings work. |
| 442 | In fact, the code is remarkably similar to here: |
| 443 | http://www.python.org/dev/peps/pep-0257/ |
| 444 | """ |
| 445 | # Get the data out of the node... |
| 446 | notice = node.childNodes[0].data |
| 447 | |
| 448 | # Figure out minimum indentation, skipping the first line (the same line |
| 449 | # as the <notice> tag)... |
| 450 | minIndent = sys.maxint |
| 451 | lines = notice.splitlines() |
| 452 | for line in lines[1:]: |
| 453 | lstrippedLine = line.lstrip() |
| 454 | if lstrippedLine: |
| 455 | indent = len(line) - len(lstrippedLine) |
| 456 | minIndent = min(indent, minIndent) |
| 457 | |
| 458 | # Strip leading / trailing blank lines and also indentation. |
| 459 | cleanLines = [lines[0].strip()] |
| 460 | for line in lines[1:]: |
| 461 | cleanLines.append(line[minIndent:].rstrip()) |
| 462 | |
| 463 | # Clear completely blank lines from front and back... |
| 464 | while cleanLines and not cleanLines[0]: |
| 465 | del cleanLines[0] |
| 466 | while cleanLines and not cleanLines[-1]: |
| 467 | del cleanLines[-1] |
| 468 | |
| 469 | return '\n'.join(cleanLines) |
| 470 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 471 | def _ParseProject(self, node): |
| 472 | """ |
| 473 | reads a <project> element from the manifest file |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 474 | """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 475 | name = self._reqatt(node, 'name') |
| 476 | |
| 477 | remote = self._get_remote(node) |
| 478 | if remote is None: |
| 479 | remote = self._default.remote |
| 480 | if remote is None: |
| 481 | raise ManifestParseError, \ |
| 482 | "no remote for project %s within %s" % \ |
| 483 | (name, self.manifestFile) |
| 484 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 485 | revisionExpr = node.getAttribute('revision') |
| 486 | if not revisionExpr: |
| 487 | revisionExpr = self._default.revisionExpr |
| 488 | if not revisionExpr: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 489 | raise ManifestParseError, \ |
| 490 | "no revision for project %s within %s" % \ |
| 491 | (name, self.manifestFile) |
| 492 | |
| 493 | path = node.getAttribute('path') |
| 494 | if not path: |
| 495 | path = name |
| 496 | if path.startswith('/'): |
| 497 | raise ManifestParseError, \ |
| 498 | "project %s path cannot be absolute in %s" % \ |
| 499 | (name, self.manifestFile) |
| 500 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 501 | if self.IsMirror: |
| 502 | relpath = None |
| 503 | worktree = None |
| 504 | gitdir = os.path.join(self.topdir, '%s.git' % name) |
| 505 | else: |
Anthony Newnam | df14a70 | 2011-01-09 17:31:57 -0800 | [diff] [blame] | 506 | worktree = os.path.join(self.topdir, path).replace('\\', '/') |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 507 | 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] | 508 | |
| 509 | project = Project(manifest = self, |
| 510 | name = name, |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 511 | remote = remote.ToRemoteSpec(name), |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 512 | gitdir = gitdir, |
| 513 | worktree = worktree, |
| 514 | relpath = path, |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 515 | revisionExpr = revisionExpr, |
| 516 | revisionId = None) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 517 | |
| 518 | for n in node.childNodes: |
Shawn O. Pearce | 242b526 | 2009-05-19 13:00:29 -0700 | [diff] [blame] | 519 | if n.nodeName == 'copyfile': |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 520 | self._ParseCopyFile(project, n) |
| 521 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 522 | return project |
| 523 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 524 | def _ParseCopyFile(self, project, node): |
| 525 | src = self._reqatt(node, 'src') |
| 526 | dest = self._reqatt(node, 'dest') |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 527 | if not self.IsMirror: |
| 528 | # src is project relative; |
| 529 | # dest is relative to the top of the tree |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 530 | 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] | 531 | |
| 532 | def _get_remote(self, node): |
| 533 | name = node.getAttribute('remote') |
| 534 | if not name: |
| 535 | return None |
| 536 | |
| 537 | v = self._remotes.get(name) |
| 538 | if not v: |
| 539 | raise ManifestParseError, \ |
| 540 | "remote %s not defined in %s" % \ |
| 541 | (name, self.manifestFile) |
| 542 | return v |
| 543 | |
| 544 | def _reqatt(self, node, attname): |
| 545 | """ |
| 546 | reads a required attribute from the node. |
| 547 | """ |
| 548 | v = node.getAttribute(attname) |
| 549 | if not v: |
| 550 | raise ManifestParseError, \ |
| 551 | "no %s in <%s> within %s" % \ |
| 552 | (attname, node.nodeName, self.manifestFile) |
| 553 | return v |