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