blob: 0664eff96da28a31df58339b8f18a50fe577fa80 [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
85 if self.remoteAlias:
86 remoteName = self.remoteAlias
87 return RemoteSpec(remoteName, url, self.reviewUrl)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070088
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070089class XmlManifest(object):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070090 """manages the repo configuration file"""
91
92 def __init__(self, repodir):
93 self.repodir = os.path.abspath(repodir)
94 self.topdir = os.path.dirname(self.repodir)
95 self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070096 self.globalConfig = GitConfig.ForUser()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070097
98 self.repoProject = MetaProject(self, 'repo',
99 gitdir = os.path.join(repodir, 'repo/.git'),
100 worktree = os.path.join(repodir, 'repo'))
101
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700102 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -0800103 gitdir = os.path.join(repodir, 'manifests.git'),
104 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700105
106 self._Unload()
107
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700108 def Override(self, name):
109 """Use a different manifest, just for the current instantiation.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700110 """
111 path = os.path.join(self.manifestProject.worktree, name)
112 if not os.path.isfile(path):
113 raise ManifestParseError('manifest %s not found' % name)
114
115 old = self.manifestFile
116 try:
117 self.manifestFile = path
118 self._Unload()
119 self._Load()
120 finally:
121 self.manifestFile = old
122
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700123 def Link(self, name):
124 """Update the repo metadata to use a different manifest.
125 """
126 self.Override(name)
127
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700128 try:
129 if os.path.exists(self.manifestFile):
130 os.remove(self.manifestFile)
131 os.symlink('manifests/%s' % name, self.manifestFile)
David Pursehouse8a68ff92012-09-24 12:15:13 +0900132 except OSError:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700133 raise ManifestParseError('cannot link manifest %s' % name)
134
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800135 def _RemoteToXml(self, r, doc, root):
136 e = doc.createElement('remote')
137 root.appendChild(e)
138 e.setAttribute('name', r.name)
139 e.setAttribute('fetch', r.fetchUrl)
140 if r.reviewUrl is not None:
141 e.setAttribute('review', r.reviewUrl)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800142
Brian Harring14a66742012-09-28 20:21:57 -0700143 def Save(self, fd, peg_rev=False, peg_rev_upstream=True):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800144 """Write the current manifest out to the given file descriptor.
145 """
Colin Cross5acde752012-03-28 20:15:45 -0700146 mp = self.manifestProject
147
148 groups = mp.config.GetString('manifest.groups')
Colin Crossc39864f2012-04-23 13:41:58 -0700149 if not groups:
Conley Owensbb1b5f52012-08-13 13:11:18 -0700150 groups = 'all'
Conley Owens971de8e2012-04-16 10:36:08 -0700151 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Colin Cross5acde752012-03-28 20:15:45 -0700152
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800153 doc = xml.dom.minidom.Document()
154 root = doc.createElement('manifest')
155 doc.appendChild(root)
156
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700157 # Save out the notice. There's a little bit of work here to give it the
158 # right whitespace, which assumes that the notice is automatically indented
159 # by 4 by minidom.
160 if self.notice:
161 notice_element = root.appendChild(doc.createElement('notice'))
162 notice_lines = self.notice.splitlines()
163 indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:]
164 notice_element.appendChild(doc.createTextNode(indented_notice))
165
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800166 d = self.default
167 sort_remotes = list(self.remotes.keys())
168 sort_remotes.sort()
169
170 for r in sort_remotes:
171 self._RemoteToXml(self.remotes[r], doc, root)
172 if self.remotes:
173 root.appendChild(doc.createTextNode(''))
174
175 have_default = False
176 e = doc.createElement('default')
177 if d.remote:
178 have_default = True
179 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700180 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800181 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700182 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700183 if d.sync_j > 1:
184 have_default = True
185 e.setAttribute('sync-j', '%d' % d.sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700186 if d.sync_c:
187 have_default = True
188 e.setAttribute('sync-c', 'true')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800189 if d.sync_s:
190 have_default = True
191 e.setAttribute('sync-s', 'true')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800192 if have_default:
193 root.appendChild(e)
194 root.appendChild(doc.createTextNode(''))
195
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700196 if self._manifest_server:
197 e = doc.createElement('manifest-server')
198 e.setAttribute('url', self._manifest_server)
199 root.appendChild(e)
200 root.appendChild(doc.createTextNode(''))
201
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800202 def output_projects(parent, parent_node, projects):
203 for p in projects:
204 output_project(parent, parent_node, self.projects[p])
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800205
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800206 def output_project(parent, parent_node, p):
Colin Cross5acde752012-03-28 20:15:45 -0700207 if not p.MatchesGroups(groups):
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800208 return
209
210 name = p.name
211 relpath = p.relpath
212 if parent:
213 name = self._UnjoinName(parent.name, name)
214 relpath = self._UnjoinRelpath(parent.relpath, relpath)
Colin Cross5acde752012-03-28 20:15:45 -0700215
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800216 e = doc.createElement('project')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800217 parent_node.appendChild(e)
218 e.setAttribute('name', name)
219 if relpath != name:
220 e.setAttribute('path', relpath)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800221 if not d.remote or p.remote.name != d.remote.name:
222 e.setAttribute('remote', p.remote.name)
223 if peg_rev:
224 if self.IsMirror:
Brian Harring14a66742012-09-28 20:21:57 -0700225 value = p.bare_git.rev_parse(p.revisionExpr + '^0')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800226 else:
Brian Harring14a66742012-09-28 20:21:57 -0700227 value = p.work_git.rev_parse(HEAD + '^0')
228 e.setAttribute('revision', value)
229 if peg_rev_upstream and value != p.revisionExpr:
230 # Only save the origin if the origin is not a sha1, and the default
231 # isn't our value, and the if the default doesn't already have that
232 # covered.
233 e.setAttribute('upstream', p.revisionExpr)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700234 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
235 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800236
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800237 for c in p.copyfiles:
238 ce = doc.createElement('copyfile')
239 ce.setAttribute('src', c.src)
240 ce.setAttribute('dest', c.dest)
241 e.appendChild(ce)
242
Conley Owensbb1b5f52012-08-13 13:11:18 -0700243 default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath]
Dmitry Fink17f85ea2012-08-06 14:52:29 -0700244 egroups = [g for g in p.groups if g not in default_groups]
Conley Owens971de8e2012-04-16 10:36:08 -0700245 if egroups:
246 e.setAttribute('groups', ','.join(egroups))
Colin Cross5acde752012-03-28 20:15:45 -0700247
James W. Mills24c13082012-04-12 15:04:13 -0500248 for a in p.annotations:
249 if a.keep == "true":
250 ae = doc.createElement('annotation')
251 ae.setAttribute('name', a.name)
252 ae.setAttribute('value', a.value)
253 e.appendChild(ae)
254
Anatol Pomazau79770d22012-04-20 14:41:59 -0700255 if p.sync_c:
256 e.setAttribute('sync-c', 'true')
257
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800258 if p.sync_s:
259 e.setAttribute('sync-s', 'true')
260
261 if p.subprojects:
262 sort_projects = [subp.name for subp in p.subprojects]
263 sort_projects.sort()
264 output_projects(p, e, sort_projects)
265
266 sort_projects = [key for key in self.projects.keys()
267 if not self.projects[key].parent]
268 sort_projects.sort()
269 output_projects(None, root, sort_projects)
270
Doug Anderson37282b42011-03-04 11:54:18 -0800271 if self._repo_hooks_project:
272 root.appendChild(doc.createTextNode(''))
273 e = doc.createElement('repo-hooks')
274 e.setAttribute('in-project', self._repo_hooks_project.name)
275 e.setAttribute('enabled-list',
276 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
277 root.appendChild(e)
278
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800279 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
280
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700281 @property
282 def projects(self):
283 self._Load()
284 return self._projects
285
286 @property
287 def remotes(self):
288 self._Load()
289 return self._remotes
290
291 @property
292 def default(self):
293 self._Load()
294 return self._default
295
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800296 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800297 def repo_hooks_project(self):
298 self._Load()
299 return self._repo_hooks_project
300
301 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700302 def notice(self):
303 self._Load()
304 return self._notice
305
306 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700307 def manifest_server(self):
308 self._Load()
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800309 return self._manifest_server
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700310
311 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800312 def IsMirror(self):
313 return self.manifestProject.config.GetBoolean('repo.mirror')
314
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700315 def _Unload(self):
316 self._loaded = False
317 self._projects = {}
318 self._remotes = {}
319 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800320 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700321 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700322 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700323 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700324
325 def _Load(self):
326 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800327 m = self.manifestProject
328 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700329 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800330 b = b[len(R_HEADS):]
331 self.branch = b
332
Colin Cross23acdd32012-04-21 00:33:54 -0700333 nodes = []
Brian Harring475a47d2012-06-07 20:05:35 -0700334 nodes.append(self._ParseManifestXml(self.manifestFile,
335 self.manifestProject.worktree))
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700336
337 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
338 if os.path.exists(local):
Sarah Owenscecd1d82012-11-01 22:59:27 -0700339 print('warning: %s is deprecated; put local manifests in %s instead'
340 % (LOCAL_MANIFEST_NAME, 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'):
348 try:
349 nodes.append(self._ParseManifestXml(local_file, self.repodir))
350 except ManifestParseError as e:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700351 print('%s' % str(e), file=sys.stderr)
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900352 except OSError:
353 pass
354
Colin Cross23acdd32012-04-21 00:33:54 -0700355 self._ParseManifest(nodes)
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700356
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800357 if self.IsMirror:
358 self._AddMetaProjectMirror(self.repoProject)
359 self._AddMetaProjectMirror(self.manifestProject)
360
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700361 self._loaded = True
362
Brian Harring475a47d2012-06-07 20:05:35 -0700363 def _ParseManifestXml(self, path, include_root):
David Pursehousef7fc8a92012-11-13 04:00:28 +0900364 try:
365 root = xml.dom.minidom.parse(path)
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900366 except (OSError, xml.parsers.expat.ExpatError) as e:
David Pursehousef7fc8a92012-11-13 04:00:28 +0900367 raise ManifestParseError("error parsing manifest %s: %s" % (path, e))
368
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700369 if not root or not root.childNodes:
Brian Harring26448742011-04-28 05:04:41 -0700370 raise ManifestParseError("no root node in %s" % (path,))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700371
Jooncheol Park34acdd22012-08-27 02:25:59 +0900372 for manifest in root.childNodes:
373 if manifest.nodeName == 'manifest':
374 break
375 else:
Brian Harring26448742011-04-28 05:04:41 -0700376 raise ManifestParseError("no <manifest> in %s" % (path,))
377
Colin Cross23acdd32012-04-21 00:33:54 -0700378 nodes = []
David Pursehouse4f7bdea2012-10-22 12:50:15 +0900379 for node in manifest.childNodes: # pylint:disable=W0631
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900380 # We only get here if manifest is initialised
David Pursehousec1b86a22012-11-14 11:36:51 +0900381 if node.nodeName == 'include':
382 name = self._reqatt(node, 'name')
383 fp = os.path.join(include_root, name)
384 if not os.path.isfile(fp):
385 raise ManifestParseError, \
386 "include %s doesn't exist or isn't a file" % \
387 (name,)
388 try:
389 nodes.extend(self._ParseManifestXml(fp, include_root))
390 # should isolate this to the exact exception, but that's
391 # tricky. actual parsing implementation may vary.
392 except (KeyboardInterrupt, RuntimeError, SystemExit):
393 raise
394 except Exception as e:
395 raise ManifestParseError(
396 "failed parsing included manifest %s: %s", (name, e))
397 else:
398 nodes.append(node)
Colin Cross23acdd32012-04-21 00:33:54 -0700399 return nodes
Brian Harring26448742011-04-28 05:04:41 -0700400
Colin Cross23acdd32012-04-21 00:33:54 -0700401 def _ParseManifest(self, node_list):
402 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700403 if node.nodeName == 'remote':
404 remote = self._ParseRemote(node)
David Pursehouse717ece92012-11-13 08:49:16 +0900405 if remote:
406 if remote.name in self._remotes:
407 if remote != self._remotes[remote.name]:
408 raise ManifestParseError(
409 'remote %s already exists with different attributes' %
410 (remote.name))
411 else:
412 self._remotes[remote.name] = remote
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700413
Colin Cross23acdd32012-04-21 00:33:54 -0700414 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700415 if node.nodeName == 'default':
416 if self._default is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800417 raise ManifestParseError(
418 'duplicate default in %s' %
419 (self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700420 self._default = self._ParseDefault(node)
421 if self._default is None:
422 self._default = _Default()
423
Colin Cross23acdd32012-04-21 00:33:54 -0700424 for node in itertools.chain(*node_list):
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700425 if node.nodeName == 'notice':
426 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800427 raise ManifestParseError(
428 'duplicate notice in %s' %
429 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700430 self._notice = self._ParseNotice(node)
431
Colin Cross23acdd32012-04-21 00:33:54 -0700432 for node in itertools.chain(*node_list):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700433 if node.nodeName == 'manifest-server':
434 url = self._reqatt(node, 'url')
435 if self._manifest_server is not None:
David Pursehousec1b86a22012-11-14 11:36:51 +0900436 raise ManifestParseError(
437 'duplicate manifest-server in %s' %
438 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700439 self._manifest_server = url
440
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800441 def recursively_add_projects(project):
442 if self._projects.get(project.name):
443 raise ManifestParseError(
444 'duplicate project %s in %s' %
445 (project.name, self.manifestFile))
446 self._projects[project.name] = project
447 for subproject in project.subprojects:
448 recursively_add_projects(subproject)
449
Colin Cross23acdd32012-04-21 00:33:54 -0700450 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700451 if node.nodeName == 'project':
452 project = self._ParseProject(node)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800453 recursively_add_projects(project)
Doug Anderson37282b42011-03-04 11:54:18 -0800454 if node.nodeName == 'repo-hooks':
455 # Get the name of the project and the (space-separated) list of enabled.
456 repo_hooks_project = self._reqatt(node, 'in-project')
457 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
458
459 # Only one project can be the hooks project
460 if self._repo_hooks_project is not None:
461 raise ManifestParseError(
462 'duplicate repo-hooks in %s' %
463 (self.manifestFile))
464
465 # Store a reference to the Project.
466 try:
467 self._repo_hooks_project = self._projects[repo_hooks_project]
468 except KeyError:
469 raise ManifestParseError(
470 'project %s not found for repo-hooks' %
471 (repo_hooks_project))
472
473 # Store the enabled hooks in the Project object.
474 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
Colin Cross23acdd32012-04-21 00:33:54 -0700475 if node.nodeName == 'remove-project':
476 name = self._reqatt(node, 'name')
477 try:
478 del self._projects[name]
479 except KeyError:
David Pursehousef9107482012-11-16 19:12:32 +0900480 raise ManifestParseError('remove-project element specifies non-existent '
481 'project: %s' % name)
Colin Cross23acdd32012-04-21 00:33:54 -0700482
483 # If the manifest removes the hooks project, treat it as if it deleted
484 # the repo-hooks element too.
485 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
486 self._repo_hooks_project = None
487
Doug Anderson37282b42011-03-04 11:54:18 -0800488
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800489 def _AddMetaProjectMirror(self, m):
490 name = None
491 m_url = m.GetRemote(m.remote.name).url
492 if m_url.endswith('/.git'):
493 raise ManifestParseError, 'refusing to mirror %s' % m_url
494
495 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700496 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800497 if not url.endswith('/'):
498 url += '/'
499 if m_url.startswith(url):
500 remote = self._default.remote
501 name = m_url[len(url):]
502
503 if name is None:
504 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700505 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Shawn O. Pearcef35b2d92012-08-02 11:46:22 -0700506 remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800507 name = m_url[s:]
508
509 if name.endswith('.git'):
510 name = name[:-4]
511
512 if name not in self._projects:
513 m.PreSync()
514 gitdir = os.path.join(self.topdir, '%s.git' % name)
515 project = Project(manifest = self,
516 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700517 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800518 gitdir = gitdir,
519 worktree = None,
520 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700521 revisionExpr = m.revisionExpr,
522 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800523 self._projects[project.name] = project
524
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700525 def _ParseRemote(self, node):
526 """
527 reads a <remote> element from the manifest file
528 """
529 name = self._reqatt(node, 'name')
Yestin Sunb292b982012-07-02 07:32:50 -0700530 alias = node.getAttribute('alias')
531 if alias == '':
532 alias = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700533 fetch = self._reqatt(node, 'fetch')
534 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800535 if review == '':
536 review = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700537 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Yestin Sunb292b982012-07-02 07:32:50 -0700538 return _XmlRemote(name, alias, fetch, manifestUrl, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700539
540 def _ParseDefault(self, node):
541 """
542 reads a <default> element from the manifest file
543 """
544 d = _Default()
545 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700546 d.revisionExpr = node.getAttribute('revision')
547 if d.revisionExpr == '':
548 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700549
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700550 sync_j = node.getAttribute('sync-j')
551 if sync_j == '' or sync_j is None:
552 d.sync_j = 1
553 else:
554 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700555
556 sync_c = node.getAttribute('sync-c')
557 if not sync_c:
558 d.sync_c = False
559 else:
560 d.sync_c = sync_c.lower() in ("yes", "true", "1")
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800561
562 sync_s = node.getAttribute('sync-s')
563 if not sync_s:
564 d.sync_s = False
565 else:
566 d.sync_s = sync_s.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700567 return d
568
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700569 def _ParseNotice(self, node):
570 """
571 reads a <notice> element from the manifest file
572
573 The <notice> element is distinct from other tags in the XML in that the
574 data is conveyed between the start and end tag (it's not an empty-element
575 tag).
576
577 The white space (carriage returns, indentation) for the notice element is
578 relevant and is parsed in a way that is based on how python docstrings work.
579 In fact, the code is remarkably similar to here:
580 http://www.python.org/dev/peps/pep-0257/
581 """
582 # Get the data out of the node...
583 notice = node.childNodes[0].data
584
585 # Figure out minimum indentation, skipping the first line (the same line
586 # as the <notice> tag)...
587 minIndent = sys.maxint
588 lines = notice.splitlines()
589 for line in lines[1:]:
590 lstrippedLine = line.lstrip()
591 if lstrippedLine:
592 indent = len(line) - len(lstrippedLine)
593 minIndent = min(indent, minIndent)
594
595 # Strip leading / trailing blank lines and also indentation.
596 cleanLines = [lines[0].strip()]
597 for line in lines[1:]:
598 cleanLines.append(line[minIndent:].rstrip())
599
600 # Clear completely blank lines from front and back...
601 while cleanLines and not cleanLines[0]:
602 del cleanLines[0]
603 while cleanLines and not cleanLines[-1]:
604 del cleanLines[-1]
605
606 return '\n'.join(cleanLines)
607
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800608 def _JoinName(self, parent_name, name):
609 return os.path.join(parent_name, name)
610
611 def _UnjoinName(self, parent_name, name):
612 return os.path.relpath(name, parent_name)
613
614 def _ParseProject(self, node, parent = None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700615 """
616 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700617 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700618 name = self._reqatt(node, 'name')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800619 if parent:
620 name = self._JoinName(parent.name, name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700621
622 remote = self._get_remote(node)
623 if remote is None:
624 remote = self._default.remote
625 if remote is None:
626 raise ManifestParseError, \
627 "no remote for project %s within %s" % \
628 (name, self.manifestFile)
629
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700630 revisionExpr = node.getAttribute('revision')
631 if not revisionExpr:
632 revisionExpr = self._default.revisionExpr
633 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700634 raise ManifestParseError, \
635 "no revision for project %s within %s" % \
636 (name, self.manifestFile)
637
638 path = node.getAttribute('path')
639 if not path:
640 path = name
641 if path.startswith('/'):
642 raise ManifestParseError, \
643 "project %s path cannot be absolute in %s" % \
644 (name, self.manifestFile)
645
Mike Pontillod3153822012-02-28 11:53:24 -0800646 rebase = node.getAttribute('rebase')
647 if not rebase:
648 rebase = True
649 else:
650 rebase = rebase.lower() in ("yes", "true", "1")
651
Anatol Pomazau79770d22012-04-20 14:41:59 -0700652 sync_c = node.getAttribute('sync-c')
653 if not sync_c:
654 sync_c = False
655 else:
656 sync_c = sync_c.lower() in ("yes", "true", "1")
657
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800658 sync_s = node.getAttribute('sync-s')
659 if not sync_s:
660 sync_s = self._default.sync_s
661 else:
662 sync_s = sync_s.lower() in ("yes", "true", "1")
663
Brian Harring14a66742012-09-28 20:21:57 -0700664 upstream = node.getAttribute('upstream')
665
Conley Owens971de8e2012-04-16 10:36:08 -0700666 groups = ''
667 if node.hasAttribute('groups'):
668 groups = node.getAttribute('groups')
David Pursehouse1d947b32012-10-25 12:23:11 +0900669 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Brian Harring7da13142012-06-15 02:24:20 -0700670
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800671 if parent is None:
672 relpath, worktree, gitdir = self.GetProjectPaths(name, path)
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700673 else:
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800674 relpath, worktree, gitdir = self.GetSubprojectPaths(parent, path)
675
676 default_groups = ['all', 'name:%s' % name, 'path:%s' % relpath]
677 groups.extend(set(default_groups).difference(groups))
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700678
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700679 project = Project(manifest = self,
680 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700681 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700682 gitdir = gitdir,
683 worktree = worktree,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800684 relpath = relpath,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700685 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800686 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700687 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700688 groups = groups,
Brian Harring14a66742012-09-28 20:21:57 -0700689 sync_c = sync_c,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800690 sync_s = sync_s,
691 upstream = upstream,
692 parent = parent)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700693
694 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700695 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700696 self._ParseCopyFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500697 if n.nodeName == 'annotation':
698 self._ParseAnnotation(project, n)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800699 if n.nodeName == 'project':
700 project.subprojects.append(self._ParseProject(n, parent = project))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700701
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700702 return project
703
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800704 def GetProjectPaths(self, name, path):
705 relpath = path
706 if self.IsMirror:
707 worktree = None
708 gitdir = os.path.join(self.topdir, '%s.git' % name)
709 else:
710 worktree = os.path.join(self.topdir, path).replace('\\', '/')
711 gitdir = os.path.join(self.repodir, 'projects', '%s.git' % path)
712 return relpath, worktree, gitdir
713
714 def GetSubprojectName(self, parent, submodule_path):
715 return os.path.join(parent.name, submodule_path)
716
717 def _JoinRelpath(self, parent_relpath, relpath):
718 return os.path.join(parent_relpath, relpath)
719
720 def _UnjoinRelpath(self, parent_relpath, relpath):
721 return os.path.relpath(relpath, parent_relpath)
722
723 def GetSubprojectPaths(self, parent, path):
724 relpath = self._JoinRelpath(parent.relpath, path)
725 gitdir = os.path.join(parent.gitdir, 'subprojects', '%s.git' % path)
726 if self.IsMirror:
727 worktree = None
728 else:
729 worktree = os.path.join(parent.worktree, path).replace('\\', '/')
730 return relpath, worktree, gitdir
731
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700732 def _ParseCopyFile(self, project, node):
733 src = self._reqatt(node, 'src')
734 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800735 if not self.IsMirror:
736 # src is project relative;
737 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800738 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700739
James W. Mills24c13082012-04-12 15:04:13 -0500740 def _ParseAnnotation(self, project, node):
741 name = self._reqatt(node, 'name')
742 value = self._reqatt(node, 'value')
743 try:
744 keep = self._reqatt(node, 'keep').lower()
745 except ManifestParseError:
746 keep = "true"
747 if keep != "true" and keep != "false":
748 raise ManifestParseError, "optional \"keep\" attribute must be \"true\" or \"false\""
749 project.AddAnnotation(name, value, keep)
750
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700751 def _get_remote(self, node):
752 name = node.getAttribute('remote')
753 if not name:
754 return None
755
756 v = self._remotes.get(name)
757 if not v:
758 raise ManifestParseError, \
759 "remote %s not defined in %s" % \
760 (name, self.manifestFile)
761 return v
762
763 def _reqatt(self, node, attname):
764 """
765 reads a required attribute from the node.
766 """
767 v = node.getAttribute(attname)
768 if not v:
769 raise ManifestParseError, \
770 "no %s in <%s> within %s" % \
771 (attname, node.nodeName, self.manifestFile)
772 return v