blob: 71dbdc7dc4269497c10168e9ceb1dd9d8a593d18 [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'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070030
Conley Owensdb728cd2011-09-26 16:34:01 -070031urlparse.uses_relative.extend(['ssh', 'git'])
32urlparse.uses_netloc.extend(['ssh', 'git'])
33
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070034class _Default(object):
35 """Project defaults within the manifest."""
36
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -070037 revisionExpr = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070038 remote = None
Shawn O. Pearce6392c872011-09-22 17:44:31 -070039 sync_j = 1
Anatol Pomazau79770d22012-04-20 14:41:59 -070040 sync_c = False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070041
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070042class _XmlRemote(object):
43 def __init__(self,
44 name,
Yestin Sunb292b982012-07-02 07:32:50 -070045 alias=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070046 fetch=None,
Conley Owensdb728cd2011-09-26 16:34:01 -070047 manifestUrl=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070048 review=None):
49 self.name = name
50 self.fetchUrl = fetch
Conley Owensdb728cd2011-09-26 16:34:01 -070051 self.manifestUrl = manifestUrl
Yestin Sunb292b982012-07-02 07:32:50 -070052 self.remoteAlias = alias
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070053 self.reviewUrl = review
Conley Owensceea3682011-10-20 10:45:47 -070054 self.resolvedFetchUrl = self._resolveFetchUrl()
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070055
Conley Owensceea3682011-10-20 10:45:47 -070056 def _resolveFetchUrl(self):
57 url = self.fetchUrl.rstrip('/')
Conley Owensdb728cd2011-09-26 16:34:01 -070058 manifestUrl = self.manifestUrl.rstrip('/')
59 # urljoin will get confused if there is no scheme in the base url
60 # ie, if manifestUrl is of the form <hostname:port>
61 if manifestUrl.find(':') != manifestUrl.find('/') - 1:
62 manifestUrl = 'gopher://' + manifestUrl
63 url = urlparse.urljoin(manifestUrl, url)
Conley Owensceea3682011-10-20 10:45:47 -070064 return re.sub(r'^gopher://', '', url)
65
66 def ToRemoteSpec(self, projectName):
Conley Owens9d8f9142011-10-20 14:36:35 -070067 url = self.resolvedFetchUrl.rstrip('/') + '/' + projectName
Yestin Sunb292b982012-07-02 07:32:50 -070068 remoteName = self.name
69 if self.remoteAlias:
70 remoteName = self.remoteAlias
71 return RemoteSpec(remoteName, url, self.reviewUrl)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070072
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070073class XmlManifest(object):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070074 """manages the repo configuration file"""
75
76 def __init__(self, repodir):
77 self.repodir = os.path.abspath(repodir)
78 self.topdir = os.path.dirname(self.repodir)
79 self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070080 self.globalConfig = GitConfig.ForUser()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070081
82 self.repoProject = MetaProject(self, 'repo',
83 gitdir = os.path.join(repodir, 'repo/.git'),
84 worktree = os.path.join(repodir, 'repo'))
85
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070086 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -080087 gitdir = os.path.join(repodir, 'manifests.git'),
88 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070089
90 self._Unload()
91
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -070092 def Override(self, name):
93 """Use a different manifest, just for the current instantiation.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070094 """
95 path = os.path.join(self.manifestProject.worktree, name)
96 if not os.path.isfile(path):
97 raise ManifestParseError('manifest %s not found' % name)
98
99 old = self.manifestFile
100 try:
101 self.manifestFile = path
102 self._Unload()
103 self._Load()
104 finally:
105 self.manifestFile = old
106
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700107 def Link(self, name):
108 """Update the repo metadata to use a different manifest.
109 """
110 self.Override(name)
111
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700112 try:
113 if os.path.exists(self.manifestFile):
114 os.remove(self.manifestFile)
115 os.symlink('manifests/%s' % name, self.manifestFile)
David Pursehouse8a68ff92012-09-24 12:15:13 +0900116 except OSError:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700117 raise ManifestParseError('cannot link manifest %s' % name)
118
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800119 def _RemoteToXml(self, r, doc, root):
120 e = doc.createElement('remote')
121 root.appendChild(e)
122 e.setAttribute('name', r.name)
123 e.setAttribute('fetch', r.fetchUrl)
124 if r.reviewUrl is not None:
125 e.setAttribute('review', r.reviewUrl)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800126
Brian Harring14a66742012-09-28 20:21:57 -0700127 def Save(self, fd, peg_rev=False, peg_rev_upstream=True):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800128 """Write the current manifest out to the given file descriptor.
129 """
Colin Cross5acde752012-03-28 20:15:45 -0700130 mp = self.manifestProject
131
132 groups = mp.config.GetString('manifest.groups')
Colin Crossc39864f2012-04-23 13:41:58 -0700133 if not groups:
Conley Owensbb1b5f52012-08-13 13:11:18 -0700134 groups = 'all'
Conley Owens971de8e2012-04-16 10:36:08 -0700135 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Colin Cross5acde752012-03-28 20:15:45 -0700136
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800137 doc = xml.dom.minidom.Document()
138 root = doc.createElement('manifest')
139 doc.appendChild(root)
140
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700141 # Save out the notice. There's a little bit of work here to give it the
142 # right whitespace, which assumes that the notice is automatically indented
143 # by 4 by minidom.
144 if self.notice:
145 notice_element = root.appendChild(doc.createElement('notice'))
146 notice_lines = self.notice.splitlines()
147 indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:]
148 notice_element.appendChild(doc.createTextNode(indented_notice))
149
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800150 d = self.default
151 sort_remotes = list(self.remotes.keys())
152 sort_remotes.sort()
153
154 for r in sort_remotes:
155 self._RemoteToXml(self.remotes[r], doc, root)
156 if self.remotes:
157 root.appendChild(doc.createTextNode(''))
158
159 have_default = False
160 e = doc.createElement('default')
161 if d.remote:
162 have_default = True
163 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700164 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800165 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700166 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700167 if d.sync_j > 1:
168 have_default = True
169 e.setAttribute('sync-j', '%d' % d.sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700170 if d.sync_c:
171 have_default = True
172 e.setAttribute('sync-c', 'true')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800173 if have_default:
174 root.appendChild(e)
175 root.appendChild(doc.createTextNode(''))
176
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700177 if self._manifest_server:
178 e = doc.createElement('manifest-server')
179 e.setAttribute('url', self._manifest_server)
180 root.appendChild(e)
181 root.appendChild(doc.createTextNode(''))
182
Che-Liang Chiou69998b02012-01-11 11:28:42 +0800183 def output_projects(parent, parent_node, projects):
184 for p in projects:
185 output_project(parent, parent_node, self.projects[p])
Colin Cross5acde752012-03-28 20:15:45 -0700186
Che-Liang Chiou69998b02012-01-11 11:28:42 +0800187 def output_project(parent, parent_node, p):
Colin Cross5acde752012-03-28 20:15:45 -0700188 if not p.MatchesGroups(groups):
Che-Liang Chiou69998b02012-01-11 11:28:42 +0800189 return
190
191 name = p.name
192 relpath = p.relpath
193 if parent:
194 name = self._UnjoinName(parent.name, name)
195 relpath = self._UnjoinRelpath(parent.relpath, relpath)
Colin Cross5acde752012-03-28 20:15:45 -0700196
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800197 e = doc.createElement('project')
Che-Liang Chiou69998b02012-01-11 11:28:42 +0800198 parent_node.appendChild(e)
199 e.setAttribute('name', name)
200 if relpath != name:
201 e.setAttribute('path', relpath)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800202 if not d.remote or p.remote.name != d.remote.name:
203 e.setAttribute('remote', p.remote.name)
204 if peg_rev:
205 if self.IsMirror:
Brian Harring14a66742012-09-28 20:21:57 -0700206 value = p.bare_git.rev_parse(p.revisionExpr + '^0')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800207 else:
Brian Harring14a66742012-09-28 20:21:57 -0700208 value = p.work_git.rev_parse(HEAD + '^0')
209 e.setAttribute('revision', value)
210 if peg_rev_upstream and value != p.revisionExpr:
211 # Only save the origin if the origin is not a sha1, and the default
212 # isn't our value, and the if the default doesn't already have that
213 # covered.
214 e.setAttribute('upstream', p.revisionExpr)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700215 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
216 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800217
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800218 for c in p.copyfiles:
219 ce = doc.createElement('copyfile')
220 ce.setAttribute('src', c.src)
221 ce.setAttribute('dest', c.dest)
222 e.appendChild(ce)
223
Conley Owensbb1b5f52012-08-13 13:11:18 -0700224 default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath]
Dmitry Fink17f85ea2012-08-06 14:52:29 -0700225 egroups = [g for g in p.groups if g not in default_groups]
Conley Owens971de8e2012-04-16 10:36:08 -0700226 if egroups:
227 e.setAttribute('groups', ','.join(egroups))
Colin Cross5acde752012-03-28 20:15:45 -0700228
James W. Mills24c13082012-04-12 15:04:13 -0500229 for a in p.annotations:
230 if a.keep == "true":
231 ae = doc.createElement('annotation')
232 ae.setAttribute('name', a.name)
233 ae.setAttribute('value', a.value)
234 e.appendChild(ae)
235
Anatol Pomazau79770d22012-04-20 14:41:59 -0700236 if p.sync_c:
237 e.setAttribute('sync-c', 'true')
238
Che-Liang Chiou69998b02012-01-11 11:28:42 +0800239 if p.subprojects:
240 sort_projects = [subp.name for subp in p.subprojects]
241 sort_projects.sort()
242 output_projects(p, e, sort_projects)
243
244 sort_projects = [key for key in self.projects.keys()
245 if not self.projects[key].parent]
246 sort_projects.sort()
247 output_projects(None, root, sort_projects)
248
Doug Anderson37282b42011-03-04 11:54:18 -0800249 if self._repo_hooks_project:
250 root.appendChild(doc.createTextNode(''))
251 e = doc.createElement('repo-hooks')
252 e.setAttribute('in-project', self._repo_hooks_project.name)
253 e.setAttribute('enabled-list',
254 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
255 root.appendChild(e)
256
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800257 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
258
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700259 @property
260 def projects(self):
261 self._Load()
262 return self._projects
263
264 @property
265 def remotes(self):
266 self._Load()
267 return self._remotes
268
269 @property
270 def default(self):
271 self._Load()
272 return self._default
273
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800274 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800275 def repo_hooks_project(self):
276 self._Load()
277 return self._repo_hooks_project
278
279 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700280 def notice(self):
281 self._Load()
282 return self._notice
283
284 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700285 def manifest_server(self):
286 self._Load()
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800287 return self._manifest_server
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700288
289 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800290 def IsMirror(self):
291 return self.manifestProject.config.GetBoolean('repo.mirror')
292
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700293 def _Unload(self):
294 self._loaded = False
295 self._projects = {}
296 self._remotes = {}
297 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800298 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700299 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700300 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700301 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700302
303 def _Load(self):
304 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800305 m = self.manifestProject
306 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700307 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800308 b = b[len(R_HEADS):]
309 self.branch = b
310
Colin Cross23acdd32012-04-21 00:33:54 -0700311 nodes = []
Brian Harring475a47d2012-06-07 20:05:35 -0700312 nodes.append(self._ParseManifestXml(self.manifestFile,
313 self.manifestProject.worktree))
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700314
315 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
316 if os.path.exists(local):
Brian Harring475a47d2012-06-07 20:05:35 -0700317 nodes.append(self._ParseManifestXml(local, self.repodir))
Colin Cross23acdd32012-04-21 00:33:54 -0700318
319 self._ParseManifest(nodes)
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700320
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800321 if self.IsMirror:
322 self._AddMetaProjectMirror(self.repoProject)
323 self._AddMetaProjectMirror(self.manifestProject)
324
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700325 self._loaded = True
326
Brian Harring475a47d2012-06-07 20:05:35 -0700327 def _ParseManifestXml(self, path, include_root):
Brian Harring26448742011-04-28 05:04:41 -0700328 root = xml.dom.minidom.parse(path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700329 if not root or not root.childNodes:
Brian Harring26448742011-04-28 05:04:41 -0700330 raise ManifestParseError("no root node in %s" % (path,))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700331
Jooncheol Park34acdd22012-08-27 02:25:59 +0900332 for manifest in root.childNodes:
333 if manifest.nodeName == 'manifest':
334 break
335 else:
Brian Harring26448742011-04-28 05:04:41 -0700336 raise ManifestParseError("no <manifest> in %s" % (path,))
337
Colin Cross23acdd32012-04-21 00:33:54 -0700338 nodes = []
David Pursehouse4f7bdea2012-10-22 12:50:15 +0900339 for node in manifest.childNodes: # pylint:disable=W0631
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900340 # We only get here if manifest is initialised
Brian Harring26448742011-04-28 05:04:41 -0700341 if node.nodeName == 'include':
342 name = self._reqatt(node, 'name')
Brian Harring475a47d2012-06-07 20:05:35 -0700343 fp = os.path.join(include_root, name)
Brian Harring26448742011-04-28 05:04:41 -0700344 if not os.path.isfile(fp):
345 raise ManifestParseError, \
346 "include %s doesn't exist or isn't a file" % \
347 (name,)
348 try:
Brian Harring475a47d2012-06-07 20:05:35 -0700349 nodes.extend(self._ParseManifestXml(fp, include_root))
Brian Harring26448742011-04-28 05:04:41 -0700350 # should isolate this to the exact exception, but that's
351 # tricky. actual parsing implementation may vary.
352 except (KeyboardInterrupt, RuntimeError, SystemExit):
353 raise
354 except Exception, e:
355 raise ManifestParseError(
356 "failed parsing included manifest %s: %s", (name, e))
Colin Cross23acdd32012-04-21 00:33:54 -0700357 else:
358 nodes.append(node)
359 return nodes
Shawn O. Pearce03eaf072008-11-20 11:42:22 -0800360
Colin Cross23acdd32012-04-21 00:33:54 -0700361 def _ParseManifest(self, node_list):
362 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700363 if node.nodeName == 'remote':
364 remote = self._ParseRemote(node)
365 if self._remotes.get(remote.name):
Doug Anderson37282b42011-03-04 11:54:18 -0800366 raise ManifestParseError(
367 'duplicate remote %s in %s' %
368 (remote.name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700369 self._remotes[remote.name] = remote
370
Colin Cross23acdd32012-04-21 00:33:54 -0700371 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700372 if node.nodeName == 'default':
373 if self._default is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800374 raise ManifestParseError(
375 'duplicate default in %s' %
376 (self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700377 self._default = self._ParseDefault(node)
378 if self._default is None:
379 self._default = _Default()
380
Colin Cross23acdd32012-04-21 00:33:54 -0700381 for node in itertools.chain(*node_list):
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700382 if node.nodeName == 'notice':
383 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800384 raise ManifestParseError(
385 'duplicate notice in %s' %
386 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700387 self._notice = self._ParseNotice(node)
388
Colin Cross23acdd32012-04-21 00:33:54 -0700389 for node in itertools.chain(*node_list):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700390 if node.nodeName == 'manifest-server':
391 url = self._reqatt(node, 'url')
392 if self._manifest_server is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800393 raise ManifestParseError(
394 'duplicate manifest-server in %s' %
395 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700396 self._manifest_server = url
397
Colin Cross23acdd32012-04-21 00:33:54 -0700398 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700399 if node.nodeName == 'project':
400 project = self._ParseProject(node)
Che-Liang Chiou69998b02012-01-11 11:28:42 +0800401 def recursively_add_projects(project):
402 if self._projects.get(project.name):
403 raise ManifestParseError(
404 'duplicate project %s in %s' %
405 (project.name, self.manifestFile))
406 self._projects[project.name] = project
407 for subproject in project.subprojects:
408 recursively_add_projects(subproject)
409 recursively_add_projects(project)
Doug Anderson37282b42011-03-04 11:54:18 -0800410 if node.nodeName == 'repo-hooks':
411 # Get the name of the project and the (space-separated) list of enabled.
412 repo_hooks_project = self._reqatt(node, 'in-project')
413 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
414
415 # Only one project can be the hooks project
416 if self._repo_hooks_project is not None:
417 raise ManifestParseError(
418 'duplicate repo-hooks in %s' %
419 (self.manifestFile))
420
421 # Store a reference to the Project.
422 try:
423 self._repo_hooks_project = self._projects[repo_hooks_project]
424 except KeyError:
425 raise ManifestParseError(
426 'project %s not found for repo-hooks' %
427 (repo_hooks_project))
428
429 # Store the enabled hooks in the Project object.
430 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
Colin Cross23acdd32012-04-21 00:33:54 -0700431 if node.nodeName == 'remove-project':
432 name = self._reqatt(node, 'name')
433 try:
434 del self._projects[name]
435 except KeyError:
436 raise ManifestParseError(
437 'project %s not found' %
438 (name))
439
440 # If the manifest removes the hooks project, treat it as if it deleted
441 # the repo-hooks element too.
442 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
443 self._repo_hooks_project = None
444
Doug Anderson37282b42011-03-04 11:54:18 -0800445
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800446 def _AddMetaProjectMirror(self, m):
447 name = None
448 m_url = m.GetRemote(m.remote.name).url
449 if m_url.endswith('/.git'):
450 raise ManifestParseError, 'refusing to mirror %s' % m_url
451
452 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700453 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800454 if not url.endswith('/'):
455 url += '/'
456 if m_url.startswith(url):
457 remote = self._default.remote
458 name = m_url[len(url):]
459
460 if name is None:
461 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700462 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Shawn O. Pearcef35b2d92012-08-02 11:46:22 -0700463 remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800464 name = m_url[s:]
465
466 if name.endswith('.git'):
467 name = name[:-4]
468
469 if name not in self._projects:
470 m.PreSync()
471 gitdir = os.path.join(self.topdir, '%s.git' % name)
472 project = Project(manifest = self,
473 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700474 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800475 gitdir = gitdir,
476 worktree = None,
477 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700478 revisionExpr = m.revisionExpr,
479 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800480 self._projects[project.name] = project
481
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700482 def _ParseRemote(self, node):
483 """
484 reads a <remote> element from the manifest file
485 """
486 name = self._reqatt(node, 'name')
Yestin Sunb292b982012-07-02 07:32:50 -0700487 alias = node.getAttribute('alias')
488 if alias == '':
489 alias = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700490 fetch = self._reqatt(node, 'fetch')
491 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800492 if review == '':
493 review = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700494 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Yestin Sunb292b982012-07-02 07:32:50 -0700495 return _XmlRemote(name, alias, fetch, manifestUrl, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700496
497 def _ParseDefault(self, node):
498 """
499 reads a <default> element from the manifest file
500 """
501 d = _Default()
502 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700503 d.revisionExpr = node.getAttribute('revision')
504 if d.revisionExpr == '':
505 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700506
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700507 sync_j = node.getAttribute('sync-j')
508 if sync_j == '' or sync_j is None:
509 d.sync_j = 1
510 else:
511 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700512
513 sync_c = node.getAttribute('sync-c')
514 if not sync_c:
515 d.sync_c = False
516 else:
517 d.sync_c = sync_c.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700518 return d
519
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700520 def _ParseNotice(self, node):
521 """
522 reads a <notice> element from the manifest file
523
524 The <notice> element is distinct from other tags in the XML in that the
525 data is conveyed between the start and end tag (it's not an empty-element
526 tag).
527
528 The white space (carriage returns, indentation) for the notice element is
529 relevant and is parsed in a way that is based on how python docstrings work.
530 In fact, the code is remarkably similar to here:
531 http://www.python.org/dev/peps/pep-0257/
532 """
533 # Get the data out of the node...
534 notice = node.childNodes[0].data
535
536 # Figure out minimum indentation, skipping the first line (the same line
537 # as the <notice> tag)...
538 minIndent = sys.maxint
539 lines = notice.splitlines()
540 for line in lines[1:]:
541 lstrippedLine = line.lstrip()
542 if lstrippedLine:
543 indent = len(line) - len(lstrippedLine)
544 minIndent = min(indent, minIndent)
545
546 # Strip leading / trailing blank lines and also indentation.
547 cleanLines = [lines[0].strip()]
548 for line in lines[1:]:
549 cleanLines.append(line[minIndent:].rstrip())
550
551 # Clear completely blank lines from front and back...
552 while cleanLines and not cleanLines[0]:
553 del cleanLines[0]
554 while cleanLines and not cleanLines[-1]:
555 del cleanLines[-1]
556
557 return '\n'.join(cleanLines)
558
Che-Liang Chiou69998b02012-01-11 11:28:42 +0800559 def _JoinName(self, parent_name, name):
560 return os.path.join(parent_name, name)
561
562 def _UnjoinName(self, parent_name, name):
563 return os.path.relpath(name, parent_name)
564
565 def _ParseProject(self, node, parent = None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700566 """
567 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700568 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700569 name = self._reqatt(node, 'name')
Che-Liang Chiou69998b02012-01-11 11:28:42 +0800570 if parent:
571 name = self._JoinName(parent.name, name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700572
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')
614 groups = [x for x in re.split('[,\s]+', groups) if x]
Brian Harring7da13142012-06-15 02:24:20 -0700615
Che-Liang Chiou69998b02012-01-11 11:28:42 +0800616 if parent is None:
617 relpath, worktree, gitdir = self.GetProjectPaths(name, path)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800618 else:
Che-Liang Chiou69998b02012-01-11 11:28:42 +0800619 relpath, worktree, gitdir = self.GetSubprojectPaths(parent, path)
620
621 default_groups = ['all', 'name:%s' % name, 'path:%s' % relpath]
622 groups.extend(set(default_groups).difference(groups))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700623
624 project = Project(manifest = self,
625 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700626 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700627 gitdir = gitdir,
628 worktree = worktree,
Che-Liang Chiou69998b02012-01-11 11:28:42 +0800629 relpath = relpath,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700630 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800631 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700632 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700633 groups = groups,
Brian Harring14a66742012-09-28 20:21:57 -0700634 sync_c = sync_c,
Che-Liang Chiou69998b02012-01-11 11:28:42 +0800635 upstream = upstream,
636 parent = parent)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700637
638 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700639 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700640 self._ParseCopyFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500641 if n.nodeName == 'annotation':
642 self._ParseAnnotation(project, n)
Che-Liang Chiou69998b02012-01-11 11:28:42 +0800643 if n.nodeName == 'project':
644 project.subprojects.append(self._ParseProject(n, parent = project))
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
Che-Liang Chiou69998b02012-01-11 11:28:42 +0800648 def GetProjectPaths(self, name, path):
649 relpath = path
650 if self.IsMirror:
651 worktree = None
652 gitdir = os.path.join(self.topdir, '%s.git' % name)
653 else:
654 worktree = os.path.join(self.topdir, path).replace('\\', '/')
655 gitdir = os.path.join(self.repodir, 'projects', '%s.git' % path)
656 return relpath, worktree, gitdir
657
658 def GetSubprojectName(self, parent, submodule_path):
659 return os.path.join(parent.name, submodule_path)
660
661 def _JoinRelpath(self, parent_relpath, relpath):
662 return os.path.join(parent_relpath, relpath)
663
664 def _UnjoinRelpath(self, parent_relpath, relpath):
665 return os.path.relpath(relpath, parent_relpath)
666
667 def GetSubprojectPaths(self, parent, path):
668 relpath = self._JoinRelpath(parent.relpath, path)
669 gitdir = os.path.join(parent.gitdir, 'subprojects', '%s.git' % path)
670 if self.IsMirror:
671 worktree = None
672 else:
673 worktree = os.path.join(parent.worktree, path).replace('\\', '/')
674 return relpath, worktree, gitdir
675
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700676 def _ParseCopyFile(self, project, node):
677 src = self._reqatt(node, 'src')
678 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800679 if not self.IsMirror:
680 # src is project relative;
681 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800682 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700683
James W. Mills24c13082012-04-12 15:04:13 -0500684 def _ParseAnnotation(self, project, node):
685 name = self._reqatt(node, 'name')
686 value = self._reqatt(node, 'value')
687 try:
688 keep = self._reqatt(node, 'keep').lower()
689 except ManifestParseError:
690 keep = "true"
691 if keep != "true" and keep != "false":
692 raise ManifestParseError, "optional \"keep\" attribute must be \"true\" or \"false\""
693 project.AddAnnotation(name, value, keep)
694
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700695 def _get_remote(self, node):
696 name = node.getAttribute('remote')
697 if not name:
698 return None
699
700 v = self._remotes.get(name)
701 if not v:
702 raise ManifestParseError, \
703 "remote %s not defined in %s" % \
704 (name, self.manifestFile)
705 return v
706
707 def _reqatt(self, node, attname):
708 """
709 reads a required attribute from the node.
710 """
711 v = node.getAttribute(attname)
712 if not v:
713 raise ManifestParseError, \
714 "no %s in <%s> within %s" % \
715 (attname, node.nodeName, self.manifestFile)
716 return v