blob: 6d70c86b33bce957ac1b452f40280702cb8409a7 [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
Brian Harring14a66742012-09-28 20:21:57 -0700171 def Save(self, fd, peg_rev=False, peg_rev_upstream=True):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800172 """Write the current manifest out to the given file descriptor.
173 """
Colin Cross5acde752012-03-28 20:15:45 -0700174 mp = self.manifestProject
175
176 groups = mp.config.GetString('manifest.groups')
Matt Gumbel0c635bb2012-12-21 10:14:53 -0800177 if groups:
178 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Colin Cross5acde752012-03-28 20:15:45 -0700179
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800180 doc = xml.dom.minidom.Document()
181 root = doc.createElement('manifest')
182 doc.appendChild(root)
183
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700184 # Save out the notice. There's a little bit of work here to give it the
185 # right whitespace, which assumes that the notice is automatically indented
186 # by 4 by minidom.
187 if self.notice:
188 notice_element = root.appendChild(doc.createElement('notice'))
189 notice_lines = self.notice.splitlines()
190 indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:]
191 notice_element.appendChild(doc.createTextNode(indented_notice))
192
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800193 d = self.default
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800194
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530195 for r in sorted(self.remotes):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800196 self._RemoteToXml(self.remotes[r], doc, root)
197 if self.remotes:
198 root.appendChild(doc.createTextNode(''))
199
200 have_default = False
201 e = doc.createElement('default')
202 if d.remote:
203 have_default = True
204 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700205 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800206 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700207 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700208 if d.sync_j > 1:
209 have_default = True
210 e.setAttribute('sync-j', '%d' % d.sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700211 if d.sync_c:
212 have_default = True
213 e.setAttribute('sync-c', 'true')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800214 if d.sync_s:
215 have_default = True
216 e.setAttribute('sync-s', 'true')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800217 if have_default:
218 root.appendChild(e)
219 root.appendChild(doc.createTextNode(''))
220
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700221 if self._manifest_server:
222 e = doc.createElement('manifest-server')
223 e.setAttribute('url', self._manifest_server)
224 root.appendChild(e)
225 root.appendChild(doc.createTextNode(''))
226
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800227 def output_projects(parent, parent_node, projects):
David James8d201162013-10-11 17:03:19 -0700228 for project_name in projects:
229 for project in self._projects[project_name]:
230 output_project(parent, parent_node, project)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800231
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800232 def output_project(parent, parent_node, p):
Colin Cross5acde752012-03-28 20:15:45 -0700233 if not p.MatchesGroups(groups):
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800234 return
235
236 name = p.name
237 relpath = p.relpath
238 if parent:
239 name = self._UnjoinName(parent.name, name)
240 relpath = self._UnjoinRelpath(parent.relpath, relpath)
Colin Cross5acde752012-03-28 20:15:45 -0700241
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800242 e = doc.createElement('project')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800243 parent_node.appendChild(e)
244 e.setAttribute('name', name)
245 if relpath != name:
246 e.setAttribute('path', relpath)
Conley Owensa17d7af2013-10-16 14:38:09 -0700247 remoteName = None
248 if d.remote:
Conley Owensce201a52013-10-16 14:42:42 -0700249 remoteName = d.remote.remoteAlias or d.remote.name
Conley Owens1e7ab2a2013-10-08 17:26:57 -0700250 if not d.remote or p.remote.name != remoteName:
Anthony King36ea2fb2014-05-06 11:54:01 +0100251 remoteName = p.remote.name
252 e.setAttribute('remote', remoteName)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800253 if peg_rev:
254 if self.IsMirror:
Brian Harring14a66742012-09-28 20:21:57 -0700255 value = p.bare_git.rev_parse(p.revisionExpr + '^0')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800256 else:
Brian Harring14a66742012-09-28 20:21:57 -0700257 value = p.work_git.rev_parse(HEAD + '^0')
258 e.setAttribute('revision', value)
259 if peg_rev_upstream and value != p.revisionExpr:
260 # Only save the origin if the origin is not a sha1, and the default
261 # isn't our value, and the if the default doesn't already have that
262 # covered.
263 e.setAttribute('upstream', p.revisionExpr)
Anthony King36ea2fb2014-05-06 11:54:01 +0100264 else:
265 revision = self.remotes[remoteName].revision or d.revisionExpr
266 if not revision or revision != p.revisionExpr:
267 e.setAttribute('revision', p.revisionExpr)
Mani Chandel7a91d512014-07-24 16:27:08 +0530268 if p.upstream and p.upstream != p.revisionExpr:
269 e.setAttribute('upstream', p.upstream)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800270
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800271 for c in p.copyfiles:
272 ce = doc.createElement('copyfile')
273 ce.setAttribute('src', c.src)
274 ce.setAttribute('dest', c.dest)
275 e.appendChild(ce)
276
Jeff Hamiltone0df2322014-04-21 17:10:59 -0500277 for l in p.linkfiles:
278 le = doc.createElement('linkfile')
279 le.setAttribute('src', l.src)
280 le.setAttribute('dest', l.dest)
281 e.appendChild(le)
282
Conley Owensbb1b5f52012-08-13 13:11:18 -0700283 default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath]
Dmitry Fink17f85ea2012-08-06 14:52:29 -0700284 egroups = [g for g in p.groups if g not in default_groups]
Conley Owens971de8e2012-04-16 10:36:08 -0700285 if egroups:
286 e.setAttribute('groups', ','.join(egroups))
Colin Cross5acde752012-03-28 20:15:45 -0700287
James W. Mills24c13082012-04-12 15:04:13 -0500288 for a in p.annotations:
289 if a.keep == "true":
290 ae = doc.createElement('annotation')
291 ae.setAttribute('name', a.name)
292 ae.setAttribute('value', a.value)
293 e.appendChild(ae)
294
Anatol Pomazau79770d22012-04-20 14:41:59 -0700295 if p.sync_c:
296 e.setAttribute('sync-c', 'true')
297
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800298 if p.sync_s:
299 e.setAttribute('sync-s', 'true')
300
301 if p.subprojects:
David James8d201162013-10-11 17:03:19 -0700302 subprojects = set(subp.name for subp in p.subprojects)
303 output_projects(p, e, list(sorted(subprojects)))
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800304
David James8d201162013-10-11 17:03:19 -0700305 projects = set(p.name for p in self._paths.values() if not p.parent)
306 output_projects(None, root, list(sorted(projects)))
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800307
Doug Anderson37282b42011-03-04 11:54:18 -0800308 if self._repo_hooks_project:
309 root.appendChild(doc.createTextNode(''))
310 e = doc.createElement('repo-hooks')
311 e.setAttribute('in-project', self._repo_hooks_project.name)
312 e.setAttribute('enabled-list',
313 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
314 root.appendChild(e)
315
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800316 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
317
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700318 @property
David James8d201162013-10-11 17:03:19 -0700319 def paths(self):
320 self._Load()
321 return self._paths
322
323 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700324 def projects(self):
325 self._Load()
Anthony Kingd58bfe52014-05-05 23:30:49 +0100326 return list(self._paths.values())
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700327
328 @property
329 def remotes(self):
330 self._Load()
331 return self._remotes
332
333 @property
334 def default(self):
335 self._Load()
336 return self._default
337
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800338 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800339 def repo_hooks_project(self):
340 self._Load()
341 return self._repo_hooks_project
342
343 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700344 def notice(self):
345 self._Load()
346 return self._notice
347
348 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700349 def manifest_server(self):
350 self._Load()
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800351 return self._manifest_server
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700352
353 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800354 def IsMirror(self):
355 return self.manifestProject.config.GetBoolean('repo.mirror')
356
Julien Campergue335f5ef2013-10-16 11:02:35 +0200357 @property
358 def IsArchive(self):
359 return self.manifestProject.config.GetBoolean('repo.archive')
360
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700361 def _Unload(self):
362 self._loaded = False
363 self._projects = {}
David James8d201162013-10-11 17:03:19 -0700364 self._paths = {}
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700365 self._remotes = {}
366 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800367 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700368 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700369 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700370 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700371
372 def _Load(self):
373 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800374 m = self.manifestProject
375 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700376 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800377 b = b[len(R_HEADS):]
378 self.branch = b
379
Colin Cross23acdd32012-04-21 00:33:54 -0700380 nodes = []
Brian Harring475a47d2012-06-07 20:05:35 -0700381 nodes.append(self._ParseManifestXml(self.manifestFile,
382 self.manifestProject.worktree))
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700383
384 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
385 if os.path.exists(local):
David Pursehouse4eb285c2013-02-14 16:28:44 +0900386 if not self.localManifestWarning:
387 self.localManifestWarning = True
388 print('warning: %s is deprecated; put local manifests in `%s` instead'
389 % (LOCAL_MANIFEST_NAME, os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME)),
390 file=sys.stderr)
Brian Harring475a47d2012-06-07 20:05:35 -0700391 nodes.append(self._ParseManifestXml(local, self.repodir))
Colin Cross23acdd32012-04-21 00:33:54 -0700392
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900393 local_dir = os.path.abspath(os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME))
394 try:
David Pursehouse52f1e5d2012-11-14 04:53:24 +0900395 for local_file in sorted(os.listdir(local_dir)):
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900396 if local_file.endswith('.xml'):
David Pursehouse5f434ed2012-11-22 13:48:10 +0900397 local = os.path.join(local_dir, local_file)
398 nodes.append(self._ParseManifestXml(local, self.repodir))
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900399 except OSError:
400 pass
401
Joe Onorato26e24752013-01-11 12:35:53 -0800402 try:
403 self._ParseManifest(nodes)
404 except ManifestParseError as e:
405 # There was a problem parsing, unload ourselves in case they catch
406 # this error and try again later, we will show the correct error
407 self._Unload()
408 raise e
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700409
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800410 if self.IsMirror:
411 self._AddMetaProjectMirror(self.repoProject)
412 self._AddMetaProjectMirror(self.manifestProject)
413
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700414 self._loaded = True
415
Brian Harring475a47d2012-06-07 20:05:35 -0700416 def _ParseManifestXml(self, path, include_root):
David Pursehousef7fc8a92012-11-13 04:00:28 +0900417 try:
418 root = xml.dom.minidom.parse(path)
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900419 except (OSError, xml.parsers.expat.ExpatError) as e:
David Pursehousef7fc8a92012-11-13 04:00:28 +0900420 raise ManifestParseError("error parsing manifest %s: %s" % (path, e))
421
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700422 if not root or not root.childNodes:
Brian Harring26448742011-04-28 05:04:41 -0700423 raise ManifestParseError("no root node in %s" % (path,))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700424
Jooncheol Park34acdd22012-08-27 02:25:59 +0900425 for manifest in root.childNodes:
426 if manifest.nodeName == 'manifest':
427 break
428 else:
Brian Harring26448742011-04-28 05:04:41 -0700429 raise ManifestParseError("no <manifest> in %s" % (path,))
430
Colin Cross23acdd32012-04-21 00:33:54 -0700431 nodes = []
David Pursehouse4f7bdea2012-10-22 12:50:15 +0900432 for node in manifest.childNodes: # pylint:disable=W0631
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900433 # We only get here if manifest is initialised
David Pursehousec1b86a22012-11-14 11:36:51 +0900434 if node.nodeName == 'include':
435 name = self._reqatt(node, 'name')
436 fp = os.path.join(include_root, name)
437 if not os.path.isfile(fp):
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530438 raise ManifestParseError("include %s doesn't exist or isn't a file"
439 % (name,))
David Pursehousec1b86a22012-11-14 11:36:51 +0900440 try:
441 nodes.extend(self._ParseManifestXml(fp, include_root))
442 # should isolate this to the exact exception, but that's
443 # tricky. actual parsing implementation may vary.
444 except (KeyboardInterrupt, RuntimeError, SystemExit):
445 raise
446 except Exception as e:
447 raise ManifestParseError(
448 "failed parsing included manifest %s: %s", (name, e))
449 else:
450 nodes.append(node)
Colin Cross23acdd32012-04-21 00:33:54 -0700451 return nodes
Brian Harring26448742011-04-28 05:04:41 -0700452
Colin Cross23acdd32012-04-21 00:33:54 -0700453 def _ParseManifest(self, node_list):
454 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700455 if node.nodeName == 'remote':
456 remote = self._ParseRemote(node)
David Pursehouse717ece92012-11-13 08:49:16 +0900457 if remote:
458 if remote.name in self._remotes:
459 if remote != self._remotes[remote.name]:
460 raise ManifestParseError(
461 'remote %s already exists with different attributes' %
462 (remote.name))
463 else:
464 self._remotes[remote.name] = remote
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700465
Colin Cross23acdd32012-04-21 00:33:54 -0700466 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700467 if node.nodeName == 'default':
Julien Campergue74879922013-10-09 14:38:46 +0200468 new_default = self._ParseDefault(node)
469 if self._default is None:
470 self._default = new_default
471 elif new_default != self._default:
David Pursehouse37128b62013-10-15 10:48:40 +0900472 raise ManifestParseError('duplicate default in %s' %
473 (self.manifestFile))
Julien Campergue74879922013-10-09 14:38:46 +0200474
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700475 if self._default is None:
476 self._default = _Default()
477
Colin Cross23acdd32012-04-21 00:33:54 -0700478 for node in itertools.chain(*node_list):
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700479 if node.nodeName == 'notice':
480 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800481 raise ManifestParseError(
482 'duplicate notice in %s' %
483 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700484 self._notice = self._ParseNotice(node)
485
Colin Cross23acdd32012-04-21 00:33:54 -0700486 for node in itertools.chain(*node_list):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700487 if node.nodeName == 'manifest-server':
488 url = self._reqatt(node, 'url')
489 if self._manifest_server is not None:
David Pursehousec1b86a22012-11-14 11:36:51 +0900490 raise ManifestParseError(
491 'duplicate manifest-server in %s' %
492 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700493 self._manifest_server = url
494
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800495 def recursively_add_projects(project):
David James8d201162013-10-11 17:03:19 -0700496 projects = self._projects.setdefault(project.name, [])
497 if project.relpath is None:
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800498 raise ManifestParseError(
David James8d201162013-10-11 17:03:19 -0700499 'missing path for %s in %s' %
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800500 (project.name, self.manifestFile))
David James8d201162013-10-11 17:03:19 -0700501 if project.relpath in self._paths:
502 raise ManifestParseError(
503 'duplicate path %s in %s' %
504 (project.relpath, self.manifestFile))
505 self._paths[project.relpath] = project
506 projects.append(project)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800507 for subproject in project.subprojects:
508 recursively_add_projects(subproject)
509
Colin Cross23acdd32012-04-21 00:33:54 -0700510 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700511 if node.nodeName == 'project':
512 project = self._ParseProject(node)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800513 recursively_add_projects(project)
Doug Anderson37282b42011-03-04 11:54:18 -0800514 if node.nodeName == 'repo-hooks':
515 # Get the name of the project and the (space-separated) list of enabled.
516 repo_hooks_project = self._reqatt(node, 'in-project')
517 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
518
519 # Only one project can be the hooks project
520 if self._repo_hooks_project is not None:
521 raise ManifestParseError(
522 'duplicate repo-hooks in %s' %
523 (self.manifestFile))
524
525 # Store a reference to the Project.
526 try:
David James8d201162013-10-11 17:03:19 -0700527 repo_hooks_projects = self._projects[repo_hooks_project]
Doug Anderson37282b42011-03-04 11:54:18 -0800528 except KeyError:
529 raise ManifestParseError(
530 'project %s not found for repo-hooks' %
531 (repo_hooks_project))
532
David James8d201162013-10-11 17:03:19 -0700533 if len(repo_hooks_projects) != 1:
534 raise ManifestParseError(
535 'internal error parsing repo-hooks in %s' %
536 (self.manifestFile))
537 self._repo_hooks_project = repo_hooks_projects[0]
538
Doug Anderson37282b42011-03-04 11:54:18 -0800539 # Store the enabled hooks in the Project object.
540 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
Colin Cross23acdd32012-04-21 00:33:54 -0700541 if node.nodeName == 'remove-project':
542 name = self._reqatt(node, 'name')
David Jamesb8433df2014-01-30 10:11:17 -0800543
544 if name not in self._projects:
David Pursehousef9107482012-11-16 19:12:32 +0900545 raise ManifestParseError('remove-project element specifies non-existent '
546 'project: %s' % name)
Colin Cross23acdd32012-04-21 00:33:54 -0700547
David Jamesb8433df2014-01-30 10:11:17 -0800548 for p in self._projects[name]:
549 del self._paths[p.relpath]
550 del self._projects[name]
551
Colin Cross23acdd32012-04-21 00:33:54 -0700552 # If the manifest removes the hooks project, treat it as if it deleted
553 # the repo-hooks element too.
554 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
555 self._repo_hooks_project = None
556
Doug Anderson37282b42011-03-04 11:54:18 -0800557
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800558 def _AddMetaProjectMirror(self, m):
559 name = None
560 m_url = m.GetRemote(m.remote.name).url
561 if m_url.endswith('/.git'):
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530562 raise ManifestParseError('refusing to mirror %s' % m_url)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800563
564 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700565 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800566 if not url.endswith('/'):
567 url += '/'
568 if m_url.startswith(url):
569 remote = self._default.remote
570 name = m_url[len(url):]
571
572 if name is None:
573 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700574 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Shawn O. Pearcef35b2d92012-08-02 11:46:22 -0700575 remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800576 name = m_url[s:]
577
578 if name.endswith('.git'):
579 name = name[:-4]
580
581 if name not in self._projects:
582 m.PreSync()
583 gitdir = os.path.join(self.topdir, '%s.git' % name)
584 project = Project(manifest = self,
585 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700586 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800587 gitdir = gitdir,
David James8d201162013-10-11 17:03:19 -0700588 objdir = gitdir,
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800589 worktree = None,
Kwanhong Leeccd218c2014-02-17 13:07:32 +0900590 relpath = name or None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700591 revisionExpr = m.revisionExpr,
592 revisionId = None)
David James8d201162013-10-11 17:03:19 -0700593 self._projects[project.name] = [project]
Kwanhong Leeccd218c2014-02-17 13:07:32 +0900594 self._paths[project.relpath] = project
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800595
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700596 def _ParseRemote(self, node):
597 """
598 reads a <remote> element from the manifest file
599 """
600 name = self._reqatt(node, 'name')
Yestin Sunb292b982012-07-02 07:32:50 -0700601 alias = node.getAttribute('alias')
602 if alias == '':
603 alias = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700604 fetch = self._reqatt(node, 'fetch')
605 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800606 if review == '':
607 review = None
Anthony King36ea2fb2014-05-06 11:54:01 +0100608 revision = node.getAttribute('revision')
609 if revision == '':
610 revision = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700611 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Anthony King36ea2fb2014-05-06 11:54:01 +0100612 return _XmlRemote(name, alias, fetch, manifestUrl, review, revision)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700613
614 def _ParseDefault(self, node):
615 """
616 reads a <default> element from the manifest file
617 """
618 d = _Default()
619 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700620 d.revisionExpr = node.getAttribute('revision')
621 if d.revisionExpr == '':
622 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700623
Bryan Jacobsf609f912013-05-06 13:36:24 -0400624 d.destBranchExpr = node.getAttribute('dest-branch') or None
625
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700626 sync_j = node.getAttribute('sync-j')
627 if sync_j == '' or sync_j is None:
628 d.sync_j = 1
629 else:
630 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700631
632 sync_c = node.getAttribute('sync-c')
633 if not sync_c:
634 d.sync_c = False
635 else:
636 d.sync_c = sync_c.lower() in ("yes", "true", "1")
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800637
638 sync_s = node.getAttribute('sync-s')
639 if not sync_s:
640 d.sync_s = False
641 else:
642 d.sync_s = sync_s.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700643 return d
644
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700645 def _ParseNotice(self, node):
646 """
647 reads a <notice> element from the manifest file
648
649 The <notice> element is distinct from other tags in the XML in that the
650 data is conveyed between the start and end tag (it's not an empty-element
651 tag).
652
653 The white space (carriage returns, indentation) for the notice element is
654 relevant and is parsed in a way that is based on how python docstrings work.
655 In fact, the code is remarkably similar to here:
656 http://www.python.org/dev/peps/pep-0257/
657 """
658 # Get the data out of the node...
659 notice = node.childNodes[0].data
660
661 # Figure out minimum indentation, skipping the first line (the same line
662 # as the <notice> tag)...
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530663 minIndent = sys.maxsize
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700664 lines = notice.splitlines()
665 for line in lines[1:]:
666 lstrippedLine = line.lstrip()
667 if lstrippedLine:
668 indent = len(line) - len(lstrippedLine)
669 minIndent = min(indent, minIndent)
670
671 # Strip leading / trailing blank lines and also indentation.
672 cleanLines = [lines[0].strip()]
673 for line in lines[1:]:
674 cleanLines.append(line[minIndent:].rstrip())
675
676 # Clear completely blank lines from front and back...
677 while cleanLines and not cleanLines[0]:
678 del cleanLines[0]
679 while cleanLines and not cleanLines[-1]:
680 del cleanLines[-1]
681
682 return '\n'.join(cleanLines)
683
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800684 def _JoinName(self, parent_name, name):
685 return os.path.join(parent_name, name)
686
687 def _UnjoinName(self, parent_name, name):
688 return os.path.relpath(name, parent_name)
689
690 def _ParseProject(self, node, parent = None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700691 """
692 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700693 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700694 name = self._reqatt(node, 'name')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800695 if parent:
696 name = self._JoinName(parent.name, name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700697
698 remote = self._get_remote(node)
699 if remote is None:
700 remote = self._default.remote
701 if remote is None:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530702 raise ManifestParseError("no remote for project %s within %s" %
703 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700704
Anthony King36ea2fb2014-05-06 11:54:01 +0100705 revisionExpr = node.getAttribute('revision') or remote.revision
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700706 if not revisionExpr:
707 revisionExpr = self._default.revisionExpr
708 if not revisionExpr:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530709 raise ManifestParseError("no revision for project %s within %s" %
710 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700711
712 path = node.getAttribute('path')
713 if not path:
714 path = name
715 if path.startswith('/'):
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530716 raise ManifestParseError("project %s path cannot be absolute in %s" %
717 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700718
Mike Pontillod3153822012-02-28 11:53:24 -0800719 rebase = node.getAttribute('rebase')
720 if not rebase:
721 rebase = True
722 else:
723 rebase = rebase.lower() in ("yes", "true", "1")
724
Anatol Pomazau79770d22012-04-20 14:41:59 -0700725 sync_c = node.getAttribute('sync-c')
726 if not sync_c:
727 sync_c = False
728 else:
729 sync_c = sync_c.lower() in ("yes", "true", "1")
730
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800731 sync_s = node.getAttribute('sync-s')
732 if not sync_s:
733 sync_s = self._default.sync_s
734 else:
735 sync_s = sync_s.lower() in ("yes", "true", "1")
736
David Pursehouseede7f122012-11-27 22:25:30 +0900737 clone_depth = node.getAttribute('clone-depth')
738 if clone_depth:
739 try:
740 clone_depth = int(clone_depth)
741 if clone_depth <= 0:
742 raise ValueError()
743 except ValueError:
744 raise ManifestParseError('invalid clone-depth %s in %s' %
745 (clone_depth, self.manifestFile))
746
Bryan Jacobsf609f912013-05-06 13:36:24 -0400747 dest_branch = node.getAttribute('dest-branch') or self._default.destBranchExpr
748
Brian Harring14a66742012-09-28 20:21:57 -0700749 upstream = node.getAttribute('upstream')
750
Conley Owens971de8e2012-04-16 10:36:08 -0700751 groups = ''
752 if node.hasAttribute('groups'):
753 groups = node.getAttribute('groups')
David Pursehouse1d947b32012-10-25 12:23:11 +0900754 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Brian Harring7da13142012-06-15 02:24:20 -0700755
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800756 if parent is None:
David James8d201162013-10-11 17:03:19 -0700757 relpath, worktree, gitdir, objdir = self.GetProjectPaths(name, path)
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700758 else:
David James8d201162013-10-11 17:03:19 -0700759 relpath, worktree, gitdir, objdir = \
760 self.GetSubprojectPaths(parent, name, path)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800761
762 default_groups = ['all', 'name:%s' % name, 'path:%s' % relpath]
763 groups.extend(set(default_groups).difference(groups))
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700764
Scott Fandb83b1b2013-02-28 09:34:14 +0800765 if self.IsMirror and node.hasAttribute('force-path'):
766 if node.getAttribute('force-path').lower() in ("yes", "true", "1"):
767 gitdir = os.path.join(self.topdir, '%s.git' % path)
768
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700769 project = Project(manifest = self,
770 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700771 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700772 gitdir = gitdir,
David James8d201162013-10-11 17:03:19 -0700773 objdir = objdir,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700774 worktree = worktree,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800775 relpath = relpath,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700776 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800777 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700778 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700779 groups = groups,
Brian Harring14a66742012-09-28 20:21:57 -0700780 sync_c = sync_c,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800781 sync_s = sync_s,
David Pursehouseede7f122012-11-27 22:25:30 +0900782 clone_depth = clone_depth,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800783 upstream = upstream,
Bryan Jacobsf609f912013-05-06 13:36:24 -0400784 parent = parent,
785 dest_branch = dest_branch)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700786
787 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700788 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700789 self._ParseCopyFile(project, n)
Jeff Hamiltone0df2322014-04-21 17:10:59 -0500790 if n.nodeName == 'linkfile':
791 self._ParseLinkFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500792 if n.nodeName == 'annotation':
793 self._ParseAnnotation(project, n)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800794 if n.nodeName == 'project':
795 project.subprojects.append(self._ParseProject(n, parent = project))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700796
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700797 return project
798
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800799 def GetProjectPaths(self, name, path):
800 relpath = path
801 if self.IsMirror:
802 worktree = None
803 gitdir = os.path.join(self.topdir, '%s.git' % name)
David James8d201162013-10-11 17:03:19 -0700804 objdir = gitdir
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800805 else:
806 worktree = os.path.join(self.topdir, path).replace('\\', '/')
807 gitdir = os.path.join(self.repodir, 'projects', '%s.git' % path)
David James8d201162013-10-11 17:03:19 -0700808 objdir = os.path.join(self.repodir, 'project-objects', '%s.git' % name)
809 return relpath, worktree, gitdir, objdir
810
811 def GetProjectsWithName(self, name):
812 return self._projects.get(name, [])
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800813
814 def GetSubprojectName(self, parent, submodule_path):
815 return os.path.join(parent.name, submodule_path)
816
817 def _JoinRelpath(self, parent_relpath, relpath):
818 return os.path.join(parent_relpath, relpath)
819
820 def _UnjoinRelpath(self, parent_relpath, relpath):
821 return os.path.relpath(relpath, parent_relpath)
822
David James8d201162013-10-11 17:03:19 -0700823 def GetSubprojectPaths(self, parent, name, path):
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800824 relpath = self._JoinRelpath(parent.relpath, path)
825 gitdir = os.path.join(parent.gitdir, 'subprojects', '%s.git' % path)
David James8d201162013-10-11 17:03:19 -0700826 objdir = os.path.join(parent.gitdir, 'subproject-objects', '%s.git' % name)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800827 if self.IsMirror:
828 worktree = None
829 else:
830 worktree = os.path.join(parent.worktree, path).replace('\\', '/')
David James8d201162013-10-11 17:03:19 -0700831 return relpath, worktree, gitdir, objdir
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800832
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700833 def _ParseCopyFile(self, project, node):
834 src = self._reqatt(node, 'src')
835 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800836 if not self.IsMirror:
837 # src is project relative;
838 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800839 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700840
Jeff Hamiltone0df2322014-04-21 17:10:59 -0500841 def _ParseLinkFile(self, project, node):
842 src = self._reqatt(node, 'src')
843 dest = self._reqatt(node, 'dest')
844 if not self.IsMirror:
845 # src is project relative;
846 # dest is relative to the top of the tree
847 project.AddLinkFile(src, dest, os.path.join(self.topdir, dest))
848
James W. Mills24c13082012-04-12 15:04:13 -0500849 def _ParseAnnotation(self, project, node):
850 name = self._reqatt(node, 'name')
851 value = self._reqatt(node, 'value')
852 try:
853 keep = self._reqatt(node, 'keep').lower()
854 except ManifestParseError:
855 keep = "true"
856 if keep != "true" and keep != "false":
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530857 raise ManifestParseError('optional "keep" attribute must be '
858 '"true" or "false"')
James W. Mills24c13082012-04-12 15:04:13 -0500859 project.AddAnnotation(name, value, keep)
860
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700861 def _get_remote(self, node):
862 name = node.getAttribute('remote')
863 if not name:
864 return None
865
866 v = self._remotes.get(name)
867 if not v:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530868 raise ManifestParseError("remote %s not defined in %s" %
869 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700870 return v
871
872 def _reqatt(self, node, attname):
873 """
874 reads a required attribute from the node.
875 """
876 v = node.getAttribute(attname)
877 if not v:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530878 raise ManifestParseError("no %s in <%s> within %s" %
879 (attname, node.nodeName, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700880 return v
Julien Camperguedd654222014-01-09 16:21:37 +0100881
882 def projectsDiff(self, manifest):
883 """return the projects differences between two manifests.
884
885 The diff will be from self to given manifest.
886
887 """
888 fromProjects = self.paths
889 toProjects = manifest.paths
890
Anthony King7446c592014-05-06 09:19:39 +0100891 fromKeys = sorted(fromProjects.keys())
892 toKeys = sorted(toProjects.keys())
Julien Camperguedd654222014-01-09 16:21:37 +0100893
894 diff = {'added': [], 'removed': [], 'changed': [], 'unreachable': []}
895
896 for proj in fromKeys:
897 if not proj in toKeys:
898 diff['removed'].append(fromProjects[proj])
899 else:
900 fromProj = fromProjects[proj]
901 toProj = toProjects[proj]
902 try:
903 fromRevId = fromProj.GetCommitRevisionId()
904 toRevId = toProj.GetCommitRevisionId()
905 except ManifestInvalidRevisionError:
906 diff['unreachable'].append((fromProj, toProj))
907 else:
908 if fromRevId != toRevId:
909 diff['changed'].append((fromProj, toProj))
910 toKeys.remove(proj)
911
912 for proj in toKeys:
913 diff['added'].append(toProjects[proj])
914
915 return diff