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 | |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 16 | from __future__ import print_function |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 17 | import itertools |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 18 | import os |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 19 | import re |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | import sys |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 21 | import xml.dom.minidom |
| 22 | |
| 23 | from pyversion import is_python3 |
| 24 | if is_python3(): |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 25 | import urllib.parse |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 26 | else: |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 27 | import imp |
| 28 | import urlparse |
| 29 | urllib = imp.new_module('urllib') |
Chirayu Desai | db2ad9d | 2013-06-11 13:42:25 +0530 | [diff] [blame] | 30 | urllib.parse = urlparse |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 31 | |
David Pursehouse | e15c65a | 2012-08-22 10:46:11 +0900 | [diff] [blame] | 32 | from git_config import GitConfig |
David Pursehouse | e00aa6b | 2012-09-11 14:33:51 +0900 | [diff] [blame] | 33 | from git_refs import R_HEADS, HEAD |
| 34 | from project import RemoteSpec, Project, MetaProject |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 35 | from error import ManifestParseError |
| 36 | |
| 37 | MANIFEST_FILE_NAME = 'manifest.xml' |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 38 | LOCAL_MANIFEST_NAME = 'local_manifest.xml' |
David Pursehouse | 2d5a0df | 2012-11-13 02:50:36 +0900 | [diff] [blame] | 39 | LOCAL_MANIFESTS_DIR_NAME = 'local_manifests' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 40 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 41 | urllib.parse.uses_relative.extend(['ssh', 'git']) |
| 42 | urllib.parse.uses_netloc.extend(['ssh', 'git']) |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 43 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 44 | class _Default(object): |
| 45 | """Project defaults within the manifest.""" |
| 46 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 47 | revisionExpr = None |
Conley Owens | b6a16e6 | 2013-09-25 15:06:09 -0700 | [diff] [blame] | 48 | destBranchExpr = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 49 | remote = None |
Shawn O. Pearce | 6392c87 | 2011-09-22 17:44:31 -0700 | [diff] [blame] | 50 | sync_j = 1 |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 51 | sync_c = False |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 52 | sync_s = False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 53 | |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 54 | class _XmlRemote(object): |
| 55 | def __init__(self, |
| 56 | name, |
Yestin Sun | b292b98 | 2012-07-02 07:32:50 -0700 | [diff] [blame] | 57 | alias=None, |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 58 | fetch=None, |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 59 | manifestUrl=None, |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 60 | review=None): |
| 61 | self.name = name |
| 62 | self.fetchUrl = fetch |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 63 | self.manifestUrl = manifestUrl |
Yestin Sun | b292b98 | 2012-07-02 07:32:50 -0700 | [diff] [blame] | 64 | self.remoteAlias = alias |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 65 | self.reviewUrl = review |
Conley Owens | ceea368 | 2011-10-20 10:45:47 -0700 | [diff] [blame] | 66 | self.resolvedFetchUrl = self._resolveFetchUrl() |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 67 | |
David Pursehouse | 717ece9 | 2012-11-13 08:49:16 +0900 | [diff] [blame] | 68 | def __eq__(self, other): |
| 69 | return self.__dict__ == other.__dict__ |
| 70 | |
| 71 | def __ne__(self, other): |
| 72 | return self.__dict__ != other.__dict__ |
| 73 | |
Conley Owens | ceea368 | 2011-10-20 10:45:47 -0700 | [diff] [blame] | 74 | def _resolveFetchUrl(self): |
| 75 | url = self.fetchUrl.rstrip('/') |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 76 | manifestUrl = self.manifestUrl.rstrip('/') |
Shawn Pearce | a9f11b3 | 2013-01-02 15:40:48 -0800 | [diff] [blame] | 77 | p = manifestUrl.startswith('persistent-http') |
| 78 | if p: |
| 79 | manifestUrl = manifestUrl[len('persistent-'):] |
| 80 | |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 81 | # urljoin will get confused if there is no scheme in the base url |
| 82 | # ie, if manifestUrl is of the form <hostname:port> |
| 83 | if manifestUrl.find(':') != manifestUrl.find('/') - 1: |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 84 | manifestUrl = 'gopher://' + manifestUrl |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 85 | url = urllib.parse.urljoin(manifestUrl, url) |
Shawn Pearce | a9f11b3 | 2013-01-02 15:40:48 -0800 | [diff] [blame] | 86 | url = re.sub(r'^gopher://', '', url) |
| 87 | if p: |
| 88 | url = 'persistent-' + url |
| 89 | return url |
Conley Owens | ceea368 | 2011-10-20 10:45:47 -0700 | [diff] [blame] | 90 | |
| 91 | def ToRemoteSpec(self, projectName): |
Conley Owens | 9d8f914 | 2011-10-20 14:36:35 -0700 | [diff] [blame] | 92 | url = self.resolvedFetchUrl.rstrip('/') + '/' + projectName |
Yestin Sun | b292b98 | 2012-07-02 07:32:50 -0700 | [diff] [blame] | 93 | remoteName = self.name |
Conley Owens | 1e7ab2a | 2013-10-08 17:26:57 -0700 | [diff] [blame] | 94 | if self.remoteAlias: |
| 95 | remoteName = self.remoteAlias |
Yestin Sun | b292b98 | 2012-07-02 07:32:50 -0700 | [diff] [blame] | 96 | return RemoteSpec(remoteName, url, self.reviewUrl) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 97 | |
Shawn O. Pearce | c8a300f | 2009-05-18 13:19:57 -0700 | [diff] [blame] | 98 | class XmlManifest(object): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 99 | """manages the repo configuration file""" |
| 100 | |
| 101 | def __init__(self, repodir): |
| 102 | self.repodir = os.path.abspath(repodir) |
| 103 | self.topdir = os.path.dirname(self.repodir) |
| 104 | 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] | 105 | self.globalConfig = GitConfig.ForUser() |
David Pursehouse | 4eb285c | 2013-02-14 16:28:44 +0900 | [diff] [blame] | 106 | self.localManifestWarning = False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 107 | |
| 108 | self.repoProject = MetaProject(self, 'repo', |
| 109 | gitdir = os.path.join(repodir, 'repo/.git'), |
| 110 | worktree = os.path.join(repodir, 'repo')) |
| 111 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 112 | self.manifestProject = MetaProject(self, 'manifests', |
Shawn O. Pearce | f5c25a6 | 2008-11-04 08:11:53 -0800 | [diff] [blame] | 113 | gitdir = os.path.join(repodir, 'manifests.git'), |
| 114 | worktree = os.path.join(repodir, 'manifests')) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 115 | |
| 116 | self._Unload() |
| 117 | |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 118 | def Override(self, name): |
| 119 | """Use a different manifest, just for the current instantiation. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 120 | """ |
| 121 | path = os.path.join(self.manifestProject.worktree, name) |
| 122 | if not os.path.isfile(path): |
| 123 | raise ManifestParseError('manifest %s not found' % name) |
| 124 | |
| 125 | old = self.manifestFile |
| 126 | try: |
| 127 | self.manifestFile = path |
| 128 | self._Unload() |
| 129 | self._Load() |
| 130 | finally: |
| 131 | self.manifestFile = old |
| 132 | |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 133 | def Link(self, name): |
| 134 | """Update the repo metadata to use a different manifest. |
| 135 | """ |
| 136 | self.Override(name) |
| 137 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 138 | try: |
Sebastian Frias | 223bf96 | 2012-11-21 19:09:25 +0100 | [diff] [blame] | 139 | if os.path.lexists(self.manifestFile): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 140 | os.remove(self.manifestFile) |
| 141 | os.symlink('manifests/%s' % name, self.manifestFile) |
Sebastian Frias | 223bf96 | 2012-11-21 19:09:25 +0100 | [diff] [blame] | 142 | except OSError as e: |
| 143 | raise ManifestParseError('cannot link manifest %s: %s' % (name, str(e))) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 144 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 145 | def _RemoteToXml(self, r, doc, root): |
| 146 | e = doc.createElement('remote') |
| 147 | root.appendChild(e) |
| 148 | e.setAttribute('name', r.name) |
| 149 | e.setAttribute('fetch', r.fetchUrl) |
Conley Owens | 1e7ab2a | 2013-10-08 17:26:57 -0700 | [diff] [blame] | 150 | if r.remoteAlias is not None: |
| 151 | e.setAttribute('alias', r.remoteAlias) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 152 | if r.reviewUrl is not None: |
| 153 | e.setAttribute('review', r.reviewUrl) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 154 | |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 155 | def Save(self, fd, peg_rev=False, peg_rev_upstream=True): |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 156 | """Write the current manifest out to the given file descriptor. |
| 157 | """ |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 158 | mp = self.manifestProject |
| 159 | |
| 160 | groups = mp.config.GetString('manifest.groups') |
Matt Gumbel | 0c635bb | 2012-12-21 10:14:53 -0800 | [diff] [blame] | 161 | if groups: |
| 162 | groups = [x for x in re.split(r'[,\s]+', groups) if x] |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 163 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 164 | doc = xml.dom.minidom.Document() |
| 165 | root = doc.createElement('manifest') |
| 166 | doc.appendChild(root) |
| 167 | |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 168 | # Save out the notice. There's a little bit of work here to give it the |
| 169 | # right whitespace, which assumes that the notice is automatically indented |
| 170 | # by 4 by minidom. |
| 171 | if self.notice: |
| 172 | notice_element = root.appendChild(doc.createElement('notice')) |
| 173 | notice_lines = self.notice.splitlines() |
| 174 | indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:] |
| 175 | notice_element.appendChild(doc.createTextNode(indented_notice)) |
| 176 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 177 | d = self.default |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 178 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 179 | for r in sorted(self.remotes): |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 180 | self._RemoteToXml(self.remotes[r], doc, root) |
| 181 | if self.remotes: |
| 182 | root.appendChild(doc.createTextNode('')) |
| 183 | |
| 184 | have_default = False |
| 185 | e = doc.createElement('default') |
| 186 | if d.remote: |
| 187 | have_default = True |
| 188 | e.setAttribute('remote', d.remote.name) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 189 | if d.revisionExpr: |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 190 | have_default = True |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 191 | e.setAttribute('revision', d.revisionExpr) |
Shawn O. Pearce | 6392c87 | 2011-09-22 17:44:31 -0700 | [diff] [blame] | 192 | if d.sync_j > 1: |
| 193 | have_default = True |
| 194 | e.setAttribute('sync-j', '%d' % d.sync_j) |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 195 | if d.sync_c: |
| 196 | have_default = True |
| 197 | e.setAttribute('sync-c', 'true') |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 198 | if d.sync_s: |
| 199 | have_default = True |
| 200 | e.setAttribute('sync-s', 'true') |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 201 | if have_default: |
| 202 | root.appendChild(e) |
| 203 | root.appendChild(doc.createTextNode('')) |
| 204 | |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 205 | if self._manifest_server: |
| 206 | e = doc.createElement('manifest-server') |
| 207 | e.setAttribute('url', self._manifest_server) |
| 208 | root.appendChild(e) |
| 209 | root.appendChild(doc.createTextNode('')) |
| 210 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 211 | def output_projects(parent, parent_node, projects): |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame^] | 212 | for project_name in projects: |
| 213 | for project in self._projects[project_name]: |
| 214 | output_project(parent, parent_node, project) |
Che-Liang Chiou | 69998b0 | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 215 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 216 | def output_project(parent, parent_node, p): |
Shawn O. Pearce | cd81dd6 | 2012-10-26 12:18:00 -0700 | [diff] [blame] | 217 | if not p.MatchesGroups(groups): |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 218 | return |
| 219 | |
| 220 | name = p.name |
| 221 | relpath = p.relpath |
| 222 | if parent: |
| 223 | name = self._UnjoinName(parent.name, name) |
| 224 | relpath = self._UnjoinRelpath(parent.relpath, relpath) |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 225 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 226 | e = doc.createElement('project') |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 227 | parent_node.appendChild(e) |
| 228 | e.setAttribute('name', name) |
| 229 | if relpath != name: |
| 230 | e.setAttribute('path', relpath) |
Conley Owens | 1e7ab2a | 2013-10-08 17:26:57 -0700 | [diff] [blame] | 231 | remoteName = d.remote.remoteAlias or d.remote.name |
| 232 | if not d.remote or p.remote.name != remoteName: |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 233 | e.setAttribute('remote', p.remote.name) |
| 234 | if peg_rev: |
| 235 | if self.IsMirror: |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 236 | value = p.bare_git.rev_parse(p.revisionExpr + '^0') |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 237 | else: |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 238 | value = p.work_git.rev_parse(HEAD + '^0') |
| 239 | e.setAttribute('revision', value) |
| 240 | if peg_rev_upstream and value != p.revisionExpr: |
| 241 | # Only save the origin if the origin is not a sha1, and the default |
| 242 | # isn't our value, and the if the default doesn't already have that |
| 243 | # covered. |
| 244 | e.setAttribute('upstream', p.revisionExpr) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 245 | elif not d.revisionExpr or p.revisionExpr != d.revisionExpr: |
| 246 | e.setAttribute('revision', p.revisionExpr) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 247 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 248 | for c in p.copyfiles: |
| 249 | ce = doc.createElement('copyfile') |
| 250 | ce.setAttribute('src', c.src) |
| 251 | ce.setAttribute('dest', c.dest) |
| 252 | e.appendChild(ce) |
| 253 | |
Conley Owens | bb1b5f5 | 2012-08-13 13:11:18 -0700 | [diff] [blame] | 254 | default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath] |
Dmitry Fink | 17f85ea | 2012-08-06 14:52:29 -0700 | [diff] [blame] | 255 | 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] | 256 | if egroups: |
| 257 | e.setAttribute('groups', ','.join(egroups)) |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 258 | |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 259 | for a in p.annotations: |
| 260 | if a.keep == "true": |
| 261 | ae = doc.createElement('annotation') |
| 262 | ae.setAttribute('name', a.name) |
| 263 | ae.setAttribute('value', a.value) |
| 264 | e.appendChild(ae) |
| 265 | |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 266 | if p.sync_c: |
| 267 | e.setAttribute('sync-c', 'true') |
| 268 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 269 | if p.sync_s: |
| 270 | e.setAttribute('sync-s', 'true') |
| 271 | |
| 272 | if p.subprojects: |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame^] | 273 | subprojects = set(subp.name for subp in p.subprojects) |
| 274 | output_projects(p, e, list(sorted(subprojects))) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 275 | |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame^] | 276 | projects = set(p.name for p in self._paths.values() if not p.parent) |
| 277 | output_projects(None, root, list(sorted(projects))) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 278 | |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 279 | if self._repo_hooks_project: |
| 280 | root.appendChild(doc.createTextNode('')) |
| 281 | e = doc.createElement('repo-hooks') |
| 282 | e.setAttribute('in-project', self._repo_hooks_project.name) |
| 283 | e.setAttribute('enabled-list', |
| 284 | ' '.join(self._repo_hooks_project.enabled_repo_hooks)) |
| 285 | root.appendChild(e) |
| 286 | |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 287 | doc.writexml(fd, '', ' ', '\n', 'UTF-8') |
| 288 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 289 | @property |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame^] | 290 | def paths(self): |
| 291 | self._Load() |
| 292 | return self._paths |
| 293 | |
| 294 | @property |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 295 | def projects(self): |
| 296 | self._Load() |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame^] | 297 | return self._paths.values() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 298 | |
| 299 | @property |
| 300 | def remotes(self): |
| 301 | self._Load() |
| 302 | return self._remotes |
| 303 | |
| 304 | @property |
| 305 | def default(self): |
| 306 | self._Load() |
| 307 | return self._default |
| 308 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 309 | @property |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 310 | def repo_hooks_project(self): |
| 311 | self._Load() |
| 312 | return self._repo_hooks_project |
| 313 | |
| 314 | @property |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 315 | def notice(self): |
| 316 | self._Load() |
| 317 | return self._notice |
| 318 | |
| 319 | @property |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 320 | def manifest_server(self): |
| 321 | self._Load() |
Shawn O. Pearce | 34fb20f | 2011-11-30 13:41:02 -0800 | [diff] [blame] | 322 | return self._manifest_server |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 323 | |
| 324 | @property |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 325 | def IsMirror(self): |
| 326 | return self.manifestProject.config.GetBoolean('repo.mirror') |
| 327 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 328 | def _Unload(self): |
| 329 | self._loaded = False |
| 330 | self._projects = {} |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame^] | 331 | self._paths = {} |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 332 | self._remotes = {} |
| 333 | self._default = None |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 334 | self._repo_hooks_project = None |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 335 | self._notice = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 336 | self.branch = None |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 337 | self._manifest_server = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 338 | |
| 339 | def _Load(self): |
| 340 | if not self._loaded: |
Shawn O. Pearce | 2450a29 | 2008-11-04 08:22:07 -0800 | [diff] [blame] | 341 | m = self.manifestProject |
| 342 | b = m.GetBranch(m.CurrentBranch).merge |
Shawn O. Pearce | 21c5c34 | 2009-06-25 16:47:30 -0700 | [diff] [blame] | 343 | if b is not None and b.startswith(R_HEADS): |
Shawn O. Pearce | 2450a29 | 2008-11-04 08:22:07 -0800 | [diff] [blame] | 344 | b = b[len(R_HEADS):] |
| 345 | self.branch = b |
| 346 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 347 | nodes = [] |
Brian Harring | 475a47d | 2012-06-07 20:05:35 -0700 | [diff] [blame] | 348 | nodes.append(self._ParseManifestXml(self.manifestFile, |
| 349 | self.manifestProject.worktree)) |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 350 | |
| 351 | local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME) |
| 352 | if os.path.exists(local): |
David Pursehouse | 4eb285c | 2013-02-14 16:28:44 +0900 | [diff] [blame] | 353 | if not self.localManifestWarning: |
| 354 | self.localManifestWarning = True |
| 355 | print('warning: %s is deprecated; put local manifests in `%s` instead' |
| 356 | % (LOCAL_MANIFEST_NAME, os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME)), |
| 357 | file=sys.stderr) |
Brian Harring | 475a47d | 2012-06-07 20:05:35 -0700 | [diff] [blame] | 358 | nodes.append(self._ParseManifestXml(local, self.repodir)) |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 359 | |
David Pursehouse | 2d5a0df | 2012-11-13 02:50:36 +0900 | [diff] [blame] | 360 | local_dir = os.path.abspath(os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME)) |
| 361 | try: |
David Pursehouse | 52f1e5d | 2012-11-14 04:53:24 +0900 | [diff] [blame] | 362 | for local_file in sorted(os.listdir(local_dir)): |
David Pursehouse | 2d5a0df | 2012-11-13 02:50:36 +0900 | [diff] [blame] | 363 | if local_file.endswith('.xml'): |
David Pursehouse | 5f434ed | 2012-11-22 13:48:10 +0900 | [diff] [blame] | 364 | local = os.path.join(local_dir, local_file) |
| 365 | nodes.append(self._ParseManifestXml(local, self.repodir)) |
David Pursehouse | 2d5a0df | 2012-11-13 02:50:36 +0900 | [diff] [blame] | 366 | except OSError: |
| 367 | pass |
| 368 | |
Joe Onorato | 26e2475 | 2013-01-11 12:35:53 -0800 | [diff] [blame] | 369 | try: |
| 370 | self._ParseManifest(nodes) |
| 371 | except ManifestParseError as e: |
| 372 | # There was a problem parsing, unload ourselves in case they catch |
| 373 | # this error and try again later, we will show the correct error |
| 374 | self._Unload() |
| 375 | raise e |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 376 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 377 | if self.IsMirror: |
| 378 | self._AddMetaProjectMirror(self.repoProject) |
| 379 | self._AddMetaProjectMirror(self.manifestProject) |
| 380 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 381 | self._loaded = True |
| 382 | |
Brian Harring | 475a47d | 2012-06-07 20:05:35 -0700 | [diff] [blame] | 383 | def _ParseManifestXml(self, path, include_root): |
David Pursehouse | f7fc8a9 | 2012-11-13 04:00:28 +0900 | [diff] [blame] | 384 | try: |
| 385 | root = xml.dom.minidom.parse(path) |
David Pursehouse | 2d5a0df | 2012-11-13 02:50:36 +0900 | [diff] [blame] | 386 | except (OSError, xml.parsers.expat.ExpatError) as e: |
David Pursehouse | f7fc8a9 | 2012-11-13 04:00:28 +0900 | [diff] [blame] | 387 | raise ManifestParseError("error parsing manifest %s: %s" % (path, e)) |
| 388 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 389 | if not root or not root.childNodes: |
Brian Harring | 2644874 | 2011-04-28 05:04:41 -0700 | [diff] [blame] | 390 | raise ManifestParseError("no root node in %s" % (path,)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 391 | |
Jooncheol Park | 34acdd2 | 2012-08-27 02:25:59 +0900 | [diff] [blame] | 392 | for manifest in root.childNodes: |
| 393 | if manifest.nodeName == 'manifest': |
| 394 | break |
| 395 | else: |
Brian Harring | 2644874 | 2011-04-28 05:04:41 -0700 | [diff] [blame] | 396 | raise ManifestParseError("no <manifest> in %s" % (path,)) |
| 397 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 398 | nodes = [] |
David Pursehouse | 4f7bdea | 2012-10-22 12:50:15 +0900 | [diff] [blame] | 399 | for node in manifest.childNodes: # pylint:disable=W0631 |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 400 | # We only get here if manifest is initialised |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 401 | if node.nodeName == 'include': |
| 402 | name = self._reqatt(node, 'name') |
| 403 | fp = os.path.join(include_root, name) |
| 404 | if not os.path.isfile(fp): |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 405 | raise ManifestParseError("include %s doesn't exist or isn't a file" |
| 406 | % (name,)) |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 407 | try: |
| 408 | nodes.extend(self._ParseManifestXml(fp, include_root)) |
| 409 | # should isolate this to the exact exception, but that's |
| 410 | # tricky. actual parsing implementation may vary. |
| 411 | except (KeyboardInterrupt, RuntimeError, SystemExit): |
| 412 | raise |
| 413 | except Exception as e: |
| 414 | raise ManifestParseError( |
| 415 | "failed parsing included manifest %s: %s", (name, e)) |
| 416 | else: |
| 417 | nodes.append(node) |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 418 | return nodes |
Shawn O. Pearce | 03eaf07 | 2008-11-20 11:42:22 -0800 | [diff] [blame] | 419 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 420 | def _ParseManifest(self, node_list): |
| 421 | for node in itertools.chain(*node_list): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 422 | if node.nodeName == 'remote': |
| 423 | remote = self._ParseRemote(node) |
David Pursehouse | 717ece9 | 2012-11-13 08:49:16 +0900 | [diff] [blame] | 424 | if remote: |
| 425 | if remote.name in self._remotes: |
| 426 | if remote != self._remotes[remote.name]: |
| 427 | raise ManifestParseError( |
| 428 | 'remote %s already exists with different attributes' % |
| 429 | (remote.name)) |
| 430 | else: |
| 431 | self._remotes[remote.name] = remote |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 432 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 433 | for node in itertools.chain(*node_list): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 434 | if node.nodeName == 'default': |
| 435 | if self._default is not None: |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 436 | raise ManifestParseError( |
| 437 | 'duplicate default in %s' % |
| 438 | (self.manifestFile)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 439 | self._default = self._ParseDefault(node) |
| 440 | if self._default is None: |
| 441 | self._default = _Default() |
| 442 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 443 | for node in itertools.chain(*node_list): |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 444 | if node.nodeName == 'notice': |
| 445 | if self._notice is not None: |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 446 | raise ManifestParseError( |
| 447 | 'duplicate notice in %s' % |
| 448 | (self.manifestFile)) |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 449 | self._notice = self._ParseNotice(node) |
| 450 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 451 | for node in itertools.chain(*node_list): |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 452 | if node.nodeName == 'manifest-server': |
| 453 | url = self._reqatt(node, 'url') |
| 454 | if self._manifest_server is not None: |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 455 | raise ManifestParseError( |
| 456 | 'duplicate manifest-server in %s' % |
| 457 | (self.manifestFile)) |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 458 | self._manifest_server = url |
| 459 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 460 | def recursively_add_projects(project): |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame^] | 461 | projects = self._projects.setdefault(project.name, []) |
| 462 | if project.relpath is None: |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 463 | raise ManifestParseError( |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame^] | 464 | 'missing path for %s in %s' % |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 465 | (project.name, self.manifestFile)) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame^] | 466 | if project.relpath in self._paths: |
| 467 | raise ManifestParseError( |
| 468 | 'duplicate path %s in %s' % |
| 469 | (project.relpath, self.manifestFile)) |
| 470 | self._paths[project.relpath] = project |
| 471 | projects.append(project) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 472 | for subproject in project.subprojects: |
| 473 | recursively_add_projects(subproject) |
| 474 | |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 475 | for node in itertools.chain(*node_list): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 476 | if node.nodeName == 'project': |
| 477 | project = self._ParseProject(node) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 478 | recursively_add_projects(project) |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 479 | if node.nodeName == 'repo-hooks': |
| 480 | # Get the name of the project and the (space-separated) list of enabled. |
| 481 | repo_hooks_project = self._reqatt(node, 'in-project') |
| 482 | enabled_repo_hooks = self._reqatt(node, 'enabled-list').split() |
| 483 | |
| 484 | # Only one project can be the hooks project |
| 485 | if self._repo_hooks_project is not None: |
| 486 | raise ManifestParseError( |
| 487 | 'duplicate repo-hooks in %s' % |
| 488 | (self.manifestFile)) |
| 489 | |
| 490 | # Store a reference to the Project. |
| 491 | try: |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame^] | 492 | repo_hooks_projects = self._projects[repo_hooks_project] |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 493 | except KeyError: |
| 494 | raise ManifestParseError( |
| 495 | 'project %s not found for repo-hooks' % |
| 496 | (repo_hooks_project)) |
| 497 | |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame^] | 498 | if len(repo_hooks_projects) != 1: |
| 499 | raise ManifestParseError( |
| 500 | 'internal error parsing repo-hooks in %s' % |
| 501 | (self.manifestFile)) |
| 502 | self._repo_hooks_project = repo_hooks_projects[0] |
| 503 | |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 504 | # Store the enabled hooks in the Project object. |
| 505 | self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 506 | if node.nodeName == 'remove-project': |
| 507 | name = self._reqatt(node, 'name') |
| 508 | try: |
| 509 | del self._projects[name] |
| 510 | except KeyError: |
David Pursehouse | f910748 | 2012-11-16 19:12:32 +0900 | [diff] [blame] | 511 | raise ManifestParseError('remove-project element specifies non-existent ' |
| 512 | 'project: %s' % name) |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 513 | |
| 514 | # If the manifest removes the hooks project, treat it as if it deleted |
| 515 | # the repo-hooks element too. |
| 516 | if self._repo_hooks_project and (self._repo_hooks_project.name == name): |
| 517 | self._repo_hooks_project = None |
| 518 | |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 519 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 520 | def _AddMetaProjectMirror(self, m): |
| 521 | name = None |
| 522 | m_url = m.GetRemote(m.remote.name).url |
| 523 | if m_url.endswith('/.git'): |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 524 | raise ManifestParseError('refusing to mirror %s' % m_url) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 525 | |
| 526 | if self._default and self._default.remote: |
Conley Owens | ceea368 | 2011-10-20 10:45:47 -0700 | [diff] [blame] | 527 | url = self._default.remote.resolvedFetchUrl |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 528 | if not url.endswith('/'): |
| 529 | url += '/' |
| 530 | if m_url.startswith(url): |
| 531 | remote = self._default.remote |
| 532 | name = m_url[len(url):] |
| 533 | |
| 534 | if name is None: |
| 535 | s = m_url.rindex('/') + 1 |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 536 | manifestUrl = self.manifestProject.config.GetString('remote.origin.url') |
Shawn O. Pearce | f35b2d9 | 2012-08-02 11:46:22 -0700 | [diff] [blame] | 537 | remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 538 | name = m_url[s:] |
| 539 | |
| 540 | if name.endswith('.git'): |
| 541 | name = name[:-4] |
| 542 | |
| 543 | if name not in self._projects: |
| 544 | m.PreSync() |
| 545 | gitdir = os.path.join(self.topdir, '%s.git' % name) |
| 546 | project = Project(manifest = self, |
| 547 | name = name, |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 548 | remote = remote.ToRemoteSpec(name), |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 549 | gitdir = gitdir, |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame^] | 550 | objdir = gitdir, |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 551 | worktree = None, |
| 552 | relpath = None, |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 553 | revisionExpr = m.revisionExpr, |
| 554 | revisionId = None) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame^] | 555 | self._projects[project.name] = [project] |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 556 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 557 | def _ParseRemote(self, node): |
| 558 | """ |
| 559 | reads a <remote> element from the manifest file |
| 560 | """ |
| 561 | name = self._reqatt(node, 'name') |
Yestin Sun | b292b98 | 2012-07-02 07:32:50 -0700 | [diff] [blame] | 562 | alias = node.getAttribute('alias') |
| 563 | if alias == '': |
| 564 | alias = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 565 | fetch = self._reqatt(node, 'fetch') |
| 566 | review = node.getAttribute('review') |
Shawn O. Pearce | ae6e094 | 2008-11-06 10:25:35 -0800 | [diff] [blame] | 567 | if review == '': |
| 568 | review = None |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 569 | manifestUrl = self.manifestProject.config.GetString('remote.origin.url') |
Yestin Sun | b292b98 | 2012-07-02 07:32:50 -0700 | [diff] [blame] | 570 | return _XmlRemote(name, alias, fetch, manifestUrl, review) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 571 | |
| 572 | def _ParseDefault(self, node): |
| 573 | """ |
| 574 | reads a <default> element from the manifest file |
| 575 | """ |
| 576 | d = _Default() |
| 577 | d.remote = self._get_remote(node) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 578 | d.revisionExpr = node.getAttribute('revision') |
| 579 | if d.revisionExpr == '': |
| 580 | d.revisionExpr = None |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 581 | |
Bryan Jacobs | f609f91 | 2013-05-06 13:36:24 -0400 | [diff] [blame] | 582 | d.destBranchExpr = node.getAttribute('dest-branch') or None |
| 583 | |
Shawn O. Pearce | 6392c87 | 2011-09-22 17:44:31 -0700 | [diff] [blame] | 584 | sync_j = node.getAttribute('sync-j') |
| 585 | if sync_j == '' or sync_j is None: |
| 586 | d.sync_j = 1 |
| 587 | else: |
| 588 | d.sync_j = int(sync_j) |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 589 | |
| 590 | sync_c = node.getAttribute('sync-c') |
| 591 | if not sync_c: |
| 592 | d.sync_c = False |
| 593 | else: |
| 594 | d.sync_c = sync_c.lower() in ("yes", "true", "1") |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 595 | |
| 596 | sync_s = node.getAttribute('sync-s') |
| 597 | if not sync_s: |
| 598 | d.sync_s = False |
| 599 | else: |
| 600 | d.sync_s = sync_s.lower() in ("yes", "true", "1") |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 601 | return d |
| 602 | |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 603 | def _ParseNotice(self, node): |
| 604 | """ |
| 605 | reads a <notice> element from the manifest file |
| 606 | |
| 607 | The <notice> element is distinct from other tags in the XML in that the |
| 608 | data is conveyed between the start and end tag (it's not an empty-element |
| 609 | tag). |
| 610 | |
| 611 | The white space (carriage returns, indentation) for the notice element is |
| 612 | relevant and is parsed in a way that is based on how python docstrings work. |
| 613 | In fact, the code is remarkably similar to here: |
| 614 | http://www.python.org/dev/peps/pep-0257/ |
| 615 | """ |
| 616 | # Get the data out of the node... |
| 617 | notice = node.childNodes[0].data |
| 618 | |
| 619 | # Figure out minimum indentation, skipping the first line (the same line |
| 620 | # as the <notice> tag)... |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 621 | minIndent = sys.maxsize |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 622 | lines = notice.splitlines() |
| 623 | for line in lines[1:]: |
| 624 | lstrippedLine = line.lstrip() |
| 625 | if lstrippedLine: |
| 626 | indent = len(line) - len(lstrippedLine) |
| 627 | minIndent = min(indent, minIndent) |
| 628 | |
| 629 | # Strip leading / trailing blank lines and also indentation. |
| 630 | cleanLines = [lines[0].strip()] |
| 631 | for line in lines[1:]: |
| 632 | cleanLines.append(line[minIndent:].rstrip()) |
| 633 | |
| 634 | # Clear completely blank lines from front and back... |
| 635 | while cleanLines and not cleanLines[0]: |
| 636 | del cleanLines[0] |
| 637 | while cleanLines and not cleanLines[-1]: |
| 638 | del cleanLines[-1] |
| 639 | |
| 640 | return '\n'.join(cleanLines) |
| 641 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 642 | def _JoinName(self, parent_name, name): |
| 643 | return os.path.join(parent_name, name) |
| 644 | |
| 645 | def _UnjoinName(self, parent_name, name): |
| 646 | return os.path.relpath(name, parent_name) |
| 647 | |
| 648 | def _ParseProject(self, node, parent = None): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 649 | """ |
| 650 | reads a <project> element from the manifest file |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 651 | """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 652 | name = self._reqatt(node, 'name') |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 653 | if parent: |
| 654 | name = self._JoinName(parent.name, name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 655 | |
| 656 | remote = self._get_remote(node) |
| 657 | if remote is None: |
| 658 | remote = self._default.remote |
| 659 | if remote is None: |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 660 | raise ManifestParseError("no remote for project %s within %s" % |
| 661 | (name, self.manifestFile)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 662 | |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 663 | revisionExpr = node.getAttribute('revision') |
| 664 | if not revisionExpr: |
| 665 | revisionExpr = self._default.revisionExpr |
| 666 | if not revisionExpr: |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 667 | raise ManifestParseError("no revision for project %s within %s" % |
| 668 | (name, self.manifestFile)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 669 | |
| 670 | path = node.getAttribute('path') |
| 671 | if not path: |
| 672 | path = name |
| 673 | if path.startswith('/'): |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 674 | raise ManifestParseError("project %s path cannot be absolute in %s" % |
| 675 | (name, self.manifestFile)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 676 | |
Mike Pontillo | d315382 | 2012-02-28 11:53:24 -0800 | [diff] [blame] | 677 | rebase = node.getAttribute('rebase') |
| 678 | if not rebase: |
| 679 | rebase = True |
| 680 | else: |
| 681 | rebase = rebase.lower() in ("yes", "true", "1") |
| 682 | |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 683 | sync_c = node.getAttribute('sync-c') |
| 684 | if not sync_c: |
| 685 | sync_c = False |
| 686 | else: |
| 687 | sync_c = sync_c.lower() in ("yes", "true", "1") |
| 688 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 689 | sync_s = node.getAttribute('sync-s') |
| 690 | if not sync_s: |
| 691 | sync_s = self._default.sync_s |
| 692 | else: |
| 693 | sync_s = sync_s.lower() in ("yes", "true", "1") |
| 694 | |
David Pursehouse | ede7f12 | 2012-11-27 22:25:30 +0900 | [diff] [blame] | 695 | clone_depth = node.getAttribute('clone-depth') |
| 696 | if clone_depth: |
| 697 | try: |
| 698 | clone_depth = int(clone_depth) |
| 699 | if clone_depth <= 0: |
| 700 | raise ValueError() |
| 701 | except ValueError: |
| 702 | raise ManifestParseError('invalid clone-depth %s in %s' % |
| 703 | (clone_depth, self.manifestFile)) |
| 704 | |
Bryan Jacobs | f609f91 | 2013-05-06 13:36:24 -0400 | [diff] [blame] | 705 | dest_branch = node.getAttribute('dest-branch') or self._default.destBranchExpr |
| 706 | |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 707 | upstream = node.getAttribute('upstream') |
| 708 | |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 709 | groups = '' |
| 710 | if node.hasAttribute('groups'): |
| 711 | groups = node.getAttribute('groups') |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 712 | groups = [x for x in re.split(r'[,\s]+', groups) if x] |
Brian Harring | 7da1314 | 2012-06-15 02:24:20 -0700 | [diff] [blame] | 713 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 714 | if parent is None: |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame^] | 715 | relpath, worktree, gitdir, objdir = self.GetProjectPaths(name, path) |
Shawn O. Pearce | cd81dd6 | 2012-10-26 12:18:00 -0700 | [diff] [blame] | 716 | else: |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame^] | 717 | relpath, worktree, gitdir, objdir = \ |
| 718 | self.GetSubprojectPaths(parent, name, path) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 719 | |
| 720 | default_groups = ['all', 'name:%s' % name, 'path:%s' % relpath] |
| 721 | groups.extend(set(default_groups).difference(groups)) |
Shawn O. Pearce | cd81dd6 | 2012-10-26 12:18:00 -0700 | [diff] [blame] | 722 | |
Scott Fan | db83b1b | 2013-02-28 09:34:14 +0800 | [diff] [blame] | 723 | if self.IsMirror and node.hasAttribute('force-path'): |
| 724 | if node.getAttribute('force-path').lower() in ("yes", "true", "1"): |
| 725 | gitdir = os.path.join(self.topdir, '%s.git' % path) |
| 726 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 727 | project = Project(manifest = self, |
| 728 | name = name, |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 729 | remote = remote.ToRemoteSpec(name), |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 730 | gitdir = gitdir, |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame^] | 731 | objdir = objdir, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 732 | worktree = worktree, |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 733 | relpath = relpath, |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 734 | revisionExpr = revisionExpr, |
Mike Pontillo | d315382 | 2012-02-28 11:53:24 -0800 | [diff] [blame] | 735 | revisionId = None, |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 736 | rebase = rebase, |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 737 | groups = groups, |
Brian Harring | 14a6674 | 2012-09-28 20:21:57 -0700 | [diff] [blame] | 738 | sync_c = sync_c, |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 739 | sync_s = sync_s, |
David Pursehouse | ede7f12 | 2012-11-27 22:25:30 +0900 | [diff] [blame] | 740 | clone_depth = clone_depth, |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 741 | upstream = upstream, |
Bryan Jacobs | f609f91 | 2013-05-06 13:36:24 -0400 | [diff] [blame] | 742 | parent = parent, |
| 743 | dest_branch = dest_branch) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 744 | |
| 745 | for n in node.childNodes: |
Shawn O. Pearce | 242b526 | 2009-05-19 13:00:29 -0700 | [diff] [blame] | 746 | if n.nodeName == 'copyfile': |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 747 | self._ParseCopyFile(project, n) |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 748 | if n.nodeName == 'annotation': |
| 749 | self._ParseAnnotation(project, n) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 750 | if n.nodeName == 'project': |
| 751 | project.subprojects.append(self._ParseProject(n, parent = project)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 752 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 753 | return project |
| 754 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 755 | def GetProjectPaths(self, name, path): |
| 756 | relpath = path |
| 757 | if self.IsMirror: |
| 758 | worktree = None |
| 759 | gitdir = os.path.join(self.topdir, '%s.git' % name) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame^] | 760 | objdir = gitdir |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 761 | else: |
| 762 | worktree = os.path.join(self.topdir, path).replace('\\', '/') |
| 763 | gitdir = os.path.join(self.repodir, 'projects', '%s.git' % path) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame^] | 764 | objdir = os.path.join(self.repodir, 'project-objects', '%s.git' % name) |
| 765 | return relpath, worktree, gitdir, objdir |
| 766 | |
| 767 | def GetProjectsWithName(self, name): |
| 768 | return self._projects.get(name, []) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 769 | |
| 770 | def GetSubprojectName(self, parent, submodule_path): |
| 771 | return os.path.join(parent.name, submodule_path) |
| 772 | |
| 773 | def _JoinRelpath(self, parent_relpath, relpath): |
| 774 | return os.path.join(parent_relpath, relpath) |
| 775 | |
| 776 | def _UnjoinRelpath(self, parent_relpath, relpath): |
| 777 | return os.path.relpath(relpath, parent_relpath) |
| 778 | |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame^] | 779 | def GetSubprojectPaths(self, parent, name, path): |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 780 | relpath = self._JoinRelpath(parent.relpath, path) |
| 781 | gitdir = os.path.join(parent.gitdir, 'subprojects', '%s.git' % path) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame^] | 782 | objdir = os.path.join(parent.gitdir, 'subproject-objects', '%s.git' % name) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 783 | if self.IsMirror: |
| 784 | worktree = None |
| 785 | else: |
| 786 | worktree = os.path.join(parent.worktree, path).replace('\\', '/') |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame^] | 787 | return relpath, worktree, gitdir, objdir |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 788 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 789 | def _ParseCopyFile(self, project, node): |
| 790 | src = self._reqatt(node, 'src') |
| 791 | dest = self._reqatt(node, 'dest') |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 792 | if not self.IsMirror: |
| 793 | # src is project relative; |
| 794 | # dest is relative to the top of the tree |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 795 | 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] | 796 | |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 797 | def _ParseAnnotation(self, project, node): |
| 798 | name = self._reqatt(node, 'name') |
| 799 | value = self._reqatt(node, 'value') |
| 800 | try: |
| 801 | keep = self._reqatt(node, 'keep').lower() |
| 802 | except ManifestParseError: |
| 803 | keep = "true" |
| 804 | if keep != "true" and keep != "false": |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 805 | raise ManifestParseError('optional "keep" attribute must be ' |
| 806 | '"true" or "false"') |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 807 | project.AddAnnotation(name, value, keep) |
| 808 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 809 | def _get_remote(self, node): |
| 810 | name = node.getAttribute('remote') |
| 811 | if not name: |
| 812 | return None |
| 813 | |
| 814 | v = self._remotes.get(name) |
| 815 | if not v: |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 816 | raise ManifestParseError("remote %s not defined in %s" % |
| 817 | (name, self.manifestFile)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 818 | return v |
| 819 | |
| 820 | def _reqatt(self, node, attname): |
| 821 | """ |
| 822 | reads a required attribute from the node. |
| 823 | """ |
| 824 | v = node.getAttribute(attname) |
| 825 | if not v: |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 826 | raise ManifestParseError("no %s in <%s> within %s" % |
| 827 | (attname, node.nodeName, self.manifestFile)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 828 | return v |