blob: d16f1a989c52194992e1fd1ce6c436e1894bf9ea [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
Colin Cross23acdd32012-04-21 00:33:54 -070016import itertools
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070017import os
Conley Owensdb728cd2011-09-26 16:34:01 -070018import re
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070019import sys
Conley Owensdb728cd2011-09-26 16:34:01 -070020import urlparse
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070021import xml.dom.minidom
22
David Pursehousee15c65a2012-08-22 10:46:11 +090023from git_config import GitConfig
David Pursehousee00aa6b2012-09-11 14:33:51 +090024from git_refs import R_HEADS, HEAD
25from project import RemoteSpec, Project, MetaProject
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070026from error import ManifestParseError
27
28MANIFEST_FILE_NAME = 'manifest.xml'
Shawn O. Pearce5cc66792008-10-23 16:19:27 -070029LOCAL_MANIFEST_NAME = 'local_manifest.xml'
David Pursehouse2d5a0df2012-11-13 02:50:36 +090030LOCAL_MANIFESTS_DIR_NAME = 'local_manifests'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070031
Conley Owensdb728cd2011-09-26 16:34:01 -070032urlparse.uses_relative.extend(['ssh', 'git'])
33urlparse.uses_netloc.extend(['ssh', 'git'])
34
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070035class _Default(object):
36 """Project defaults within the manifest."""
37
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -070038 revisionExpr = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070039 remote = None
Shawn O. Pearce6392c872011-09-22 17:44:31 -070040 sync_j = 1
Anatol Pomazau79770d22012-04-20 14:41:59 -070041 sync_c = False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070042
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070043class _XmlRemote(object):
44 def __init__(self,
45 name,
Yestin Sunb292b982012-07-02 07:32:50 -070046 alias=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070047 fetch=None,
Conley Owensdb728cd2011-09-26 16:34:01 -070048 manifestUrl=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070049 review=None):
50 self.name = name
51 self.fetchUrl = fetch
Conley Owensdb728cd2011-09-26 16:34:01 -070052 self.manifestUrl = manifestUrl
Yestin Sunb292b982012-07-02 07:32:50 -070053 self.remoteAlias = alias
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070054 self.reviewUrl = review
Conley Owensceea3682011-10-20 10:45:47 -070055 self.resolvedFetchUrl = self._resolveFetchUrl()
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070056
Conley Owensceea3682011-10-20 10:45:47 -070057 def _resolveFetchUrl(self):
58 url = self.fetchUrl.rstrip('/')
Conley Owensdb728cd2011-09-26 16:34:01 -070059 manifestUrl = self.manifestUrl.rstrip('/')
60 # urljoin will get confused if there is no scheme in the base url
61 # ie, if manifestUrl is of the form <hostname:port>
62 if manifestUrl.find(':') != manifestUrl.find('/') - 1:
63 manifestUrl = 'gopher://' + manifestUrl
64 url = urlparse.urljoin(manifestUrl, url)
Conley Owensceea3682011-10-20 10:45:47 -070065 return re.sub(r'^gopher://', '', url)
66
67 def ToRemoteSpec(self, projectName):
Conley Owens9d8f9142011-10-20 14:36:35 -070068 url = self.resolvedFetchUrl.rstrip('/') + '/' + projectName
Yestin Sunb292b982012-07-02 07:32:50 -070069 remoteName = self.name
70 if self.remoteAlias:
71 remoteName = self.remoteAlias
72 return RemoteSpec(remoteName, url, self.reviewUrl)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070073
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070074class XmlManifest(object):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070075 """manages the repo configuration file"""
76
77 def __init__(self, repodir):
78 self.repodir = os.path.abspath(repodir)
79 self.topdir = os.path.dirname(self.repodir)
80 self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070081 self.globalConfig = GitConfig.ForUser()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070082
83 self.repoProject = MetaProject(self, 'repo',
84 gitdir = os.path.join(repodir, 'repo/.git'),
85 worktree = os.path.join(repodir, 'repo'))
86
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070087 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -080088 gitdir = os.path.join(repodir, 'manifests.git'),
89 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070090
91 self._Unload()
92
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -070093 def Override(self, name):
94 """Use a different manifest, just for the current instantiation.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070095 """
96 path = os.path.join(self.manifestProject.worktree, name)
97 if not os.path.isfile(path):
98 raise ManifestParseError('manifest %s not found' % name)
99
100 old = self.manifestFile
101 try:
102 self.manifestFile = path
103 self._Unload()
104 self._Load()
105 finally:
106 self.manifestFile = old
107
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700108 def Link(self, name):
109 """Update the repo metadata to use a different manifest.
110 """
111 self.Override(name)
112
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700113 try:
114 if os.path.exists(self.manifestFile):
115 os.remove(self.manifestFile)
116 os.symlink('manifests/%s' % name, self.manifestFile)
David Pursehouse8a68ff92012-09-24 12:15:13 +0900117 except OSError:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700118 raise ManifestParseError('cannot link manifest %s' % name)
119
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800120 def _RemoteToXml(self, r, doc, root):
121 e = doc.createElement('remote')
122 root.appendChild(e)
123 e.setAttribute('name', r.name)
124 e.setAttribute('fetch', r.fetchUrl)
125 if r.reviewUrl is not None:
126 e.setAttribute('review', r.reviewUrl)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800127
Brian Harring14a66742012-09-28 20:21:57 -0700128 def Save(self, fd, peg_rev=False, peg_rev_upstream=True):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800129 """Write the current manifest out to the given file descriptor.
130 """
Colin Cross5acde752012-03-28 20:15:45 -0700131 mp = self.manifestProject
132
133 groups = mp.config.GetString('manifest.groups')
Colin Crossc39864f2012-04-23 13:41:58 -0700134 if not groups:
Conley Owensbb1b5f52012-08-13 13:11:18 -0700135 groups = 'all'
Conley Owens971de8e2012-04-16 10:36:08 -0700136 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Colin Cross5acde752012-03-28 20:15:45 -0700137
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800138 doc = xml.dom.minidom.Document()
139 root = doc.createElement('manifest')
140 doc.appendChild(root)
141
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700142 # Save out the notice. There's a little bit of work here to give it the
143 # right whitespace, which assumes that the notice is automatically indented
144 # by 4 by minidom.
145 if self.notice:
146 notice_element = root.appendChild(doc.createElement('notice'))
147 notice_lines = self.notice.splitlines()
148 indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:]
149 notice_element.appendChild(doc.createTextNode(indented_notice))
150
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800151 d = self.default
152 sort_remotes = list(self.remotes.keys())
153 sort_remotes.sort()
154
155 for r in sort_remotes:
156 self._RemoteToXml(self.remotes[r], doc, root)
157 if self.remotes:
158 root.appendChild(doc.createTextNode(''))
159
160 have_default = False
161 e = doc.createElement('default')
162 if d.remote:
163 have_default = True
164 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700165 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800166 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700167 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700168 if d.sync_j > 1:
169 have_default = True
170 e.setAttribute('sync-j', '%d' % d.sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700171 if d.sync_c:
172 have_default = True
173 e.setAttribute('sync-c', 'true')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800174 if have_default:
175 root.appendChild(e)
176 root.appendChild(doc.createTextNode(''))
177
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700178 if self._manifest_server:
179 e = doc.createElement('manifest-server')
180 e.setAttribute('url', self._manifest_server)
181 root.appendChild(e)
182 root.appendChild(doc.createTextNode(''))
183
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700184 sort_projects = list(self.projects.keys())
185 sort_projects.sort()
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800186
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700187 for p in sort_projects:
188 p = self.projects[p]
189
Colin Cross5acde752012-03-28 20:15:45 -0700190 if not p.MatchesGroups(groups):
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700191 continue
Colin Cross5acde752012-03-28 20:15:45 -0700192
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800193 e = doc.createElement('project')
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700194 root.appendChild(e)
195 e.setAttribute('name', p.name)
196 if p.relpath != p.name:
197 e.setAttribute('path', p.relpath)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800198 if not d.remote or p.remote.name != d.remote.name:
199 e.setAttribute('remote', p.remote.name)
200 if peg_rev:
201 if self.IsMirror:
Brian Harring14a66742012-09-28 20:21:57 -0700202 value = p.bare_git.rev_parse(p.revisionExpr + '^0')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800203 else:
Brian Harring14a66742012-09-28 20:21:57 -0700204 value = p.work_git.rev_parse(HEAD + '^0')
205 e.setAttribute('revision', value)
206 if peg_rev_upstream and value != p.revisionExpr:
207 # Only save the origin if the origin is not a sha1, and the default
208 # isn't our value, and the if the default doesn't already have that
209 # covered.
210 e.setAttribute('upstream', p.revisionExpr)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700211 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
212 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800213
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800214 for c in p.copyfiles:
215 ce = doc.createElement('copyfile')
216 ce.setAttribute('src', c.src)
217 ce.setAttribute('dest', c.dest)
218 e.appendChild(ce)
219
Conley Owensbb1b5f52012-08-13 13:11:18 -0700220 default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath]
Dmitry Fink17f85ea2012-08-06 14:52:29 -0700221 egroups = [g for g in p.groups if g not in default_groups]
Conley Owens971de8e2012-04-16 10:36:08 -0700222 if egroups:
223 e.setAttribute('groups', ','.join(egroups))
Colin Cross5acde752012-03-28 20:15:45 -0700224
James W. Mills24c13082012-04-12 15:04:13 -0500225 for a in p.annotations:
226 if a.keep == "true":
227 ae = doc.createElement('annotation')
228 ae.setAttribute('name', a.name)
229 ae.setAttribute('value', a.value)
230 e.appendChild(ae)
231
Anatol Pomazau79770d22012-04-20 14:41:59 -0700232 if p.sync_c:
233 e.setAttribute('sync-c', 'true')
234
Doug Anderson37282b42011-03-04 11:54:18 -0800235 if self._repo_hooks_project:
236 root.appendChild(doc.createTextNode(''))
237 e = doc.createElement('repo-hooks')
238 e.setAttribute('in-project', self._repo_hooks_project.name)
239 e.setAttribute('enabled-list',
240 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
241 root.appendChild(e)
242
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800243 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
244
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700245 @property
246 def projects(self):
247 self._Load()
248 return self._projects
249
250 @property
251 def remotes(self):
252 self._Load()
253 return self._remotes
254
255 @property
256 def default(self):
257 self._Load()
258 return self._default
259
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800260 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800261 def repo_hooks_project(self):
262 self._Load()
263 return self._repo_hooks_project
264
265 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700266 def notice(self):
267 self._Load()
268 return self._notice
269
270 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700271 def manifest_server(self):
272 self._Load()
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800273 return self._manifest_server
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700274
275 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800276 def IsMirror(self):
277 return self.manifestProject.config.GetBoolean('repo.mirror')
278
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700279 def _Unload(self):
280 self._loaded = False
281 self._projects = {}
282 self._remotes = {}
283 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800284 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700285 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700286 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700287 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700288
289 def _Load(self):
290 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800291 m = self.manifestProject
292 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700293 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800294 b = b[len(R_HEADS):]
295 self.branch = b
296
Colin Cross23acdd32012-04-21 00:33:54 -0700297 nodes = []
Brian Harring475a47d2012-06-07 20:05:35 -0700298 nodes.append(self._ParseManifestXml(self.manifestFile,
299 self.manifestProject.worktree))
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700300
301 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
302 if os.path.exists(local):
David Pursehouse5566ae52012-11-13 03:04:18 +0900303 print >>sys.stderr, 'warning: %s is deprecated; put local manifests in %s instead' % \
304 (LOCAL_MANIFEST_NAME, LOCAL_MANIFESTS_DIR_NAME)
Brian Harring475a47d2012-06-07 20:05:35 -0700305 nodes.append(self._ParseManifestXml(local, self.repodir))
Colin Cross23acdd32012-04-21 00:33:54 -0700306
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900307 local_dir = os.path.abspath(os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME))
308 try:
309 for local_file in os.listdir(local_dir):
310 if local_file.endswith('.xml'):
311 try:
312 nodes.append(self._ParseManifestXml(local_file, self.repodir))
313 except ManifestParseError as e:
314 print >>sys.stderr, '%s' % str(e)
315 except OSError:
316 pass
317
Colin Cross23acdd32012-04-21 00:33:54 -0700318 self._ParseManifest(nodes)
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700319
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800320 if self.IsMirror:
321 self._AddMetaProjectMirror(self.repoProject)
322 self._AddMetaProjectMirror(self.manifestProject)
323
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700324 self._loaded = True
325
Brian Harring475a47d2012-06-07 20:05:35 -0700326 def _ParseManifestXml(self, path, include_root):
David Pursehousef7fc8a92012-11-13 04:00:28 +0900327 try:
328 root = xml.dom.minidom.parse(path)
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900329 except (OSError, xml.parsers.expat.ExpatError) as e:
David Pursehousef7fc8a92012-11-13 04:00:28 +0900330 raise ManifestParseError("error parsing manifest %s: %s" % (path, e))
331
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700332 if not root or not root.childNodes:
Brian Harring26448742011-04-28 05:04:41 -0700333 raise ManifestParseError("no root node in %s" % (path,))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700334
Jooncheol Park34acdd22012-08-27 02:25:59 +0900335 for manifest in root.childNodes:
336 if manifest.nodeName == 'manifest':
337 break
338 else:
Brian Harring26448742011-04-28 05:04:41 -0700339 raise ManifestParseError("no <manifest> in %s" % (path,))
340
Colin Cross23acdd32012-04-21 00:33:54 -0700341 nodes = []
David Pursehouse4f7bdea2012-10-22 12:50:15 +0900342 for node in manifest.childNodes: # pylint:disable=W0631
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900343 # We only get here if manifest is initialised
Brian Harring26448742011-04-28 05:04:41 -0700344 if node.nodeName == 'include':
345 name = self._reqatt(node, 'name')
Brian Harring475a47d2012-06-07 20:05:35 -0700346 fp = os.path.join(include_root, name)
Brian Harring26448742011-04-28 05:04:41 -0700347 if not os.path.isfile(fp):
348 raise ManifestParseError, \
349 "include %s doesn't exist or isn't a file" % \
350 (name,)
351 try:
Brian Harring475a47d2012-06-07 20:05:35 -0700352 nodes.extend(self._ParseManifestXml(fp, include_root))
Brian Harring26448742011-04-28 05:04:41 -0700353 # should isolate this to the exact exception, but that's
354 # tricky. actual parsing implementation may vary.
355 except (KeyboardInterrupt, RuntimeError, SystemExit):
356 raise
Sarah Owensa5be53f2012-09-09 15:37:57 -0700357 except Exception as e:
Brian Harring26448742011-04-28 05:04:41 -0700358 raise ManifestParseError(
359 "failed parsing included manifest %s: %s", (name, e))
Colin Cross23acdd32012-04-21 00:33:54 -0700360 else:
361 nodes.append(node)
362 return nodes
Brian Harring26448742011-04-28 05:04:41 -0700363
Colin Cross23acdd32012-04-21 00:33:54 -0700364 def _ParseManifest(self, node_list):
365 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700366 if node.nodeName == 'remote':
367 remote = self._ParseRemote(node)
368 if self._remotes.get(remote.name):
Doug Anderson37282b42011-03-04 11:54:18 -0800369 raise ManifestParseError(
370 'duplicate remote %s in %s' %
371 (remote.name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700372 self._remotes[remote.name] = remote
373
Colin Cross23acdd32012-04-21 00:33:54 -0700374 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700375 if node.nodeName == 'default':
376 if self._default is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800377 raise ManifestParseError(
378 'duplicate default in %s' %
379 (self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700380 self._default = self._ParseDefault(node)
381 if self._default is None:
382 self._default = _Default()
383
Colin Cross23acdd32012-04-21 00:33:54 -0700384 for node in itertools.chain(*node_list):
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700385 if node.nodeName == 'notice':
386 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800387 raise ManifestParseError(
388 'duplicate notice in %s' %
389 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700390 self._notice = self._ParseNotice(node)
391
Colin Cross23acdd32012-04-21 00:33:54 -0700392 for node in itertools.chain(*node_list):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700393 if node.nodeName == 'manifest-server':
394 url = self._reqatt(node, 'url')
395 if self._manifest_server is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800396 raise ManifestParseError(
397 'duplicate manifest-server in %s' %
398 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700399 self._manifest_server = url
400
Colin Cross23acdd32012-04-21 00:33:54 -0700401 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700402 if node.nodeName == 'project':
403 project = self._ParseProject(node)
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700404 if self._projects.get(project.name):
405 raise ManifestParseError(
406 'duplicate project %s in %s' %
407 (project.name, self.manifestFile))
408 self._projects[project.name] = project
Doug Anderson37282b42011-03-04 11:54:18 -0800409 if node.nodeName == 'repo-hooks':
410 # Get the name of the project and the (space-separated) list of enabled.
411 repo_hooks_project = self._reqatt(node, 'in-project')
412 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
413
414 # Only one project can be the hooks project
415 if self._repo_hooks_project is not None:
416 raise ManifestParseError(
417 'duplicate repo-hooks in %s' %
418 (self.manifestFile))
419
420 # Store a reference to the Project.
421 try:
422 self._repo_hooks_project = self._projects[repo_hooks_project]
423 except KeyError:
424 raise ManifestParseError(
425 'project %s not found for repo-hooks' %
426 (repo_hooks_project))
427
428 # Store the enabled hooks in the Project object.
429 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
Colin Cross23acdd32012-04-21 00:33:54 -0700430 if node.nodeName == 'remove-project':
431 name = self._reqatt(node, 'name')
432 try:
433 del self._projects[name]
434 except KeyError:
435 raise ManifestParseError(
436 'project %s not found' %
437 (name))
438
439 # If the manifest removes the hooks project, treat it as if it deleted
440 # the repo-hooks element too.
441 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
442 self._repo_hooks_project = None
443
Doug Anderson37282b42011-03-04 11:54:18 -0800444
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800445 def _AddMetaProjectMirror(self, m):
446 name = None
447 m_url = m.GetRemote(m.remote.name).url
448 if m_url.endswith('/.git'):
449 raise ManifestParseError, 'refusing to mirror %s' % m_url
450
451 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700452 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800453 if not url.endswith('/'):
454 url += '/'
455 if m_url.startswith(url):
456 remote = self._default.remote
457 name = m_url[len(url):]
458
459 if name is None:
460 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700461 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Shawn O. Pearcef35b2d92012-08-02 11:46:22 -0700462 remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800463 name = m_url[s:]
464
465 if name.endswith('.git'):
466 name = name[:-4]
467
468 if name not in self._projects:
469 m.PreSync()
470 gitdir = os.path.join(self.topdir, '%s.git' % name)
471 project = Project(manifest = self,
472 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700473 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800474 gitdir = gitdir,
475 worktree = None,
476 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700477 revisionExpr = m.revisionExpr,
478 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800479 self._projects[project.name] = project
480
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700481 def _ParseRemote(self, node):
482 """
483 reads a <remote> element from the manifest file
484 """
485 name = self._reqatt(node, 'name')
Yestin Sunb292b982012-07-02 07:32:50 -0700486 alias = node.getAttribute('alias')
487 if alias == '':
488 alias = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700489 fetch = self._reqatt(node, 'fetch')
490 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800491 if review == '':
492 review = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700493 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Yestin Sunb292b982012-07-02 07:32:50 -0700494 return _XmlRemote(name, alias, fetch, manifestUrl, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700495
496 def _ParseDefault(self, node):
497 """
498 reads a <default> element from the manifest file
499 """
500 d = _Default()
501 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700502 d.revisionExpr = node.getAttribute('revision')
503 if d.revisionExpr == '':
504 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700505
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700506 sync_j = node.getAttribute('sync-j')
507 if sync_j == '' or sync_j is None:
508 d.sync_j = 1
509 else:
510 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700511
512 sync_c = node.getAttribute('sync-c')
513 if not sync_c:
514 d.sync_c = False
515 else:
516 d.sync_c = sync_c.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700517 return d
518
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700519 def _ParseNotice(self, node):
520 """
521 reads a <notice> element from the manifest file
522
523 The <notice> element is distinct from other tags in the XML in that the
524 data is conveyed between the start and end tag (it's not an empty-element
525 tag).
526
527 The white space (carriage returns, indentation) for the notice element is
528 relevant and is parsed in a way that is based on how python docstrings work.
529 In fact, the code is remarkably similar to here:
530 http://www.python.org/dev/peps/pep-0257/
531 """
532 # Get the data out of the node...
533 notice = node.childNodes[0].data
534
535 # Figure out minimum indentation, skipping the first line (the same line
536 # as the <notice> tag)...
537 minIndent = sys.maxint
538 lines = notice.splitlines()
539 for line in lines[1:]:
540 lstrippedLine = line.lstrip()
541 if lstrippedLine:
542 indent = len(line) - len(lstrippedLine)
543 minIndent = min(indent, minIndent)
544
545 # Strip leading / trailing blank lines and also indentation.
546 cleanLines = [lines[0].strip()]
547 for line in lines[1:]:
548 cleanLines.append(line[minIndent:].rstrip())
549
550 # Clear completely blank lines from front and back...
551 while cleanLines and not cleanLines[0]:
552 del cleanLines[0]
553 while cleanLines and not cleanLines[-1]:
554 del cleanLines[-1]
555
556 return '\n'.join(cleanLines)
557
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700558 def _ParseProject(self, node):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700559 """
560 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700561 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700562 name = self._reqatt(node, 'name')
563
564 remote = self._get_remote(node)
565 if remote is None:
566 remote = self._default.remote
567 if remote is None:
568 raise ManifestParseError, \
569 "no remote for project %s within %s" % \
570 (name, self.manifestFile)
571
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700572 revisionExpr = node.getAttribute('revision')
573 if not revisionExpr:
574 revisionExpr = self._default.revisionExpr
575 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700576 raise ManifestParseError, \
577 "no revision for project %s within %s" % \
578 (name, self.manifestFile)
579
580 path = node.getAttribute('path')
581 if not path:
582 path = name
583 if path.startswith('/'):
584 raise ManifestParseError, \
585 "project %s path cannot be absolute in %s" % \
586 (name, self.manifestFile)
587
Mike Pontillod3153822012-02-28 11:53:24 -0800588 rebase = node.getAttribute('rebase')
589 if not rebase:
590 rebase = True
591 else:
592 rebase = rebase.lower() in ("yes", "true", "1")
593
Anatol Pomazau79770d22012-04-20 14:41:59 -0700594 sync_c = node.getAttribute('sync-c')
595 if not sync_c:
596 sync_c = False
597 else:
598 sync_c = sync_c.lower() in ("yes", "true", "1")
599
Brian Harring14a66742012-09-28 20:21:57 -0700600 upstream = node.getAttribute('upstream')
601
Conley Owens971de8e2012-04-16 10:36:08 -0700602 groups = ''
603 if node.hasAttribute('groups'):
604 groups = node.getAttribute('groups')
David Pursehouse1d947b32012-10-25 12:23:11 +0900605 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Brian Harring7da13142012-06-15 02:24:20 -0700606
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700607 default_groups = ['all', 'name:%s' % name, 'path:%s' % path]
Che-Liang Chiou69998b02012-01-11 11:28:42 +0800608 groups.extend(set(default_groups).difference(groups))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700609
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700610 if self.IsMirror:
611 worktree = None
612 gitdir = os.path.join(self.topdir, '%s.git' % name)
613 else:
614 worktree = os.path.join(self.topdir, path).replace('\\', '/')
615 gitdir = os.path.join(self.repodir, 'projects/%s.git' % path)
616
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700617 project = Project(manifest = self,
618 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700619 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700620 gitdir = gitdir,
621 worktree = worktree,
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700622 relpath = path,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700623 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800624 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700625 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700626 groups = groups,
Brian Harring14a66742012-09-28 20:21:57 -0700627 sync_c = sync_c,
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700628 upstream = upstream)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700629
630 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700631 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700632 self._ParseCopyFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500633 if n.nodeName == 'annotation':
634 self._ParseAnnotation(project, n)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700635
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700636 return project
637
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700638 def _ParseCopyFile(self, project, node):
639 src = self._reqatt(node, 'src')
640 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800641 if not self.IsMirror:
642 # src is project relative;
643 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800644 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700645
James W. Mills24c13082012-04-12 15:04:13 -0500646 def _ParseAnnotation(self, project, node):
647 name = self._reqatt(node, 'name')
648 value = self._reqatt(node, 'value')
649 try:
650 keep = self._reqatt(node, 'keep').lower()
651 except ManifestParseError:
652 keep = "true"
653 if keep != "true" and keep != "false":
654 raise ManifestParseError, "optional \"keep\" attribute must be \"true\" or \"false\""
655 project.AddAnnotation(name, value, keep)
656
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700657 def _get_remote(self, node):
658 name = node.getAttribute('remote')
659 if not name:
660 return None
661
662 v = self._remotes.get(name)
663 if not v:
664 raise ManifestParseError, \
665 "remote %s not defined in %s" % \
666 (name, self.manifestFile)
667 return v
668
669 def _reqatt(self, node, attname):
670 """
671 reads a required attribute from the node.
672 """
673 v = node.getAttribute(attname)
674 if not v:
675 raise ManifestParseError, \
676 "no %s in <%s> within %s" % \
677 (attname, node.nodeName, self.manifestFile)
678 return v