blob: 890c954de759216f5833c2170b22a3f1f3d39a12 [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001#
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 Owenscecd1d82012-11-01 22:59:27 -070016from __future__ import print_function
Colin Cross23acdd32012-04-21 00:33:54 -070017import itertools
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070018import os
Conley Owensdb728cd2011-09-26 16:34:01 -070019import re
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070020import sys
David Pursehouse59bbb582013-05-17 10:49:33 +090021import xml.dom.minidom
22
23from pyversion import is_python3
24if is_python3():
Chirayu Desai217ea7d2013-03-01 19:14:38 +053025 import urllib.parse
David Pursehouse59bbb582013-05-17 10:49:33 +090026else:
Chirayu Desai217ea7d2013-03-01 19:14:38 +053027 import imp
28 import urlparse
29 urllib = imp.new_module('urllib')
Chirayu Desaidb2ad9d2013-06-11 13:42:25 +053030 urllib.parse = urlparse
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070031
David Pursehousee15c65a2012-08-22 10:46:11 +090032from git_config import GitConfig
David Pursehousee00aa6b2012-09-11 14:33:51 +090033from git_refs import R_HEADS, HEAD
34from project import RemoteSpec, Project, MetaProject
Julien Camperguedd654222014-01-09 16:21:37 +010035from error import ManifestParseError, ManifestInvalidRevisionError
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070036
37MANIFEST_FILE_NAME = 'manifest.xml'
Shawn O. Pearce5cc66792008-10-23 16:19:27 -070038LOCAL_MANIFEST_NAME = 'local_manifest.xml'
David Pursehouse2d5a0df2012-11-13 02:50:36 +090039LOCAL_MANIFESTS_DIR_NAME = 'local_manifests'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070040
Chirayu Desai217ea7d2013-03-01 19:14:38 +053041urllib.parse.uses_relative.extend(['ssh', 'git'])
42urllib.parse.uses_netloc.extend(['ssh', 'git'])
Conley Owensdb728cd2011-09-26 16:34:01 -070043
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070044class _Default(object):
45 """Project defaults within the manifest."""
46
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -070047 revisionExpr = None
Conley Owensb6a16e62013-09-25 15:06:09 -070048 destBranchExpr = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070049 remote = None
Shawn O. Pearce6392c872011-09-22 17:44:31 -070050 sync_j = 1
Anatol Pomazau79770d22012-04-20 14:41:59 -070051 sync_c = False
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +080052 sync_s = False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070053
Julien Campergue74879922013-10-09 14:38:46 +020054 def __eq__(self, other):
55 return self.__dict__ == other.__dict__
56
57 def __ne__(self, other):
58 return self.__dict__ != other.__dict__
59
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070060class _XmlRemote(object):
61 def __init__(self,
62 name,
Yestin Sunb292b982012-07-02 07:32:50 -070063 alias=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070064 fetch=None,
Conley Owensdb728cd2011-09-26 16:34:01 -070065 manifestUrl=None,
Anthony King36ea2fb2014-05-06 11:54:01 +010066 review=None,
67 revision=None):
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070068 self.name = name
69 self.fetchUrl = fetch
Conley Owensdb728cd2011-09-26 16:34:01 -070070 self.manifestUrl = manifestUrl
Yestin Sunb292b982012-07-02 07:32:50 -070071 self.remoteAlias = alias
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070072 self.reviewUrl = review
Anthony King36ea2fb2014-05-06 11:54:01 +010073 self.revision = revision
Conley Owensceea3682011-10-20 10:45:47 -070074 self.resolvedFetchUrl = self._resolveFetchUrl()
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070075
David Pursehouse717ece92012-11-13 08:49:16 +090076 def __eq__(self, other):
77 return self.__dict__ == other.__dict__
78
79 def __ne__(self, other):
80 return self.__dict__ != other.__dict__
81
Conley Owensceea3682011-10-20 10:45:47 -070082 def _resolveFetchUrl(self):
83 url = self.fetchUrl.rstrip('/')
Conley Owensdb728cd2011-09-26 16:34:01 -070084 manifestUrl = self.manifestUrl.rstrip('/')
Conley Owens2d0f5082014-01-31 15:03:51 -080085 # urljoin will gets confused over quite a few things. The ones we care
86 # about here are:
87 # * no scheme in the base url, like <hostname:port>
88 # * persistent-https://
T.R. Fullhart48633072014-09-10 13:44:39 -070089 # * rpc://
Conley Owens2d0f5082014-01-31 15:03:51 -080090 # We handle this by replacing these with obscure protocols
91 # and then replacing them with the original when we are done.
92 # gopher -> <none>
93 # wais -> persistent-https
T.R. Fullhart48633072014-09-10 13:44:39 -070094 # nntp -> rpc
Conley Owensdb728cd2011-09-26 16:34:01 -070095 if manifestUrl.find(':') != manifestUrl.find('/') - 1:
David Pursehousec1b86a22012-11-14 11:36:51 +090096 manifestUrl = 'gopher://' + manifestUrl
Conley Owens2d0f5082014-01-31 15:03:51 -080097 manifestUrl = re.sub(r'^persistent-https://', 'wais://', manifestUrl)
T.R. Fullhart48633072014-09-10 13:44:39 -070098 manifestUrl = re.sub(r'^rpc://', 'nntp://', manifestUrl)
Chirayu Desai217ea7d2013-03-01 19:14:38 +053099 url = urllib.parse.urljoin(manifestUrl, url)
Shawn Pearcea9f11b32013-01-02 15:40:48 -0800100 url = re.sub(r'^gopher://', '', url)
Conley Owens2d0f5082014-01-31 15:03:51 -0800101 url = re.sub(r'^wais://', 'persistent-https://', url)
T.R. Fullhart48633072014-09-10 13:44:39 -0700102 url = re.sub(r'^nntp://', 'rpc://', url)
Shawn Pearcea9f11b32013-01-02 15:40:48 -0800103 return url
Conley Owensceea3682011-10-20 10:45:47 -0700104
105 def ToRemoteSpec(self, projectName):
Conley Owens9d8f9142011-10-20 14:36:35 -0700106 url = self.resolvedFetchUrl.rstrip('/') + '/' + projectName
Yestin Sunb292b982012-07-02 07:32:50 -0700107 remoteName = self.name
Conley Owens1e7ab2a2013-10-08 17:26:57 -0700108 if self.remoteAlias:
David Pursehouse37128b62013-10-15 10:48:40 +0900109 remoteName = self.remoteAlias
Yestin Sunb292b982012-07-02 07:32:50 -0700110 return RemoteSpec(remoteName, url, self.reviewUrl)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700111
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -0700112class XmlManifest(object):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700113 """manages the repo configuration file"""
114
115 def __init__(self, repodir):
116 self.repodir = os.path.abspath(repodir)
117 self.topdir = os.path.dirname(self.repodir)
118 self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700119 self.globalConfig = GitConfig.ForUser()
David Pursehouse4eb285c2013-02-14 16:28:44 +0900120 self.localManifestWarning = False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700121
122 self.repoProject = MetaProject(self, 'repo',
123 gitdir = os.path.join(repodir, 'repo/.git'),
124 worktree = os.path.join(repodir, 'repo'))
125
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700126 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -0800127 gitdir = os.path.join(repodir, 'manifests.git'),
128 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700129
130 self._Unload()
131
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700132 def Override(self, name):
133 """Use a different manifest, just for the current instantiation.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700134 """
135 path = os.path.join(self.manifestProject.worktree, name)
136 if not os.path.isfile(path):
137 raise ManifestParseError('manifest %s not found' % name)
138
139 old = self.manifestFile
140 try:
141 self.manifestFile = path
142 self._Unload()
143 self._Load()
144 finally:
145 self.manifestFile = old
146
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700147 def Link(self, name):
148 """Update the repo metadata to use a different manifest.
149 """
150 self.Override(name)
151
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700152 try:
Sebastian Frias223bf962012-11-21 19:09:25 +0100153 if os.path.lexists(self.manifestFile):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700154 os.remove(self.manifestFile)
155 os.symlink('manifests/%s' % name, self.manifestFile)
Sebastian Frias223bf962012-11-21 19:09:25 +0100156 except OSError as e:
157 raise ManifestParseError('cannot link manifest %s: %s' % (name, str(e)))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700158
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800159 def _RemoteToXml(self, r, doc, root):
160 e = doc.createElement('remote')
161 root.appendChild(e)
162 e.setAttribute('name', r.name)
163 e.setAttribute('fetch', r.fetchUrl)
Conley Owens1e7ab2a2013-10-08 17:26:57 -0700164 if r.remoteAlias is not None:
165 e.setAttribute('alias', r.remoteAlias)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800166 if r.reviewUrl is not None:
167 e.setAttribute('review', r.reviewUrl)
Anthony King36ea2fb2014-05-06 11:54:01 +0100168 if r.revision is not None:
169 e.setAttribute('revision', r.revision)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800170
Josh Triplett884a3872014-06-12 14:57:29 -0700171 def _ParseGroups(self, groups):
172 return [x for x in re.split(r'[,\s]+', groups) if x]
173
Brian Harring14a66742012-09-28 20:21:57 -0700174 def Save(self, fd, peg_rev=False, peg_rev_upstream=True):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800175 """Write the current manifest out to the given file descriptor.
176 """
Colin Cross5acde752012-03-28 20:15:45 -0700177 mp = self.manifestProject
178
179 groups = mp.config.GetString('manifest.groups')
Matt Gumbel0c635bb2012-12-21 10:14:53 -0800180 if groups:
Josh Triplett884a3872014-06-12 14:57:29 -0700181 groups = self._ParseGroups(groups)
Colin Cross5acde752012-03-28 20:15:45 -0700182
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800183 doc = xml.dom.minidom.Document()
184 root = doc.createElement('manifest')
185 doc.appendChild(root)
186
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700187 # Save out the notice. There's a little bit of work here to give it the
188 # right whitespace, which assumes that the notice is automatically indented
189 # by 4 by minidom.
190 if self.notice:
191 notice_element = root.appendChild(doc.createElement('notice'))
192 notice_lines = self.notice.splitlines()
193 indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:]
194 notice_element.appendChild(doc.createTextNode(indented_notice))
195
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800196 d = self.default
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800197
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530198 for r in sorted(self.remotes):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800199 self._RemoteToXml(self.remotes[r], doc, root)
200 if self.remotes:
201 root.appendChild(doc.createTextNode(''))
202
203 have_default = False
204 e = doc.createElement('default')
205 if d.remote:
206 have_default = True
207 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700208 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800209 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700210 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700211 if d.sync_j > 1:
212 have_default = True
213 e.setAttribute('sync-j', '%d' % d.sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700214 if d.sync_c:
215 have_default = True
216 e.setAttribute('sync-c', 'true')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800217 if d.sync_s:
218 have_default = True
219 e.setAttribute('sync-s', 'true')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800220 if have_default:
221 root.appendChild(e)
222 root.appendChild(doc.createTextNode(''))
223
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700224 if self._manifest_server:
225 e = doc.createElement('manifest-server')
226 e.setAttribute('url', self._manifest_server)
227 root.appendChild(e)
228 root.appendChild(doc.createTextNode(''))
229
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800230 def output_projects(parent, parent_node, projects):
David James8d201162013-10-11 17:03:19 -0700231 for project_name in projects:
232 for project in self._projects[project_name]:
233 output_project(parent, parent_node, project)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800234
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800235 def output_project(parent, parent_node, p):
Colin Cross5acde752012-03-28 20:15:45 -0700236 if not p.MatchesGroups(groups):
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800237 return
238
239 name = p.name
240 relpath = p.relpath
241 if parent:
242 name = self._UnjoinName(parent.name, name)
243 relpath = self._UnjoinRelpath(parent.relpath, relpath)
Colin Cross5acde752012-03-28 20:15:45 -0700244
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800245 e = doc.createElement('project')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800246 parent_node.appendChild(e)
247 e.setAttribute('name', name)
248 if relpath != name:
249 e.setAttribute('path', relpath)
Conley Owensa17d7af2013-10-16 14:38:09 -0700250 remoteName = None
251 if d.remote:
Conley Owensce201a52013-10-16 14:42:42 -0700252 remoteName = d.remote.remoteAlias or d.remote.name
Conley Owens1e7ab2a2013-10-08 17:26:57 -0700253 if not d.remote or p.remote.name != remoteName:
Anthony King36ea2fb2014-05-06 11:54:01 +0100254 remoteName = p.remote.name
255 e.setAttribute('remote', remoteName)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800256 if peg_rev:
257 if self.IsMirror:
Brian Harring14a66742012-09-28 20:21:57 -0700258 value = p.bare_git.rev_parse(p.revisionExpr + '^0')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800259 else:
Brian Harring14a66742012-09-28 20:21:57 -0700260 value = p.work_git.rev_parse(HEAD + '^0')
261 e.setAttribute('revision', value)
262 if peg_rev_upstream and value != p.revisionExpr:
263 # Only save the origin if the origin is not a sha1, and the default
264 # isn't our value, and the if the default doesn't already have that
265 # covered.
266 e.setAttribute('upstream', p.revisionExpr)
Anthony King36ea2fb2014-05-06 11:54:01 +0100267 else:
268 revision = self.remotes[remoteName].revision or d.revisionExpr
269 if not revision or revision != p.revisionExpr:
270 e.setAttribute('revision', p.revisionExpr)
Mani Chandel7a91d512014-07-24 16:27:08 +0530271 if p.upstream and p.upstream != p.revisionExpr:
272 e.setAttribute('upstream', p.upstream)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800273
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800274 for c in p.copyfiles:
275 ce = doc.createElement('copyfile')
276 ce.setAttribute('src', c.src)
277 ce.setAttribute('dest', c.dest)
278 e.appendChild(ce)
279
Jeff Hamiltone0df2322014-04-21 17:10:59 -0500280 for l in p.linkfiles:
281 le = doc.createElement('linkfile')
282 le.setAttribute('src', l.src)
283 le.setAttribute('dest', l.dest)
284 e.appendChild(le)
285
Conley Owensbb1b5f52012-08-13 13:11:18 -0700286 default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath]
Dmitry Fink17f85ea2012-08-06 14:52:29 -0700287 egroups = [g for g in p.groups if g not in default_groups]
Conley Owens971de8e2012-04-16 10:36:08 -0700288 if egroups:
289 e.setAttribute('groups', ','.join(egroups))
Colin Cross5acde752012-03-28 20:15:45 -0700290
James W. Mills24c13082012-04-12 15:04:13 -0500291 for a in p.annotations:
292 if a.keep == "true":
293 ae = doc.createElement('annotation')
294 ae.setAttribute('name', a.name)
295 ae.setAttribute('value', a.value)
296 e.appendChild(ae)
297
Anatol Pomazau79770d22012-04-20 14:41:59 -0700298 if p.sync_c:
299 e.setAttribute('sync-c', 'true')
300
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800301 if p.sync_s:
302 e.setAttribute('sync-s', 'true')
303
304 if p.subprojects:
David James8d201162013-10-11 17:03:19 -0700305 subprojects = set(subp.name for subp in p.subprojects)
306 output_projects(p, e, list(sorted(subprojects)))
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800307
David James8d201162013-10-11 17:03:19 -0700308 projects = set(p.name for p in self._paths.values() if not p.parent)
309 output_projects(None, root, list(sorted(projects)))
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800310
Doug Anderson37282b42011-03-04 11:54:18 -0800311 if self._repo_hooks_project:
312 root.appendChild(doc.createTextNode(''))
313 e = doc.createElement('repo-hooks')
314 e.setAttribute('in-project', self._repo_hooks_project.name)
315 e.setAttribute('enabled-list',
316 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
317 root.appendChild(e)
318
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800319 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
320
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700321 @property
David James8d201162013-10-11 17:03:19 -0700322 def paths(self):
323 self._Load()
324 return self._paths
325
326 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700327 def projects(self):
328 self._Load()
Anthony Kingd58bfe52014-05-05 23:30:49 +0100329 return list(self._paths.values())
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700330
331 @property
332 def remotes(self):
333 self._Load()
334 return self._remotes
335
336 @property
337 def default(self):
338 self._Load()
339 return self._default
340
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800341 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800342 def repo_hooks_project(self):
343 self._Load()
344 return self._repo_hooks_project
345
346 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700347 def notice(self):
348 self._Load()
349 return self._notice
350
351 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700352 def manifest_server(self):
353 self._Load()
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800354 return self._manifest_server
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700355
356 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800357 def IsMirror(self):
358 return self.manifestProject.config.GetBoolean('repo.mirror')
359
Julien Campergue335f5ef2013-10-16 11:02:35 +0200360 @property
361 def IsArchive(self):
362 return self.manifestProject.config.GetBoolean('repo.archive')
363
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700364 def _Unload(self):
365 self._loaded = False
366 self._projects = {}
David James8d201162013-10-11 17:03:19 -0700367 self._paths = {}
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700368 self._remotes = {}
369 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800370 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700371 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700372 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700373 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700374
375 def _Load(self):
376 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800377 m = self.manifestProject
378 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700379 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800380 b = b[len(R_HEADS):]
381 self.branch = b
382
Colin Cross23acdd32012-04-21 00:33:54 -0700383 nodes = []
Brian Harring475a47d2012-06-07 20:05:35 -0700384 nodes.append(self._ParseManifestXml(self.manifestFile,
385 self.manifestProject.worktree))
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700386
387 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
388 if os.path.exists(local):
David Pursehouse4eb285c2013-02-14 16:28:44 +0900389 if not self.localManifestWarning:
390 self.localManifestWarning = True
391 print('warning: %s is deprecated; put local manifests in `%s` instead'
392 % (LOCAL_MANIFEST_NAME, os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME)),
393 file=sys.stderr)
Brian Harring475a47d2012-06-07 20:05:35 -0700394 nodes.append(self._ParseManifestXml(local, self.repodir))
Colin Cross23acdd32012-04-21 00:33:54 -0700395
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900396 local_dir = os.path.abspath(os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME))
397 try:
David Pursehouse52f1e5d2012-11-14 04:53:24 +0900398 for local_file in sorted(os.listdir(local_dir)):
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900399 if local_file.endswith('.xml'):
David Pursehouse5f434ed2012-11-22 13:48:10 +0900400 local = os.path.join(local_dir, local_file)
401 nodes.append(self._ParseManifestXml(local, self.repodir))
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900402 except OSError:
403 pass
404
Joe Onorato26e24752013-01-11 12:35:53 -0800405 try:
406 self._ParseManifest(nodes)
407 except ManifestParseError as e:
408 # There was a problem parsing, unload ourselves in case they catch
409 # this error and try again later, we will show the correct error
410 self._Unload()
411 raise e
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700412
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800413 if self.IsMirror:
414 self._AddMetaProjectMirror(self.repoProject)
415 self._AddMetaProjectMirror(self.manifestProject)
416
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700417 self._loaded = True
418
Brian Harring475a47d2012-06-07 20:05:35 -0700419 def _ParseManifestXml(self, path, include_root):
David Pursehousef7fc8a92012-11-13 04:00:28 +0900420 try:
421 root = xml.dom.minidom.parse(path)
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900422 except (OSError, xml.parsers.expat.ExpatError) as e:
David Pursehousef7fc8a92012-11-13 04:00:28 +0900423 raise ManifestParseError("error parsing manifest %s: %s" % (path, e))
424
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700425 if not root or not root.childNodes:
Brian Harring26448742011-04-28 05:04:41 -0700426 raise ManifestParseError("no root node in %s" % (path,))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700427
Jooncheol Park34acdd22012-08-27 02:25:59 +0900428 for manifest in root.childNodes:
429 if manifest.nodeName == 'manifest':
430 break
431 else:
Brian Harring26448742011-04-28 05:04:41 -0700432 raise ManifestParseError("no <manifest> in %s" % (path,))
433
Colin Cross23acdd32012-04-21 00:33:54 -0700434 nodes = []
David Pursehouse4f7bdea2012-10-22 12:50:15 +0900435 for node in manifest.childNodes: # pylint:disable=W0631
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900436 # We only get here if manifest is initialised
David Pursehousec1b86a22012-11-14 11:36:51 +0900437 if node.nodeName == 'include':
438 name = self._reqatt(node, 'name')
439 fp = os.path.join(include_root, name)
440 if not os.path.isfile(fp):
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530441 raise ManifestParseError("include %s doesn't exist or isn't a file"
442 % (name,))
David Pursehousec1b86a22012-11-14 11:36:51 +0900443 try:
444 nodes.extend(self._ParseManifestXml(fp, include_root))
445 # should isolate this to the exact exception, but that's
446 # tricky. actual parsing implementation may vary.
447 except (KeyboardInterrupt, RuntimeError, SystemExit):
448 raise
449 except Exception as e:
450 raise ManifestParseError(
451 "failed parsing included manifest %s: %s", (name, e))
452 else:
453 nodes.append(node)
Colin Cross23acdd32012-04-21 00:33:54 -0700454 return nodes
Brian Harring26448742011-04-28 05:04:41 -0700455
Colin Cross23acdd32012-04-21 00:33:54 -0700456 def _ParseManifest(self, node_list):
457 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700458 if node.nodeName == 'remote':
459 remote = self._ParseRemote(node)
David Pursehouse717ece92012-11-13 08:49:16 +0900460 if remote:
461 if remote.name in self._remotes:
462 if remote != self._remotes[remote.name]:
463 raise ManifestParseError(
464 'remote %s already exists with different attributes' %
465 (remote.name))
466 else:
467 self._remotes[remote.name] = remote
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700468
Colin Cross23acdd32012-04-21 00:33:54 -0700469 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700470 if node.nodeName == 'default':
Julien Campergue74879922013-10-09 14:38:46 +0200471 new_default = self._ParseDefault(node)
472 if self._default is None:
473 self._default = new_default
474 elif new_default != self._default:
David Pursehouse37128b62013-10-15 10:48:40 +0900475 raise ManifestParseError('duplicate default in %s' %
476 (self.manifestFile))
Julien Campergue74879922013-10-09 14:38:46 +0200477
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700478 if self._default is None:
479 self._default = _Default()
480
Colin Cross23acdd32012-04-21 00:33:54 -0700481 for node in itertools.chain(*node_list):
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700482 if node.nodeName == 'notice':
483 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800484 raise ManifestParseError(
485 'duplicate notice in %s' %
486 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700487 self._notice = self._ParseNotice(node)
488
Colin Cross23acdd32012-04-21 00:33:54 -0700489 for node in itertools.chain(*node_list):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700490 if node.nodeName == 'manifest-server':
491 url = self._reqatt(node, 'url')
492 if self._manifest_server is not None:
David Pursehousec1b86a22012-11-14 11:36:51 +0900493 raise ManifestParseError(
494 'duplicate manifest-server in %s' %
495 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700496 self._manifest_server = url
497
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800498 def recursively_add_projects(project):
David James8d201162013-10-11 17:03:19 -0700499 projects = self._projects.setdefault(project.name, [])
500 if project.relpath is None:
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800501 raise ManifestParseError(
David James8d201162013-10-11 17:03:19 -0700502 'missing path for %s in %s' %
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800503 (project.name, self.manifestFile))
David James8d201162013-10-11 17:03:19 -0700504 if project.relpath in self._paths:
505 raise ManifestParseError(
506 'duplicate path %s in %s' %
507 (project.relpath, self.manifestFile))
508 self._paths[project.relpath] = project
509 projects.append(project)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800510 for subproject in project.subprojects:
511 recursively_add_projects(subproject)
512
Colin Cross23acdd32012-04-21 00:33:54 -0700513 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700514 if node.nodeName == 'project':
515 project = self._ParseProject(node)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800516 recursively_add_projects(project)
Josh Triplett884a3872014-06-12 14:57:29 -0700517 if node.nodeName == 'extend-project':
518 name = self._reqatt(node, 'name')
519
520 if name not in self._projects:
521 raise ManifestParseError('extend-project element specifies non-existent '
522 'project: %s' % name)
523
524 path = node.getAttribute('path')
525 groups = node.getAttribute('groups')
526 if groups:
527 groups = self._ParseGroups(groups)
528
529 for p in self._projects[name]:
530 if path and p.relpath != path:
531 continue
532 if groups:
533 p.groups.extend(groups)
Doug Anderson37282b42011-03-04 11:54:18 -0800534 if node.nodeName == 'repo-hooks':
535 # Get the name of the project and the (space-separated) list of enabled.
536 repo_hooks_project = self._reqatt(node, 'in-project')
537 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
538
539 # Only one project can be the hooks project
540 if self._repo_hooks_project is not None:
541 raise ManifestParseError(
542 'duplicate repo-hooks in %s' %
543 (self.manifestFile))
544
545 # Store a reference to the Project.
546 try:
David James8d201162013-10-11 17:03:19 -0700547 repo_hooks_projects = self._projects[repo_hooks_project]
Doug Anderson37282b42011-03-04 11:54:18 -0800548 except KeyError:
549 raise ManifestParseError(
550 'project %s not found for repo-hooks' %
551 (repo_hooks_project))
552
David James8d201162013-10-11 17:03:19 -0700553 if len(repo_hooks_projects) != 1:
554 raise ManifestParseError(
555 'internal error parsing repo-hooks in %s' %
556 (self.manifestFile))
557 self._repo_hooks_project = repo_hooks_projects[0]
558
Doug Anderson37282b42011-03-04 11:54:18 -0800559 # Store the enabled hooks in the Project object.
560 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
Colin Cross23acdd32012-04-21 00:33:54 -0700561 if node.nodeName == 'remove-project':
562 name = self._reqatt(node, 'name')
David Jamesb8433df2014-01-30 10:11:17 -0800563
564 if name not in self._projects:
David Pursehousef9107482012-11-16 19:12:32 +0900565 raise ManifestParseError('remove-project element specifies non-existent '
566 'project: %s' % name)
Colin Cross23acdd32012-04-21 00:33:54 -0700567
David Jamesb8433df2014-01-30 10:11:17 -0800568 for p in self._projects[name]:
569 del self._paths[p.relpath]
570 del self._projects[name]
571
Colin Cross23acdd32012-04-21 00:33:54 -0700572 # If the manifest removes the hooks project, treat it as if it deleted
573 # the repo-hooks element too.
574 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
575 self._repo_hooks_project = None
576
Doug Anderson37282b42011-03-04 11:54:18 -0800577
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800578 def _AddMetaProjectMirror(self, m):
579 name = None
580 m_url = m.GetRemote(m.remote.name).url
581 if m_url.endswith('/.git'):
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530582 raise ManifestParseError('refusing to mirror %s' % m_url)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800583
584 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700585 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800586 if not url.endswith('/'):
587 url += '/'
588 if m_url.startswith(url):
589 remote = self._default.remote
590 name = m_url[len(url):]
591
592 if name is None:
593 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700594 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Shawn O. Pearcef35b2d92012-08-02 11:46:22 -0700595 remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800596 name = m_url[s:]
597
598 if name.endswith('.git'):
599 name = name[:-4]
600
601 if name not in self._projects:
602 m.PreSync()
603 gitdir = os.path.join(self.topdir, '%s.git' % name)
604 project = Project(manifest = self,
605 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700606 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800607 gitdir = gitdir,
David James8d201162013-10-11 17:03:19 -0700608 objdir = gitdir,
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800609 worktree = None,
Kwanhong Leeccd218c2014-02-17 13:07:32 +0900610 relpath = name or None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700611 revisionExpr = m.revisionExpr,
612 revisionId = None)
David James8d201162013-10-11 17:03:19 -0700613 self._projects[project.name] = [project]
Kwanhong Leeccd218c2014-02-17 13:07:32 +0900614 self._paths[project.relpath] = project
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800615
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700616 def _ParseRemote(self, node):
617 """
618 reads a <remote> element from the manifest file
619 """
620 name = self._reqatt(node, 'name')
Yestin Sunb292b982012-07-02 07:32:50 -0700621 alias = node.getAttribute('alias')
622 if alias == '':
623 alias = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700624 fetch = self._reqatt(node, 'fetch')
625 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800626 if review == '':
627 review = None
Anthony King36ea2fb2014-05-06 11:54:01 +0100628 revision = node.getAttribute('revision')
629 if revision == '':
630 revision = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700631 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Anthony King36ea2fb2014-05-06 11:54:01 +0100632 return _XmlRemote(name, alias, fetch, manifestUrl, review, revision)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700633
634 def _ParseDefault(self, node):
635 """
636 reads a <default> element from the manifest file
637 """
638 d = _Default()
639 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700640 d.revisionExpr = node.getAttribute('revision')
641 if d.revisionExpr == '':
642 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700643
Bryan Jacobsf609f912013-05-06 13:36:24 -0400644 d.destBranchExpr = node.getAttribute('dest-branch') or None
645
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700646 sync_j = node.getAttribute('sync-j')
647 if sync_j == '' or sync_j is None:
648 d.sync_j = 1
649 else:
650 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700651
652 sync_c = node.getAttribute('sync-c')
653 if not sync_c:
654 d.sync_c = False
655 else:
656 d.sync_c = sync_c.lower() in ("yes", "true", "1")
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800657
658 sync_s = node.getAttribute('sync-s')
659 if not sync_s:
660 d.sync_s = False
661 else:
662 d.sync_s = sync_s.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700663 return d
664
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700665 def _ParseNotice(self, node):
666 """
667 reads a <notice> element from the manifest file
668
669 The <notice> element is distinct from other tags in the XML in that the
670 data is conveyed between the start and end tag (it's not an empty-element
671 tag).
672
673 The white space (carriage returns, indentation) for the notice element is
674 relevant and is parsed in a way that is based on how python docstrings work.
675 In fact, the code is remarkably similar to here:
676 http://www.python.org/dev/peps/pep-0257/
677 """
678 # Get the data out of the node...
679 notice = node.childNodes[0].data
680
681 # Figure out minimum indentation, skipping the first line (the same line
682 # as the <notice> tag)...
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530683 minIndent = sys.maxsize
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700684 lines = notice.splitlines()
685 for line in lines[1:]:
686 lstrippedLine = line.lstrip()
687 if lstrippedLine:
688 indent = len(line) - len(lstrippedLine)
689 minIndent = min(indent, minIndent)
690
691 # Strip leading / trailing blank lines and also indentation.
692 cleanLines = [lines[0].strip()]
693 for line in lines[1:]:
694 cleanLines.append(line[minIndent:].rstrip())
695
696 # Clear completely blank lines from front and back...
697 while cleanLines and not cleanLines[0]:
698 del cleanLines[0]
699 while cleanLines and not cleanLines[-1]:
700 del cleanLines[-1]
701
702 return '\n'.join(cleanLines)
703
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800704 def _JoinName(self, parent_name, name):
705 return os.path.join(parent_name, name)
706
707 def _UnjoinName(self, parent_name, name):
708 return os.path.relpath(name, parent_name)
709
710 def _ParseProject(self, node, parent = None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700711 """
712 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700713 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700714 name = self._reqatt(node, 'name')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800715 if parent:
716 name = self._JoinName(parent.name, name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700717
718 remote = self._get_remote(node)
719 if remote is None:
720 remote = self._default.remote
721 if remote is None:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530722 raise ManifestParseError("no remote for project %s within %s" %
723 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700724
Anthony King36ea2fb2014-05-06 11:54:01 +0100725 revisionExpr = node.getAttribute('revision') or remote.revision
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700726 if not revisionExpr:
727 revisionExpr = self._default.revisionExpr
728 if not revisionExpr:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530729 raise ManifestParseError("no revision for project %s within %s" %
730 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700731
732 path = node.getAttribute('path')
733 if not path:
734 path = name
735 if path.startswith('/'):
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530736 raise ManifestParseError("project %s path cannot be absolute in %s" %
737 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700738
Mike Pontillod3153822012-02-28 11:53:24 -0800739 rebase = node.getAttribute('rebase')
740 if not rebase:
741 rebase = True
742 else:
743 rebase = rebase.lower() in ("yes", "true", "1")
744
Anatol Pomazau79770d22012-04-20 14:41:59 -0700745 sync_c = node.getAttribute('sync-c')
746 if not sync_c:
747 sync_c = False
748 else:
749 sync_c = sync_c.lower() in ("yes", "true", "1")
750
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800751 sync_s = node.getAttribute('sync-s')
752 if not sync_s:
753 sync_s = self._default.sync_s
754 else:
755 sync_s = sync_s.lower() in ("yes", "true", "1")
756
David Pursehouseede7f122012-11-27 22:25:30 +0900757 clone_depth = node.getAttribute('clone-depth')
758 if clone_depth:
759 try:
760 clone_depth = int(clone_depth)
761 if clone_depth <= 0:
762 raise ValueError()
763 except ValueError:
764 raise ManifestParseError('invalid clone-depth %s in %s' %
765 (clone_depth, self.manifestFile))
766
Bryan Jacobsf609f912013-05-06 13:36:24 -0400767 dest_branch = node.getAttribute('dest-branch') or self._default.destBranchExpr
768
Brian Harring14a66742012-09-28 20:21:57 -0700769 upstream = node.getAttribute('upstream')
770
Conley Owens971de8e2012-04-16 10:36:08 -0700771 groups = ''
772 if node.hasAttribute('groups'):
773 groups = node.getAttribute('groups')
Josh Triplett884a3872014-06-12 14:57:29 -0700774 groups = self._ParseGroups(groups)
Brian Harring7da13142012-06-15 02:24:20 -0700775
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800776 if parent is None:
David James8d201162013-10-11 17:03:19 -0700777 relpath, worktree, gitdir, objdir = self.GetProjectPaths(name, path)
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700778 else:
David James8d201162013-10-11 17:03:19 -0700779 relpath, worktree, gitdir, objdir = \
780 self.GetSubprojectPaths(parent, name, path)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800781
782 default_groups = ['all', 'name:%s' % name, 'path:%s' % relpath]
783 groups.extend(set(default_groups).difference(groups))
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700784
Scott Fandb83b1b2013-02-28 09:34:14 +0800785 if self.IsMirror and node.hasAttribute('force-path'):
786 if node.getAttribute('force-path').lower() in ("yes", "true", "1"):
787 gitdir = os.path.join(self.topdir, '%s.git' % path)
788
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700789 project = Project(manifest = self,
790 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700791 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700792 gitdir = gitdir,
David James8d201162013-10-11 17:03:19 -0700793 objdir = objdir,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700794 worktree = worktree,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800795 relpath = relpath,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700796 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800797 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700798 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700799 groups = groups,
Brian Harring14a66742012-09-28 20:21:57 -0700800 sync_c = sync_c,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800801 sync_s = sync_s,
David Pursehouseede7f122012-11-27 22:25:30 +0900802 clone_depth = clone_depth,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800803 upstream = upstream,
Bryan Jacobsf609f912013-05-06 13:36:24 -0400804 parent = parent,
805 dest_branch = dest_branch)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700806
807 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700808 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700809 self._ParseCopyFile(project, n)
Jeff Hamiltone0df2322014-04-21 17:10:59 -0500810 if n.nodeName == 'linkfile':
811 self._ParseLinkFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500812 if n.nodeName == 'annotation':
813 self._ParseAnnotation(project, n)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800814 if n.nodeName == 'project':
815 project.subprojects.append(self._ParseProject(n, parent = project))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700816
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700817 return project
818
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800819 def GetProjectPaths(self, name, path):
820 relpath = path
821 if self.IsMirror:
822 worktree = None
823 gitdir = os.path.join(self.topdir, '%s.git' % name)
David James8d201162013-10-11 17:03:19 -0700824 objdir = gitdir
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800825 else:
826 worktree = os.path.join(self.topdir, path).replace('\\', '/')
827 gitdir = os.path.join(self.repodir, 'projects', '%s.git' % path)
David James8d201162013-10-11 17:03:19 -0700828 objdir = os.path.join(self.repodir, 'project-objects', '%s.git' % name)
829 return relpath, worktree, gitdir, objdir
830
831 def GetProjectsWithName(self, name):
832 return self._projects.get(name, [])
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800833
834 def GetSubprojectName(self, parent, submodule_path):
835 return os.path.join(parent.name, submodule_path)
836
837 def _JoinRelpath(self, parent_relpath, relpath):
838 return os.path.join(parent_relpath, relpath)
839
840 def _UnjoinRelpath(self, parent_relpath, relpath):
841 return os.path.relpath(relpath, parent_relpath)
842
David James8d201162013-10-11 17:03:19 -0700843 def GetSubprojectPaths(self, parent, name, path):
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800844 relpath = self._JoinRelpath(parent.relpath, path)
845 gitdir = os.path.join(parent.gitdir, 'subprojects', '%s.git' % path)
David James8d201162013-10-11 17:03:19 -0700846 objdir = os.path.join(parent.gitdir, 'subproject-objects', '%s.git' % name)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800847 if self.IsMirror:
848 worktree = None
849 else:
850 worktree = os.path.join(parent.worktree, path).replace('\\', '/')
David James8d201162013-10-11 17:03:19 -0700851 return relpath, worktree, gitdir, objdir
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800852
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700853 def _ParseCopyFile(self, project, node):
854 src = self._reqatt(node, 'src')
855 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800856 if not self.IsMirror:
857 # src is project relative;
858 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800859 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700860
Jeff Hamiltone0df2322014-04-21 17:10:59 -0500861 def _ParseLinkFile(self, project, node):
862 src = self._reqatt(node, 'src')
863 dest = self._reqatt(node, 'dest')
864 if not self.IsMirror:
865 # src is project relative;
866 # dest is relative to the top of the tree
867 project.AddLinkFile(src, dest, os.path.join(self.topdir, dest))
868
James W. Mills24c13082012-04-12 15:04:13 -0500869 def _ParseAnnotation(self, project, node):
870 name = self._reqatt(node, 'name')
871 value = self._reqatt(node, 'value')
872 try:
873 keep = self._reqatt(node, 'keep').lower()
874 except ManifestParseError:
875 keep = "true"
876 if keep != "true" and keep != "false":
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530877 raise ManifestParseError('optional "keep" attribute must be '
878 '"true" or "false"')
James W. Mills24c13082012-04-12 15:04:13 -0500879 project.AddAnnotation(name, value, keep)
880
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700881 def _get_remote(self, node):
882 name = node.getAttribute('remote')
883 if not name:
884 return None
885
886 v = self._remotes.get(name)
887 if not v:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530888 raise ManifestParseError("remote %s not defined in %s" %
889 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700890 return v
891
892 def _reqatt(self, node, attname):
893 """
894 reads a required attribute from the node.
895 """
896 v = node.getAttribute(attname)
897 if not v:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530898 raise ManifestParseError("no %s in <%s> within %s" %
899 (attname, node.nodeName, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700900 return v
Julien Camperguedd654222014-01-09 16:21:37 +0100901
902 def projectsDiff(self, manifest):
903 """return the projects differences between two manifests.
904
905 The diff will be from self to given manifest.
906
907 """
908 fromProjects = self.paths
909 toProjects = manifest.paths
910
Anthony King7446c592014-05-06 09:19:39 +0100911 fromKeys = sorted(fromProjects.keys())
912 toKeys = sorted(toProjects.keys())
Julien Camperguedd654222014-01-09 16:21:37 +0100913
914 diff = {'added': [], 'removed': [], 'changed': [], 'unreachable': []}
915
916 for proj in fromKeys:
917 if not proj in toKeys:
918 diff['removed'].append(fromProjects[proj])
919 else:
920 fromProj = fromProjects[proj]
921 toProj = toProjects[proj]
922 try:
923 fromRevId = fromProj.GetCommitRevisionId()
924 toRevId = toProj.GetCommitRevisionId()
925 except ManifestInvalidRevisionError:
926 diff['unreachable'].append((fromProj, toProj))
927 else:
928 if fromRevId != toRevId:
929 diff['changed'].append((fromProj, toProj))
930 toKeys.remove(proj)
931
932 for proj in toKeys:
933 diff['added'].append(toProjects[proj])
934
935 return diff