blob: be185477d1d0cc38708eca078cb712f333ebb446 [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
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070024from project import RemoteSpec, Project, MetaProject, R_HEADS, HEAD
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070025from error import ManifestParseError
26
27MANIFEST_FILE_NAME = 'manifest.xml'
Shawn O. Pearce5cc66792008-10-23 16:19:27 -070028LOCAL_MANIFEST_NAME = 'local_manifest.xml'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070029
Conley Owensdb728cd2011-09-26 16:34:01 -070030urlparse.uses_relative.extend(['ssh', 'git'])
31urlparse.uses_netloc.extend(['ssh', 'git'])
32
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070033class _Default(object):
34 """Project defaults within the manifest."""
35
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -070036 revisionExpr = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070037 remote = None
Shawn O. Pearce6392c872011-09-22 17:44:31 -070038 sync_j = 1
Anatol Pomazau79770d22012-04-20 14:41:59 -070039 sync_c = False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070040
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070041class _XmlRemote(object):
42 def __init__(self,
43 name,
Yestin Sunb292b982012-07-02 07:32:50 -070044 alias=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070045 fetch=None,
Conley Owensdb728cd2011-09-26 16:34:01 -070046 manifestUrl=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070047 review=None):
48 self.name = name
49 self.fetchUrl = fetch
Conley Owensdb728cd2011-09-26 16:34:01 -070050 self.manifestUrl = manifestUrl
Yestin Sunb292b982012-07-02 07:32:50 -070051 self.remoteAlias = alias
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070052 self.reviewUrl = review
Conley Owensceea3682011-10-20 10:45:47 -070053 self.resolvedFetchUrl = self._resolveFetchUrl()
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070054
Conley Owensceea3682011-10-20 10:45:47 -070055 def _resolveFetchUrl(self):
56 url = self.fetchUrl.rstrip('/')
Conley Owensdb728cd2011-09-26 16:34:01 -070057 manifestUrl = self.manifestUrl.rstrip('/')
58 # urljoin will get confused if there is no scheme in the base url
59 # ie, if manifestUrl is of the form <hostname:port>
60 if manifestUrl.find(':') != manifestUrl.find('/') - 1:
61 manifestUrl = 'gopher://' + manifestUrl
62 url = urlparse.urljoin(manifestUrl, url)
Conley Owensceea3682011-10-20 10:45:47 -070063 return re.sub(r'^gopher://', '', url)
64
65 def ToRemoteSpec(self, projectName):
Conley Owens9d8f9142011-10-20 14:36:35 -070066 url = self.resolvedFetchUrl.rstrip('/') + '/' + projectName
Yestin Sunb292b982012-07-02 07:32:50 -070067 remoteName = self.name
68 if self.remoteAlias:
69 remoteName = self.remoteAlias
70 return RemoteSpec(remoteName, url, self.reviewUrl)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070071
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070072class XmlManifest(object):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070073 """manages the repo configuration file"""
74
75 def __init__(self, repodir):
76 self.repodir = os.path.abspath(repodir)
77 self.topdir = os.path.dirname(self.repodir)
78 self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070079 self.globalConfig = GitConfig.ForUser()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070080
81 self.repoProject = MetaProject(self, 'repo',
82 gitdir = os.path.join(repodir, 'repo/.git'),
83 worktree = os.path.join(repodir, 'repo'))
84
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070085 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -080086 gitdir = os.path.join(repodir, 'manifests.git'),
87 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070088
89 self._Unload()
90
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -070091 def Override(self, name):
92 """Use a different manifest, just for the current instantiation.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070093 """
94 path = os.path.join(self.manifestProject.worktree, name)
95 if not os.path.isfile(path):
96 raise ManifestParseError('manifest %s not found' % name)
97
98 old = self.manifestFile
99 try:
100 self.manifestFile = path
101 self._Unload()
102 self._Load()
103 finally:
104 self.manifestFile = old
105
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700106 def Link(self, name):
107 """Update the repo metadata to use a different manifest.
108 """
109 self.Override(name)
110
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700111 try:
112 if os.path.exists(self.manifestFile):
113 os.remove(self.manifestFile)
114 os.symlink('manifests/%s' % name, self.manifestFile)
115 except OSError, e:
116 raise ManifestParseError('cannot link manifest %s' % name)
117
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800118 def _RemoteToXml(self, r, doc, root):
119 e = doc.createElement('remote')
120 root.appendChild(e)
121 e.setAttribute('name', r.name)
122 e.setAttribute('fetch', r.fetchUrl)
123 if r.reviewUrl is not None:
124 e.setAttribute('review', r.reviewUrl)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800125
Brian Harring14a66742012-09-28 20:21:57 -0700126 def Save(self, fd, peg_rev=False, peg_rev_upstream=True):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800127 """Write the current manifest out to the given file descriptor.
128 """
Colin Cross5acde752012-03-28 20:15:45 -0700129 mp = self.manifestProject
130
131 groups = mp.config.GetString('manifest.groups')
Colin Crossc39864f2012-04-23 13:41:58 -0700132 if not groups:
Conley Owensbb1b5f52012-08-13 13:11:18 -0700133 groups = 'all'
Conley Owens971de8e2012-04-16 10:36:08 -0700134 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Colin Cross5acde752012-03-28 20:15:45 -0700135
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800136 doc = xml.dom.minidom.Document()
137 root = doc.createElement('manifest')
138 doc.appendChild(root)
139
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700140 # Save out the notice. There's a little bit of work here to give it the
141 # right whitespace, which assumes that the notice is automatically indented
142 # by 4 by minidom.
143 if self.notice:
144 notice_element = root.appendChild(doc.createElement('notice'))
145 notice_lines = self.notice.splitlines()
146 indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:]
147 notice_element.appendChild(doc.createTextNode(indented_notice))
148
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800149 d = self.default
150 sort_remotes = list(self.remotes.keys())
151 sort_remotes.sort()
152
153 for r in sort_remotes:
154 self._RemoteToXml(self.remotes[r], doc, root)
155 if self.remotes:
156 root.appendChild(doc.createTextNode(''))
157
158 have_default = False
159 e = doc.createElement('default')
160 if d.remote:
161 have_default = True
162 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700163 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800164 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700165 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700166 if d.sync_j > 1:
167 have_default = True
168 e.setAttribute('sync-j', '%d' % d.sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700169 if d.sync_c:
170 have_default = True
171 e.setAttribute('sync-c', 'true')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800172 if have_default:
173 root.appendChild(e)
174 root.appendChild(doc.createTextNode(''))
175
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700176 if self._manifest_server:
177 e = doc.createElement('manifest-server')
178 e.setAttribute('url', self._manifest_server)
179 root.appendChild(e)
180 root.appendChild(doc.createTextNode(''))
181
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800182 sort_projects = list(self.projects.keys())
183 sort_projects.sort()
184
185 for p in sort_projects:
186 p = self.projects[p]
Colin Cross5acde752012-03-28 20:15:45 -0700187
188 if not p.MatchesGroups(groups):
189 continue
190
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800191 e = doc.createElement('project')
192 root.appendChild(e)
193 e.setAttribute('name', p.name)
194 if p.relpath != p.name:
195 e.setAttribute('path', p.relpath)
196 if not d.remote or p.remote.name != d.remote.name:
197 e.setAttribute('remote', p.remote.name)
198 if peg_rev:
199 if self.IsMirror:
Brian Harring14a66742012-09-28 20:21:57 -0700200 value = p.bare_git.rev_parse(p.revisionExpr + '^0')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800201 else:
Brian Harring14a66742012-09-28 20:21:57 -0700202 value = p.work_git.rev_parse(HEAD + '^0')
203 e.setAttribute('revision', value)
204 if peg_rev_upstream and value != p.revisionExpr:
205 # Only save the origin if the origin is not a sha1, and the default
206 # isn't our value, and the if the default doesn't already have that
207 # covered.
208 e.setAttribute('upstream', p.revisionExpr)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700209 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
210 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800211
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800212 for c in p.copyfiles:
213 ce = doc.createElement('copyfile')
214 ce.setAttribute('src', c.src)
215 ce.setAttribute('dest', c.dest)
216 e.appendChild(ce)
217
Conley Owensbb1b5f52012-08-13 13:11:18 -0700218 default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath]
Dmitry Fink17f85ea2012-08-06 14:52:29 -0700219 egroups = [g for g in p.groups if g not in default_groups]
Conley Owens971de8e2012-04-16 10:36:08 -0700220 if egroups:
221 e.setAttribute('groups', ','.join(egroups))
Colin Cross5acde752012-03-28 20:15:45 -0700222
James W. Mills24c13082012-04-12 15:04:13 -0500223 for a in p.annotations:
224 if a.keep == "true":
225 ae = doc.createElement('annotation')
226 ae.setAttribute('name', a.name)
227 ae.setAttribute('value', a.value)
228 e.appendChild(ae)
229
Anatol Pomazau79770d22012-04-20 14:41:59 -0700230 if p.sync_c:
231 e.setAttribute('sync-c', 'true')
232
Doug Anderson37282b42011-03-04 11:54:18 -0800233 if self._repo_hooks_project:
234 root.appendChild(doc.createTextNode(''))
235 e = doc.createElement('repo-hooks')
236 e.setAttribute('in-project', self._repo_hooks_project.name)
237 e.setAttribute('enabled-list',
238 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
239 root.appendChild(e)
240
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800241 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
242
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700243 @property
244 def projects(self):
245 self._Load()
246 return self._projects
247
248 @property
249 def remotes(self):
250 self._Load()
251 return self._remotes
252
253 @property
254 def default(self):
255 self._Load()
256 return self._default
257
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800258 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800259 def repo_hooks_project(self):
260 self._Load()
261 return self._repo_hooks_project
262
263 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700264 def notice(self):
265 self._Load()
266 return self._notice
267
268 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700269 def manifest_server(self):
270 self._Load()
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800271 return self._manifest_server
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700272
273 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800274 def IsMirror(self):
275 return self.manifestProject.config.GetBoolean('repo.mirror')
276
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700277 def _Unload(self):
278 self._loaded = False
279 self._projects = {}
280 self._remotes = {}
281 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800282 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700283 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700284 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700285 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700286
287 def _Load(self):
288 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800289 m = self.manifestProject
290 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700291 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800292 b = b[len(R_HEADS):]
293 self.branch = b
294
Colin Cross23acdd32012-04-21 00:33:54 -0700295 nodes = []
Brian Harring475a47d2012-06-07 20:05:35 -0700296 nodes.append(self._ParseManifestXml(self.manifestFile,
297 self.manifestProject.worktree))
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700298
299 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
300 if os.path.exists(local):
Brian Harring475a47d2012-06-07 20:05:35 -0700301 nodes.append(self._ParseManifestXml(local, self.repodir))
Colin Cross23acdd32012-04-21 00:33:54 -0700302
303 self._ParseManifest(nodes)
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700304
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800305 if self.IsMirror:
306 self._AddMetaProjectMirror(self.repoProject)
307 self._AddMetaProjectMirror(self.manifestProject)
308
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700309 self._loaded = True
310
Brian Harring475a47d2012-06-07 20:05:35 -0700311 def _ParseManifestXml(self, path, include_root):
Brian Harring26448742011-04-28 05:04:41 -0700312 root = xml.dom.minidom.parse(path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700313 if not root or not root.childNodes:
Brian Harring26448742011-04-28 05:04:41 -0700314 raise ManifestParseError("no root node in %s" % (path,))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700315
Jooncheol Park34acdd22012-08-27 02:25:59 +0900316 for manifest in root.childNodes:
317 if manifest.nodeName == 'manifest':
318 break
319 else:
Brian Harring26448742011-04-28 05:04:41 -0700320 raise ManifestParseError("no <manifest> in %s" % (path,))
321
Colin Cross23acdd32012-04-21 00:33:54 -0700322 nodes = []
Jooncheol Park34acdd22012-08-27 02:25:59 +0900323 for node in manifest.childNodes:
Brian Harring26448742011-04-28 05:04:41 -0700324 if node.nodeName == 'include':
325 name = self._reqatt(node, 'name')
Brian Harring475a47d2012-06-07 20:05:35 -0700326 fp = os.path.join(include_root, name)
Brian Harring26448742011-04-28 05:04:41 -0700327 if not os.path.isfile(fp):
328 raise ManifestParseError, \
329 "include %s doesn't exist or isn't a file" % \
330 (name,)
331 try:
Brian Harring475a47d2012-06-07 20:05:35 -0700332 nodes.extend(self._ParseManifestXml(fp, include_root))
Brian Harring26448742011-04-28 05:04:41 -0700333 # should isolate this to the exact exception, but that's
334 # tricky. actual parsing implementation may vary.
335 except (KeyboardInterrupt, RuntimeError, SystemExit):
336 raise
337 except Exception, e:
338 raise ManifestParseError(
339 "failed parsing included manifest %s: %s", (name, e))
Colin Cross23acdd32012-04-21 00:33:54 -0700340 else:
341 nodes.append(node)
342 return nodes
Brian Harring26448742011-04-28 05:04:41 -0700343
Colin Cross23acdd32012-04-21 00:33:54 -0700344 def _ParseManifest(self, node_list):
345 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700346 if node.nodeName == 'remote':
347 remote = self._ParseRemote(node)
348 if self._remotes.get(remote.name):
Doug Anderson37282b42011-03-04 11:54:18 -0800349 raise ManifestParseError(
350 'duplicate remote %s in %s' %
351 (remote.name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700352 self._remotes[remote.name] = remote
353
Colin Cross23acdd32012-04-21 00:33:54 -0700354 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700355 if node.nodeName == 'default':
356 if self._default is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800357 raise ManifestParseError(
358 'duplicate default in %s' %
359 (self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700360 self._default = self._ParseDefault(node)
361 if self._default is None:
362 self._default = _Default()
363
Colin Cross23acdd32012-04-21 00:33:54 -0700364 for node in itertools.chain(*node_list):
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700365 if node.nodeName == 'notice':
366 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800367 raise ManifestParseError(
368 'duplicate notice in %s' %
369 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700370 self._notice = self._ParseNotice(node)
371
Colin Cross23acdd32012-04-21 00:33:54 -0700372 for node in itertools.chain(*node_list):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700373 if node.nodeName == 'manifest-server':
374 url = self._reqatt(node, 'url')
375 if self._manifest_server is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800376 raise ManifestParseError(
377 'duplicate manifest-server in %s' %
378 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700379 self._manifest_server = url
380
Colin Cross23acdd32012-04-21 00:33:54 -0700381 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700382 if node.nodeName == 'project':
383 project = self._ParseProject(node)
384 if self._projects.get(project.name):
Doug Anderson37282b42011-03-04 11:54:18 -0800385 raise ManifestParseError(
386 'duplicate project %s in %s' %
387 (project.name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700388 self._projects[project.name] = project
Doug Anderson37282b42011-03-04 11:54:18 -0800389 if node.nodeName == 'repo-hooks':
390 # Get the name of the project and the (space-separated) list of enabled.
391 repo_hooks_project = self._reqatt(node, 'in-project')
392 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
393
394 # Only one project can be the hooks project
395 if self._repo_hooks_project is not None:
396 raise ManifestParseError(
397 'duplicate repo-hooks in %s' %
398 (self.manifestFile))
399
400 # Store a reference to the Project.
401 try:
402 self._repo_hooks_project = self._projects[repo_hooks_project]
403 except KeyError:
404 raise ManifestParseError(
405 'project %s not found for repo-hooks' %
406 (repo_hooks_project))
407
408 # Store the enabled hooks in the Project object.
409 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
Colin Cross23acdd32012-04-21 00:33:54 -0700410 if node.nodeName == 'remove-project':
411 name = self._reqatt(node, 'name')
412 try:
413 del self._projects[name]
414 except KeyError:
415 raise ManifestParseError(
416 'project %s not found' %
417 (name))
418
419 # If the manifest removes the hooks project, treat it as if it deleted
420 # the repo-hooks element too.
421 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
422 self._repo_hooks_project = None
423
Doug Anderson37282b42011-03-04 11:54:18 -0800424
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800425 def _AddMetaProjectMirror(self, m):
426 name = None
427 m_url = m.GetRemote(m.remote.name).url
428 if m_url.endswith('/.git'):
429 raise ManifestParseError, 'refusing to mirror %s' % m_url
430
431 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700432 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800433 if not url.endswith('/'):
434 url += '/'
435 if m_url.startswith(url):
436 remote = self._default.remote
437 name = m_url[len(url):]
438
439 if name is None:
440 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700441 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Shawn O. Pearcef35b2d92012-08-02 11:46:22 -0700442 remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800443 name = m_url[s:]
444
445 if name.endswith('.git'):
446 name = name[:-4]
447
448 if name not in self._projects:
449 m.PreSync()
450 gitdir = os.path.join(self.topdir, '%s.git' % name)
451 project = Project(manifest = self,
452 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700453 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800454 gitdir = gitdir,
455 worktree = None,
456 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700457 revisionExpr = m.revisionExpr,
458 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800459 self._projects[project.name] = project
460
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700461 def _ParseRemote(self, node):
462 """
463 reads a <remote> element from the manifest file
464 """
465 name = self._reqatt(node, 'name')
Yestin Sunb292b982012-07-02 07:32:50 -0700466 alias = node.getAttribute('alias')
467 if alias == '':
468 alias = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700469 fetch = self._reqatt(node, 'fetch')
470 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800471 if review == '':
472 review = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700473 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Yestin Sunb292b982012-07-02 07:32:50 -0700474 return _XmlRemote(name, alias, fetch, manifestUrl, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700475
476 def _ParseDefault(self, node):
477 """
478 reads a <default> element from the manifest file
479 """
480 d = _Default()
481 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700482 d.revisionExpr = node.getAttribute('revision')
483 if d.revisionExpr == '':
484 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700485
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700486 sync_j = node.getAttribute('sync-j')
487 if sync_j == '' or sync_j is None:
488 d.sync_j = 1
489 else:
490 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700491
492 sync_c = node.getAttribute('sync-c')
493 if not sync_c:
494 d.sync_c = False
495 else:
496 d.sync_c = sync_c.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700497 return d
498
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700499 def _ParseNotice(self, node):
500 """
501 reads a <notice> element from the manifest file
502
503 The <notice> element is distinct from other tags in the XML in that the
504 data is conveyed between the start and end tag (it's not an empty-element
505 tag).
506
507 The white space (carriage returns, indentation) for the notice element is
508 relevant and is parsed in a way that is based on how python docstrings work.
509 In fact, the code is remarkably similar to here:
510 http://www.python.org/dev/peps/pep-0257/
511 """
512 # Get the data out of the node...
513 notice = node.childNodes[0].data
514
515 # Figure out minimum indentation, skipping the first line (the same line
516 # as the <notice> tag)...
517 minIndent = sys.maxint
518 lines = notice.splitlines()
519 for line in lines[1:]:
520 lstrippedLine = line.lstrip()
521 if lstrippedLine:
522 indent = len(line) - len(lstrippedLine)
523 minIndent = min(indent, minIndent)
524
525 # Strip leading / trailing blank lines and also indentation.
526 cleanLines = [lines[0].strip()]
527 for line in lines[1:]:
528 cleanLines.append(line[minIndent:].rstrip())
529
530 # Clear completely blank lines from front and back...
531 while cleanLines and not cleanLines[0]:
532 del cleanLines[0]
533 while cleanLines and not cleanLines[-1]:
534 del cleanLines[-1]
535
536 return '\n'.join(cleanLines)
537
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700538 def _ParseProject(self, node):
539 """
540 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700541 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700542 name = self._reqatt(node, 'name')
543
544 remote = self._get_remote(node)
545 if remote is None:
546 remote = self._default.remote
547 if remote is None:
548 raise ManifestParseError, \
549 "no remote for project %s within %s" % \
550 (name, self.manifestFile)
551
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700552 revisionExpr = node.getAttribute('revision')
553 if not revisionExpr:
554 revisionExpr = self._default.revisionExpr
555 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700556 raise ManifestParseError, \
557 "no revision for project %s within %s" % \
558 (name, self.manifestFile)
559
560 path = node.getAttribute('path')
561 if not path:
562 path = name
563 if path.startswith('/'):
564 raise ManifestParseError, \
565 "project %s path cannot be absolute in %s" % \
566 (name, self.manifestFile)
567
Mike Pontillod3153822012-02-28 11:53:24 -0800568 rebase = node.getAttribute('rebase')
569 if not rebase:
570 rebase = True
571 else:
572 rebase = rebase.lower() in ("yes", "true", "1")
573
Anatol Pomazau79770d22012-04-20 14:41:59 -0700574 sync_c = node.getAttribute('sync-c')
575 if not sync_c:
576 sync_c = False
577 else:
578 sync_c = sync_c.lower() in ("yes", "true", "1")
579
Brian Harring14a66742012-09-28 20:21:57 -0700580 upstream = node.getAttribute('upstream')
581
Conley Owens971de8e2012-04-16 10:36:08 -0700582 groups = ''
583 if node.hasAttribute('groups'):
584 groups = node.getAttribute('groups')
585 groups = [x for x in re.split('[,\s]+', groups) if x]
Brian Harring7da13142012-06-15 02:24:20 -0700586
587 default_groups = ['default', 'name:%s' % name, 'path:%s' % path]
588 groups.extend(set(default_groups).difference(groups))
Colin Cross5acde752012-03-28 20:15:45 -0700589
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800590 if self.IsMirror:
591 relpath = None
592 worktree = None
593 gitdir = os.path.join(self.topdir, '%s.git' % name)
594 else:
Anthony Newnamdf14a702011-01-09 17:31:57 -0800595 worktree = os.path.join(self.topdir, path).replace('\\', '/')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800596 gitdir = os.path.join(self.repodir, 'projects/%s.git' % path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700597
598 project = Project(manifest = self,
599 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700600 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700601 gitdir = gitdir,
602 worktree = worktree,
603 relpath = path,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700604 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800605 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700606 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700607 groups = groups,
Brian Harring14a66742012-09-28 20:21:57 -0700608 sync_c = sync_c,
609 upstream = upstream)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700610
611 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700612 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700613 self._ParseCopyFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500614 if n.nodeName == 'annotation':
615 self._ParseAnnotation(project, n)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700616
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700617 return project
618
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700619 def _ParseCopyFile(self, project, node):
620 src = self._reqatt(node, 'src')
621 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800622 if not self.IsMirror:
623 # src is project relative;
624 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800625 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700626
James W. Mills24c13082012-04-12 15:04:13 -0500627 def _ParseAnnotation(self, project, node):
628 name = self._reqatt(node, 'name')
629 value = self._reqatt(node, 'value')
630 try:
631 keep = self._reqatt(node, 'keep').lower()
632 except ManifestParseError:
633 keep = "true"
634 if keep != "true" and keep != "false":
635 raise ManifestParseError, "optional \"keep\" attribute must be \"true\" or \"false\""
636 project.AddAnnotation(name, value, keep)
637
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700638 def _get_remote(self, node):
639 name = node.getAttribute('remote')
640 if not name:
641 return None
642
643 v = self._remotes.get(name)
644 if not v:
645 raise ManifestParseError, \
646 "remote %s not defined in %s" % \
647 (name, self.manifestFile)
648 return v
649
650 def _reqatt(self, node, attname):
651 """
652 reads a required attribute from the node.
653 """
654 v = node.getAttribute(attname)
655 if not v:
656 raise ManifestParseError, \
657 "no %s in <%s> within %s" % \
658 (attname, node.nodeName, self.manifestFile)
659 return v