blob: 31987248b1a39087645c4056df39bbad58f21516 [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
David Pursehouse717ece92012-11-13 08:49:16 +090057 def __eq__(self, other):
58 return self.__dict__ == other.__dict__
59
60 def __ne__(self, other):
61 return self.__dict__ != other.__dict__
62
Conley Owensceea3682011-10-20 10:45:47 -070063 def _resolveFetchUrl(self):
64 url = self.fetchUrl.rstrip('/')
Conley Owensdb728cd2011-09-26 16:34:01 -070065 manifestUrl = self.manifestUrl.rstrip('/')
66 # urljoin will get confused if there is no scheme in the base url
67 # ie, if manifestUrl is of the form <hostname:port>
68 if manifestUrl.find(':') != manifestUrl.find('/') - 1:
69 manifestUrl = 'gopher://' + manifestUrl
70 url = urlparse.urljoin(manifestUrl, url)
Conley Owensceea3682011-10-20 10:45:47 -070071 return re.sub(r'^gopher://', '', url)
72
73 def ToRemoteSpec(self, projectName):
Conley Owens9d8f9142011-10-20 14:36:35 -070074 url = self.resolvedFetchUrl.rstrip('/') + '/' + projectName
Yestin Sunb292b982012-07-02 07:32:50 -070075 remoteName = self.name
76 if self.remoteAlias:
77 remoteName = self.remoteAlias
78 return RemoteSpec(remoteName, url, self.reviewUrl)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070079
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070080class XmlManifest(object):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070081 """manages the repo configuration file"""
82
83 def __init__(self, repodir):
84 self.repodir = os.path.abspath(repodir)
85 self.topdir = os.path.dirname(self.repodir)
86 self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070087 self.globalConfig = GitConfig.ForUser()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070088
89 self.repoProject = MetaProject(self, 'repo',
90 gitdir = os.path.join(repodir, 'repo/.git'),
91 worktree = os.path.join(repodir, 'repo'))
92
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070093 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -080094 gitdir = os.path.join(repodir, 'manifests.git'),
95 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070096
97 self._Unload()
98
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -070099 def Override(self, name):
100 """Use a different manifest, just for the current instantiation.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700101 """
102 path = os.path.join(self.manifestProject.worktree, name)
103 if not os.path.isfile(path):
104 raise ManifestParseError('manifest %s not found' % name)
105
106 old = self.manifestFile
107 try:
108 self.manifestFile = path
109 self._Unload()
110 self._Load()
111 finally:
112 self.manifestFile = old
113
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700114 def Link(self, name):
115 """Update the repo metadata to use a different manifest.
116 """
117 self.Override(name)
118
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700119 try:
120 if os.path.exists(self.manifestFile):
121 os.remove(self.manifestFile)
122 os.symlink('manifests/%s' % name, self.manifestFile)
David Pursehouse8a68ff92012-09-24 12:15:13 +0900123 except OSError:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700124 raise ManifestParseError('cannot link manifest %s' % name)
125
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800126 def _RemoteToXml(self, r, doc, root):
127 e = doc.createElement('remote')
128 root.appendChild(e)
129 e.setAttribute('name', r.name)
130 e.setAttribute('fetch', r.fetchUrl)
131 if r.reviewUrl is not None:
132 e.setAttribute('review', r.reviewUrl)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800133
Brian Harring14a66742012-09-28 20:21:57 -0700134 def Save(self, fd, peg_rev=False, peg_rev_upstream=True):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800135 """Write the current manifest out to the given file descriptor.
136 """
Colin Cross5acde752012-03-28 20:15:45 -0700137 mp = self.manifestProject
138
139 groups = mp.config.GetString('manifest.groups')
Colin Crossc39864f2012-04-23 13:41:58 -0700140 if not groups:
Conley Owensbb1b5f52012-08-13 13:11:18 -0700141 groups = 'all'
Conley Owens971de8e2012-04-16 10:36:08 -0700142 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Colin Cross5acde752012-03-28 20:15:45 -0700143
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800144 doc = xml.dom.minidom.Document()
145 root = doc.createElement('manifest')
146 doc.appendChild(root)
147
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700148 # Save out the notice. There's a little bit of work here to give it the
149 # right whitespace, which assumes that the notice is automatically indented
150 # by 4 by minidom.
151 if self.notice:
152 notice_element = root.appendChild(doc.createElement('notice'))
153 notice_lines = self.notice.splitlines()
154 indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:]
155 notice_element.appendChild(doc.createTextNode(indented_notice))
156
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800157 d = self.default
158 sort_remotes = list(self.remotes.keys())
159 sort_remotes.sort()
160
161 for r in sort_remotes:
162 self._RemoteToXml(self.remotes[r], doc, root)
163 if self.remotes:
164 root.appendChild(doc.createTextNode(''))
165
166 have_default = False
167 e = doc.createElement('default')
168 if d.remote:
169 have_default = True
170 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700171 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800172 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700173 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700174 if d.sync_j > 1:
175 have_default = True
176 e.setAttribute('sync-j', '%d' % d.sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700177 if d.sync_c:
178 have_default = True
179 e.setAttribute('sync-c', 'true')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800180 if have_default:
181 root.appendChild(e)
182 root.appendChild(doc.createTextNode(''))
183
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700184 if self._manifest_server:
185 e = doc.createElement('manifest-server')
186 e.setAttribute('url', self._manifest_server)
187 root.appendChild(e)
188 root.appendChild(doc.createTextNode(''))
189
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700190 sort_projects = list(self.projects.keys())
191 sort_projects.sort()
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800192
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700193 for p in sort_projects:
194 p = self.projects[p]
195
Colin Cross5acde752012-03-28 20:15:45 -0700196 if not p.MatchesGroups(groups):
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700197 continue
Colin Cross5acde752012-03-28 20:15:45 -0700198
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800199 e = doc.createElement('project')
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700200 root.appendChild(e)
201 e.setAttribute('name', p.name)
202 if p.relpath != p.name:
203 e.setAttribute('path', p.relpath)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800204 if not d.remote or p.remote.name != d.remote.name:
205 e.setAttribute('remote', p.remote.name)
206 if peg_rev:
207 if self.IsMirror:
Brian Harring14a66742012-09-28 20:21:57 -0700208 value = p.bare_git.rev_parse(p.revisionExpr + '^0')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800209 else:
Brian Harring14a66742012-09-28 20:21:57 -0700210 value = p.work_git.rev_parse(HEAD + '^0')
211 e.setAttribute('revision', value)
212 if peg_rev_upstream and value != p.revisionExpr:
213 # Only save the origin if the origin is not a sha1, and the default
214 # isn't our value, and the if the default doesn't already have that
215 # covered.
216 e.setAttribute('upstream', p.revisionExpr)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700217 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
218 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800219
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800220 for c in p.copyfiles:
221 ce = doc.createElement('copyfile')
222 ce.setAttribute('src', c.src)
223 ce.setAttribute('dest', c.dest)
224 e.appendChild(ce)
225
Conley Owensbb1b5f52012-08-13 13:11:18 -0700226 default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath]
Dmitry Fink17f85ea2012-08-06 14:52:29 -0700227 egroups = [g for g in p.groups if g not in default_groups]
Conley Owens971de8e2012-04-16 10:36:08 -0700228 if egroups:
229 e.setAttribute('groups', ','.join(egroups))
Colin Cross5acde752012-03-28 20:15:45 -0700230
James W. Mills24c13082012-04-12 15:04:13 -0500231 for a in p.annotations:
232 if a.keep == "true":
233 ae = doc.createElement('annotation')
234 ae.setAttribute('name', a.name)
235 ae.setAttribute('value', a.value)
236 e.appendChild(ae)
237
Anatol Pomazau79770d22012-04-20 14:41:59 -0700238 if p.sync_c:
239 e.setAttribute('sync-c', 'true')
240
Doug Anderson37282b42011-03-04 11:54:18 -0800241 if self._repo_hooks_project:
242 root.appendChild(doc.createTextNode(''))
243 e = doc.createElement('repo-hooks')
244 e.setAttribute('in-project', self._repo_hooks_project.name)
245 e.setAttribute('enabled-list',
246 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
247 root.appendChild(e)
248
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800249 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
250
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700251 @property
252 def projects(self):
253 self._Load()
254 return self._projects
255
256 @property
257 def remotes(self):
258 self._Load()
259 return self._remotes
260
261 @property
262 def default(self):
263 self._Load()
264 return self._default
265
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800266 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800267 def repo_hooks_project(self):
268 self._Load()
269 return self._repo_hooks_project
270
271 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700272 def notice(self):
273 self._Load()
274 return self._notice
275
276 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700277 def manifest_server(self):
278 self._Load()
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800279 return self._manifest_server
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700280
281 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800282 def IsMirror(self):
283 return self.manifestProject.config.GetBoolean('repo.mirror')
284
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700285 def _Unload(self):
286 self._loaded = False
287 self._projects = {}
288 self._remotes = {}
289 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800290 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700291 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700292 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700293 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700294
295 def _Load(self):
296 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800297 m = self.manifestProject
298 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700299 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800300 b = b[len(R_HEADS):]
301 self.branch = b
302
Colin Cross23acdd32012-04-21 00:33:54 -0700303 nodes = []
Brian Harring475a47d2012-06-07 20:05:35 -0700304 nodes.append(self._ParseManifestXml(self.manifestFile,
305 self.manifestProject.worktree))
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700306
307 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
308 if os.path.exists(local):
David Pursehouse5566ae52012-11-13 03:04:18 +0900309 print >>sys.stderr, 'warning: %s is deprecated; put local manifests in %s instead' % \
310 (LOCAL_MANIFEST_NAME, LOCAL_MANIFESTS_DIR_NAME)
Brian Harring475a47d2012-06-07 20:05:35 -0700311 nodes.append(self._ParseManifestXml(local, self.repodir))
Colin Cross23acdd32012-04-21 00:33:54 -0700312
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900313 local_dir = os.path.abspath(os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME))
314 try:
David Pursehouse52f1e5d2012-11-14 04:53:24 +0900315 for local_file in sorted(os.listdir(local_dir)):
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900316 if local_file.endswith('.xml'):
317 try:
318 nodes.append(self._ParseManifestXml(local_file, self.repodir))
319 except ManifestParseError as e:
320 print >>sys.stderr, '%s' % str(e)
321 except OSError:
322 pass
323
Colin Cross23acdd32012-04-21 00:33:54 -0700324 self._ParseManifest(nodes)
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700325
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800326 if self.IsMirror:
327 self._AddMetaProjectMirror(self.repoProject)
328 self._AddMetaProjectMirror(self.manifestProject)
329
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700330 self._loaded = True
331
Brian Harring475a47d2012-06-07 20:05:35 -0700332 def _ParseManifestXml(self, path, include_root):
David Pursehousef7fc8a92012-11-13 04:00:28 +0900333 try:
334 root = xml.dom.minidom.parse(path)
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900335 except (OSError, xml.parsers.expat.ExpatError) as e:
David Pursehousef7fc8a92012-11-13 04:00:28 +0900336 raise ManifestParseError("error parsing manifest %s: %s" % (path, e))
337
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700338 if not root or not root.childNodes:
Brian Harring26448742011-04-28 05:04:41 -0700339 raise ManifestParseError("no root node in %s" % (path,))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700340
Jooncheol Park34acdd22012-08-27 02:25:59 +0900341 for manifest in root.childNodes:
342 if manifest.nodeName == 'manifest':
343 break
344 else:
Brian Harring26448742011-04-28 05:04:41 -0700345 raise ManifestParseError("no <manifest> in %s" % (path,))
346
Colin Cross23acdd32012-04-21 00:33:54 -0700347 nodes = []
David Pursehouse4f7bdea2012-10-22 12:50:15 +0900348 for node in manifest.childNodes: # pylint:disable=W0631
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900349 # We only get here if manifest is initialised
Brian Harring26448742011-04-28 05:04:41 -0700350 if node.nodeName == 'include':
351 name = self._reqatt(node, 'name')
Brian Harring475a47d2012-06-07 20:05:35 -0700352 fp = os.path.join(include_root, name)
Brian Harring26448742011-04-28 05:04:41 -0700353 if not os.path.isfile(fp):
354 raise ManifestParseError, \
355 "include %s doesn't exist or isn't a file" % \
356 (name,)
357 try:
Brian Harring475a47d2012-06-07 20:05:35 -0700358 nodes.extend(self._ParseManifestXml(fp, include_root))
Brian Harring26448742011-04-28 05:04:41 -0700359 # should isolate this to the exact exception, but that's
360 # tricky. actual parsing implementation may vary.
361 except (KeyboardInterrupt, RuntimeError, SystemExit):
362 raise
Sarah Owensa5be53f2012-09-09 15:37:57 -0700363 except Exception as e:
Brian Harring26448742011-04-28 05:04:41 -0700364 raise ManifestParseError(
365 "failed parsing included manifest %s: %s", (name, e))
Colin Cross23acdd32012-04-21 00:33:54 -0700366 else:
367 nodes.append(node)
368 return nodes
Brian Harring26448742011-04-28 05:04:41 -0700369
Colin Cross23acdd32012-04-21 00:33:54 -0700370 def _ParseManifest(self, node_list):
371 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700372 if node.nodeName == 'remote':
373 remote = self._ParseRemote(node)
David Pursehouse717ece92012-11-13 08:49:16 +0900374 if remote:
375 if remote.name in self._remotes:
376 if remote != self._remotes[remote.name]:
377 raise ManifestParseError(
378 'remote %s already exists with different attributes' %
379 (remote.name))
380 else:
381 self._remotes[remote.name] = remote
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700382
Colin Cross23acdd32012-04-21 00:33:54 -0700383 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700384 if node.nodeName == 'default':
385 if self._default is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800386 raise ManifestParseError(
387 'duplicate default in %s' %
388 (self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700389 self._default = self._ParseDefault(node)
390 if self._default is None:
391 self._default = _Default()
392
Colin Cross23acdd32012-04-21 00:33:54 -0700393 for node in itertools.chain(*node_list):
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700394 if node.nodeName == 'notice':
395 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800396 raise ManifestParseError(
397 'duplicate notice in %s' %
398 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700399 self._notice = self._ParseNotice(node)
400
Colin Cross23acdd32012-04-21 00:33:54 -0700401 for node in itertools.chain(*node_list):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700402 if node.nodeName == 'manifest-server':
403 url = self._reqatt(node, 'url')
404 if self._manifest_server is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800405 raise ManifestParseError(
406 'duplicate manifest-server in %s' %
407 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700408 self._manifest_server = url
409
Colin Cross23acdd32012-04-21 00:33:54 -0700410 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700411 if node.nodeName == 'project':
412 project = self._ParseProject(node)
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700413 if self._projects.get(project.name):
414 raise ManifestParseError(
415 'duplicate project %s in %s' %
416 (project.name, self.manifestFile))
417 self._projects[project.name] = project
Doug Anderson37282b42011-03-04 11:54:18 -0800418 if node.nodeName == 'repo-hooks':
419 # Get the name of the project and the (space-separated) list of enabled.
420 repo_hooks_project = self._reqatt(node, 'in-project')
421 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
422
423 # Only one project can be the hooks project
424 if self._repo_hooks_project is not None:
425 raise ManifestParseError(
426 'duplicate repo-hooks in %s' %
427 (self.manifestFile))
428
429 # Store a reference to the Project.
430 try:
431 self._repo_hooks_project = self._projects[repo_hooks_project]
432 except KeyError:
433 raise ManifestParseError(
434 'project %s not found for repo-hooks' %
435 (repo_hooks_project))
436
437 # Store the enabled hooks in the Project object.
438 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
Colin Cross23acdd32012-04-21 00:33:54 -0700439 if node.nodeName == 'remove-project':
440 name = self._reqatt(node, 'name')
441 try:
442 del self._projects[name]
443 except KeyError:
444 raise ManifestParseError(
445 'project %s not found' %
446 (name))
447
448 # If the manifest removes the hooks project, treat it as if it deleted
449 # the repo-hooks element too.
450 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
451 self._repo_hooks_project = None
452
Doug Anderson37282b42011-03-04 11:54:18 -0800453
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800454 def _AddMetaProjectMirror(self, m):
455 name = None
456 m_url = m.GetRemote(m.remote.name).url
457 if m_url.endswith('/.git'):
458 raise ManifestParseError, 'refusing to mirror %s' % m_url
459
460 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700461 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800462 if not url.endswith('/'):
463 url += '/'
464 if m_url.startswith(url):
465 remote = self._default.remote
466 name = m_url[len(url):]
467
468 if name is None:
469 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700470 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Shawn O. Pearcef35b2d92012-08-02 11:46:22 -0700471 remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800472 name = m_url[s:]
473
474 if name.endswith('.git'):
475 name = name[:-4]
476
477 if name not in self._projects:
478 m.PreSync()
479 gitdir = os.path.join(self.topdir, '%s.git' % name)
480 project = Project(manifest = self,
481 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700482 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800483 gitdir = gitdir,
484 worktree = None,
485 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700486 revisionExpr = m.revisionExpr,
487 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800488 self._projects[project.name] = project
489
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700490 def _ParseRemote(self, node):
491 """
492 reads a <remote> element from the manifest file
493 """
494 name = self._reqatt(node, 'name')
Yestin Sunb292b982012-07-02 07:32:50 -0700495 alias = node.getAttribute('alias')
496 if alias == '':
497 alias = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700498 fetch = self._reqatt(node, 'fetch')
499 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800500 if review == '':
501 review = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700502 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Yestin Sunb292b982012-07-02 07:32:50 -0700503 return _XmlRemote(name, alias, fetch, manifestUrl, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700504
505 def _ParseDefault(self, node):
506 """
507 reads a <default> element from the manifest file
508 """
509 d = _Default()
510 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700511 d.revisionExpr = node.getAttribute('revision')
512 if d.revisionExpr == '':
513 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700514
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700515 sync_j = node.getAttribute('sync-j')
516 if sync_j == '' or sync_j is None:
517 d.sync_j = 1
518 else:
519 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700520
521 sync_c = node.getAttribute('sync-c')
522 if not sync_c:
523 d.sync_c = False
524 else:
525 d.sync_c = sync_c.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700526 return d
527
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700528 def _ParseNotice(self, node):
529 """
530 reads a <notice> element from the manifest file
531
532 The <notice> element is distinct from other tags in the XML in that the
533 data is conveyed between the start and end tag (it's not an empty-element
534 tag).
535
536 The white space (carriage returns, indentation) for the notice element is
537 relevant and is parsed in a way that is based on how python docstrings work.
538 In fact, the code is remarkably similar to here:
539 http://www.python.org/dev/peps/pep-0257/
540 """
541 # Get the data out of the node...
542 notice = node.childNodes[0].data
543
544 # Figure out minimum indentation, skipping the first line (the same line
545 # as the <notice> tag)...
546 minIndent = sys.maxint
547 lines = notice.splitlines()
548 for line in lines[1:]:
549 lstrippedLine = line.lstrip()
550 if lstrippedLine:
551 indent = len(line) - len(lstrippedLine)
552 minIndent = min(indent, minIndent)
553
554 # Strip leading / trailing blank lines and also indentation.
555 cleanLines = [lines[0].strip()]
556 for line in lines[1:]:
557 cleanLines.append(line[minIndent:].rstrip())
558
559 # Clear completely blank lines from front and back...
560 while cleanLines and not cleanLines[0]:
561 del cleanLines[0]
562 while cleanLines and not cleanLines[-1]:
563 del cleanLines[-1]
564
565 return '\n'.join(cleanLines)
566
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700567 def _ParseProject(self, node):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700568 """
569 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700570 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700571 name = self._reqatt(node, 'name')
572
573 remote = self._get_remote(node)
574 if remote is None:
575 remote = self._default.remote
576 if remote is None:
577 raise ManifestParseError, \
578 "no remote for project %s within %s" % \
579 (name, self.manifestFile)
580
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700581 revisionExpr = node.getAttribute('revision')
582 if not revisionExpr:
583 revisionExpr = self._default.revisionExpr
584 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700585 raise ManifestParseError, \
586 "no revision for project %s within %s" % \
587 (name, self.manifestFile)
588
589 path = node.getAttribute('path')
590 if not path:
591 path = name
592 if path.startswith('/'):
593 raise ManifestParseError, \
594 "project %s path cannot be absolute in %s" % \
595 (name, self.manifestFile)
596
Mike Pontillod3153822012-02-28 11:53:24 -0800597 rebase = node.getAttribute('rebase')
598 if not rebase:
599 rebase = True
600 else:
601 rebase = rebase.lower() in ("yes", "true", "1")
602
Anatol Pomazau79770d22012-04-20 14:41:59 -0700603 sync_c = node.getAttribute('sync-c')
604 if not sync_c:
605 sync_c = False
606 else:
607 sync_c = sync_c.lower() in ("yes", "true", "1")
608
Brian Harring14a66742012-09-28 20:21:57 -0700609 upstream = node.getAttribute('upstream')
610
Conley Owens971de8e2012-04-16 10:36:08 -0700611 groups = ''
612 if node.hasAttribute('groups'):
613 groups = node.getAttribute('groups')
David Pursehouse1d947b32012-10-25 12:23:11 +0900614 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Brian Harring7da13142012-06-15 02:24:20 -0700615
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700616 default_groups = ['all', 'name:%s' % name, 'path:%s' % path]
Che-Liang Chiou69998b02012-01-11 11:28:42 +0800617 groups.extend(set(default_groups).difference(groups))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700618
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700619 if self.IsMirror:
620 worktree = None
621 gitdir = os.path.join(self.topdir, '%s.git' % name)
622 else:
623 worktree = os.path.join(self.topdir, path).replace('\\', '/')
624 gitdir = os.path.join(self.repodir, 'projects/%s.git' % path)
625
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700626 project = Project(manifest = self,
627 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700628 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700629 gitdir = gitdir,
630 worktree = worktree,
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700631 relpath = path,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700632 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800633 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700634 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700635 groups = groups,
Brian Harring14a66742012-09-28 20:21:57 -0700636 sync_c = sync_c,
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700637 upstream = upstream)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700638
639 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700640 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700641 self._ParseCopyFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500642 if n.nodeName == 'annotation':
643 self._ParseAnnotation(project, n)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700644
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700645 return project
646
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700647 def _ParseCopyFile(self, project, node):
648 src = self._reqatt(node, 'src')
649 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800650 if not self.IsMirror:
651 # src is project relative;
652 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800653 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700654
James W. Mills24c13082012-04-12 15:04:13 -0500655 def _ParseAnnotation(self, project, node):
656 name = self._reqatt(node, 'name')
657 value = self._reqatt(node, 'value')
658 try:
659 keep = self._reqatt(node, 'keep').lower()
660 except ManifestParseError:
661 keep = "true"
662 if keep != "true" and keep != "false":
663 raise ManifestParseError, "optional \"keep\" attribute must be \"true\" or \"false\""
664 project.AddAnnotation(name, value, keep)
665
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700666 def _get_remote(self, node):
667 name = node.getAttribute('remote')
668 if not name:
669 return None
670
671 v = self._remotes.get(name)
672 if not v:
673 raise ManifestParseError, \
674 "remote %s not defined in %s" % \
675 (name, self.manifestFile)
676 return v
677
678 def _reqatt(self, node, attname):
679 """
680 reads a required attribute from the node.
681 """
682 v = node.getAttribute(attname)
683 if not v:
684 raise ManifestParseError, \
685 "no %s in <%s> within %s" % \
686 (attname, node.nodeName, self.manifestFile)
687 return v