blob: 122393cf91c7e9a01e502e59c296c56ee642fdc9 [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
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070043
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070044class _XmlRemote(object):
45 def __init__(self,
46 name,
Yestin Sunb292b982012-07-02 07:32:50 -070047 alias=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070048 fetch=None,
Conley Owensdb728cd2011-09-26 16:34:01 -070049 manifestUrl=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070050 review=None):
51 self.name = name
52 self.fetchUrl = fetch
Conley Owensdb728cd2011-09-26 16:34:01 -070053 self.manifestUrl = manifestUrl
Yestin Sunb292b982012-07-02 07:32:50 -070054 self.remoteAlias = alias
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070055 self.reviewUrl = review
Conley Owensceea3682011-10-20 10:45:47 -070056 self.resolvedFetchUrl = self._resolveFetchUrl()
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070057
David Pursehouse717ece92012-11-13 08:49:16 +090058 def __eq__(self, other):
59 return self.__dict__ == other.__dict__
60
61 def __ne__(self, other):
62 return self.__dict__ != other.__dict__
63
Conley Owensceea3682011-10-20 10:45:47 -070064 def _resolveFetchUrl(self):
65 url = self.fetchUrl.rstrip('/')
Conley Owensdb728cd2011-09-26 16:34:01 -070066 manifestUrl = self.manifestUrl.rstrip('/')
67 # urljoin will get confused if there is no scheme in the base url
68 # ie, if manifestUrl is of the form <hostname:port>
69 if manifestUrl.find(':') != manifestUrl.find('/') - 1:
David Pursehousec1b86a22012-11-14 11:36:51 +090070 manifestUrl = 'gopher://' + manifestUrl
Conley Owensdb728cd2011-09-26 16:34:01 -070071 url = urlparse.urljoin(manifestUrl, url)
Conley Owensceea3682011-10-20 10:45:47 -070072 return re.sub(r'^gopher://', '', url)
73
74 def ToRemoteSpec(self, projectName):
Conley Owens9d8f9142011-10-20 14:36:35 -070075 url = self.resolvedFetchUrl.rstrip('/') + '/' + projectName
Yestin Sunb292b982012-07-02 07:32:50 -070076 remoteName = self.name
77 if self.remoteAlias:
78 remoteName = self.remoteAlias
79 return RemoteSpec(remoteName, url, self.reviewUrl)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070080
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070081class XmlManifest(object):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070082 """manages the repo configuration file"""
83
84 def __init__(self, repodir):
85 self.repodir = os.path.abspath(repodir)
86 self.topdir = os.path.dirname(self.repodir)
87 self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070088 self.globalConfig = GitConfig.ForUser()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070089
90 self.repoProject = MetaProject(self, 'repo',
91 gitdir = os.path.join(repodir, 'repo/.git'),
92 worktree = os.path.join(repodir, 'repo'))
93
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070094 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -080095 gitdir = os.path.join(repodir, 'manifests.git'),
96 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070097
98 self._Unload()
99
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700100 def Override(self, name):
101 """Use a different manifest, just for the current instantiation.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700102 """
103 path = os.path.join(self.manifestProject.worktree, name)
104 if not os.path.isfile(path):
105 raise ManifestParseError('manifest %s not found' % name)
106
107 old = self.manifestFile
108 try:
109 self.manifestFile = path
110 self._Unload()
111 self._Load()
112 finally:
113 self.manifestFile = old
114
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700115 def Link(self, name):
116 """Update the repo metadata to use a different manifest.
117 """
118 self.Override(name)
119
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700120 try:
121 if os.path.exists(self.manifestFile):
122 os.remove(self.manifestFile)
123 os.symlink('manifests/%s' % name, self.manifestFile)
David Pursehouse8a68ff92012-09-24 12:15:13 +0900124 except OSError:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700125 raise ManifestParseError('cannot link manifest %s' % name)
126
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800127 def _RemoteToXml(self, r, doc, root):
128 e = doc.createElement('remote')
129 root.appendChild(e)
130 e.setAttribute('name', r.name)
131 e.setAttribute('fetch', r.fetchUrl)
132 if r.reviewUrl is not None:
133 e.setAttribute('review', r.reviewUrl)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800134
Brian Harring14a66742012-09-28 20:21:57 -0700135 def Save(self, fd, peg_rev=False, peg_rev_upstream=True):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800136 """Write the current manifest out to the given file descriptor.
137 """
Colin Cross5acde752012-03-28 20:15:45 -0700138 mp = self.manifestProject
139
140 groups = mp.config.GetString('manifest.groups')
Colin Crossc39864f2012-04-23 13:41:58 -0700141 if not groups:
Conley Owensbb1b5f52012-08-13 13:11:18 -0700142 groups = 'all'
Conley Owens971de8e2012-04-16 10:36:08 -0700143 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')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800181 if have_default:
182 root.appendChild(e)
183 root.appendChild(doc.createTextNode(''))
184
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700185 if self._manifest_server:
186 e = doc.createElement('manifest-server')
187 e.setAttribute('url', self._manifest_server)
188 root.appendChild(e)
189 root.appendChild(doc.createTextNode(''))
190
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700191 sort_projects = list(self.projects.keys())
192 sort_projects.sort()
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800193
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700194 for p in sort_projects:
195 p = self.projects[p]
196
Colin Cross5acde752012-03-28 20:15:45 -0700197 if not p.MatchesGroups(groups):
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700198 continue
Colin Cross5acde752012-03-28 20:15:45 -0700199
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800200 e = doc.createElement('project')
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700201 root.appendChild(e)
202 e.setAttribute('name', p.name)
203 if p.relpath != p.name:
204 e.setAttribute('path', p.relpath)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800205 if not d.remote or p.remote.name != d.remote.name:
206 e.setAttribute('remote', p.remote.name)
207 if peg_rev:
208 if self.IsMirror:
Brian Harring14a66742012-09-28 20:21:57 -0700209 value = p.bare_git.rev_parse(p.revisionExpr + '^0')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800210 else:
Brian Harring14a66742012-09-28 20:21:57 -0700211 value = p.work_git.rev_parse(HEAD + '^0')
212 e.setAttribute('revision', value)
213 if peg_rev_upstream and value != p.revisionExpr:
214 # Only save the origin if the origin is not a sha1, and the default
215 # isn't our value, and the if the default doesn't already have that
216 # covered.
217 e.setAttribute('upstream', p.revisionExpr)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700218 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
219 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800220
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800221 for c in p.copyfiles:
222 ce = doc.createElement('copyfile')
223 ce.setAttribute('src', c.src)
224 ce.setAttribute('dest', c.dest)
225 e.appendChild(ce)
226
Conley Owensbb1b5f52012-08-13 13:11:18 -0700227 default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath]
Dmitry Fink17f85ea2012-08-06 14:52:29 -0700228 egroups = [g for g in p.groups if g not in default_groups]
Conley Owens971de8e2012-04-16 10:36:08 -0700229 if egroups:
230 e.setAttribute('groups', ','.join(egroups))
Colin Cross5acde752012-03-28 20:15:45 -0700231
James W. Mills24c13082012-04-12 15:04:13 -0500232 for a in p.annotations:
233 if a.keep == "true":
234 ae = doc.createElement('annotation')
235 ae.setAttribute('name', a.name)
236 ae.setAttribute('value', a.value)
237 e.appendChild(ae)
238
Anatol Pomazau79770d22012-04-20 14:41:59 -0700239 if p.sync_c:
240 e.setAttribute('sync-c', 'true')
241
Doug Anderson37282b42011-03-04 11:54:18 -0800242 if self._repo_hooks_project:
243 root.appendChild(doc.createTextNode(''))
244 e = doc.createElement('repo-hooks')
245 e.setAttribute('in-project', self._repo_hooks_project.name)
246 e.setAttribute('enabled-list',
247 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
248 root.appendChild(e)
249
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800250 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
251
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700252 @property
253 def projects(self):
254 self._Load()
255 return self._projects
256
257 @property
258 def remotes(self):
259 self._Load()
260 return self._remotes
261
262 @property
263 def default(self):
264 self._Load()
265 return self._default
266
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800267 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800268 def repo_hooks_project(self):
269 self._Load()
270 return self._repo_hooks_project
271
272 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700273 def notice(self):
274 self._Load()
275 return self._notice
276
277 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700278 def manifest_server(self):
279 self._Load()
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800280 return self._manifest_server
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700281
282 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800283 def IsMirror(self):
284 return self.manifestProject.config.GetBoolean('repo.mirror')
285
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700286 def _Unload(self):
287 self._loaded = False
288 self._projects = {}
289 self._remotes = {}
290 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800291 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700292 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700293 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700294 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700295
296 def _Load(self):
297 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800298 m = self.manifestProject
299 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700300 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800301 b = b[len(R_HEADS):]
302 self.branch = b
303
Colin Cross23acdd32012-04-21 00:33:54 -0700304 nodes = []
Brian Harring475a47d2012-06-07 20:05:35 -0700305 nodes.append(self._ParseManifestXml(self.manifestFile,
306 self.manifestProject.worktree))
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700307
308 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
309 if os.path.exists(local):
Sarah Owenscecd1d82012-11-01 22:59:27 -0700310 print('warning: %s is deprecated; put local manifests in %s instead'
311 % (LOCAL_MANIFEST_NAME, LOCAL_MANIFESTS_DIR_NAME),
312 file=sys.stderr)
Brian Harring475a47d2012-06-07 20:05:35 -0700313 nodes.append(self._ParseManifestXml(local, self.repodir))
Colin Cross23acdd32012-04-21 00:33:54 -0700314
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900315 local_dir = os.path.abspath(os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME))
316 try:
David Pursehouse52f1e5d2012-11-14 04:53:24 +0900317 for local_file in sorted(os.listdir(local_dir)):
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900318 if local_file.endswith('.xml'):
319 try:
320 nodes.append(self._ParseManifestXml(local_file, self.repodir))
321 except ManifestParseError as e:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700322 print('%s' % str(e), file=sys.stderr)
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900323 except OSError:
324 pass
325
Colin Cross23acdd32012-04-21 00:33:54 -0700326 self._ParseManifest(nodes)
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700327
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800328 if self.IsMirror:
329 self._AddMetaProjectMirror(self.repoProject)
330 self._AddMetaProjectMirror(self.manifestProject)
331
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700332 self._loaded = True
333
Brian Harring475a47d2012-06-07 20:05:35 -0700334 def _ParseManifestXml(self, path, include_root):
David Pursehousef7fc8a92012-11-13 04:00:28 +0900335 try:
336 root = xml.dom.minidom.parse(path)
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900337 except (OSError, xml.parsers.expat.ExpatError) as e:
David Pursehousef7fc8a92012-11-13 04:00:28 +0900338 raise ManifestParseError("error parsing manifest %s: %s" % (path, e))
339
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700340 if not root or not root.childNodes:
Brian Harring26448742011-04-28 05:04:41 -0700341 raise ManifestParseError("no root node in %s" % (path,))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700342
Jooncheol Park34acdd22012-08-27 02:25:59 +0900343 for manifest in root.childNodes:
344 if manifest.nodeName == 'manifest':
345 break
346 else:
Brian Harring26448742011-04-28 05:04:41 -0700347 raise ManifestParseError("no <manifest> in %s" % (path,))
348
Colin Cross23acdd32012-04-21 00:33:54 -0700349 nodes = []
David Pursehouse4f7bdea2012-10-22 12:50:15 +0900350 for node in manifest.childNodes: # pylint:disable=W0631
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900351 # We only get here if manifest is initialised
David Pursehousec1b86a22012-11-14 11:36:51 +0900352 if node.nodeName == 'include':
353 name = self._reqatt(node, 'name')
354 fp = os.path.join(include_root, name)
355 if not os.path.isfile(fp):
356 raise ManifestParseError, \
357 "include %s doesn't exist or isn't a file" % \
358 (name,)
359 try:
360 nodes.extend(self._ParseManifestXml(fp, include_root))
361 # should isolate this to the exact exception, but that's
362 # tricky. actual parsing implementation may vary.
363 except (KeyboardInterrupt, RuntimeError, SystemExit):
364 raise
365 except Exception as e:
366 raise ManifestParseError(
367 "failed parsing included manifest %s: %s", (name, e))
368 else:
369 nodes.append(node)
Colin Cross23acdd32012-04-21 00:33:54 -0700370 return nodes
Brian Harring26448742011-04-28 05:04:41 -0700371
Colin Cross23acdd32012-04-21 00:33:54 -0700372 def _ParseManifest(self, node_list):
373 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700374 if node.nodeName == 'remote':
375 remote = self._ParseRemote(node)
David Pursehouse717ece92012-11-13 08:49:16 +0900376 if remote:
377 if remote.name in self._remotes:
378 if remote != self._remotes[remote.name]:
379 raise ManifestParseError(
380 'remote %s already exists with different attributes' %
381 (remote.name))
382 else:
383 self._remotes[remote.name] = remote
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700384
Colin Cross23acdd32012-04-21 00:33:54 -0700385 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700386 if node.nodeName == 'default':
387 if self._default is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800388 raise ManifestParseError(
389 'duplicate default in %s' %
390 (self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700391 self._default = self._ParseDefault(node)
392 if self._default is None:
393 self._default = _Default()
394
Colin Cross23acdd32012-04-21 00:33:54 -0700395 for node in itertools.chain(*node_list):
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700396 if node.nodeName == 'notice':
397 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800398 raise ManifestParseError(
399 'duplicate notice in %s' %
400 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700401 self._notice = self._ParseNotice(node)
402
Colin Cross23acdd32012-04-21 00:33:54 -0700403 for node in itertools.chain(*node_list):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700404 if node.nodeName == 'manifest-server':
405 url = self._reqatt(node, 'url')
406 if self._manifest_server is not None:
David Pursehousec1b86a22012-11-14 11:36:51 +0900407 raise ManifestParseError(
408 'duplicate manifest-server in %s' %
409 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700410 self._manifest_server = url
411
Colin Cross23acdd32012-04-21 00:33:54 -0700412 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700413 if node.nodeName == 'project':
414 project = self._ParseProject(node)
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700415 if self._projects.get(project.name):
416 raise ManifestParseError(
417 'duplicate project %s in %s' %
418 (project.name, self.manifestFile))
419 self._projects[project.name] = project
Doug Anderson37282b42011-03-04 11:54:18 -0800420 if node.nodeName == 'repo-hooks':
421 # Get the name of the project and the (space-separated) list of enabled.
422 repo_hooks_project = self._reqatt(node, 'in-project')
423 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
424
425 # Only one project can be the hooks project
426 if self._repo_hooks_project is not None:
427 raise ManifestParseError(
428 'duplicate repo-hooks in %s' %
429 (self.manifestFile))
430
431 # Store a reference to the Project.
432 try:
433 self._repo_hooks_project = self._projects[repo_hooks_project]
434 except KeyError:
435 raise ManifestParseError(
436 'project %s not found for repo-hooks' %
437 (repo_hooks_project))
438
439 # Store the enabled hooks in the Project object.
440 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
Colin Cross23acdd32012-04-21 00:33:54 -0700441 if node.nodeName == 'remove-project':
442 name = self._reqatt(node, 'name')
443 try:
444 del self._projects[name]
445 except KeyError:
David Pursehousef9107482012-11-16 19:12:32 +0900446 raise ManifestParseError('remove-project element specifies non-existent '
447 'project: %s' % name)
Colin Cross23acdd32012-04-21 00:33:54 -0700448
449 # If the manifest removes the hooks project, treat it as if it deleted
450 # the repo-hooks element too.
451 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
452 self._repo_hooks_project = None
453
Doug Anderson37282b42011-03-04 11:54:18 -0800454
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800455 def _AddMetaProjectMirror(self, m):
456 name = None
457 m_url = m.GetRemote(m.remote.name).url
458 if m_url.endswith('/.git'):
459 raise ManifestParseError, 'refusing to mirror %s' % m_url
460
461 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700462 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800463 if not url.endswith('/'):
464 url += '/'
465 if m_url.startswith(url):
466 remote = self._default.remote
467 name = m_url[len(url):]
468
469 if name is None:
470 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700471 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Shawn O. Pearcef35b2d92012-08-02 11:46:22 -0700472 remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800473 name = m_url[s:]
474
475 if name.endswith('.git'):
476 name = name[:-4]
477
478 if name not in self._projects:
479 m.PreSync()
480 gitdir = os.path.join(self.topdir, '%s.git' % name)
481 project = Project(manifest = self,
482 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700483 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800484 gitdir = gitdir,
485 worktree = None,
486 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700487 revisionExpr = m.revisionExpr,
488 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800489 self._projects[project.name] = project
490
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700491 def _ParseRemote(self, node):
492 """
493 reads a <remote> element from the manifest file
494 """
495 name = self._reqatt(node, 'name')
Yestin Sunb292b982012-07-02 07:32:50 -0700496 alias = node.getAttribute('alias')
497 if alias == '':
498 alias = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700499 fetch = self._reqatt(node, 'fetch')
500 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800501 if review == '':
502 review = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700503 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Yestin Sunb292b982012-07-02 07:32:50 -0700504 return _XmlRemote(name, alias, fetch, manifestUrl, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700505
506 def _ParseDefault(self, node):
507 """
508 reads a <default> element from the manifest file
509 """
510 d = _Default()
511 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700512 d.revisionExpr = node.getAttribute('revision')
513 if d.revisionExpr == '':
514 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700515
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700516 sync_j = node.getAttribute('sync-j')
517 if sync_j == '' or sync_j is None:
518 d.sync_j = 1
519 else:
520 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700521
522 sync_c = node.getAttribute('sync-c')
523 if not sync_c:
524 d.sync_c = False
525 else:
526 d.sync_c = sync_c.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700527 return d
528
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700529 def _ParseNotice(self, node):
530 """
531 reads a <notice> element from the manifest file
532
533 The <notice> element is distinct from other tags in the XML in that the
534 data is conveyed between the start and end tag (it's not an empty-element
535 tag).
536
537 The white space (carriage returns, indentation) for the notice element is
538 relevant and is parsed in a way that is based on how python docstrings work.
539 In fact, the code is remarkably similar to here:
540 http://www.python.org/dev/peps/pep-0257/
541 """
542 # Get the data out of the node...
543 notice = node.childNodes[0].data
544
545 # Figure out minimum indentation, skipping the first line (the same line
546 # as the <notice> tag)...
547 minIndent = sys.maxint
548 lines = notice.splitlines()
549 for line in lines[1:]:
550 lstrippedLine = line.lstrip()
551 if lstrippedLine:
552 indent = len(line) - len(lstrippedLine)
553 minIndent = min(indent, minIndent)
554
555 # Strip leading / trailing blank lines and also indentation.
556 cleanLines = [lines[0].strip()]
557 for line in lines[1:]:
558 cleanLines.append(line[minIndent:].rstrip())
559
560 # Clear completely blank lines from front and back...
561 while cleanLines and not cleanLines[0]:
562 del cleanLines[0]
563 while cleanLines and not cleanLines[-1]:
564 del cleanLines[-1]
565
566 return '\n'.join(cleanLines)
567
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700568 def _ParseProject(self, node):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700569 """
570 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700571 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700572 name = self._reqatt(node, 'name')
573
574 remote = self._get_remote(node)
575 if remote is None:
576 remote = self._default.remote
577 if remote is None:
578 raise ManifestParseError, \
579 "no remote for project %s within %s" % \
580 (name, self.manifestFile)
581
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700582 revisionExpr = node.getAttribute('revision')
583 if not revisionExpr:
584 revisionExpr = self._default.revisionExpr
585 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700586 raise ManifestParseError, \
587 "no revision for project %s within %s" % \
588 (name, self.manifestFile)
589
590 path = node.getAttribute('path')
591 if not path:
592 path = name
593 if path.startswith('/'):
594 raise ManifestParseError, \
595 "project %s path cannot be absolute in %s" % \
596 (name, self.manifestFile)
597
Mike Pontillod3153822012-02-28 11:53:24 -0800598 rebase = node.getAttribute('rebase')
599 if not rebase:
600 rebase = True
601 else:
602 rebase = rebase.lower() in ("yes", "true", "1")
603
Anatol Pomazau79770d22012-04-20 14:41:59 -0700604 sync_c = node.getAttribute('sync-c')
605 if not sync_c:
606 sync_c = False
607 else:
608 sync_c = sync_c.lower() in ("yes", "true", "1")
609
Brian Harring14a66742012-09-28 20:21:57 -0700610 upstream = node.getAttribute('upstream')
611
Conley Owens971de8e2012-04-16 10:36:08 -0700612 groups = ''
613 if node.hasAttribute('groups'):
614 groups = node.getAttribute('groups')
David Pursehouse1d947b32012-10-25 12:23:11 +0900615 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Brian Harring7da13142012-06-15 02:24:20 -0700616
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700617 default_groups = ['all', 'name:%s' % name, 'path:%s' % path]
Che-Liang Chiou69998b02012-01-11 11:28:42 +0800618 groups.extend(set(default_groups).difference(groups))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700619
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700620 if self.IsMirror:
621 worktree = None
622 gitdir = os.path.join(self.topdir, '%s.git' % name)
623 else:
624 worktree = os.path.join(self.topdir, path).replace('\\', '/')
625 gitdir = os.path.join(self.repodir, 'projects/%s.git' % path)
626
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700627 project = Project(manifest = self,
628 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700629 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700630 gitdir = gitdir,
631 worktree = worktree,
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700632 relpath = path,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700633 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800634 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700635 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700636 groups = groups,
Brian Harring14a66742012-09-28 20:21:57 -0700637 sync_c = sync_c,
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700638 upstream = upstream)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700639
640 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700641 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700642 self._ParseCopyFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500643 if n.nodeName == 'annotation':
644 self._ParseAnnotation(project, n)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700645
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700646 return project
647
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700648 def _ParseCopyFile(self, project, node):
649 src = self._reqatt(node, 'src')
650 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800651 if not self.IsMirror:
652 # src is project relative;
653 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800654 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700655
James W. Mills24c13082012-04-12 15:04:13 -0500656 def _ParseAnnotation(self, project, node):
657 name = self._reqatt(node, 'name')
658 value = self._reqatt(node, 'value')
659 try:
660 keep = self._reqatt(node, 'keep').lower()
661 except ManifestParseError:
662 keep = "true"
663 if keep != "true" and keep != "false":
664 raise ManifestParseError, "optional \"keep\" attribute must be \"true\" or \"false\""
665 project.AddAnnotation(name, value, keep)
666
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700667 def _get_remote(self, node):
668 name = node.getAttribute('remote')
669 if not name:
670 return None
671
672 v = self._remotes.get(name)
673 if not v:
674 raise ManifestParseError, \
675 "remote %s not defined in %s" % \
676 (name, self.manifestFile)
677 return v
678
679 def _reqatt(self, node, attname):
680 """
681 reads a required attribute from the node.
682 """
683 v = node.getAttribute(attname)
684 if not v:
685 raise ManifestParseError, \
686 "no %s in <%s> within %s" % \
687 (attname, node.nodeName, self.manifestFile)
688 return v