blob: bf981f032b2b1d4b3b63fd6f3db5e79c9f8f0406 [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
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700183 sort_projects = list(self.projects.keys())
184 sort_projects.sort()
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800185
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700186 for p in sort_projects:
187 p = self.projects[p]
188
Colin Cross5acde752012-03-28 20:15:45 -0700189 if not p.MatchesGroups(groups):
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700190 continue
Colin Cross5acde752012-03-28 20:15:45 -0700191
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800192 e = doc.createElement('project')
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700193 root.appendChild(e)
194 e.setAttribute('name', p.name)
195 if p.relpath != p.name:
196 e.setAttribute('path', p.relpath)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800197 if not d.remote or p.remote.name != d.remote.name:
198 e.setAttribute('remote', p.remote.name)
199 if peg_rev:
200 if self.IsMirror:
Brian Harring14a66742012-09-28 20:21:57 -0700201 value = p.bare_git.rev_parse(p.revisionExpr + '^0')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800202 else:
Brian Harring14a66742012-09-28 20:21:57 -0700203 value = p.work_git.rev_parse(HEAD + '^0')
204 e.setAttribute('revision', value)
205 if peg_rev_upstream and value != p.revisionExpr:
206 # Only save the origin if the origin is not a sha1, and the default
207 # isn't our value, and the if the default doesn't already have that
208 # covered.
209 e.setAttribute('upstream', p.revisionExpr)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700210 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
211 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800212
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800213 for c in p.copyfiles:
214 ce = doc.createElement('copyfile')
215 ce.setAttribute('src', c.src)
216 ce.setAttribute('dest', c.dest)
217 e.appendChild(ce)
218
Conley Owensbb1b5f52012-08-13 13:11:18 -0700219 default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath]
Dmitry Fink17f85ea2012-08-06 14:52:29 -0700220 egroups = [g for g in p.groups if g not in default_groups]
Conley Owens971de8e2012-04-16 10:36:08 -0700221 if egroups:
222 e.setAttribute('groups', ','.join(egroups))
Colin Cross5acde752012-03-28 20:15:45 -0700223
James W. Mills24c13082012-04-12 15:04:13 -0500224 for a in p.annotations:
225 if a.keep == "true":
226 ae = doc.createElement('annotation')
227 ae.setAttribute('name', a.name)
228 ae.setAttribute('value', a.value)
229 e.appendChild(ae)
230
Anatol Pomazau79770d22012-04-20 14:41:59 -0700231 if p.sync_c:
232 e.setAttribute('sync-c', 'true')
233
Doug Anderson37282b42011-03-04 11:54:18 -0800234 if self._repo_hooks_project:
235 root.appendChild(doc.createTextNode(''))
236 e = doc.createElement('repo-hooks')
237 e.setAttribute('in-project', self._repo_hooks_project.name)
238 e.setAttribute('enabled-list',
239 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
240 root.appendChild(e)
241
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800242 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
243
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700244 @property
245 def projects(self):
246 self._Load()
247 return self._projects
248
249 @property
250 def remotes(self):
251 self._Load()
252 return self._remotes
253
254 @property
255 def default(self):
256 self._Load()
257 return self._default
258
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800259 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800260 def repo_hooks_project(self):
261 self._Load()
262 return self._repo_hooks_project
263
264 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700265 def notice(self):
266 self._Load()
267 return self._notice
268
269 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700270 def manifest_server(self):
271 self._Load()
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800272 return self._manifest_server
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700273
274 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800275 def IsMirror(self):
276 return self.manifestProject.config.GetBoolean('repo.mirror')
277
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700278 def _Unload(self):
279 self._loaded = False
280 self._projects = {}
281 self._remotes = {}
282 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800283 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700284 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700285 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700286 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700287
288 def _Load(self):
289 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800290 m = self.manifestProject
291 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700292 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800293 b = b[len(R_HEADS):]
294 self.branch = b
295
Colin Cross23acdd32012-04-21 00:33:54 -0700296 nodes = []
Brian Harring475a47d2012-06-07 20:05:35 -0700297 nodes.append(self._ParseManifestXml(self.manifestFile,
298 self.manifestProject.worktree))
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700299
300 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
301 if os.path.exists(local):
Brian Harring475a47d2012-06-07 20:05:35 -0700302 nodes.append(self._ParseManifestXml(local, self.repodir))
Colin Cross23acdd32012-04-21 00:33:54 -0700303
304 self._ParseManifest(nodes)
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700305
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800306 if self.IsMirror:
307 self._AddMetaProjectMirror(self.repoProject)
308 self._AddMetaProjectMirror(self.manifestProject)
309
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700310 self._loaded = True
311
Brian Harring475a47d2012-06-07 20:05:35 -0700312 def _ParseManifestXml(self, path, include_root):
David Pursehousef7fc8a92012-11-13 04:00:28 +0900313 try:
314 root = xml.dom.minidom.parse(path)
315 except (OSError, xml.parsers.expat.ExpatError), e:
316 raise ManifestParseError("error parsing manifest %s: %s" % (path, e))
317
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700318 if not root or not root.childNodes:
Brian Harring26448742011-04-28 05:04:41 -0700319 raise ManifestParseError("no root node in %s" % (path,))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700320
Jooncheol Park34acdd22012-08-27 02:25:59 +0900321 for manifest in root.childNodes:
322 if manifest.nodeName == 'manifest':
323 break
324 else:
Brian Harring26448742011-04-28 05:04:41 -0700325 raise ManifestParseError("no <manifest> in %s" % (path,))
326
Colin Cross23acdd32012-04-21 00:33:54 -0700327 nodes = []
David Pursehouse4f7bdea2012-10-22 12:50:15 +0900328 for node in manifest.childNodes: # pylint:disable=W0631
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900329 # We only get here if manifest is initialised
Brian Harring26448742011-04-28 05:04:41 -0700330 if node.nodeName == 'include':
331 name = self._reqatt(node, 'name')
Brian Harring475a47d2012-06-07 20:05:35 -0700332 fp = os.path.join(include_root, name)
Brian Harring26448742011-04-28 05:04:41 -0700333 if not os.path.isfile(fp):
334 raise ManifestParseError, \
335 "include %s doesn't exist or isn't a file" % \
336 (name,)
337 try:
Brian Harring475a47d2012-06-07 20:05:35 -0700338 nodes.extend(self._ParseManifestXml(fp, include_root))
Brian Harring26448742011-04-28 05:04:41 -0700339 # should isolate this to the exact exception, but that's
340 # tricky. actual parsing implementation may vary.
341 except (KeyboardInterrupt, RuntimeError, SystemExit):
342 raise
Sarah Owensa5be53f2012-09-09 15:37:57 -0700343 except Exception as e:
Brian Harring26448742011-04-28 05:04:41 -0700344 raise ManifestParseError(
345 "failed parsing included manifest %s: %s", (name, e))
Colin Cross23acdd32012-04-21 00:33:54 -0700346 else:
347 nodes.append(node)
348 return nodes
Brian Harring26448742011-04-28 05:04:41 -0700349
Colin Cross23acdd32012-04-21 00:33:54 -0700350 def _ParseManifest(self, node_list):
351 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700352 if node.nodeName == 'remote':
353 remote = self._ParseRemote(node)
354 if self._remotes.get(remote.name):
Doug Anderson37282b42011-03-04 11:54:18 -0800355 raise ManifestParseError(
356 'duplicate remote %s in %s' %
357 (remote.name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700358 self._remotes[remote.name] = remote
359
Colin Cross23acdd32012-04-21 00:33:54 -0700360 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700361 if node.nodeName == 'default':
362 if self._default is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800363 raise ManifestParseError(
364 'duplicate default in %s' %
365 (self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700366 self._default = self._ParseDefault(node)
367 if self._default is None:
368 self._default = _Default()
369
Colin Cross23acdd32012-04-21 00:33:54 -0700370 for node in itertools.chain(*node_list):
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700371 if node.nodeName == 'notice':
372 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800373 raise ManifestParseError(
374 'duplicate notice in %s' %
375 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700376 self._notice = self._ParseNotice(node)
377
Colin Cross23acdd32012-04-21 00:33:54 -0700378 for node in itertools.chain(*node_list):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700379 if node.nodeName == 'manifest-server':
380 url = self._reqatt(node, 'url')
381 if self._manifest_server is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800382 raise ManifestParseError(
383 'duplicate manifest-server in %s' %
384 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700385 self._manifest_server = url
386
Colin Cross23acdd32012-04-21 00:33:54 -0700387 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700388 if node.nodeName == 'project':
389 project = self._ParseProject(node)
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700390 if self._projects.get(project.name):
391 raise ManifestParseError(
392 'duplicate project %s in %s' %
393 (project.name, self.manifestFile))
394 self._projects[project.name] = project
Doug Anderson37282b42011-03-04 11:54:18 -0800395 if node.nodeName == 'repo-hooks':
396 # Get the name of the project and the (space-separated) list of enabled.
397 repo_hooks_project = self._reqatt(node, 'in-project')
398 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
399
400 # Only one project can be the hooks project
401 if self._repo_hooks_project is not None:
402 raise ManifestParseError(
403 'duplicate repo-hooks in %s' %
404 (self.manifestFile))
405
406 # Store a reference to the Project.
407 try:
408 self._repo_hooks_project = self._projects[repo_hooks_project]
409 except KeyError:
410 raise ManifestParseError(
411 'project %s not found for repo-hooks' %
412 (repo_hooks_project))
413
414 # Store the enabled hooks in the Project object.
415 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
Colin Cross23acdd32012-04-21 00:33:54 -0700416 if node.nodeName == 'remove-project':
417 name = self._reqatt(node, 'name')
418 try:
419 del self._projects[name]
420 except KeyError:
421 raise ManifestParseError(
422 'project %s not found' %
423 (name))
424
425 # If the manifest removes the hooks project, treat it as if it deleted
426 # the repo-hooks element too.
427 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
428 self._repo_hooks_project = None
429
Doug Anderson37282b42011-03-04 11:54:18 -0800430
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800431 def _AddMetaProjectMirror(self, m):
432 name = None
433 m_url = m.GetRemote(m.remote.name).url
434 if m_url.endswith('/.git'):
435 raise ManifestParseError, 'refusing to mirror %s' % m_url
436
437 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700438 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800439 if not url.endswith('/'):
440 url += '/'
441 if m_url.startswith(url):
442 remote = self._default.remote
443 name = m_url[len(url):]
444
445 if name is None:
446 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700447 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Shawn O. Pearcef35b2d92012-08-02 11:46:22 -0700448 remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800449 name = m_url[s:]
450
451 if name.endswith('.git'):
452 name = name[:-4]
453
454 if name not in self._projects:
455 m.PreSync()
456 gitdir = os.path.join(self.topdir, '%s.git' % name)
457 project = Project(manifest = self,
458 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700459 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800460 gitdir = gitdir,
461 worktree = None,
462 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700463 revisionExpr = m.revisionExpr,
464 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800465 self._projects[project.name] = project
466
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700467 def _ParseRemote(self, node):
468 """
469 reads a <remote> element from the manifest file
470 """
471 name = self._reqatt(node, 'name')
Yestin Sunb292b982012-07-02 07:32:50 -0700472 alias = node.getAttribute('alias')
473 if alias == '':
474 alias = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700475 fetch = self._reqatt(node, 'fetch')
476 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800477 if review == '':
478 review = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700479 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Yestin Sunb292b982012-07-02 07:32:50 -0700480 return _XmlRemote(name, alias, fetch, manifestUrl, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700481
482 def _ParseDefault(self, node):
483 """
484 reads a <default> element from the manifest file
485 """
486 d = _Default()
487 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700488 d.revisionExpr = node.getAttribute('revision')
489 if d.revisionExpr == '':
490 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700491
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700492 sync_j = node.getAttribute('sync-j')
493 if sync_j == '' or sync_j is None:
494 d.sync_j = 1
495 else:
496 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700497
498 sync_c = node.getAttribute('sync-c')
499 if not sync_c:
500 d.sync_c = False
501 else:
502 d.sync_c = sync_c.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700503 return d
504
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700505 def _ParseNotice(self, node):
506 """
507 reads a <notice> element from the manifest file
508
509 The <notice> element is distinct from other tags in the XML in that the
510 data is conveyed between the start and end tag (it's not an empty-element
511 tag).
512
513 The white space (carriage returns, indentation) for the notice element is
514 relevant and is parsed in a way that is based on how python docstrings work.
515 In fact, the code is remarkably similar to here:
516 http://www.python.org/dev/peps/pep-0257/
517 """
518 # Get the data out of the node...
519 notice = node.childNodes[0].data
520
521 # Figure out minimum indentation, skipping the first line (the same line
522 # as the <notice> tag)...
523 minIndent = sys.maxint
524 lines = notice.splitlines()
525 for line in lines[1:]:
526 lstrippedLine = line.lstrip()
527 if lstrippedLine:
528 indent = len(line) - len(lstrippedLine)
529 minIndent = min(indent, minIndent)
530
531 # Strip leading / trailing blank lines and also indentation.
532 cleanLines = [lines[0].strip()]
533 for line in lines[1:]:
534 cleanLines.append(line[minIndent:].rstrip())
535
536 # Clear completely blank lines from front and back...
537 while cleanLines and not cleanLines[0]:
538 del cleanLines[0]
539 while cleanLines and not cleanLines[-1]:
540 del cleanLines[-1]
541
542 return '\n'.join(cleanLines)
543
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700544 def _ParseProject(self, node):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700545 """
546 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700547 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700548 name = self._reqatt(node, 'name')
549
550 remote = self._get_remote(node)
551 if remote is None:
552 remote = self._default.remote
553 if remote is None:
554 raise ManifestParseError, \
555 "no remote for project %s within %s" % \
556 (name, self.manifestFile)
557
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700558 revisionExpr = node.getAttribute('revision')
559 if not revisionExpr:
560 revisionExpr = self._default.revisionExpr
561 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700562 raise ManifestParseError, \
563 "no revision for project %s within %s" % \
564 (name, self.manifestFile)
565
566 path = node.getAttribute('path')
567 if not path:
568 path = name
569 if path.startswith('/'):
570 raise ManifestParseError, \
571 "project %s path cannot be absolute in %s" % \
572 (name, self.manifestFile)
573
Mike Pontillod3153822012-02-28 11:53:24 -0800574 rebase = node.getAttribute('rebase')
575 if not rebase:
576 rebase = True
577 else:
578 rebase = rebase.lower() in ("yes", "true", "1")
579
Anatol Pomazau79770d22012-04-20 14:41:59 -0700580 sync_c = node.getAttribute('sync-c')
581 if not sync_c:
582 sync_c = False
583 else:
584 sync_c = sync_c.lower() in ("yes", "true", "1")
585
Brian Harring14a66742012-09-28 20:21:57 -0700586 upstream = node.getAttribute('upstream')
587
Conley Owens971de8e2012-04-16 10:36:08 -0700588 groups = ''
589 if node.hasAttribute('groups'):
590 groups = node.getAttribute('groups')
David Pursehouse1d947b32012-10-25 12:23:11 +0900591 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Brian Harring7da13142012-06-15 02:24:20 -0700592
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700593 default_groups = ['all', 'name:%s' % name, 'path:%s' % path]
Che-Liang Chiou69998b02012-01-11 11:28:42 +0800594 groups.extend(set(default_groups).difference(groups))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700595
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700596 if self.IsMirror:
597 worktree = None
598 gitdir = os.path.join(self.topdir, '%s.git' % name)
599 else:
600 worktree = os.path.join(self.topdir, path).replace('\\', '/')
601 gitdir = os.path.join(self.repodir, 'projects/%s.git' % path)
602
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700603 project = Project(manifest = self,
604 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700605 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700606 gitdir = gitdir,
607 worktree = worktree,
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700608 relpath = path,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700609 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800610 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700611 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700612 groups = groups,
Brian Harring14a66742012-09-28 20:21:57 -0700613 sync_c = sync_c,
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700614 upstream = upstream)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700615
616 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700617 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700618 self._ParseCopyFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500619 if n.nodeName == 'annotation':
620 self._ParseAnnotation(project, n)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700621
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700622 return project
623
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700624 def _ParseCopyFile(self, project, node):
625 src = self._reqatt(node, 'src')
626 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800627 if not self.IsMirror:
628 # src is project relative;
629 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800630 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700631
James W. Mills24c13082012-04-12 15:04:13 -0500632 def _ParseAnnotation(self, project, node):
633 name = self._reqatt(node, 'name')
634 value = self._reqatt(node, 'value')
635 try:
636 keep = self._reqatt(node, 'keep').lower()
637 except ManifestParseError:
638 keep = "true"
639 if keep != "true" and keep != "false":
640 raise ManifestParseError, "optional \"keep\" attribute must be \"true\" or \"false\""
641 project.AddAnnotation(name, value, keep)
642
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700643 def _get_remote(self, node):
644 name = node.getAttribute('remote')
645 if not name:
646 return None
647
648 v = self._remotes.get(name)
649 if not v:
650 raise ManifestParseError, \
651 "remote %s not defined in %s" % \
652 (name, self.manifestFile)
653 return v
654
655 def _reqatt(self, node, attname):
656 """
657 reads a required attribute from the node.
658 """
659 v = node.getAttribute(attname)
660 if not v:
661 raise ManifestParseError, \
662 "no %s in <%s> within %s" % \
663 (attname, node.nodeName, self.manifestFile)
664 return v