blob: 73f6c428d495a0bf0054fd1cfe64eeba7c374068 [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('/')
68 # urljoin will get confused if there is no scheme in the base url
69 # ie, if manifestUrl is of the form <hostname:port>
70 if manifestUrl.find(':') != manifestUrl.find('/') - 1:
David Pursehousec1b86a22012-11-14 11:36:51 +090071 manifestUrl = 'gopher://' + manifestUrl
Conley Owensdb728cd2011-09-26 16:34:01 -070072 url = urlparse.urljoin(manifestUrl, url)
Conley Owensceea3682011-10-20 10:45:47 -070073 return re.sub(r'^gopher://', '', url)
74
75 def ToRemoteSpec(self, projectName):
Conley Owens9d8f9142011-10-20 14:36:35 -070076 url = self.resolvedFetchUrl.rstrip('/') + '/' + projectName
Yestin Sunb292b982012-07-02 07:32:50 -070077 remoteName = self.name
78 if self.remoteAlias:
79 remoteName = self.remoteAlias
80 return RemoteSpec(remoteName, url, self.reviewUrl)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070081
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070082class XmlManifest(object):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070083 """manages the repo configuration file"""
84
85 def __init__(self, repodir):
86 self.repodir = os.path.abspath(repodir)
87 self.topdir = os.path.dirname(self.repodir)
88 self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070089 self.globalConfig = GitConfig.ForUser()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070090
91 self.repoProject = MetaProject(self, 'repo',
92 gitdir = os.path.join(repodir, 'repo/.git'),
93 worktree = os.path.join(repodir, 'repo'))
94
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070095 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -080096 gitdir = os.path.join(repodir, 'manifests.git'),
97 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070098
99 self._Unload()
100
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700101 def Override(self, name):
102 """Use a different manifest, just for the current instantiation.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700103 """
104 path = os.path.join(self.manifestProject.worktree, name)
105 if not os.path.isfile(path):
106 raise ManifestParseError('manifest %s not found' % name)
107
108 old = self.manifestFile
109 try:
110 self.manifestFile = path
111 self._Unload()
112 self._Load()
113 finally:
114 self.manifestFile = old
115
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700116 def Link(self, name):
117 """Update the repo metadata to use a different manifest.
118 """
119 self.Override(name)
120
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700121 try:
122 if os.path.exists(self.manifestFile):
123 os.remove(self.manifestFile)
124 os.symlink('manifests/%s' % name, self.manifestFile)
David Pursehouse8a68ff92012-09-24 12:15:13 +0900125 except OSError:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700126 raise ManifestParseError('cannot link manifest %s' % name)
127
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800128 def _RemoteToXml(self, r, doc, root):
129 e = doc.createElement('remote')
130 root.appendChild(e)
131 e.setAttribute('name', r.name)
132 e.setAttribute('fetch', r.fetchUrl)
133 if r.reviewUrl is not None:
134 e.setAttribute('review', r.reviewUrl)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800135
Brian Harring14a66742012-09-28 20:21:57 -0700136 def Save(self, fd, peg_rev=False, peg_rev_upstream=True):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800137 """Write the current manifest out to the given file descriptor.
138 """
Colin Cross5acde752012-03-28 20:15:45 -0700139 mp = self.manifestProject
140
141 groups = mp.config.GetString('manifest.groups')
Matt Gumbel0c635bb2012-12-21 10:14:53 -0800142 if groups:
143 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Colin Cross5acde752012-03-28 20:15:45 -0700144
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800145 doc = xml.dom.minidom.Document()
146 root = doc.createElement('manifest')
147 doc.appendChild(root)
148
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700149 # Save out the notice. There's a little bit of work here to give it the
150 # right whitespace, which assumes that the notice is automatically indented
151 # by 4 by minidom.
152 if self.notice:
153 notice_element = root.appendChild(doc.createElement('notice'))
154 notice_lines = self.notice.splitlines()
155 indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:]
156 notice_element.appendChild(doc.createTextNode(indented_notice))
157
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800158 d = self.default
159 sort_remotes = list(self.remotes.keys())
160 sort_remotes.sort()
161
162 for r in sort_remotes:
163 self._RemoteToXml(self.remotes[r], doc, root)
164 if self.remotes:
165 root.appendChild(doc.createTextNode(''))
166
167 have_default = False
168 e = doc.createElement('default')
169 if d.remote:
170 have_default = True
171 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700172 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800173 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700174 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700175 if d.sync_j > 1:
176 have_default = True
177 e.setAttribute('sync-j', '%d' % d.sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700178 if d.sync_c:
179 have_default = True
180 e.setAttribute('sync-c', 'true')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800181 if d.sync_s:
182 have_default = True
183 e.setAttribute('sync-s', 'true')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800184 if have_default:
185 root.appendChild(e)
186 root.appendChild(doc.createTextNode(''))
187
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700188 if self._manifest_server:
189 e = doc.createElement('manifest-server')
190 e.setAttribute('url', self._manifest_server)
191 root.appendChild(e)
192 root.appendChild(doc.createTextNode(''))
193
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800194 def output_projects(parent, parent_node, projects):
195 for p in projects:
196 output_project(parent, parent_node, self.projects[p])
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800197
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800198 def output_project(parent, parent_node, p):
Colin Cross5acde752012-03-28 20:15:45 -0700199 if not p.MatchesGroups(groups):
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800200 return
201
202 name = p.name
203 relpath = p.relpath
204 if parent:
205 name = self._UnjoinName(parent.name, name)
206 relpath = self._UnjoinRelpath(parent.relpath, relpath)
Colin Cross5acde752012-03-28 20:15:45 -0700207
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800208 e = doc.createElement('project')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800209 parent_node.appendChild(e)
210 e.setAttribute('name', name)
211 if relpath != name:
212 e.setAttribute('path', relpath)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800213 if not d.remote or p.remote.name != d.remote.name:
214 e.setAttribute('remote', p.remote.name)
215 if peg_rev:
216 if self.IsMirror:
Brian Harring14a66742012-09-28 20:21:57 -0700217 value = p.bare_git.rev_parse(p.revisionExpr + '^0')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800218 else:
Brian Harring14a66742012-09-28 20:21:57 -0700219 value = p.work_git.rev_parse(HEAD + '^0')
220 e.setAttribute('revision', value)
221 if peg_rev_upstream and value != p.revisionExpr:
222 # Only save the origin if the origin is not a sha1, and the default
223 # isn't our value, and the if the default doesn't already have that
224 # covered.
225 e.setAttribute('upstream', p.revisionExpr)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700226 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
227 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800228
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800229 for c in p.copyfiles:
230 ce = doc.createElement('copyfile')
231 ce.setAttribute('src', c.src)
232 ce.setAttribute('dest', c.dest)
233 e.appendChild(ce)
234
Conley Owensbb1b5f52012-08-13 13:11:18 -0700235 default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath]
Dmitry Fink17f85ea2012-08-06 14:52:29 -0700236 egroups = [g for g in p.groups if g not in default_groups]
Conley Owens971de8e2012-04-16 10:36:08 -0700237 if egroups:
238 e.setAttribute('groups', ','.join(egroups))
Colin Cross5acde752012-03-28 20:15:45 -0700239
James W. Mills24c13082012-04-12 15:04:13 -0500240 for a in p.annotations:
241 if a.keep == "true":
242 ae = doc.createElement('annotation')
243 ae.setAttribute('name', a.name)
244 ae.setAttribute('value', a.value)
245 e.appendChild(ae)
246
Anatol Pomazau79770d22012-04-20 14:41:59 -0700247 if p.sync_c:
248 e.setAttribute('sync-c', 'true')
249
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800250 if p.sync_s:
251 e.setAttribute('sync-s', 'true')
252
253 if p.subprojects:
254 sort_projects = [subp.name for subp in p.subprojects]
255 sort_projects.sort()
256 output_projects(p, e, sort_projects)
257
258 sort_projects = [key for key in self.projects.keys()
259 if not self.projects[key].parent]
260 sort_projects.sort()
261 output_projects(None, root, sort_projects)
262
Doug Anderson37282b42011-03-04 11:54:18 -0800263 if self._repo_hooks_project:
264 root.appendChild(doc.createTextNode(''))
265 e = doc.createElement('repo-hooks')
266 e.setAttribute('in-project', self._repo_hooks_project.name)
267 e.setAttribute('enabled-list',
268 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
269 root.appendChild(e)
270
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800271 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
272
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700273 @property
274 def projects(self):
275 self._Load()
276 return self._projects
277
278 @property
279 def remotes(self):
280 self._Load()
281 return self._remotes
282
283 @property
284 def default(self):
285 self._Load()
286 return self._default
287
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800288 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800289 def repo_hooks_project(self):
290 self._Load()
291 return self._repo_hooks_project
292
293 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700294 def notice(self):
295 self._Load()
296 return self._notice
297
298 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700299 def manifest_server(self):
300 self._Load()
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800301 return self._manifest_server
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700302
303 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800304 def IsMirror(self):
305 return self.manifestProject.config.GetBoolean('repo.mirror')
306
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700307 def _Unload(self):
308 self._loaded = False
309 self._projects = {}
310 self._remotes = {}
311 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800312 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700313 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700314 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700315 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700316
317 def _Load(self):
318 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800319 m = self.manifestProject
320 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700321 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800322 b = b[len(R_HEADS):]
323 self.branch = b
324
Colin Cross23acdd32012-04-21 00:33:54 -0700325 nodes = []
Brian Harring475a47d2012-06-07 20:05:35 -0700326 nodes.append(self._ParseManifestXml(self.manifestFile,
327 self.manifestProject.worktree))
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700328
329 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
330 if os.path.exists(local):
Sarah Owenscecd1d82012-11-01 22:59:27 -0700331 print('warning: %s is deprecated; put local manifests in %s instead'
332 % (LOCAL_MANIFEST_NAME, LOCAL_MANIFESTS_DIR_NAME),
333 file=sys.stderr)
Brian Harring475a47d2012-06-07 20:05:35 -0700334 nodes.append(self._ParseManifestXml(local, self.repodir))
Colin Cross23acdd32012-04-21 00:33:54 -0700335
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900336 local_dir = os.path.abspath(os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME))
337 try:
David Pursehouse52f1e5d2012-11-14 04:53:24 +0900338 for local_file in sorted(os.listdir(local_dir)):
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900339 if local_file.endswith('.xml'):
340 try:
341 nodes.append(self._ParseManifestXml(local_file, self.repodir))
342 except ManifestParseError as e:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700343 print('%s' % str(e), file=sys.stderr)
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900344 except OSError:
345 pass
346
Colin Cross23acdd32012-04-21 00:33:54 -0700347 self._ParseManifest(nodes)
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700348
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800349 if self.IsMirror:
350 self._AddMetaProjectMirror(self.repoProject)
351 self._AddMetaProjectMirror(self.manifestProject)
352
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700353 self._loaded = True
354
Brian Harring475a47d2012-06-07 20:05:35 -0700355 def _ParseManifestXml(self, path, include_root):
David Pursehousef7fc8a92012-11-13 04:00:28 +0900356 try:
357 root = xml.dom.minidom.parse(path)
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900358 except (OSError, xml.parsers.expat.ExpatError) as e:
David Pursehousef7fc8a92012-11-13 04:00:28 +0900359 raise ManifestParseError("error parsing manifest %s: %s" % (path, e))
360
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700361 if not root or not root.childNodes:
Brian Harring26448742011-04-28 05:04:41 -0700362 raise ManifestParseError("no root node in %s" % (path,))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700363
Jooncheol Park34acdd22012-08-27 02:25:59 +0900364 for manifest in root.childNodes:
365 if manifest.nodeName == 'manifest':
366 break
367 else:
Brian Harring26448742011-04-28 05:04:41 -0700368 raise ManifestParseError("no <manifest> in %s" % (path,))
369
Colin Cross23acdd32012-04-21 00:33:54 -0700370 nodes = []
David Pursehouse4f7bdea2012-10-22 12:50:15 +0900371 for node in manifest.childNodes: # pylint:disable=W0631
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900372 # We only get here if manifest is initialised
David Pursehousec1b86a22012-11-14 11:36:51 +0900373 if node.nodeName == 'include':
374 name = self._reqatt(node, 'name')
375 fp = os.path.join(include_root, name)
376 if not os.path.isfile(fp):
377 raise ManifestParseError, \
378 "include %s doesn't exist or isn't a file" % \
379 (name,)
380 try:
381 nodes.extend(self._ParseManifestXml(fp, include_root))
382 # should isolate this to the exact exception, but that's
383 # tricky. actual parsing implementation may vary.
384 except (KeyboardInterrupt, RuntimeError, SystemExit):
385 raise
386 except Exception as e:
387 raise ManifestParseError(
388 "failed parsing included manifest %s: %s", (name, e))
389 else:
390 nodes.append(node)
Colin Cross23acdd32012-04-21 00:33:54 -0700391 return nodes
Brian Harring26448742011-04-28 05:04:41 -0700392
Colin Cross23acdd32012-04-21 00:33:54 -0700393 def _ParseManifest(self, node_list):
394 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700395 if node.nodeName == 'remote':
396 remote = self._ParseRemote(node)
David Pursehouse717ece92012-11-13 08:49:16 +0900397 if remote:
398 if remote.name in self._remotes:
399 if remote != self._remotes[remote.name]:
400 raise ManifestParseError(
401 'remote %s already exists with different attributes' %
402 (remote.name))
403 else:
404 self._remotes[remote.name] = remote
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700405
Colin Cross23acdd32012-04-21 00:33:54 -0700406 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700407 if node.nodeName == 'default':
408 if self._default is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800409 raise ManifestParseError(
410 'duplicate default in %s' %
411 (self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700412 self._default = self._ParseDefault(node)
413 if self._default is None:
414 self._default = _Default()
415
Colin Cross23acdd32012-04-21 00:33:54 -0700416 for node in itertools.chain(*node_list):
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700417 if node.nodeName == 'notice':
418 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800419 raise ManifestParseError(
420 'duplicate notice in %s' %
421 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700422 self._notice = self._ParseNotice(node)
423
Colin Cross23acdd32012-04-21 00:33:54 -0700424 for node in itertools.chain(*node_list):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700425 if node.nodeName == 'manifest-server':
426 url = self._reqatt(node, 'url')
427 if self._manifest_server is not None:
David Pursehousec1b86a22012-11-14 11:36:51 +0900428 raise ManifestParseError(
429 'duplicate manifest-server in %s' %
430 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700431 self._manifest_server = url
432
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800433 def recursively_add_projects(project):
434 if self._projects.get(project.name):
435 raise ManifestParseError(
436 'duplicate project %s in %s' %
437 (project.name, self.manifestFile))
438 self._projects[project.name] = project
439 for subproject in project.subprojects:
440 recursively_add_projects(subproject)
441
Colin Cross23acdd32012-04-21 00:33:54 -0700442 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700443 if node.nodeName == 'project':
444 project = self._ParseProject(node)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800445 recursively_add_projects(project)
Doug Anderson37282b42011-03-04 11:54:18 -0800446 if node.nodeName == 'repo-hooks':
447 # Get the name of the project and the (space-separated) list of enabled.
448 repo_hooks_project = self._reqatt(node, 'in-project')
449 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
450
451 # Only one project can be the hooks project
452 if self._repo_hooks_project is not None:
453 raise ManifestParseError(
454 'duplicate repo-hooks in %s' %
455 (self.manifestFile))
456
457 # Store a reference to the Project.
458 try:
459 self._repo_hooks_project = self._projects[repo_hooks_project]
460 except KeyError:
461 raise ManifestParseError(
462 'project %s not found for repo-hooks' %
463 (repo_hooks_project))
464
465 # Store the enabled hooks in the Project object.
466 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
Colin Cross23acdd32012-04-21 00:33:54 -0700467 if node.nodeName == 'remove-project':
468 name = self._reqatt(node, 'name')
469 try:
470 del self._projects[name]
471 except KeyError:
David Pursehousef9107482012-11-16 19:12:32 +0900472 raise ManifestParseError('remove-project element specifies non-existent '
473 'project: %s' % name)
Colin Cross23acdd32012-04-21 00:33:54 -0700474
475 # If the manifest removes the hooks project, treat it as if it deleted
476 # the repo-hooks element too.
477 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
478 self._repo_hooks_project = None
479
Doug Anderson37282b42011-03-04 11:54:18 -0800480
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800481 def _AddMetaProjectMirror(self, m):
482 name = None
483 m_url = m.GetRemote(m.remote.name).url
484 if m_url.endswith('/.git'):
485 raise ManifestParseError, 'refusing to mirror %s' % m_url
486
487 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700488 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800489 if not url.endswith('/'):
490 url += '/'
491 if m_url.startswith(url):
492 remote = self._default.remote
493 name = m_url[len(url):]
494
495 if name is None:
496 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700497 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Shawn O. Pearcef35b2d92012-08-02 11:46:22 -0700498 remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800499 name = m_url[s:]
500
501 if name.endswith('.git'):
502 name = name[:-4]
503
504 if name not in self._projects:
505 m.PreSync()
506 gitdir = os.path.join(self.topdir, '%s.git' % name)
507 project = Project(manifest = self,
508 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700509 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800510 gitdir = gitdir,
511 worktree = None,
512 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700513 revisionExpr = m.revisionExpr,
514 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800515 self._projects[project.name] = project
516
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700517 def _ParseRemote(self, node):
518 """
519 reads a <remote> element from the manifest file
520 """
521 name = self._reqatt(node, 'name')
Yestin Sunb292b982012-07-02 07:32:50 -0700522 alias = node.getAttribute('alias')
523 if alias == '':
524 alias = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700525 fetch = self._reqatt(node, 'fetch')
526 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800527 if review == '':
528 review = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700529 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Yestin Sunb292b982012-07-02 07:32:50 -0700530 return _XmlRemote(name, alias, fetch, manifestUrl, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700531
532 def _ParseDefault(self, node):
533 """
534 reads a <default> element from the manifest file
535 """
536 d = _Default()
537 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700538 d.revisionExpr = node.getAttribute('revision')
539 if d.revisionExpr == '':
540 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700541
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700542 sync_j = node.getAttribute('sync-j')
543 if sync_j == '' or sync_j is None:
544 d.sync_j = 1
545 else:
546 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700547
548 sync_c = node.getAttribute('sync-c')
549 if not sync_c:
550 d.sync_c = False
551 else:
552 d.sync_c = sync_c.lower() in ("yes", "true", "1")
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800553
554 sync_s = node.getAttribute('sync-s')
555 if not sync_s:
556 d.sync_s = False
557 else:
558 d.sync_s = sync_s.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700559 return d
560
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700561 def _ParseNotice(self, node):
562 """
563 reads a <notice> element from the manifest file
564
565 The <notice> element is distinct from other tags in the XML in that the
566 data is conveyed between the start and end tag (it's not an empty-element
567 tag).
568
569 The white space (carriage returns, indentation) for the notice element is
570 relevant and is parsed in a way that is based on how python docstrings work.
571 In fact, the code is remarkably similar to here:
572 http://www.python.org/dev/peps/pep-0257/
573 """
574 # Get the data out of the node...
575 notice = node.childNodes[0].data
576
577 # Figure out minimum indentation, skipping the first line (the same line
578 # as the <notice> tag)...
579 minIndent = sys.maxint
580 lines = notice.splitlines()
581 for line in lines[1:]:
582 lstrippedLine = line.lstrip()
583 if lstrippedLine:
584 indent = len(line) - len(lstrippedLine)
585 minIndent = min(indent, minIndent)
586
587 # Strip leading / trailing blank lines and also indentation.
588 cleanLines = [lines[0].strip()]
589 for line in lines[1:]:
590 cleanLines.append(line[minIndent:].rstrip())
591
592 # Clear completely blank lines from front and back...
593 while cleanLines and not cleanLines[0]:
594 del cleanLines[0]
595 while cleanLines and not cleanLines[-1]:
596 del cleanLines[-1]
597
598 return '\n'.join(cleanLines)
599
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800600 def _JoinName(self, parent_name, name):
601 return os.path.join(parent_name, name)
602
603 def _UnjoinName(self, parent_name, name):
604 return os.path.relpath(name, parent_name)
605
606 def _ParseProject(self, node, parent = None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700607 """
608 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700609 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700610 name = self._reqatt(node, 'name')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800611 if parent:
612 name = self._JoinName(parent.name, name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700613
614 remote = self._get_remote(node)
615 if remote is None:
616 remote = self._default.remote
617 if remote is None:
618 raise ManifestParseError, \
619 "no remote for project %s within %s" % \
620 (name, self.manifestFile)
621
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700622 revisionExpr = node.getAttribute('revision')
623 if not revisionExpr:
624 revisionExpr = self._default.revisionExpr
625 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700626 raise ManifestParseError, \
627 "no revision for project %s within %s" % \
628 (name, self.manifestFile)
629
630 path = node.getAttribute('path')
631 if not path:
632 path = name
633 if path.startswith('/'):
634 raise ManifestParseError, \
635 "project %s path cannot be absolute in %s" % \
636 (name, self.manifestFile)
637
Mike Pontillod3153822012-02-28 11:53:24 -0800638 rebase = node.getAttribute('rebase')
639 if not rebase:
640 rebase = True
641 else:
642 rebase = rebase.lower() in ("yes", "true", "1")
643
Anatol Pomazau79770d22012-04-20 14:41:59 -0700644 sync_c = node.getAttribute('sync-c')
645 if not sync_c:
646 sync_c = False
647 else:
648 sync_c = sync_c.lower() in ("yes", "true", "1")
649
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800650 sync_s = node.getAttribute('sync-s')
651 if not sync_s:
652 sync_s = self._default.sync_s
653 else:
654 sync_s = sync_s.lower() in ("yes", "true", "1")
655
Brian Harring14a66742012-09-28 20:21:57 -0700656 upstream = node.getAttribute('upstream')
657
Conley Owens971de8e2012-04-16 10:36:08 -0700658 groups = ''
659 if node.hasAttribute('groups'):
660 groups = node.getAttribute('groups')
David Pursehouse1d947b32012-10-25 12:23:11 +0900661 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Brian Harring7da13142012-06-15 02:24:20 -0700662
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800663 if parent is None:
664 relpath, worktree, gitdir = self.GetProjectPaths(name, path)
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700665 else:
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800666 relpath, worktree, gitdir = self.GetSubprojectPaths(parent, path)
667
668 default_groups = ['all', 'name:%s' % name, 'path:%s' % relpath]
669 groups.extend(set(default_groups).difference(groups))
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700670
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700671 project = Project(manifest = self,
672 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700673 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700674 gitdir = gitdir,
675 worktree = worktree,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800676 relpath = relpath,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700677 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800678 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700679 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700680 groups = groups,
Brian Harring14a66742012-09-28 20:21:57 -0700681 sync_c = sync_c,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800682 sync_s = sync_s,
683 upstream = upstream,
684 parent = parent)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700685
686 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700687 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700688 self._ParseCopyFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500689 if n.nodeName == 'annotation':
690 self._ParseAnnotation(project, n)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800691 if n.nodeName == 'project':
692 project.subprojects.append(self._ParseProject(n, parent = project))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700693
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700694 return project
695
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800696 def GetProjectPaths(self, name, path):
697 relpath = path
698 if self.IsMirror:
699 worktree = None
700 gitdir = os.path.join(self.topdir, '%s.git' % name)
701 else:
702 worktree = os.path.join(self.topdir, path).replace('\\', '/')
703 gitdir = os.path.join(self.repodir, 'projects', '%s.git' % path)
704 return relpath, worktree, gitdir
705
706 def GetSubprojectName(self, parent, submodule_path):
707 return os.path.join(parent.name, submodule_path)
708
709 def _JoinRelpath(self, parent_relpath, relpath):
710 return os.path.join(parent_relpath, relpath)
711
712 def _UnjoinRelpath(self, parent_relpath, relpath):
713 return os.path.relpath(relpath, parent_relpath)
714
715 def GetSubprojectPaths(self, parent, path):
716 relpath = self._JoinRelpath(parent.relpath, path)
717 gitdir = os.path.join(parent.gitdir, 'subprojects', '%s.git' % path)
718 if self.IsMirror:
719 worktree = None
720 else:
721 worktree = os.path.join(parent.worktree, path).replace('\\', '/')
722 return relpath, worktree, gitdir
723
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700724 def _ParseCopyFile(self, project, node):
725 src = self._reqatt(node, 'src')
726 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800727 if not self.IsMirror:
728 # src is project relative;
729 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800730 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700731
James W. Mills24c13082012-04-12 15:04:13 -0500732 def _ParseAnnotation(self, project, node):
733 name = self._reqatt(node, 'name')
734 value = self._reqatt(node, 'value')
735 try:
736 keep = self._reqatt(node, 'keep').lower()
737 except ManifestParseError:
738 keep = "true"
739 if keep != "true" and keep != "false":
740 raise ManifestParseError, "optional \"keep\" attribute must be \"true\" or \"false\""
741 project.AddAnnotation(name, value, keep)
742
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700743 def _get_remote(self, node):
744 name = node.getAttribute('remote')
745 if not name:
746 return None
747
748 v = self._remotes.get(name)
749 if not v:
750 raise ManifestParseError, \
751 "remote %s not defined in %s" % \
752 (name, self.manifestFile)
753 return v
754
755 def _reqatt(self, node, attname):
756 """
757 reads a required attribute from the node.
758 """
759 v = node.getAttribute(attname)
760 if not v:
761 raise ManifestParseError, \
762 "no %s in <%s> within %s" % \
763 (attname, node.nodeName, self.manifestFile)
764 return v