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