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