blob: 51d51b9514914a414b5be6b47e39888ecff7a783 [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
Conley Owensdb728cd2011-09-26 16:34:01 -070021import urlparse
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070022import xml.dom.minidom
23
David Pursehousee15c65a2012-08-22 10:46:11 +090024from git_config import GitConfig
David Pursehousee00aa6b2012-09-11 14:33:51 +090025from git_refs import R_HEADS, HEAD
26from project import RemoteSpec, Project, MetaProject
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070027from error import ManifestParseError
28
29MANIFEST_FILE_NAME = 'manifest.xml'
Shawn O. Pearce5cc66792008-10-23 16:19:27 -070030LOCAL_MANIFEST_NAME = 'local_manifest.xml'
David Pursehouse2d5a0df2012-11-13 02:50:36 +090031LOCAL_MANIFESTS_DIR_NAME = 'local_manifests'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070032
Conley Owensdb728cd2011-09-26 16:34:01 -070033urlparse.uses_relative.extend(['ssh', 'git'])
34urlparse.uses_netloc.extend(['ssh', 'git'])
35
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070036class _Default(object):
37 """Project defaults within the manifest."""
38
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -070039 revisionExpr = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070040 remote = None
Shawn O. Pearce6392c872011-09-22 17:44:31 -070041 sync_j = 1
Anatol Pomazau79770d22012-04-20 14:41:59 -070042 sync_c = False
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +080043 sync_s = False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070044
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070045class _XmlRemote(object):
46 def __init__(self,
47 name,
Yestin Sunb292b982012-07-02 07:32:50 -070048 alias=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070049 fetch=None,
Conley Owensdb728cd2011-09-26 16:34:01 -070050 manifestUrl=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070051 review=None):
52 self.name = name
53 self.fetchUrl = fetch
Conley Owensdb728cd2011-09-26 16:34:01 -070054 self.manifestUrl = manifestUrl
Yestin Sunb292b982012-07-02 07:32:50 -070055 self.remoteAlias = alias
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070056 self.reviewUrl = review
Conley Owensceea3682011-10-20 10:45:47 -070057 self.resolvedFetchUrl = self._resolveFetchUrl()
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070058
David Pursehouse717ece92012-11-13 08:49:16 +090059 def __eq__(self, other):
60 return self.__dict__ == other.__dict__
61
62 def __ne__(self, other):
63 return self.__dict__ != other.__dict__
64
Conley Owensceea3682011-10-20 10:45:47 -070065 def _resolveFetchUrl(self):
66 url = self.fetchUrl.rstrip('/')
Conley Owensdb728cd2011-09-26 16:34:01 -070067 manifestUrl = self.manifestUrl.rstrip('/')
Shawn Pearcea9f11b32013-01-02 15:40:48 -080068 p = manifestUrl.startswith('persistent-http')
69 if p:
70 manifestUrl = manifestUrl[len('persistent-'):]
71
Conley Owensdb728cd2011-09-26 16:34:01 -070072 # urljoin will get confused if there is no scheme in the base url
73 # ie, if manifestUrl is of the form <hostname:port>
74 if manifestUrl.find(':') != manifestUrl.find('/') - 1:
David Pursehousec1b86a22012-11-14 11:36:51 +090075 manifestUrl = 'gopher://' + manifestUrl
Conley Owensdb728cd2011-09-26 16:34:01 -070076 url = urlparse.urljoin(manifestUrl, url)
Shawn Pearcea9f11b32013-01-02 15:40:48 -080077 url = re.sub(r'^gopher://', '', url)
78 if p:
79 url = 'persistent-' + url
80 return url
Conley Owensceea3682011-10-20 10:45:47 -070081
82 def ToRemoteSpec(self, projectName):
Conley Owens9d8f9142011-10-20 14:36:35 -070083 url = self.resolvedFetchUrl.rstrip('/') + '/' + projectName
Yestin Sunb292b982012-07-02 07:32:50 -070084 remoteName = self.name
Yestin Sunb292b982012-07-02 07:32:50 -070085 return RemoteSpec(remoteName, url, self.reviewUrl)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070086
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070087class XmlManifest(object):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070088 """manages the repo configuration file"""
89
90 def __init__(self, repodir):
91 self.repodir = os.path.abspath(repodir)
92 self.topdir = os.path.dirname(self.repodir)
93 self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070094 self.globalConfig = GitConfig.ForUser()
David Pursehouse4eb285c2013-02-14 16:28:44 +090095 self.localManifestWarning = False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070096
97 self.repoProject = MetaProject(self, 'repo',
98 gitdir = os.path.join(repodir, 'repo/.git'),
99 worktree = os.path.join(repodir, 'repo'))
100
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700101 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -0800102 gitdir = os.path.join(repodir, 'manifests.git'),
103 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700104
105 self._Unload()
106
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700107 def Override(self, name):
108 """Use a different manifest, just for the current instantiation.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700109 """
110 path = os.path.join(self.manifestProject.worktree, name)
111 if not os.path.isfile(path):
112 raise ManifestParseError('manifest %s not found' % name)
113
114 old = self.manifestFile
115 try:
116 self.manifestFile = path
117 self._Unload()
118 self._Load()
119 finally:
120 self.manifestFile = old
121
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700122 def Link(self, name):
123 """Update the repo metadata to use a different manifest.
124 """
125 self.Override(name)
126
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700127 try:
Sebastian Frias223bf962012-11-21 19:09:25 +0100128 if os.path.lexists(self.manifestFile):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700129 os.remove(self.manifestFile)
130 os.symlink('manifests/%s' % name, self.manifestFile)
Sebastian Frias223bf962012-11-21 19:09:25 +0100131 except OSError as e:
132 raise ManifestParseError('cannot link manifest %s: %s' % (name, str(e)))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700133
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800134 def _RemoteToXml(self, r, doc, root):
135 e = doc.createElement('remote')
136 root.appendChild(e)
137 e.setAttribute('name', r.name)
138 e.setAttribute('fetch', r.fetchUrl)
139 if r.reviewUrl is not None:
140 e.setAttribute('review', r.reviewUrl)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800141
Brian Harring14a66742012-09-28 20:21:57 -0700142 def Save(self, fd, peg_rev=False, peg_rev_upstream=True):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800143 """Write the current manifest out to the given file descriptor.
144 """
Colin Cross5acde752012-03-28 20:15:45 -0700145 mp = self.manifestProject
146
147 groups = mp.config.GetString('manifest.groups')
Matt Gumbel0c635bb2012-12-21 10:14:53 -0800148 if groups:
149 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Colin Cross5acde752012-03-28 20:15:45 -0700150
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800151 doc = xml.dom.minidom.Document()
152 root = doc.createElement('manifest')
153 doc.appendChild(root)
154
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700155 # Save out the notice. There's a little bit of work here to give it the
156 # right whitespace, which assumes that the notice is automatically indented
157 # by 4 by minidom.
158 if self.notice:
159 notice_element = root.appendChild(doc.createElement('notice'))
160 notice_lines = self.notice.splitlines()
161 indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:]
162 notice_element.appendChild(doc.createTextNode(indented_notice))
163
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800164 d = self.default
165 sort_remotes = list(self.remotes.keys())
166 sort_remotes.sort()
167
168 for r in sort_remotes:
169 self._RemoteToXml(self.remotes[r], doc, root)
170 if self.remotes:
171 root.appendChild(doc.createTextNode(''))
172
173 have_default = False
174 e = doc.createElement('default')
175 if d.remote:
176 have_default = True
177 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700178 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800179 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700180 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700181 if d.sync_j > 1:
182 have_default = True
183 e.setAttribute('sync-j', '%d' % d.sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700184 if d.sync_c:
185 have_default = True
186 e.setAttribute('sync-c', 'true')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800187 if d.sync_s:
188 have_default = True
189 e.setAttribute('sync-s', 'true')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800190 if have_default:
191 root.appendChild(e)
192 root.appendChild(doc.createTextNode(''))
193
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700194 if self._manifest_server:
195 e = doc.createElement('manifest-server')
196 e.setAttribute('url', self._manifest_server)
197 root.appendChild(e)
198 root.appendChild(doc.createTextNode(''))
199
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800200 def output_projects(parent, parent_node, projects):
201 for p in projects:
202 output_project(parent, parent_node, self.projects[p])
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800203
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800204 def output_project(parent, parent_node, p):
Colin Cross5acde752012-03-28 20:15:45 -0700205 if not p.MatchesGroups(groups):
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800206 return
207
208 name = p.name
209 relpath = p.relpath
210 if parent:
211 name = self._UnjoinName(parent.name, name)
212 relpath = self._UnjoinRelpath(parent.relpath, relpath)
Colin Cross5acde752012-03-28 20:15:45 -0700213
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800214 e = doc.createElement('project')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800215 parent_node.appendChild(e)
216 e.setAttribute('name', name)
217 if relpath != name:
218 e.setAttribute('path', relpath)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800219 if not d.remote or p.remote.name != d.remote.name:
220 e.setAttribute('remote', p.remote.name)
221 if peg_rev:
222 if self.IsMirror:
Brian Harring14a66742012-09-28 20:21:57 -0700223 value = p.bare_git.rev_parse(p.revisionExpr + '^0')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800224 else:
Brian Harring14a66742012-09-28 20:21:57 -0700225 value = p.work_git.rev_parse(HEAD + '^0')
226 e.setAttribute('revision', value)
227 if peg_rev_upstream and value != p.revisionExpr:
228 # Only save the origin if the origin is not a sha1, and the default
229 # isn't our value, and the if the default doesn't already have that
230 # covered.
231 e.setAttribute('upstream', p.revisionExpr)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700232 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
233 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800234
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800235 for c in p.copyfiles:
236 ce = doc.createElement('copyfile')
237 ce.setAttribute('src', c.src)
238 ce.setAttribute('dest', c.dest)
239 e.appendChild(ce)
240
Conley Owensbb1b5f52012-08-13 13:11:18 -0700241 default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath]
Dmitry Fink17f85ea2012-08-06 14:52:29 -0700242 egroups = [g for g in p.groups if g not in default_groups]
Conley Owens971de8e2012-04-16 10:36:08 -0700243 if egroups:
244 e.setAttribute('groups', ','.join(egroups))
Colin Cross5acde752012-03-28 20:15:45 -0700245
James W. Mills24c13082012-04-12 15:04:13 -0500246 for a in p.annotations:
247 if a.keep == "true":
248 ae = doc.createElement('annotation')
249 ae.setAttribute('name', a.name)
250 ae.setAttribute('value', a.value)
251 e.appendChild(ae)
252
Anatol Pomazau79770d22012-04-20 14:41:59 -0700253 if p.sync_c:
254 e.setAttribute('sync-c', 'true')
255
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800256 if p.sync_s:
257 e.setAttribute('sync-s', 'true')
258
259 if p.subprojects:
260 sort_projects = [subp.name for subp in p.subprojects]
261 sort_projects.sort()
262 output_projects(p, e, sort_projects)
263
264 sort_projects = [key for key in self.projects.keys()
265 if not self.projects[key].parent]
266 sort_projects.sort()
267 output_projects(None, root, sort_projects)
268
Doug Anderson37282b42011-03-04 11:54:18 -0800269 if self._repo_hooks_project:
270 root.appendChild(doc.createTextNode(''))
271 e = doc.createElement('repo-hooks')
272 e.setAttribute('in-project', self._repo_hooks_project.name)
273 e.setAttribute('enabled-list',
274 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
275 root.appendChild(e)
276
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800277 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
278
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700279 @property
280 def projects(self):
281 self._Load()
282 return self._projects
283
284 @property
285 def remotes(self):
286 self._Load()
287 return self._remotes
288
289 @property
290 def default(self):
291 self._Load()
292 return self._default
293
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800294 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800295 def repo_hooks_project(self):
296 self._Load()
297 return self._repo_hooks_project
298
299 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700300 def notice(self):
301 self._Load()
302 return self._notice
303
304 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700305 def manifest_server(self):
306 self._Load()
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800307 return self._manifest_server
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700308
309 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800310 def IsMirror(self):
311 return self.manifestProject.config.GetBoolean('repo.mirror')
312
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700313 def _Unload(self):
314 self._loaded = False
315 self._projects = {}
316 self._remotes = {}
317 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800318 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700319 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700320 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700321 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700322
323 def _Load(self):
324 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800325 m = self.manifestProject
326 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700327 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800328 b = b[len(R_HEADS):]
329 self.branch = b
330
Colin Cross23acdd32012-04-21 00:33:54 -0700331 nodes = []
Brian Harring475a47d2012-06-07 20:05:35 -0700332 nodes.append(self._ParseManifestXml(self.manifestFile,
333 self.manifestProject.worktree))
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700334
335 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
336 if os.path.exists(local):
David Pursehouse4eb285c2013-02-14 16:28:44 +0900337 if not self.localManifestWarning:
338 self.localManifestWarning = True
339 print('warning: %s is deprecated; put local manifests in `%s` instead'
340 % (LOCAL_MANIFEST_NAME, os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME)),
341 file=sys.stderr)
Brian Harring475a47d2012-06-07 20:05:35 -0700342 nodes.append(self._ParseManifestXml(local, self.repodir))
Colin Cross23acdd32012-04-21 00:33:54 -0700343
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900344 local_dir = os.path.abspath(os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME))
345 try:
David Pursehouse52f1e5d2012-11-14 04:53:24 +0900346 for local_file in sorted(os.listdir(local_dir)):
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900347 if local_file.endswith('.xml'):
David Pursehouse5f434ed2012-11-22 13:48:10 +0900348 local = os.path.join(local_dir, local_file)
349 nodes.append(self._ParseManifestXml(local, self.repodir))
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900350 except OSError:
351 pass
352
Joe Onorato26e24752013-01-11 12:35:53 -0800353 try:
354 self._ParseManifest(nodes)
355 except ManifestParseError as e:
356 # There was a problem parsing, unload ourselves in case they catch
357 # this error and try again later, we will show the correct error
358 self._Unload()
359 raise e
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700360
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800361 if self.IsMirror:
362 self._AddMetaProjectMirror(self.repoProject)
363 self._AddMetaProjectMirror(self.manifestProject)
364
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700365 self._loaded = True
366
Brian Harring475a47d2012-06-07 20:05:35 -0700367 def _ParseManifestXml(self, path, include_root):
David Pursehousef7fc8a92012-11-13 04:00:28 +0900368 try:
369 root = xml.dom.minidom.parse(path)
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900370 except (OSError, xml.parsers.expat.ExpatError) as e:
David Pursehousef7fc8a92012-11-13 04:00:28 +0900371 raise ManifestParseError("error parsing manifest %s: %s" % (path, e))
372
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700373 if not root or not root.childNodes:
Brian Harring26448742011-04-28 05:04:41 -0700374 raise ManifestParseError("no root node in %s" % (path,))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700375
Jooncheol Park34acdd22012-08-27 02:25:59 +0900376 for manifest in root.childNodes:
377 if manifest.nodeName == 'manifest':
378 break
379 else:
Brian Harring26448742011-04-28 05:04:41 -0700380 raise ManifestParseError("no <manifest> in %s" % (path,))
381
Colin Cross23acdd32012-04-21 00:33:54 -0700382 nodes = []
David Pursehouse4f7bdea2012-10-22 12:50:15 +0900383 for node in manifest.childNodes: # pylint:disable=W0631
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900384 # We only get here if manifest is initialised
David Pursehousec1b86a22012-11-14 11:36:51 +0900385 if node.nodeName == 'include':
386 name = self._reqatt(node, 'name')
387 fp = os.path.join(include_root, name)
388 if not os.path.isfile(fp):
389 raise ManifestParseError, \
390 "include %s doesn't exist or isn't a file" % \
391 (name,)
392 try:
393 nodes.extend(self._ParseManifestXml(fp, include_root))
394 # should isolate this to the exact exception, but that's
395 # tricky. actual parsing implementation may vary.
396 except (KeyboardInterrupt, RuntimeError, SystemExit):
397 raise
398 except Exception as e:
399 raise ManifestParseError(
400 "failed parsing included manifest %s: %s", (name, e))
401 else:
402 nodes.append(node)
Colin Cross23acdd32012-04-21 00:33:54 -0700403 return nodes
Brian Harring26448742011-04-28 05:04:41 -0700404
Colin Cross23acdd32012-04-21 00:33:54 -0700405 def _ParseManifest(self, node_list):
406 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700407 if node.nodeName == 'remote':
408 remote = self._ParseRemote(node)
David Pursehouse717ece92012-11-13 08:49:16 +0900409 if remote:
410 if remote.name in self._remotes:
411 if remote != self._remotes[remote.name]:
412 raise ManifestParseError(
413 'remote %s already exists with different attributes' %
414 (remote.name))
415 else:
416 self._remotes[remote.name] = remote
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700417
Colin Cross23acdd32012-04-21 00:33:54 -0700418 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700419 if node.nodeName == 'default':
420 if self._default is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800421 raise ManifestParseError(
422 'duplicate default in %s' %
423 (self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700424 self._default = self._ParseDefault(node)
425 if self._default is None:
426 self._default = _Default()
427
Colin Cross23acdd32012-04-21 00:33:54 -0700428 for node in itertools.chain(*node_list):
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700429 if node.nodeName == 'notice':
430 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800431 raise ManifestParseError(
432 'duplicate notice in %s' %
433 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700434 self._notice = self._ParseNotice(node)
435
Colin Cross23acdd32012-04-21 00:33:54 -0700436 for node in itertools.chain(*node_list):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700437 if node.nodeName == 'manifest-server':
438 url = self._reqatt(node, 'url')
439 if self._manifest_server is not None:
David Pursehousec1b86a22012-11-14 11:36:51 +0900440 raise ManifestParseError(
441 'duplicate manifest-server in %s' %
442 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700443 self._manifest_server = url
444
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800445 def recursively_add_projects(project):
446 if self._projects.get(project.name):
447 raise ManifestParseError(
448 'duplicate project %s in %s' %
449 (project.name, self.manifestFile))
450 self._projects[project.name] = project
451 for subproject in project.subprojects:
452 recursively_add_projects(subproject)
453
Colin Cross23acdd32012-04-21 00:33:54 -0700454 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700455 if node.nodeName == 'project':
456 project = self._ParseProject(node)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800457 recursively_add_projects(project)
Doug Anderson37282b42011-03-04 11:54:18 -0800458 if node.nodeName == 'repo-hooks':
459 # Get the name of the project and the (space-separated) list of enabled.
460 repo_hooks_project = self._reqatt(node, 'in-project')
461 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
462
463 # Only one project can be the hooks project
464 if self._repo_hooks_project is not None:
465 raise ManifestParseError(
466 'duplicate repo-hooks in %s' %
467 (self.manifestFile))
468
469 # Store a reference to the Project.
470 try:
471 self._repo_hooks_project = self._projects[repo_hooks_project]
472 except KeyError:
473 raise ManifestParseError(
474 'project %s not found for repo-hooks' %
475 (repo_hooks_project))
476
477 # Store the enabled hooks in the Project object.
478 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
Colin Cross23acdd32012-04-21 00:33:54 -0700479 if node.nodeName == 'remove-project':
480 name = self._reqatt(node, 'name')
481 try:
482 del self._projects[name]
483 except KeyError:
David Pursehousef9107482012-11-16 19:12:32 +0900484 raise ManifestParseError('remove-project element specifies non-existent '
485 'project: %s' % name)
Colin Cross23acdd32012-04-21 00:33:54 -0700486
487 # If the manifest removes the hooks project, treat it as if it deleted
488 # the repo-hooks element too.
489 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
490 self._repo_hooks_project = None
491
Doug Anderson37282b42011-03-04 11:54:18 -0800492
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800493 def _AddMetaProjectMirror(self, m):
494 name = None
495 m_url = m.GetRemote(m.remote.name).url
496 if m_url.endswith('/.git'):
497 raise ManifestParseError, 'refusing to mirror %s' % m_url
498
499 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700500 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800501 if not url.endswith('/'):
502 url += '/'
503 if m_url.startswith(url):
504 remote = self._default.remote
505 name = m_url[len(url):]
506
507 if name is None:
508 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700509 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Shawn O. Pearcef35b2d92012-08-02 11:46:22 -0700510 remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800511 name = m_url[s:]
512
513 if name.endswith('.git'):
514 name = name[:-4]
515
516 if name not in self._projects:
517 m.PreSync()
518 gitdir = os.path.join(self.topdir, '%s.git' % name)
519 project = Project(manifest = self,
520 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700521 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800522 gitdir = gitdir,
523 worktree = None,
524 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700525 revisionExpr = m.revisionExpr,
526 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800527 self._projects[project.name] = project
528
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700529 def _ParseRemote(self, node):
530 """
531 reads a <remote> element from the manifest file
532 """
533 name = self._reqatt(node, 'name')
Yestin Sunb292b982012-07-02 07:32:50 -0700534 alias = node.getAttribute('alias')
535 if alias == '':
536 alias = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700537 fetch = self._reqatt(node, 'fetch')
538 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800539 if review == '':
540 review = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700541 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Yestin Sunb292b982012-07-02 07:32:50 -0700542 return _XmlRemote(name, alias, fetch, manifestUrl, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700543
544 def _ParseDefault(self, node):
545 """
546 reads a <default> element from the manifest file
547 """
548 d = _Default()
549 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700550 d.revisionExpr = node.getAttribute('revision')
551 if d.revisionExpr == '':
552 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700553
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700554 sync_j = node.getAttribute('sync-j')
555 if sync_j == '' or sync_j is None:
556 d.sync_j = 1
557 else:
558 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700559
560 sync_c = node.getAttribute('sync-c')
561 if not sync_c:
562 d.sync_c = False
563 else:
564 d.sync_c = sync_c.lower() in ("yes", "true", "1")
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800565
566 sync_s = node.getAttribute('sync-s')
567 if not sync_s:
568 d.sync_s = False
569 else:
570 d.sync_s = sync_s.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700571 return d
572
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700573 def _ParseNotice(self, node):
574 """
575 reads a <notice> element from the manifest file
576
577 The <notice> element is distinct from other tags in the XML in that the
578 data is conveyed between the start and end tag (it's not an empty-element
579 tag).
580
581 The white space (carriage returns, indentation) for the notice element is
582 relevant and is parsed in a way that is based on how python docstrings work.
583 In fact, the code is remarkably similar to here:
584 http://www.python.org/dev/peps/pep-0257/
585 """
586 # Get the data out of the node...
587 notice = node.childNodes[0].data
588
589 # Figure out minimum indentation, skipping the first line (the same line
590 # as the <notice> tag)...
591 minIndent = sys.maxint
592 lines = notice.splitlines()
593 for line in lines[1:]:
594 lstrippedLine = line.lstrip()
595 if lstrippedLine:
596 indent = len(line) - len(lstrippedLine)
597 minIndent = min(indent, minIndent)
598
599 # Strip leading / trailing blank lines and also indentation.
600 cleanLines = [lines[0].strip()]
601 for line in lines[1:]:
602 cleanLines.append(line[minIndent:].rstrip())
603
604 # Clear completely blank lines from front and back...
605 while cleanLines and not cleanLines[0]:
606 del cleanLines[0]
607 while cleanLines and not cleanLines[-1]:
608 del cleanLines[-1]
609
610 return '\n'.join(cleanLines)
611
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800612 def _JoinName(self, parent_name, name):
613 return os.path.join(parent_name, name)
614
615 def _UnjoinName(self, parent_name, name):
616 return os.path.relpath(name, parent_name)
617
618 def _ParseProject(self, node, parent = None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700619 """
620 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700621 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700622 name = self._reqatt(node, 'name')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800623 if parent:
624 name = self._JoinName(parent.name, name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700625
626 remote = self._get_remote(node)
627 if remote is None:
628 remote = self._default.remote
629 if remote is None:
630 raise ManifestParseError, \
631 "no remote for project %s within %s" % \
632 (name, self.manifestFile)
633
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700634 revisionExpr = node.getAttribute('revision')
635 if not revisionExpr:
636 revisionExpr = self._default.revisionExpr
637 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700638 raise ManifestParseError, \
639 "no revision for project %s within %s" % \
640 (name, self.manifestFile)
641
642 path = node.getAttribute('path')
643 if not path:
644 path = name
645 if path.startswith('/'):
646 raise ManifestParseError, \
647 "project %s path cannot be absolute in %s" % \
648 (name, self.manifestFile)
649
Mike Pontillod3153822012-02-28 11:53:24 -0800650 rebase = node.getAttribute('rebase')
651 if not rebase:
652 rebase = True
653 else:
654 rebase = rebase.lower() in ("yes", "true", "1")
655
Anatol Pomazau79770d22012-04-20 14:41:59 -0700656 sync_c = node.getAttribute('sync-c')
657 if not sync_c:
658 sync_c = False
659 else:
660 sync_c = sync_c.lower() in ("yes", "true", "1")
661
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800662 sync_s = node.getAttribute('sync-s')
663 if not sync_s:
664 sync_s = self._default.sync_s
665 else:
666 sync_s = sync_s.lower() in ("yes", "true", "1")
667
David Pursehouseede7f122012-11-27 22:25:30 +0900668 clone_depth = node.getAttribute('clone-depth')
669 if clone_depth:
670 try:
671 clone_depth = int(clone_depth)
672 if clone_depth <= 0:
673 raise ValueError()
674 except ValueError:
675 raise ManifestParseError('invalid clone-depth %s in %s' %
676 (clone_depth, self.manifestFile))
677
Brian Harring14a66742012-09-28 20:21:57 -0700678 upstream = node.getAttribute('upstream')
679
Conley Owens971de8e2012-04-16 10:36:08 -0700680 groups = ''
681 if node.hasAttribute('groups'):
682 groups = node.getAttribute('groups')
David Pursehouse1d947b32012-10-25 12:23:11 +0900683 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Brian Harring7da13142012-06-15 02:24:20 -0700684
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800685 if parent is None:
686 relpath, worktree, gitdir = self.GetProjectPaths(name, path)
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700687 else:
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800688 relpath, worktree, gitdir = self.GetSubprojectPaths(parent, path)
689
690 default_groups = ['all', 'name:%s' % name, 'path:%s' % relpath]
691 groups.extend(set(default_groups).difference(groups))
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700692
Scott Fandb83b1b2013-02-28 09:34:14 +0800693 if self.IsMirror and node.hasAttribute('force-path'):
694 if node.getAttribute('force-path').lower() in ("yes", "true", "1"):
695 gitdir = os.path.join(self.topdir, '%s.git' % path)
696
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700697 project = Project(manifest = self,
698 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700699 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700700 gitdir = gitdir,
701 worktree = worktree,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800702 relpath = relpath,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700703 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800704 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700705 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700706 groups = groups,
Brian Harring14a66742012-09-28 20:21:57 -0700707 sync_c = sync_c,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800708 sync_s = sync_s,
David Pursehouseede7f122012-11-27 22:25:30 +0900709 clone_depth = clone_depth,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800710 upstream = upstream,
711 parent = parent)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700712
713 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700714 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700715 self._ParseCopyFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500716 if n.nodeName == 'annotation':
717 self._ParseAnnotation(project, n)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800718 if n.nodeName == 'project':
719 project.subprojects.append(self._ParseProject(n, parent = project))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700720
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700721 return project
722
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800723 def GetProjectPaths(self, name, path):
724 relpath = path
725 if self.IsMirror:
726 worktree = None
727 gitdir = os.path.join(self.topdir, '%s.git' % name)
728 else:
729 worktree = os.path.join(self.topdir, path).replace('\\', '/')
730 gitdir = os.path.join(self.repodir, 'projects', '%s.git' % path)
731 return relpath, worktree, gitdir
732
733 def GetSubprojectName(self, parent, submodule_path):
734 return os.path.join(parent.name, submodule_path)
735
736 def _JoinRelpath(self, parent_relpath, relpath):
737 return os.path.join(parent_relpath, relpath)
738
739 def _UnjoinRelpath(self, parent_relpath, relpath):
740 return os.path.relpath(relpath, parent_relpath)
741
742 def GetSubprojectPaths(self, parent, path):
743 relpath = self._JoinRelpath(parent.relpath, path)
744 gitdir = os.path.join(parent.gitdir, 'subprojects', '%s.git' % path)
745 if self.IsMirror:
746 worktree = None
747 else:
748 worktree = os.path.join(parent.worktree, path).replace('\\', '/')
749 return relpath, worktree, gitdir
750
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700751 def _ParseCopyFile(self, project, node):
752 src = self._reqatt(node, 'src')
753 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800754 if not self.IsMirror:
755 # src is project relative;
756 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800757 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700758
James W. Mills24c13082012-04-12 15:04:13 -0500759 def _ParseAnnotation(self, project, node):
760 name = self._reqatt(node, 'name')
761 value = self._reqatt(node, 'value')
762 try:
763 keep = self._reqatt(node, 'keep').lower()
764 except ManifestParseError:
765 keep = "true"
766 if keep != "true" and keep != "false":
767 raise ManifestParseError, "optional \"keep\" attribute must be \"true\" or \"false\""
768 project.AddAnnotation(name, value, keep)
769
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700770 def _get_remote(self, node):
771 name = node.getAttribute('remote')
772 if not name:
773 return None
774
775 v = self._remotes.get(name)
776 if not v:
777 raise ManifestParseError, \
778 "remote %s not defined in %s" % \
779 (name, self.manifestFile)
780 return v
781
782 def _reqatt(self, node, attname):
783 """
784 reads a required attribute from the node.
785 """
786 v = node.getAttribute(attname)
787 if not v:
788 raise ManifestParseError, \
789 "no %s in <%s> within %s" % \
790 (attname, node.nodeName, self.manifestFile)
791 return v