blob: 1b954561a474b8d752d639ceb50d32d6160d9984 [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
Sarah Owenscecd1d82012-11-01 22:59:27 -070016from __future__ import print_function
Colin Cross23acdd32012-04-21 00:33:54 -070017import itertools
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070018import os
Conley Owensdb728cd2011-09-26 16:34:01 -070019import re
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070020import sys
Conley Owensdb728cd2011-09-26 16:34:01 -070021import urlparse
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070022import xml.dom.minidom
23
David Pursehousee15c65a2012-08-22 10:46:11 +090024from git_config import GitConfig
David Pursehousee00aa6b2012-09-11 14:33:51 +090025from git_refs import R_HEADS, HEAD
26from project import RemoteSpec, Project, MetaProject
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070027from error import ManifestParseError
28
29MANIFEST_FILE_NAME = 'manifest.xml'
Shawn O. Pearce5cc66792008-10-23 16:19:27 -070030LOCAL_MANIFEST_NAME = 'local_manifest.xml'
David Pursehouse2d5a0df2012-11-13 02:50:36 +090031LOCAL_MANIFESTS_DIR_NAME = 'local_manifests'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070032
Conley Owensdb728cd2011-09-26 16:34:01 -070033urlparse.uses_relative.extend(['ssh', 'git'])
34urlparse.uses_netloc.extend(['ssh', 'git'])
35
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070036class _Default(object):
37 """Project defaults within the manifest."""
38
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -070039 revisionExpr = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070040 remote = None
Shawn O. Pearce6392c872011-09-22 17:44:31 -070041 sync_j = 1
Anatol Pomazau79770d22012-04-20 14:41:59 -070042 sync_c = False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070043
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070044class _XmlRemote(object):
45 def __init__(self,
46 name,
Yestin Sunb292b982012-07-02 07:32:50 -070047 alias=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070048 fetch=None,
Conley Owensdb728cd2011-09-26 16:34:01 -070049 manifestUrl=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070050 review=None):
51 self.name = name
52 self.fetchUrl = fetch
Conley Owensdb728cd2011-09-26 16:34:01 -070053 self.manifestUrl = manifestUrl
Yestin Sunb292b982012-07-02 07:32:50 -070054 self.remoteAlias = alias
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070055 self.reviewUrl = review
Conley Owensceea3682011-10-20 10:45:47 -070056 self.resolvedFetchUrl = self._resolveFetchUrl()
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070057
David Pursehouse717ece92012-11-13 08:49:16 +090058 def __eq__(self, other):
59 return self.__dict__ == other.__dict__
60
61 def __ne__(self, other):
62 return self.__dict__ != other.__dict__
63
Conley Owensceea3682011-10-20 10:45:47 -070064 def _resolveFetchUrl(self):
65 url = self.fetchUrl.rstrip('/')
Conley Owensdb728cd2011-09-26 16:34:01 -070066 manifestUrl = self.manifestUrl.rstrip('/')
67 # urljoin will get confused if there is no scheme in the base url
68 # ie, if manifestUrl is of the form <hostname:port>
69 if manifestUrl.find(':') != manifestUrl.find('/') - 1:
David Pursehousec1b86a22012-11-14 11:36:51 +090070 manifestUrl = 'gopher://' + manifestUrl
Conley Owensdb728cd2011-09-26 16:34:01 -070071 url = urlparse.urljoin(manifestUrl, url)
Conley Owensceea3682011-10-20 10:45:47 -070072 return re.sub(r'^gopher://', '', url)
73
74 def ToRemoteSpec(self, projectName):
Conley Owens9d8f9142011-10-20 14:36:35 -070075 url = self.resolvedFetchUrl.rstrip('/') + '/' + projectName
Yestin Sunb292b982012-07-02 07:32:50 -070076 remoteName = self.name
77 if self.remoteAlias:
78 remoteName = self.remoteAlias
79 return RemoteSpec(remoteName, url, self.reviewUrl)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070080
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070081class XmlManifest(object):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070082 """manages the repo configuration file"""
83
84 def __init__(self, repodir):
85 self.repodir = os.path.abspath(repodir)
86 self.topdir = os.path.dirname(self.repodir)
87 self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070088 self.globalConfig = GitConfig.ForUser()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070089
90 self.repoProject = MetaProject(self, 'repo',
91 gitdir = os.path.join(repodir, 'repo/.git'),
92 worktree = os.path.join(repodir, 'repo'))
93
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070094 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -080095 gitdir = os.path.join(repodir, 'manifests.git'),
96 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070097
98 self._Unload()
99
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700100 def Override(self, name):
101 """Use a different manifest, just for the current instantiation.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700102 """
103 path = os.path.join(self.manifestProject.worktree, name)
104 if not os.path.isfile(path):
105 raise ManifestParseError('manifest %s not found' % name)
106
107 old = self.manifestFile
108 try:
109 self.manifestFile = path
110 self._Unload()
111 self._Load()
112 finally:
113 self.manifestFile = old
114
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700115 def Link(self, name):
116 """Update the repo metadata to use a different manifest.
117 """
118 self.Override(name)
119
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700120 try:
121 if os.path.exists(self.manifestFile):
122 os.remove(self.manifestFile)
123 os.symlink('manifests/%s' % name, self.manifestFile)
David Pursehouse8a68ff92012-09-24 12:15:13 +0900124 except OSError:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700125 raise ManifestParseError('cannot link manifest %s' % name)
126
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800127 def _RemoteToXml(self, r, doc, root):
128 e = doc.createElement('remote')
129 root.appendChild(e)
130 e.setAttribute('name', r.name)
131 e.setAttribute('fetch', r.fetchUrl)
132 if r.reviewUrl is not None:
133 e.setAttribute('review', r.reviewUrl)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800134
Brian Harring14a66742012-09-28 20:21:57 -0700135 def Save(self, fd, peg_rev=False, peg_rev_upstream=True):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800136 """Write the current manifest out to the given file descriptor.
137 """
Colin Cross5acde752012-03-28 20:15:45 -0700138 mp = self.manifestProject
139
140 groups = mp.config.GetString('manifest.groups')
Colin Crossc39864f2012-04-23 13:41:58 -0700141 if not groups:
Conley Owensbb1b5f52012-08-13 13:11:18 -0700142 groups = 'all'
Conley Owens971de8e2012-04-16 10:36:08 -0700143 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Colin Cross5acde752012-03-28 20:15:45 -0700144
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800145 doc = xml.dom.minidom.Document()
146 root = doc.createElement('manifest')
147 doc.appendChild(root)
148
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700149 # Save out the notice. There's a little bit of work here to give it the
150 # right whitespace, which assumes that the notice is automatically indented
151 # by 4 by minidom.
152 if self.notice:
153 notice_element = root.appendChild(doc.createElement('notice'))
154 notice_lines = self.notice.splitlines()
155 indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:]
156 notice_element.appendChild(doc.createTextNode(indented_notice))
157
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800158 d = self.default
159 sort_remotes = list(self.remotes.keys())
160 sort_remotes.sort()
161
162 for r in sort_remotes:
163 self._RemoteToXml(self.remotes[r], doc, root)
164 if self.remotes:
165 root.appendChild(doc.createTextNode(''))
166
167 have_default = False
168 e = doc.createElement('default')
169 if d.remote:
170 have_default = True
171 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700172 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800173 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700174 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700175 if d.sync_j > 1:
176 have_default = True
177 e.setAttribute('sync-j', '%d' % d.sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700178 if d.sync_c:
179 have_default = True
180 e.setAttribute('sync-c', 'true')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800181 if have_default:
182 root.appendChild(e)
183 root.appendChild(doc.createTextNode(''))
184
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700185 if self._manifest_server:
186 e = doc.createElement('manifest-server')
187 e.setAttribute('url', self._manifest_server)
188 root.appendChild(e)
189 root.appendChild(doc.createTextNode(''))
190
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700191 sort_projects = list(self.projects.keys())
192 sort_projects.sort()
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800193
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700194 for p in sort_projects:
195 p = self.projects[p]
196
Colin Cross5acde752012-03-28 20:15:45 -0700197 if not p.MatchesGroups(groups):
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700198 continue
Colin Cross5acde752012-03-28 20:15:45 -0700199
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800200 e = doc.createElement('project')
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700201 root.appendChild(e)
202 e.setAttribute('name', p.name)
203 if p.relpath != p.name:
204 e.setAttribute('path', p.relpath)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800205 if not d.remote or p.remote.name != d.remote.name:
206 e.setAttribute('remote', p.remote.name)
207 if peg_rev:
208 if self.IsMirror:
Brian Harring14a66742012-09-28 20:21:57 -0700209 value = p.bare_git.rev_parse(p.revisionExpr + '^0')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800210 else:
Brian Harring14a66742012-09-28 20:21:57 -0700211 value = p.work_git.rev_parse(HEAD + '^0')
212 e.setAttribute('revision', value)
213 if peg_rev_upstream and value != p.revisionExpr:
214 # Only save the origin if the origin is not a sha1, and the default
215 # isn't our value, and the if the default doesn't already have that
216 # covered.
217 e.setAttribute('upstream', p.revisionExpr)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700218 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
219 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800220
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800221 for c in p.copyfiles:
222 ce = doc.createElement('copyfile')
223 ce.setAttribute('src', c.src)
224 ce.setAttribute('dest', c.dest)
225 e.appendChild(ce)
226
Conley Owensbb1b5f52012-08-13 13:11:18 -0700227 default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath]
Dmitry Fink17f85ea2012-08-06 14:52:29 -0700228 egroups = [g for g in p.groups if g not in default_groups]
Conley Owens971de8e2012-04-16 10:36:08 -0700229 if egroups:
230 e.setAttribute('groups', ','.join(egroups))
Colin Cross5acde752012-03-28 20:15:45 -0700231
James W. Mills24c13082012-04-12 15:04:13 -0500232 for a in p.annotations:
233 if a.keep == "true":
234 ae = doc.createElement('annotation')
235 ae.setAttribute('name', a.name)
236 ae.setAttribute('value', a.value)
237 e.appendChild(ae)
238
Anatol Pomazau79770d22012-04-20 14:41:59 -0700239 if p.sync_c:
240 e.setAttribute('sync-c', 'true')
241
Doug Anderson37282b42011-03-04 11:54:18 -0800242 if self._repo_hooks_project:
243 root.appendChild(doc.createTextNode(''))
244 e = doc.createElement('repo-hooks')
245 e.setAttribute('in-project', self._repo_hooks_project.name)
246 e.setAttribute('enabled-list',
247 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
248 root.appendChild(e)
249
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800250 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
251
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700252 @property
253 def projects(self):
254 self._Load()
255 return self._projects
256
257 @property
258 def remotes(self):
259 self._Load()
260 return self._remotes
261
262 @property
263 def default(self):
264 self._Load()
265 return self._default
266
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800267 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800268 def repo_hooks_project(self):
269 self._Load()
270 return self._repo_hooks_project
271
272 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700273 def notice(self):
274 self._Load()
275 return self._notice
276
277 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700278 def manifest_server(self):
279 self._Load()
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800280 return self._manifest_server
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700281
282 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800283 def IsMirror(self):
284 return self.manifestProject.config.GetBoolean('repo.mirror')
285
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700286 def _Unload(self):
287 self._loaded = False
288 self._projects = {}
289 self._remotes = {}
290 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800291 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700292 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700293 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700294 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700295
296 def _Load(self):
297 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800298 m = self.manifestProject
299 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700300 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800301 b = b[len(R_HEADS):]
302 self.branch = b
303
Colin Cross23acdd32012-04-21 00:33:54 -0700304 nodes = []
Brian Harring475a47d2012-06-07 20:05:35 -0700305 nodes.append(self._ParseManifestXml(self.manifestFile,
306 self.manifestProject.worktree))
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700307
308 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
309 if os.path.exists(local):
Sarah Owenscecd1d82012-11-01 22:59:27 -0700310 print('warning: %s is deprecated; put local manifests in %s instead'
311 % (LOCAL_MANIFEST_NAME, LOCAL_MANIFESTS_DIR_NAME),
312 file=sys.stderr)
Brian Harring475a47d2012-06-07 20:05:35 -0700313 nodes.append(self._ParseManifestXml(local, self.repodir))
Colin Cross23acdd32012-04-21 00:33:54 -0700314
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900315 local_dir = os.path.abspath(os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME))
316 try:
David Pursehouse52f1e5d2012-11-14 04:53:24 +0900317 for local_file in sorted(os.listdir(local_dir)):
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900318 if local_file.endswith('.xml'):
319 try:
320 nodes.append(self._ParseManifestXml(local_file, self.repodir))
321 except ManifestParseError as e:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700322 print('%s' % str(e), file=sys.stderr)
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900323 except OSError:
324 pass
325
Colin Cross23acdd32012-04-21 00:33:54 -0700326 self._ParseManifest(nodes)
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700327
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800328 if self.IsMirror:
329 self._AddMetaProjectMirror(self.repoProject)
330 self._AddMetaProjectMirror(self.manifestProject)
331
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700332 self._loaded = True
333
Brian Harring475a47d2012-06-07 20:05:35 -0700334 def _ParseManifestXml(self, path, include_root):
David Pursehousef7fc8a92012-11-13 04:00:28 +0900335 try:
336 root = xml.dom.minidom.parse(path)
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900337 except (OSError, xml.parsers.expat.ExpatError) as e:
David Pursehousef7fc8a92012-11-13 04:00:28 +0900338 raise ManifestParseError("error parsing manifest %s: %s" % (path, e))
339
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700340 if not root or not root.childNodes:
Brian Harring26448742011-04-28 05:04:41 -0700341 raise ManifestParseError("no root node in %s" % (path,))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700342
Jooncheol Park34acdd22012-08-27 02:25:59 +0900343 for manifest in root.childNodes:
344 if manifest.nodeName == 'manifest':
345 break
346 else:
Brian Harring26448742011-04-28 05:04:41 -0700347 raise ManifestParseError("no <manifest> in %s" % (path,))
348
Colin Cross23acdd32012-04-21 00:33:54 -0700349 nodes = []
David Pursehouse4f7bdea2012-10-22 12:50:15 +0900350 for node in manifest.childNodes: # pylint:disable=W0631
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900351 # We only get here if manifest is initialised
David Pursehousec1b86a22012-11-14 11:36:51 +0900352 if node.nodeName == 'include':
353 name = self._reqatt(node, 'name')
354 fp = os.path.join(include_root, name)
355 if not os.path.isfile(fp):
356 raise ManifestParseError, \
357 "include %s doesn't exist or isn't a file" % \
358 (name,)
359 try:
360 nodes.extend(self._ParseManifestXml(fp, include_root))
361 # should isolate this to the exact exception, but that's
362 # tricky. actual parsing implementation may vary.
363 except (KeyboardInterrupt, RuntimeError, SystemExit):
364 raise
365 except Exception as e:
366 raise ManifestParseError(
367 "failed parsing included manifest %s: %s", (name, e))
368 else:
369 nodes.append(node)
Colin Cross23acdd32012-04-21 00:33:54 -0700370 return nodes
Brian Harring26448742011-04-28 05:04:41 -0700371
Colin Cross23acdd32012-04-21 00:33:54 -0700372 def _ParseManifest(self, node_list):
373 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700374 if node.nodeName == 'remote':
375 remote = self._ParseRemote(node)
David Pursehouse717ece92012-11-13 08:49:16 +0900376 if remote:
377 if remote.name in self._remotes:
378 if remote != self._remotes[remote.name]:
379 raise ManifestParseError(
380 'remote %s already exists with different attributes' %
381 (remote.name))
382 else:
383 self._remotes[remote.name] = remote
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700384
Colin Cross23acdd32012-04-21 00:33:54 -0700385 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700386 if node.nodeName == 'default':
387 if self._default is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800388 raise ManifestParseError(
389 'duplicate default in %s' %
390 (self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700391 self._default = self._ParseDefault(node)
392 if self._default is None:
393 self._default = _Default()
394
Colin Cross23acdd32012-04-21 00:33:54 -0700395 for node in itertools.chain(*node_list):
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700396 if node.nodeName == 'notice':
397 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800398 raise ManifestParseError(
399 'duplicate notice in %s' %
400 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700401 self._notice = self._ParseNotice(node)
402
Colin Cross23acdd32012-04-21 00:33:54 -0700403 for node in itertools.chain(*node_list):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700404 if node.nodeName == 'manifest-server':
405 url = self._reqatt(node, 'url')
406 if self._manifest_server is not None:
David Pursehousec1b86a22012-11-14 11:36:51 +0900407 raise ManifestParseError(
408 'duplicate manifest-server in %s' %
409 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700410 self._manifest_server = url
411
Colin Cross23acdd32012-04-21 00:33:54 -0700412 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700413 if node.nodeName == 'project':
414 project = self._ParseProject(node)
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700415 if self._projects.get(project.name):
416 raise ManifestParseError(
417 'duplicate project %s in %s' %
418 (project.name, self.manifestFile))
419 self._projects[project.name] = project
Doug Anderson37282b42011-03-04 11:54:18 -0800420 if node.nodeName == 'repo-hooks':
421 # Get the name of the project and the (space-separated) list of enabled.
422 repo_hooks_project = self._reqatt(node, 'in-project')
423 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
424
425 # Only one project can be the hooks project
426 if self._repo_hooks_project is not None:
427 raise ManifestParseError(
428 'duplicate repo-hooks in %s' %
429 (self.manifestFile))
430
431 # Store a reference to the Project.
432 try:
433 self._repo_hooks_project = self._projects[repo_hooks_project]
434 except KeyError:
435 raise ManifestParseError(
436 'project %s not found for repo-hooks' %
437 (repo_hooks_project))
438
439 # Store the enabled hooks in the Project object.
440 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
Colin Cross23acdd32012-04-21 00:33:54 -0700441 if node.nodeName == 'remove-project':
442 name = self._reqatt(node, 'name')
443 try:
444 del self._projects[name]
445 except KeyError:
446 raise ManifestParseError(
447 'project %s not found' %
448 (name))
449
450 # If the manifest removes the hooks project, treat it as if it deleted
451 # the repo-hooks element too.
452 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
453 self._repo_hooks_project = None
454
Doug Anderson37282b42011-03-04 11:54:18 -0800455
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800456 def _AddMetaProjectMirror(self, m):
457 name = None
458 m_url = m.GetRemote(m.remote.name).url
459 if m_url.endswith('/.git'):
460 raise ManifestParseError, 'refusing to mirror %s' % m_url
461
462 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700463 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800464 if not url.endswith('/'):
465 url += '/'
466 if m_url.startswith(url):
467 remote = self._default.remote
468 name = m_url[len(url):]
469
470 if name is None:
471 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700472 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Shawn O. Pearcef35b2d92012-08-02 11:46:22 -0700473 remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800474 name = m_url[s:]
475
476 if name.endswith('.git'):
477 name = name[:-4]
478
479 if name not in self._projects:
480 m.PreSync()
481 gitdir = os.path.join(self.topdir, '%s.git' % name)
482 project = Project(manifest = self,
483 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700484 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800485 gitdir = gitdir,
486 worktree = None,
487 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700488 revisionExpr = m.revisionExpr,
489 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800490 self._projects[project.name] = project
491
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700492 def _ParseRemote(self, node):
493 """
494 reads a <remote> element from the manifest file
495 """
496 name = self._reqatt(node, 'name')
Yestin Sunb292b982012-07-02 07:32:50 -0700497 alias = node.getAttribute('alias')
498 if alias == '':
499 alias = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700500 fetch = self._reqatt(node, 'fetch')
501 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800502 if review == '':
503 review = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700504 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Yestin Sunb292b982012-07-02 07:32:50 -0700505 return _XmlRemote(name, alias, fetch, manifestUrl, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700506
507 def _ParseDefault(self, node):
508 """
509 reads a <default> element from the manifest file
510 """
511 d = _Default()
512 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700513 d.revisionExpr = node.getAttribute('revision')
514 if d.revisionExpr == '':
515 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700516
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700517 sync_j = node.getAttribute('sync-j')
518 if sync_j == '' or sync_j is None:
519 d.sync_j = 1
520 else:
521 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700522
523 sync_c = node.getAttribute('sync-c')
524 if not sync_c:
525 d.sync_c = False
526 else:
527 d.sync_c = sync_c.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700528 return d
529
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700530 def _ParseNotice(self, node):
531 """
532 reads a <notice> element from the manifest file
533
534 The <notice> element is distinct from other tags in the XML in that the
535 data is conveyed between the start and end tag (it's not an empty-element
536 tag).
537
538 The white space (carriage returns, indentation) for the notice element is
539 relevant and is parsed in a way that is based on how python docstrings work.
540 In fact, the code is remarkably similar to here:
541 http://www.python.org/dev/peps/pep-0257/
542 """
543 # Get the data out of the node...
544 notice = node.childNodes[0].data
545
546 # Figure out minimum indentation, skipping the first line (the same line
547 # as the <notice> tag)...
548 minIndent = sys.maxint
549 lines = notice.splitlines()
550 for line in lines[1:]:
551 lstrippedLine = line.lstrip()
552 if lstrippedLine:
553 indent = len(line) - len(lstrippedLine)
554 minIndent = min(indent, minIndent)
555
556 # Strip leading / trailing blank lines and also indentation.
557 cleanLines = [lines[0].strip()]
558 for line in lines[1:]:
559 cleanLines.append(line[minIndent:].rstrip())
560
561 # Clear completely blank lines from front and back...
562 while cleanLines and not cleanLines[0]:
563 del cleanLines[0]
564 while cleanLines and not cleanLines[-1]:
565 del cleanLines[-1]
566
567 return '\n'.join(cleanLines)
568
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700569 def _ParseProject(self, node):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700570 """
571 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700572 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700573 name = self._reqatt(node, 'name')
574
575 remote = self._get_remote(node)
576 if remote is None:
577 remote = self._default.remote
578 if remote is None:
579 raise ManifestParseError, \
580 "no remote for project %s within %s" % \
581 (name, self.manifestFile)
582
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700583 revisionExpr = node.getAttribute('revision')
584 if not revisionExpr:
585 revisionExpr = self._default.revisionExpr
586 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700587 raise ManifestParseError, \
588 "no revision for project %s within %s" % \
589 (name, self.manifestFile)
590
591 path = node.getAttribute('path')
592 if not path:
593 path = name
594 if path.startswith('/'):
595 raise ManifestParseError, \
596 "project %s path cannot be absolute in %s" % \
597 (name, self.manifestFile)
598
Mike Pontillod3153822012-02-28 11:53:24 -0800599 rebase = node.getAttribute('rebase')
600 if not rebase:
601 rebase = True
602 else:
603 rebase = rebase.lower() in ("yes", "true", "1")
604
Anatol Pomazau79770d22012-04-20 14:41:59 -0700605 sync_c = node.getAttribute('sync-c')
606 if not sync_c:
607 sync_c = False
608 else:
609 sync_c = sync_c.lower() in ("yes", "true", "1")
610
Brian Harring14a66742012-09-28 20:21:57 -0700611 upstream = node.getAttribute('upstream')
612
Conley Owens971de8e2012-04-16 10:36:08 -0700613 groups = ''
614 if node.hasAttribute('groups'):
615 groups = node.getAttribute('groups')
David Pursehouse1d947b32012-10-25 12:23:11 +0900616 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Brian Harring7da13142012-06-15 02:24:20 -0700617
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700618 default_groups = ['all', 'name:%s' % name, 'path:%s' % path]
Che-Liang Chiou69998b02012-01-11 11:28:42 +0800619 groups.extend(set(default_groups).difference(groups))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700620
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700621 if self.IsMirror:
622 worktree = None
623 gitdir = os.path.join(self.topdir, '%s.git' % name)
624 else:
625 worktree = os.path.join(self.topdir, path).replace('\\', '/')
626 gitdir = os.path.join(self.repodir, 'projects/%s.git' % path)
627
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700628 project = Project(manifest = self,
629 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700630 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700631 gitdir = gitdir,
632 worktree = worktree,
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700633 relpath = path,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700634 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800635 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700636 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700637 groups = groups,
Brian Harring14a66742012-09-28 20:21:57 -0700638 sync_c = sync_c,
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700639 upstream = upstream)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700640
641 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700642 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700643 self._ParseCopyFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500644 if n.nodeName == 'annotation':
645 self._ParseAnnotation(project, n)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700646
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700647 return project
648
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700649 def _ParseCopyFile(self, project, node):
650 src = self._reqatt(node, 'src')
651 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800652 if not self.IsMirror:
653 # src is project relative;
654 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800655 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700656
James W. Mills24c13082012-04-12 15:04:13 -0500657 def _ParseAnnotation(self, project, node):
658 name = self._reqatt(node, 'name')
659 value = self._reqatt(node, 'value')
660 try:
661 keep = self._reqatt(node, 'keep').lower()
662 except ManifestParseError:
663 keep = "true"
664 if keep != "true" and keep != "false":
665 raise ManifestParseError, "optional \"keep\" attribute must be \"true\" or \"false\""
666 project.AddAnnotation(name, value, keep)
667
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700668 def _get_remote(self, node):
669 name = node.getAttribute('remote')
670 if not name:
671 return None
672
673 v = self._remotes.get(name)
674 if not v:
675 raise ManifestParseError, \
676 "remote %s not defined in %s" % \
677 (name, self.manifestFile)
678 return v
679
680 def _reqatt(self, node, attname):
681 """
682 reads a required attribute from the node.
683 """
684 v = node.getAttribute(attname)
685 if not v:
686 raise ManifestParseError, \
687 "no %s in <%s> within %s" % \
688 (attname, node.nodeName, self.manifestFile)
689 return v