blob: 4e47679166fce101124d6bbe5a9d21af9d5ec4b0 [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001#
2# Copyright (C) 2008 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
Colin Cross23acdd32012-04-21 00:33:54 -070016import itertools
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070017import os
Conley Owensdb728cd2011-09-26 16:34:01 -070018import re
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070019import sys
Conley Owensdb728cd2011-09-26 16:34:01 -070020import urlparse
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070021import xml.dom.minidom
22
David Pursehousee15c65a2012-08-22 10:46:11 +090023from git_config import GitConfig
David Pursehousee00aa6b2012-09-11 14:33:51 +090024from git_refs import R_HEADS, HEAD
25from project import RemoteSpec, Project, MetaProject
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070026from error import ManifestParseError
27
28MANIFEST_FILE_NAME = 'manifest.xml'
Shawn O. Pearce5cc66792008-10-23 16:19:27 -070029LOCAL_MANIFEST_NAME = 'local_manifest.xml'
David Pursehouse2d5a0df2012-11-13 02:50:36 +090030LOCAL_MANIFESTS_DIR_NAME = 'local_manifests'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070031
Conley Owensdb728cd2011-09-26 16:34:01 -070032urlparse.uses_relative.extend(['ssh', 'git'])
33urlparse.uses_netloc.extend(['ssh', 'git'])
34
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070035class _Default(object):
36 """Project defaults within the manifest."""
37
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -070038 revisionExpr = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070039 remote = None
Shawn O. Pearce6392c872011-09-22 17:44:31 -070040 sync_j = 1
Anatol Pomazau79770d22012-04-20 14:41:59 -070041 sync_c = False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070042
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070043class _XmlRemote(object):
44 def __init__(self,
45 name,
Yestin Sunb292b982012-07-02 07:32:50 -070046 alias=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070047 fetch=None,
Conley Owensdb728cd2011-09-26 16:34:01 -070048 manifestUrl=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070049 review=None):
50 self.name = name
51 self.fetchUrl = fetch
Conley Owensdb728cd2011-09-26 16:34:01 -070052 self.manifestUrl = manifestUrl
Yestin Sunb292b982012-07-02 07:32:50 -070053 self.remoteAlias = alias
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070054 self.reviewUrl = review
Conley Owensceea3682011-10-20 10:45:47 -070055 self.resolvedFetchUrl = self._resolveFetchUrl()
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070056
Conley Owensceea3682011-10-20 10:45:47 -070057 def _resolveFetchUrl(self):
58 url = self.fetchUrl.rstrip('/')
Conley Owensdb728cd2011-09-26 16:34:01 -070059 manifestUrl = self.manifestUrl.rstrip('/')
60 # urljoin will get confused if there is no scheme in the base url
61 # ie, if manifestUrl is of the form <hostname:port>
62 if manifestUrl.find(':') != manifestUrl.find('/') - 1:
63 manifestUrl = 'gopher://' + manifestUrl
64 url = urlparse.urljoin(manifestUrl, url)
Conley Owensceea3682011-10-20 10:45:47 -070065 return re.sub(r'^gopher://', '', url)
66
67 def ToRemoteSpec(self, projectName):
Conley Owens9d8f9142011-10-20 14:36:35 -070068 url = self.resolvedFetchUrl.rstrip('/') + '/' + projectName
Yestin Sunb292b982012-07-02 07:32:50 -070069 remoteName = self.name
70 if self.remoteAlias:
71 remoteName = self.remoteAlias
72 return RemoteSpec(remoteName, url, self.reviewUrl)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070073
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070074class XmlManifest(object):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070075 """manages the repo configuration file"""
76
77 def __init__(self, repodir):
78 self.repodir = os.path.abspath(repodir)
79 self.topdir = os.path.dirname(self.repodir)
80 self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070081 self.globalConfig = GitConfig.ForUser()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070082
83 self.repoProject = MetaProject(self, 'repo',
84 gitdir = os.path.join(repodir, 'repo/.git'),
85 worktree = os.path.join(repodir, 'repo'))
86
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070087 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -080088 gitdir = os.path.join(repodir, 'manifests.git'),
89 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070090
91 self._Unload()
92
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -070093 def Override(self, name):
94 """Use a different manifest, just for the current instantiation.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070095 """
96 path = os.path.join(self.manifestProject.worktree, name)
97 if not os.path.isfile(path):
98 raise ManifestParseError('manifest %s not found' % name)
99
100 old = self.manifestFile
101 try:
102 self.manifestFile = path
103 self._Unload()
104 self._Load()
105 finally:
106 self.manifestFile = old
107
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700108 def Link(self, name):
109 """Update the repo metadata to use a different manifest.
110 """
111 self.Override(name)
112
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700113 try:
114 if os.path.exists(self.manifestFile):
115 os.remove(self.manifestFile)
116 os.symlink('manifests/%s' % name, self.manifestFile)
David Pursehouse8a68ff92012-09-24 12:15:13 +0900117 except OSError:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700118 raise ManifestParseError('cannot link manifest %s' % name)
119
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800120 def _RemoteToXml(self, r, doc, root):
121 e = doc.createElement('remote')
122 root.appendChild(e)
123 e.setAttribute('name', r.name)
124 e.setAttribute('fetch', r.fetchUrl)
125 if r.reviewUrl is not None:
126 e.setAttribute('review', r.reviewUrl)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800127
Brian Harring14a66742012-09-28 20:21:57 -0700128 def Save(self, fd, peg_rev=False, peg_rev_upstream=True):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800129 """Write the current manifest out to the given file descriptor.
130 """
Colin Cross5acde752012-03-28 20:15:45 -0700131 mp = self.manifestProject
132
133 groups = mp.config.GetString('manifest.groups')
Colin Crossc39864f2012-04-23 13:41:58 -0700134 if not groups:
Conley Owensbb1b5f52012-08-13 13:11:18 -0700135 groups = 'all'
Conley Owens971de8e2012-04-16 10:36:08 -0700136 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Colin Cross5acde752012-03-28 20:15:45 -0700137
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800138 doc = xml.dom.minidom.Document()
139 root = doc.createElement('manifest')
140 doc.appendChild(root)
141
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700142 # Save out the notice. There's a little bit of work here to give it the
143 # right whitespace, which assumes that the notice is automatically indented
144 # by 4 by minidom.
145 if self.notice:
146 notice_element = root.appendChild(doc.createElement('notice'))
147 notice_lines = self.notice.splitlines()
148 indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:]
149 notice_element.appendChild(doc.createTextNode(indented_notice))
150
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800151 d = self.default
152 sort_remotes = list(self.remotes.keys())
153 sort_remotes.sort()
154
155 for r in sort_remotes:
156 self._RemoteToXml(self.remotes[r], doc, root)
157 if self.remotes:
158 root.appendChild(doc.createTextNode(''))
159
160 have_default = False
161 e = doc.createElement('default')
162 if d.remote:
163 have_default = True
164 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700165 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800166 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700167 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700168 if d.sync_j > 1:
169 have_default = True
170 e.setAttribute('sync-j', '%d' % d.sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700171 if d.sync_c:
172 have_default = True
173 e.setAttribute('sync-c', 'true')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800174 if have_default:
175 root.appendChild(e)
176 root.appendChild(doc.createTextNode(''))
177
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700178 if self._manifest_server:
179 e = doc.createElement('manifest-server')
180 e.setAttribute('url', self._manifest_server)
181 root.appendChild(e)
182 root.appendChild(doc.createTextNode(''))
183
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700184 sort_projects = list(self.projects.keys())
185 sort_projects.sort()
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800186
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700187 for p in sort_projects:
188 p = self.projects[p]
189
Colin Cross5acde752012-03-28 20:15:45 -0700190 if not p.MatchesGroups(groups):
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700191 continue
Colin Cross5acde752012-03-28 20:15:45 -0700192
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800193 e = doc.createElement('project')
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700194 root.appendChild(e)
195 e.setAttribute('name', p.name)
196 if p.relpath != p.name:
197 e.setAttribute('path', p.relpath)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800198 if not d.remote or p.remote.name != d.remote.name:
199 e.setAttribute('remote', p.remote.name)
200 if peg_rev:
201 if self.IsMirror:
Brian Harring14a66742012-09-28 20:21:57 -0700202 value = p.bare_git.rev_parse(p.revisionExpr + '^0')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800203 else:
Brian Harring14a66742012-09-28 20:21:57 -0700204 value = p.work_git.rev_parse(HEAD + '^0')
205 e.setAttribute('revision', value)
206 if peg_rev_upstream and value != p.revisionExpr:
207 # Only save the origin if the origin is not a sha1, and the default
208 # isn't our value, and the if the default doesn't already have that
209 # covered.
210 e.setAttribute('upstream', p.revisionExpr)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700211 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
212 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800213
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800214 for c in p.copyfiles:
215 ce = doc.createElement('copyfile')
216 ce.setAttribute('src', c.src)
217 ce.setAttribute('dest', c.dest)
218 e.appendChild(ce)
219
Conley Owensbb1b5f52012-08-13 13:11:18 -0700220 default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath]
Dmitry Fink17f85ea2012-08-06 14:52:29 -0700221 egroups = [g for g in p.groups if g not in default_groups]
Conley Owens971de8e2012-04-16 10:36:08 -0700222 if egroups:
223 e.setAttribute('groups', ','.join(egroups))
Colin Cross5acde752012-03-28 20:15:45 -0700224
James W. Mills24c13082012-04-12 15:04:13 -0500225 for a in p.annotations:
226 if a.keep == "true":
227 ae = doc.createElement('annotation')
228 ae.setAttribute('name', a.name)
229 ae.setAttribute('value', a.value)
230 e.appendChild(ae)
231
Anatol Pomazau79770d22012-04-20 14:41:59 -0700232 if p.sync_c:
233 e.setAttribute('sync-c', 'true')
234
Doug Anderson37282b42011-03-04 11:54:18 -0800235 if self._repo_hooks_project:
236 root.appendChild(doc.createTextNode(''))
237 e = doc.createElement('repo-hooks')
238 e.setAttribute('in-project', self._repo_hooks_project.name)
239 e.setAttribute('enabled-list',
240 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
241 root.appendChild(e)
242
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800243 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
244
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700245 @property
246 def projects(self):
247 self._Load()
248 return self._projects
249
250 @property
251 def remotes(self):
252 self._Load()
253 return self._remotes
254
255 @property
256 def default(self):
257 self._Load()
258 return self._default
259
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800260 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800261 def repo_hooks_project(self):
262 self._Load()
263 return self._repo_hooks_project
264
265 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700266 def notice(self):
267 self._Load()
268 return self._notice
269
270 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700271 def manifest_server(self):
272 self._Load()
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800273 return self._manifest_server
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700274
275 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800276 def IsMirror(self):
277 return self.manifestProject.config.GetBoolean('repo.mirror')
278
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700279 def _Unload(self):
280 self._loaded = False
281 self._projects = {}
282 self._remotes = {}
283 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800284 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700285 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700286 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700287 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700288
289 def _Load(self):
290 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800291 m = self.manifestProject
292 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700293 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800294 b = b[len(R_HEADS):]
295 self.branch = b
296
Colin Cross23acdd32012-04-21 00:33:54 -0700297 nodes = []
Brian Harring475a47d2012-06-07 20:05:35 -0700298 nodes.append(self._ParseManifestXml(self.manifestFile,
299 self.manifestProject.worktree))
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700300
301 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
302 if os.path.exists(local):
Brian Harring475a47d2012-06-07 20:05:35 -0700303 nodes.append(self._ParseManifestXml(local, self.repodir))
Colin Cross23acdd32012-04-21 00:33:54 -0700304
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900305 local_dir = os.path.abspath(os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME))
306 try:
307 for local_file in os.listdir(local_dir):
308 if local_file.endswith('.xml'):
309 try:
310 nodes.append(self._ParseManifestXml(local_file, self.repodir))
311 except ManifestParseError as e:
312 print >>sys.stderr, '%s' % str(e)
313 except OSError:
314 pass
315
Colin Cross23acdd32012-04-21 00:33:54 -0700316 self._ParseManifest(nodes)
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700317
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800318 if self.IsMirror:
319 self._AddMetaProjectMirror(self.repoProject)
320 self._AddMetaProjectMirror(self.manifestProject)
321
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700322 self._loaded = True
323
Brian Harring475a47d2012-06-07 20:05:35 -0700324 def _ParseManifestXml(self, path, include_root):
David Pursehousef7fc8a92012-11-13 04:00:28 +0900325 try:
326 root = xml.dom.minidom.parse(path)
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900327 except (OSError, xml.parsers.expat.ExpatError) as e:
David Pursehousef7fc8a92012-11-13 04:00:28 +0900328 raise ManifestParseError("error parsing manifest %s: %s" % (path, e))
329
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700330 if not root or not root.childNodes:
Brian Harring26448742011-04-28 05:04:41 -0700331 raise ManifestParseError("no root node in %s" % (path,))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700332
Jooncheol Park34acdd22012-08-27 02:25:59 +0900333 for manifest in root.childNodes:
334 if manifest.nodeName == 'manifest':
335 break
336 else:
Brian Harring26448742011-04-28 05:04:41 -0700337 raise ManifestParseError("no <manifest> in %s" % (path,))
338
Colin Cross23acdd32012-04-21 00:33:54 -0700339 nodes = []
David Pursehouse4f7bdea2012-10-22 12:50:15 +0900340 for node in manifest.childNodes: # pylint:disable=W0631
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900341 # We only get here if manifest is initialised
Brian Harring26448742011-04-28 05:04:41 -0700342 if node.nodeName == 'include':
343 name = self._reqatt(node, 'name')
Brian Harring475a47d2012-06-07 20:05:35 -0700344 fp = os.path.join(include_root, name)
Brian Harring26448742011-04-28 05:04:41 -0700345 if not os.path.isfile(fp):
346 raise ManifestParseError, \
347 "include %s doesn't exist or isn't a file" % \
348 (name,)
349 try:
Brian Harring475a47d2012-06-07 20:05:35 -0700350 nodes.extend(self._ParseManifestXml(fp, include_root))
Brian Harring26448742011-04-28 05:04:41 -0700351 # should isolate this to the exact exception, but that's
352 # tricky. actual parsing implementation may vary.
353 except (KeyboardInterrupt, RuntimeError, SystemExit):
354 raise
Sarah Owensa5be53f2012-09-09 15:37:57 -0700355 except Exception as e:
Brian Harring26448742011-04-28 05:04:41 -0700356 raise ManifestParseError(
357 "failed parsing included manifest %s: %s", (name, e))
Colin Cross23acdd32012-04-21 00:33:54 -0700358 else:
359 nodes.append(node)
360 return nodes
Brian Harring26448742011-04-28 05:04:41 -0700361
Colin Cross23acdd32012-04-21 00:33:54 -0700362 def _ParseManifest(self, node_list):
363 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700364 if node.nodeName == 'remote':
365 remote = self._ParseRemote(node)
366 if self._remotes.get(remote.name):
Doug Anderson37282b42011-03-04 11:54:18 -0800367 raise ManifestParseError(
368 'duplicate remote %s in %s' %
369 (remote.name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700370 self._remotes[remote.name] = remote
371
Colin Cross23acdd32012-04-21 00:33:54 -0700372 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700373 if node.nodeName == 'default':
374 if self._default is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800375 raise ManifestParseError(
376 'duplicate default in %s' %
377 (self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700378 self._default = self._ParseDefault(node)
379 if self._default is None:
380 self._default = _Default()
381
Colin Cross23acdd32012-04-21 00:33:54 -0700382 for node in itertools.chain(*node_list):
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700383 if node.nodeName == 'notice':
384 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800385 raise ManifestParseError(
386 'duplicate notice in %s' %
387 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700388 self._notice = self._ParseNotice(node)
389
Colin Cross23acdd32012-04-21 00:33:54 -0700390 for node in itertools.chain(*node_list):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700391 if node.nodeName == 'manifest-server':
392 url = self._reqatt(node, 'url')
393 if self._manifest_server is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800394 raise ManifestParseError(
395 'duplicate manifest-server in %s' %
396 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700397 self._manifest_server = url
398
Colin Cross23acdd32012-04-21 00:33:54 -0700399 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700400 if node.nodeName == 'project':
401 project = self._ParseProject(node)
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700402 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
Doug Anderson37282b42011-03-04 11:54:18 -0800407 if node.nodeName == 'repo-hooks':
408 # Get the name of the project and the (space-separated) list of enabled.
409 repo_hooks_project = self._reqatt(node, 'in-project')
410 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
411
412 # Only one project can be the hooks project
413 if self._repo_hooks_project is not None:
414 raise ManifestParseError(
415 'duplicate repo-hooks in %s' %
416 (self.manifestFile))
417
418 # Store a reference to the Project.
419 try:
420 self._repo_hooks_project = self._projects[repo_hooks_project]
421 except KeyError:
422 raise ManifestParseError(
423 'project %s not found for repo-hooks' %
424 (repo_hooks_project))
425
426 # Store the enabled hooks in the Project object.
427 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
Colin Cross23acdd32012-04-21 00:33:54 -0700428 if node.nodeName == 'remove-project':
429 name = self._reqatt(node, 'name')
430 try:
431 del self._projects[name]
432 except KeyError:
433 raise ManifestParseError(
434 'project %s not found' %
435 (name))
436
437 # If the manifest removes the hooks project, treat it as if it deleted
438 # the repo-hooks element too.
439 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
440 self._repo_hooks_project = None
441
Doug Anderson37282b42011-03-04 11:54:18 -0800442
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800443 def _AddMetaProjectMirror(self, m):
444 name = None
445 m_url = m.GetRemote(m.remote.name).url
446 if m_url.endswith('/.git'):
447 raise ManifestParseError, 'refusing to mirror %s' % m_url
448
449 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700450 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800451 if not url.endswith('/'):
452 url += '/'
453 if m_url.startswith(url):
454 remote = self._default.remote
455 name = m_url[len(url):]
456
457 if name is None:
458 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700459 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Shawn O. Pearcef35b2d92012-08-02 11:46:22 -0700460 remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800461 name = m_url[s:]
462
463 if name.endswith('.git'):
464 name = name[:-4]
465
466 if name not in self._projects:
467 m.PreSync()
468 gitdir = os.path.join(self.topdir, '%s.git' % name)
469 project = Project(manifest = self,
470 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700471 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800472 gitdir = gitdir,
473 worktree = None,
474 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700475 revisionExpr = m.revisionExpr,
476 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800477 self._projects[project.name] = project
478
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700479 def _ParseRemote(self, node):
480 """
481 reads a <remote> element from the manifest file
482 """
483 name = self._reqatt(node, 'name')
Yestin Sunb292b982012-07-02 07:32:50 -0700484 alias = node.getAttribute('alias')
485 if alias == '':
486 alias = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700487 fetch = self._reqatt(node, 'fetch')
488 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800489 if review == '':
490 review = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700491 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Yestin Sunb292b982012-07-02 07:32:50 -0700492 return _XmlRemote(name, alias, fetch, manifestUrl, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700493
494 def _ParseDefault(self, node):
495 """
496 reads a <default> element from the manifest file
497 """
498 d = _Default()
499 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700500 d.revisionExpr = node.getAttribute('revision')
501 if d.revisionExpr == '':
502 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700503
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700504 sync_j = node.getAttribute('sync-j')
505 if sync_j == '' or sync_j is None:
506 d.sync_j = 1
507 else:
508 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700509
510 sync_c = node.getAttribute('sync-c')
511 if not sync_c:
512 d.sync_c = False
513 else:
514 d.sync_c = sync_c.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700515 return d
516
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700517 def _ParseNotice(self, node):
518 """
519 reads a <notice> element from the manifest file
520
521 The <notice> element is distinct from other tags in the XML in that the
522 data is conveyed between the start and end tag (it's not an empty-element
523 tag).
524
525 The white space (carriage returns, indentation) for the notice element is
526 relevant and is parsed in a way that is based on how python docstrings work.
527 In fact, the code is remarkably similar to here:
528 http://www.python.org/dev/peps/pep-0257/
529 """
530 # Get the data out of the node...
531 notice = node.childNodes[0].data
532
533 # Figure out minimum indentation, skipping the first line (the same line
534 # as the <notice> tag)...
535 minIndent = sys.maxint
536 lines = notice.splitlines()
537 for line in lines[1:]:
538 lstrippedLine = line.lstrip()
539 if lstrippedLine:
540 indent = len(line) - len(lstrippedLine)
541 minIndent = min(indent, minIndent)
542
543 # Strip leading / trailing blank lines and also indentation.
544 cleanLines = [lines[0].strip()]
545 for line in lines[1:]:
546 cleanLines.append(line[minIndent:].rstrip())
547
548 # Clear completely blank lines from front and back...
549 while cleanLines and not cleanLines[0]:
550 del cleanLines[0]
551 while cleanLines and not cleanLines[-1]:
552 del cleanLines[-1]
553
554 return '\n'.join(cleanLines)
555
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700556 def _ParseProject(self, node):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700557 """
558 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700559 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700560 name = self._reqatt(node, 'name')
561
562 remote = self._get_remote(node)
563 if remote is None:
564 remote = self._default.remote
565 if remote is None:
566 raise ManifestParseError, \
567 "no remote for project %s within %s" % \
568 (name, self.manifestFile)
569
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700570 revisionExpr = node.getAttribute('revision')
571 if not revisionExpr:
572 revisionExpr = self._default.revisionExpr
573 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700574 raise ManifestParseError, \
575 "no revision for project %s within %s" % \
576 (name, self.manifestFile)
577
578 path = node.getAttribute('path')
579 if not path:
580 path = name
581 if path.startswith('/'):
582 raise ManifestParseError, \
583 "project %s path cannot be absolute in %s" % \
584 (name, self.manifestFile)
585
Mike Pontillod3153822012-02-28 11:53:24 -0800586 rebase = node.getAttribute('rebase')
587 if not rebase:
588 rebase = True
589 else:
590 rebase = rebase.lower() in ("yes", "true", "1")
591
Anatol Pomazau79770d22012-04-20 14:41:59 -0700592 sync_c = node.getAttribute('sync-c')
593 if not sync_c:
594 sync_c = False
595 else:
596 sync_c = sync_c.lower() in ("yes", "true", "1")
597
Brian Harring14a66742012-09-28 20:21:57 -0700598 upstream = node.getAttribute('upstream')
599
Conley Owens971de8e2012-04-16 10:36:08 -0700600 groups = ''
601 if node.hasAttribute('groups'):
602 groups = node.getAttribute('groups')
David Pursehouse1d947b32012-10-25 12:23:11 +0900603 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Brian Harring7da13142012-06-15 02:24:20 -0700604
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700605 default_groups = ['all', 'name:%s' % name, 'path:%s' % path]
Che-Liang Chiou69998b02012-01-11 11:28:42 +0800606 groups.extend(set(default_groups).difference(groups))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700607
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700608 if self.IsMirror:
609 worktree = None
610 gitdir = os.path.join(self.topdir, '%s.git' % name)
611 else:
612 worktree = os.path.join(self.topdir, path).replace('\\', '/')
613 gitdir = os.path.join(self.repodir, 'projects/%s.git' % path)
614
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700615 project = Project(manifest = self,
616 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700617 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700618 gitdir = gitdir,
619 worktree = worktree,
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700620 relpath = path,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700621 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800622 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700623 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700624 groups = groups,
Brian Harring14a66742012-09-28 20:21:57 -0700625 sync_c = sync_c,
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700626 upstream = upstream)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700627
628 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700629 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700630 self._ParseCopyFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500631 if n.nodeName == 'annotation':
632 self._ParseAnnotation(project, n)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700633
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700634 return project
635
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700636 def _ParseCopyFile(self, project, node):
637 src = self._reqatt(node, 'src')
638 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800639 if not self.IsMirror:
640 # src is project relative;
641 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800642 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700643
James W. Mills24c13082012-04-12 15:04:13 -0500644 def _ParseAnnotation(self, project, node):
645 name = self._reqatt(node, 'name')
646 value = self._reqatt(node, 'value')
647 try:
648 keep = self._reqatt(node, 'keep').lower()
649 except ManifestParseError:
650 keep = "true"
651 if keep != "true" and keep != "false":
652 raise ManifestParseError, "optional \"keep\" attribute must be \"true\" or \"false\""
653 project.AddAnnotation(name, value, keep)
654
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700655 def _get_remote(self, node):
656 name = node.getAttribute('remote')
657 if not name:
658 return None
659
660 v = self._remotes.get(name)
661 if not v:
662 raise ManifestParseError, \
663 "remote %s not defined in %s" % \
664 (name, self.manifestFile)
665 return v
666
667 def _reqatt(self, node, attname):
668 """
669 reads a required attribute from the node.
670 """
671 v = node.getAttribute(attname)
672 if not v:
673 raise ManifestParseError, \
674 "no %s in <%s> within %s" % \
675 (attname, node.nodeName, self.manifestFile)
676 return v