blob: bdbb1d4029c8a6c5802f579af3939f7062c4e47b [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
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070035from error import ManifestParseError
36
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
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070054class _XmlRemote(object):
55 def __init__(self,
56 name,
Yestin Sunb292b982012-07-02 07:32:50 -070057 alias=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070058 fetch=None,
Conley Owensdb728cd2011-09-26 16:34:01 -070059 manifestUrl=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070060 review=None):
61 self.name = name
62 self.fetchUrl = fetch
Conley Owensdb728cd2011-09-26 16:34:01 -070063 self.manifestUrl = manifestUrl
Yestin Sunb292b982012-07-02 07:32:50 -070064 self.remoteAlias = alias
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070065 self.reviewUrl = review
Conley Owensceea3682011-10-20 10:45:47 -070066 self.resolvedFetchUrl = self._resolveFetchUrl()
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070067
David Pursehouse717ece92012-11-13 08:49:16 +090068 def __eq__(self, other):
69 return self.__dict__ == other.__dict__
70
71 def __ne__(self, other):
72 return self.__dict__ != other.__dict__
73
Conley Owensceea3682011-10-20 10:45:47 -070074 def _resolveFetchUrl(self):
75 url = self.fetchUrl.rstrip('/')
Conley Owensdb728cd2011-09-26 16:34:01 -070076 manifestUrl = self.manifestUrl.rstrip('/')
Shawn Pearcea9f11b32013-01-02 15:40:48 -080077 p = manifestUrl.startswith('persistent-http')
78 if p:
79 manifestUrl = manifestUrl[len('persistent-'):]
80
Conley Owensdb728cd2011-09-26 16:34:01 -070081 # urljoin will get confused if there is no scheme in the base url
82 # ie, if manifestUrl is of the form <hostname:port>
83 if manifestUrl.find(':') != manifestUrl.find('/') - 1:
David Pursehousec1b86a22012-11-14 11:36:51 +090084 manifestUrl = 'gopher://' + manifestUrl
Chirayu Desai217ea7d2013-03-01 19:14:38 +053085 url = urllib.parse.urljoin(manifestUrl, url)
Shawn Pearcea9f11b32013-01-02 15:40:48 -080086 url = re.sub(r'^gopher://', '', url)
87 if p:
88 url = 'persistent-' + url
89 return url
Conley Owensceea3682011-10-20 10:45:47 -070090
91 def ToRemoteSpec(self, projectName):
Conley Owens9d8f9142011-10-20 14:36:35 -070092 url = self.resolvedFetchUrl.rstrip('/') + '/' + projectName
Yestin Sunb292b982012-07-02 07:32:50 -070093 remoteName = self.name
Conley Owens1e7ab2a2013-10-08 17:26:57 -070094 if self.remoteAlias:
95 remoteName = self.remoteAlias
Yestin Sunb292b982012-07-02 07:32:50 -070096 return RemoteSpec(remoteName, url, self.reviewUrl)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070097
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070098class XmlManifest(object):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070099 """manages the repo configuration file"""
100
101 def __init__(self, repodir):
102 self.repodir = os.path.abspath(repodir)
103 self.topdir = os.path.dirname(self.repodir)
104 self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700105 self.globalConfig = GitConfig.ForUser()
David Pursehouse4eb285c2013-02-14 16:28:44 +0900106 self.localManifestWarning = False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700107
108 self.repoProject = MetaProject(self, 'repo',
109 gitdir = os.path.join(repodir, 'repo/.git'),
110 worktree = os.path.join(repodir, 'repo'))
111
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700112 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -0800113 gitdir = os.path.join(repodir, 'manifests.git'),
114 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700115
116 self._Unload()
117
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700118 def Override(self, name):
119 """Use a different manifest, just for the current instantiation.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700120 """
121 path = os.path.join(self.manifestProject.worktree, name)
122 if not os.path.isfile(path):
123 raise ManifestParseError('manifest %s not found' % name)
124
125 old = self.manifestFile
126 try:
127 self.manifestFile = path
128 self._Unload()
129 self._Load()
130 finally:
131 self.manifestFile = old
132
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700133 def Link(self, name):
134 """Update the repo metadata to use a different manifest.
135 """
136 self.Override(name)
137
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700138 try:
Sebastian Frias223bf962012-11-21 19:09:25 +0100139 if os.path.lexists(self.manifestFile):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700140 os.remove(self.manifestFile)
141 os.symlink('manifests/%s' % name, self.manifestFile)
Sebastian Frias223bf962012-11-21 19:09:25 +0100142 except OSError as e:
143 raise ManifestParseError('cannot link manifest %s: %s' % (name, str(e)))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700144
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800145 def _RemoteToXml(self, r, doc, root):
146 e = doc.createElement('remote')
147 root.appendChild(e)
148 e.setAttribute('name', r.name)
149 e.setAttribute('fetch', r.fetchUrl)
Conley Owens1e7ab2a2013-10-08 17:26:57 -0700150 if r.remoteAlias is not None:
151 e.setAttribute('alias', r.remoteAlias)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800152 if r.reviewUrl is not None:
153 e.setAttribute('review', r.reviewUrl)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800154
Brian Harring14a66742012-09-28 20:21:57 -0700155 def Save(self, fd, peg_rev=False, peg_rev_upstream=True):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800156 """Write the current manifest out to the given file descriptor.
157 """
Colin Cross5acde752012-03-28 20:15:45 -0700158 mp = self.manifestProject
159
160 groups = mp.config.GetString('manifest.groups')
Matt Gumbel0c635bb2012-12-21 10:14:53 -0800161 if groups:
162 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Colin Cross5acde752012-03-28 20:15:45 -0700163
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800164 doc = xml.dom.minidom.Document()
165 root = doc.createElement('manifest')
166 doc.appendChild(root)
167
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700168 # Save out the notice. There's a little bit of work here to give it the
169 # right whitespace, which assumes that the notice is automatically indented
170 # by 4 by minidom.
171 if self.notice:
172 notice_element = root.appendChild(doc.createElement('notice'))
173 notice_lines = self.notice.splitlines()
174 indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:]
175 notice_element.appendChild(doc.createTextNode(indented_notice))
176
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800177 d = self.default
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800178
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530179 for r in sorted(self.remotes):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800180 self._RemoteToXml(self.remotes[r], doc, root)
181 if self.remotes:
182 root.appendChild(doc.createTextNode(''))
183
184 have_default = False
185 e = doc.createElement('default')
186 if d.remote:
187 have_default = True
188 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700189 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800190 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700191 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700192 if d.sync_j > 1:
193 have_default = True
194 e.setAttribute('sync-j', '%d' % d.sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700195 if d.sync_c:
196 have_default = True
197 e.setAttribute('sync-c', 'true')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800198 if d.sync_s:
199 have_default = True
200 e.setAttribute('sync-s', 'true')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800201 if have_default:
202 root.appendChild(e)
203 root.appendChild(doc.createTextNode(''))
204
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700205 if self._manifest_server:
206 e = doc.createElement('manifest-server')
207 e.setAttribute('url', self._manifest_server)
208 root.appendChild(e)
209 root.appendChild(doc.createTextNode(''))
210
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800211 def output_projects(parent, parent_node, projects):
212 for p in projects:
213 output_project(parent, parent_node, self.projects[p])
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800214
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800215 def output_project(parent, parent_node, p):
Colin Cross5acde752012-03-28 20:15:45 -0700216 if not p.MatchesGroups(groups):
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800217 return
218
219 name = p.name
220 relpath = p.relpath
221 if parent:
222 name = self._UnjoinName(parent.name, name)
223 relpath = self._UnjoinRelpath(parent.relpath, relpath)
Colin Cross5acde752012-03-28 20:15:45 -0700224
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800225 e = doc.createElement('project')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800226 parent_node.appendChild(e)
227 e.setAttribute('name', name)
228 if relpath != name:
229 e.setAttribute('path', relpath)
Conley Owens1e7ab2a2013-10-08 17:26:57 -0700230 remoteName = d.remote.remoteAlias or d.remote.name
231 if not d.remote or p.remote.name != remoteName:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800232 e.setAttribute('remote', p.remote.name)
233 if peg_rev:
234 if self.IsMirror:
Brian Harring14a66742012-09-28 20:21:57 -0700235 value = p.bare_git.rev_parse(p.revisionExpr + '^0')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800236 else:
Brian Harring14a66742012-09-28 20:21:57 -0700237 value = p.work_git.rev_parse(HEAD + '^0')
238 e.setAttribute('revision', value)
239 if peg_rev_upstream and value != p.revisionExpr:
240 # Only save the origin if the origin is not a sha1, and the default
241 # isn't our value, and the if the default doesn't already have that
242 # covered.
243 e.setAttribute('upstream', p.revisionExpr)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700244 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
245 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800246
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800247 for c in p.copyfiles:
248 ce = doc.createElement('copyfile')
249 ce.setAttribute('src', c.src)
250 ce.setAttribute('dest', c.dest)
251 e.appendChild(ce)
252
Conley Owensbb1b5f52012-08-13 13:11:18 -0700253 default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath]
Dmitry Fink17f85ea2012-08-06 14:52:29 -0700254 egroups = [g for g in p.groups if g not in default_groups]
Conley Owens971de8e2012-04-16 10:36:08 -0700255 if egroups:
256 e.setAttribute('groups', ','.join(egroups))
Colin Cross5acde752012-03-28 20:15:45 -0700257
James W. Mills24c13082012-04-12 15:04:13 -0500258 for a in p.annotations:
259 if a.keep == "true":
260 ae = doc.createElement('annotation')
261 ae.setAttribute('name', a.name)
262 ae.setAttribute('value', a.value)
263 e.appendChild(ae)
264
Anatol Pomazau79770d22012-04-20 14:41:59 -0700265 if p.sync_c:
266 e.setAttribute('sync-c', 'true')
267
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800268 if p.sync_s:
269 e.setAttribute('sync-s', 'true')
270
271 if p.subprojects:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530272 sort_projects = list(sorted([subp.name for subp in p.subprojects]))
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800273 output_projects(p, e, sort_projects)
274
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530275 sort_projects = list(sorted([key for key, value in self.projects.items()
276 if not value.parent]))
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800277 sort_projects.sort()
278 output_projects(None, root, sort_projects)
279
Doug Anderson37282b42011-03-04 11:54:18 -0800280 if self._repo_hooks_project:
281 root.appendChild(doc.createTextNode(''))
282 e = doc.createElement('repo-hooks')
283 e.setAttribute('in-project', self._repo_hooks_project.name)
284 e.setAttribute('enabled-list',
285 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
286 root.appendChild(e)
287
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800288 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
289
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700290 @property
291 def projects(self):
292 self._Load()
293 return self._projects
294
295 @property
296 def remotes(self):
297 self._Load()
298 return self._remotes
299
300 @property
301 def default(self):
302 self._Load()
303 return self._default
304
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800305 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800306 def repo_hooks_project(self):
307 self._Load()
308 return self._repo_hooks_project
309
310 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700311 def notice(self):
312 self._Load()
313 return self._notice
314
315 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700316 def manifest_server(self):
317 self._Load()
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800318 return self._manifest_server
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700319
320 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800321 def IsMirror(self):
322 return self.manifestProject.config.GetBoolean('repo.mirror')
323
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700324 def _Unload(self):
325 self._loaded = False
326 self._projects = {}
327 self._remotes = {}
328 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800329 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700330 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700331 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700332 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700333
334 def _Load(self):
335 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800336 m = self.manifestProject
337 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700338 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800339 b = b[len(R_HEADS):]
340 self.branch = b
341
Colin Cross23acdd32012-04-21 00:33:54 -0700342 nodes = []
Brian Harring475a47d2012-06-07 20:05:35 -0700343 nodes.append(self._ParseManifestXml(self.manifestFile,
344 self.manifestProject.worktree))
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700345
346 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
347 if os.path.exists(local):
David Pursehouse4eb285c2013-02-14 16:28:44 +0900348 if not self.localManifestWarning:
349 self.localManifestWarning = True
350 print('warning: %s is deprecated; put local manifests in `%s` instead'
351 % (LOCAL_MANIFEST_NAME, os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME)),
352 file=sys.stderr)
Brian Harring475a47d2012-06-07 20:05:35 -0700353 nodes.append(self._ParseManifestXml(local, self.repodir))
Colin Cross23acdd32012-04-21 00:33:54 -0700354
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900355 local_dir = os.path.abspath(os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME))
356 try:
David Pursehouse52f1e5d2012-11-14 04:53:24 +0900357 for local_file in sorted(os.listdir(local_dir)):
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900358 if local_file.endswith('.xml'):
David Pursehouse5f434ed2012-11-22 13:48:10 +0900359 local = os.path.join(local_dir, local_file)
360 nodes.append(self._ParseManifestXml(local, self.repodir))
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900361 except OSError:
362 pass
363
Joe Onorato26e24752013-01-11 12:35:53 -0800364 try:
365 self._ParseManifest(nodes)
366 except ManifestParseError as e:
367 # There was a problem parsing, unload ourselves in case they catch
368 # this error and try again later, we will show the correct error
369 self._Unload()
370 raise e
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700371
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800372 if self.IsMirror:
373 self._AddMetaProjectMirror(self.repoProject)
374 self._AddMetaProjectMirror(self.manifestProject)
375
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700376 self._loaded = True
377
Brian Harring475a47d2012-06-07 20:05:35 -0700378 def _ParseManifestXml(self, path, include_root):
David Pursehousef7fc8a92012-11-13 04:00:28 +0900379 try:
380 root = xml.dom.minidom.parse(path)
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900381 except (OSError, xml.parsers.expat.ExpatError) as e:
David Pursehousef7fc8a92012-11-13 04:00:28 +0900382 raise ManifestParseError("error parsing manifest %s: %s" % (path, e))
383
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700384 if not root or not root.childNodes:
Brian Harring26448742011-04-28 05:04:41 -0700385 raise ManifestParseError("no root node in %s" % (path,))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700386
Jooncheol Park34acdd22012-08-27 02:25:59 +0900387 for manifest in root.childNodes:
388 if manifest.nodeName == 'manifest':
389 break
390 else:
Brian Harring26448742011-04-28 05:04:41 -0700391 raise ManifestParseError("no <manifest> in %s" % (path,))
392
Colin Cross23acdd32012-04-21 00:33:54 -0700393 nodes = []
David Pursehouse4f7bdea2012-10-22 12:50:15 +0900394 for node in manifest.childNodes: # pylint:disable=W0631
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900395 # We only get here if manifest is initialised
David Pursehousec1b86a22012-11-14 11:36:51 +0900396 if node.nodeName == 'include':
397 name = self._reqatt(node, 'name')
398 fp = os.path.join(include_root, name)
399 if not os.path.isfile(fp):
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530400 raise ManifestParseError("include %s doesn't exist or isn't a file"
401 % (name,))
David Pursehousec1b86a22012-11-14 11:36:51 +0900402 try:
403 nodes.extend(self._ParseManifestXml(fp, include_root))
404 # should isolate this to the exact exception, but that's
405 # tricky. actual parsing implementation may vary.
406 except (KeyboardInterrupt, RuntimeError, SystemExit):
407 raise
408 except Exception as e:
409 raise ManifestParseError(
410 "failed parsing included manifest %s: %s", (name, e))
411 else:
412 nodes.append(node)
Colin Cross23acdd32012-04-21 00:33:54 -0700413 return nodes
Brian Harring26448742011-04-28 05:04:41 -0700414
Colin Cross23acdd32012-04-21 00:33:54 -0700415 def _ParseManifest(self, node_list):
416 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700417 if node.nodeName == 'remote':
418 remote = self._ParseRemote(node)
David Pursehouse717ece92012-11-13 08:49:16 +0900419 if remote:
420 if remote.name in self._remotes:
421 if remote != self._remotes[remote.name]:
422 raise ManifestParseError(
423 'remote %s already exists with different attributes' %
424 (remote.name))
425 else:
426 self._remotes[remote.name] = remote
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700427
Colin Cross23acdd32012-04-21 00:33:54 -0700428 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700429 if node.nodeName == 'default':
430 if self._default is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800431 raise ManifestParseError(
432 'duplicate default in %s' %
433 (self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700434 self._default = self._ParseDefault(node)
435 if self._default is None:
436 self._default = _Default()
437
Colin Cross23acdd32012-04-21 00:33:54 -0700438 for node in itertools.chain(*node_list):
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700439 if node.nodeName == 'notice':
440 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800441 raise ManifestParseError(
442 'duplicate notice in %s' %
443 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700444 self._notice = self._ParseNotice(node)
445
Colin Cross23acdd32012-04-21 00:33:54 -0700446 for node in itertools.chain(*node_list):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700447 if node.nodeName == 'manifest-server':
448 url = self._reqatt(node, 'url')
449 if self._manifest_server is not None:
David Pursehousec1b86a22012-11-14 11:36:51 +0900450 raise ManifestParseError(
451 'duplicate manifest-server in %s' %
452 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700453 self._manifest_server = url
454
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800455 def recursively_add_projects(project):
456 if self._projects.get(project.name):
457 raise ManifestParseError(
458 'duplicate project %s in %s' %
459 (project.name, self.manifestFile))
460 self._projects[project.name] = project
461 for subproject in project.subprojects:
462 recursively_add_projects(subproject)
463
Colin Cross23acdd32012-04-21 00:33:54 -0700464 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700465 if node.nodeName == 'project':
466 project = self._ParseProject(node)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800467 recursively_add_projects(project)
Doug Anderson37282b42011-03-04 11:54:18 -0800468 if node.nodeName == 'repo-hooks':
469 # Get the name of the project and the (space-separated) list of enabled.
470 repo_hooks_project = self._reqatt(node, 'in-project')
471 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
472
473 # Only one project can be the hooks project
474 if self._repo_hooks_project is not None:
475 raise ManifestParseError(
476 'duplicate repo-hooks in %s' %
477 (self.manifestFile))
478
479 # Store a reference to the Project.
480 try:
481 self._repo_hooks_project = self._projects[repo_hooks_project]
482 except KeyError:
483 raise ManifestParseError(
484 'project %s not found for repo-hooks' %
485 (repo_hooks_project))
486
487 # Store the enabled hooks in the Project object.
488 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
Colin Cross23acdd32012-04-21 00:33:54 -0700489 if node.nodeName == 'remove-project':
490 name = self._reqatt(node, 'name')
491 try:
492 del self._projects[name]
493 except KeyError:
David Pursehousef9107482012-11-16 19:12:32 +0900494 raise ManifestParseError('remove-project element specifies non-existent '
495 'project: %s' % name)
Colin Cross23acdd32012-04-21 00:33:54 -0700496
497 # If the manifest removes the hooks project, treat it as if it deleted
498 # the repo-hooks element too.
499 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
500 self._repo_hooks_project = None
501
Doug Anderson37282b42011-03-04 11:54:18 -0800502
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800503 def _AddMetaProjectMirror(self, m):
504 name = None
505 m_url = m.GetRemote(m.remote.name).url
506 if m_url.endswith('/.git'):
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530507 raise ManifestParseError('refusing to mirror %s' % m_url)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800508
509 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700510 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800511 if not url.endswith('/'):
512 url += '/'
513 if m_url.startswith(url):
514 remote = self._default.remote
515 name = m_url[len(url):]
516
517 if name is None:
518 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700519 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Shawn O. Pearcef35b2d92012-08-02 11:46:22 -0700520 remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800521 name = m_url[s:]
522
523 if name.endswith('.git'):
524 name = name[:-4]
525
526 if name not in self._projects:
527 m.PreSync()
528 gitdir = os.path.join(self.topdir, '%s.git' % name)
529 project = Project(manifest = self,
530 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700531 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800532 gitdir = gitdir,
533 worktree = None,
534 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700535 revisionExpr = m.revisionExpr,
536 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800537 self._projects[project.name] = project
538
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700539 def _ParseRemote(self, node):
540 """
541 reads a <remote> element from the manifest file
542 """
543 name = self._reqatt(node, 'name')
Yestin Sunb292b982012-07-02 07:32:50 -0700544 alias = node.getAttribute('alias')
545 if alias == '':
546 alias = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700547 fetch = self._reqatt(node, 'fetch')
548 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800549 if review == '':
550 review = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700551 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Yestin Sunb292b982012-07-02 07:32:50 -0700552 return _XmlRemote(name, alias, fetch, manifestUrl, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700553
554 def _ParseDefault(self, node):
555 """
556 reads a <default> element from the manifest file
557 """
558 d = _Default()
559 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700560 d.revisionExpr = node.getAttribute('revision')
561 if d.revisionExpr == '':
562 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700563
Bryan Jacobsf609f912013-05-06 13:36:24 -0400564 d.destBranchExpr = node.getAttribute('dest-branch') or None
565
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700566 sync_j = node.getAttribute('sync-j')
567 if sync_j == '' or sync_j is None:
568 d.sync_j = 1
569 else:
570 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700571
572 sync_c = node.getAttribute('sync-c')
573 if not sync_c:
574 d.sync_c = False
575 else:
576 d.sync_c = sync_c.lower() in ("yes", "true", "1")
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800577
578 sync_s = node.getAttribute('sync-s')
579 if not sync_s:
580 d.sync_s = False
581 else:
582 d.sync_s = sync_s.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700583 return d
584
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700585 def _ParseNotice(self, node):
586 """
587 reads a <notice> element from the manifest file
588
589 The <notice> element is distinct from other tags in the XML in that the
590 data is conveyed between the start and end tag (it's not an empty-element
591 tag).
592
593 The white space (carriage returns, indentation) for the notice element is
594 relevant and is parsed in a way that is based on how python docstrings work.
595 In fact, the code is remarkably similar to here:
596 http://www.python.org/dev/peps/pep-0257/
597 """
598 # Get the data out of the node...
599 notice = node.childNodes[0].data
600
601 # Figure out minimum indentation, skipping the first line (the same line
602 # as the <notice> tag)...
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530603 minIndent = sys.maxsize
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700604 lines = notice.splitlines()
605 for line in lines[1:]:
606 lstrippedLine = line.lstrip()
607 if lstrippedLine:
608 indent = len(line) - len(lstrippedLine)
609 minIndent = min(indent, minIndent)
610
611 # Strip leading / trailing blank lines and also indentation.
612 cleanLines = [lines[0].strip()]
613 for line in lines[1:]:
614 cleanLines.append(line[minIndent:].rstrip())
615
616 # Clear completely blank lines from front and back...
617 while cleanLines and not cleanLines[0]:
618 del cleanLines[0]
619 while cleanLines and not cleanLines[-1]:
620 del cleanLines[-1]
621
622 return '\n'.join(cleanLines)
623
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800624 def _JoinName(self, parent_name, name):
625 return os.path.join(parent_name, name)
626
627 def _UnjoinName(self, parent_name, name):
628 return os.path.relpath(name, parent_name)
629
630 def _ParseProject(self, node, parent = None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700631 """
632 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700633 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700634 name = self._reqatt(node, 'name')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800635 if parent:
636 name = self._JoinName(parent.name, name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700637
638 remote = self._get_remote(node)
639 if remote is None:
640 remote = self._default.remote
641 if remote is None:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530642 raise ManifestParseError("no remote for project %s within %s" %
643 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700644
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700645 revisionExpr = node.getAttribute('revision')
646 if not revisionExpr:
647 revisionExpr = self._default.revisionExpr
648 if not revisionExpr:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530649 raise ManifestParseError("no revision for project %s within %s" %
650 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700651
652 path = node.getAttribute('path')
653 if not path:
654 path = name
655 if path.startswith('/'):
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530656 raise ManifestParseError("project %s path cannot be absolute in %s" %
657 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700658
Mike Pontillod3153822012-02-28 11:53:24 -0800659 rebase = node.getAttribute('rebase')
660 if not rebase:
661 rebase = True
662 else:
663 rebase = rebase.lower() in ("yes", "true", "1")
664
Anatol Pomazau79770d22012-04-20 14:41:59 -0700665 sync_c = node.getAttribute('sync-c')
666 if not sync_c:
667 sync_c = False
668 else:
669 sync_c = sync_c.lower() in ("yes", "true", "1")
670
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800671 sync_s = node.getAttribute('sync-s')
672 if not sync_s:
673 sync_s = self._default.sync_s
674 else:
675 sync_s = sync_s.lower() in ("yes", "true", "1")
676
David Pursehouseede7f122012-11-27 22:25:30 +0900677 clone_depth = node.getAttribute('clone-depth')
678 if clone_depth:
679 try:
680 clone_depth = int(clone_depth)
681 if clone_depth <= 0:
682 raise ValueError()
683 except ValueError:
684 raise ManifestParseError('invalid clone-depth %s in %s' %
685 (clone_depth, self.manifestFile))
686
Bryan Jacobsf609f912013-05-06 13:36:24 -0400687 dest_branch = node.getAttribute('dest-branch') or self._default.destBranchExpr
688
Brian Harring14a66742012-09-28 20:21:57 -0700689 upstream = node.getAttribute('upstream')
690
Conley Owens971de8e2012-04-16 10:36:08 -0700691 groups = ''
692 if node.hasAttribute('groups'):
693 groups = node.getAttribute('groups')
David Pursehouse1d947b32012-10-25 12:23:11 +0900694 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Brian Harring7da13142012-06-15 02:24:20 -0700695
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800696 if parent is None:
697 relpath, worktree, gitdir = self.GetProjectPaths(name, path)
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700698 else:
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800699 relpath, worktree, gitdir = self.GetSubprojectPaths(parent, path)
700
701 default_groups = ['all', 'name:%s' % name, 'path:%s' % relpath]
702 groups.extend(set(default_groups).difference(groups))
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700703
Scott Fandb83b1b2013-02-28 09:34:14 +0800704 if self.IsMirror and node.hasAttribute('force-path'):
705 if node.getAttribute('force-path').lower() in ("yes", "true", "1"):
706 gitdir = os.path.join(self.topdir, '%s.git' % path)
707
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700708 project = Project(manifest = self,
709 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700710 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700711 gitdir = gitdir,
712 worktree = worktree,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800713 relpath = relpath,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700714 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800715 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700716 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700717 groups = groups,
Brian Harring14a66742012-09-28 20:21:57 -0700718 sync_c = sync_c,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800719 sync_s = sync_s,
David Pursehouseede7f122012-11-27 22:25:30 +0900720 clone_depth = clone_depth,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800721 upstream = upstream,
Bryan Jacobsf609f912013-05-06 13:36:24 -0400722 parent = parent,
723 dest_branch = dest_branch)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700724
725 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700726 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700727 self._ParseCopyFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500728 if n.nodeName == 'annotation':
729 self._ParseAnnotation(project, n)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800730 if n.nodeName == 'project':
731 project.subprojects.append(self._ParseProject(n, parent = project))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700732
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700733 return project
734
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800735 def GetProjectPaths(self, name, path):
736 relpath = path
737 if self.IsMirror:
738 worktree = None
739 gitdir = os.path.join(self.topdir, '%s.git' % name)
740 else:
741 worktree = os.path.join(self.topdir, path).replace('\\', '/')
742 gitdir = os.path.join(self.repodir, 'projects', '%s.git' % path)
743 return relpath, worktree, gitdir
744
745 def GetSubprojectName(self, parent, submodule_path):
746 return os.path.join(parent.name, submodule_path)
747
748 def _JoinRelpath(self, parent_relpath, relpath):
749 return os.path.join(parent_relpath, relpath)
750
751 def _UnjoinRelpath(self, parent_relpath, relpath):
752 return os.path.relpath(relpath, parent_relpath)
753
754 def GetSubprojectPaths(self, parent, path):
755 relpath = self._JoinRelpath(parent.relpath, path)
756 gitdir = os.path.join(parent.gitdir, 'subprojects', '%s.git' % path)
757 if self.IsMirror:
758 worktree = None
759 else:
760 worktree = os.path.join(parent.worktree, path).replace('\\', '/')
761 return relpath, worktree, gitdir
762
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700763 def _ParseCopyFile(self, project, node):
764 src = self._reqatt(node, 'src')
765 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800766 if not self.IsMirror:
767 # src is project relative;
768 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800769 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700770
James W. Mills24c13082012-04-12 15:04:13 -0500771 def _ParseAnnotation(self, project, node):
772 name = self._reqatt(node, 'name')
773 value = self._reqatt(node, 'value')
774 try:
775 keep = self._reqatt(node, 'keep').lower()
776 except ManifestParseError:
777 keep = "true"
778 if keep != "true" and keep != "false":
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530779 raise ManifestParseError('optional "keep" attribute must be '
780 '"true" or "false"')
James W. Mills24c13082012-04-12 15:04:13 -0500781 project.AddAnnotation(name, value, keep)
782
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700783 def _get_remote(self, node):
784 name = node.getAttribute('remote')
785 if not name:
786 return None
787
788 v = self._remotes.get(name)
789 if not v:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530790 raise ManifestParseError("remote %s not defined in %s" %
791 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700792 return v
793
794 def _reqatt(self, node, attname):
795 """
796 reads a required attribute from the node.
797 """
798 v = node.getAttribute(attname)
799 if not v:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530800 raise ManifestParseError("no %s in <%s> within %s" %
801 (attname, node.nodeName, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700802 return v