blob: 1300e916a94d255f2af86f9994dde14a2786cbc6 [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
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +080043 sync_s = False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070044
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070045class _XmlRemote(object):
46 def __init__(self,
47 name,
Yestin Sunb292b982012-07-02 07:32:50 -070048 alias=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070049 fetch=None,
Conley Owensdb728cd2011-09-26 16:34:01 -070050 manifestUrl=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070051 review=None):
52 self.name = name
53 self.fetchUrl = fetch
Conley Owensdb728cd2011-09-26 16:34:01 -070054 self.manifestUrl = manifestUrl
Yestin Sunb292b982012-07-02 07:32:50 -070055 self.remoteAlias = alias
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070056 self.reviewUrl = review
Conley Owensceea3682011-10-20 10:45:47 -070057 self.resolvedFetchUrl = self._resolveFetchUrl()
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070058
David Pursehouse717ece92012-11-13 08:49:16 +090059 def __eq__(self, other):
60 return self.__dict__ == other.__dict__
61
62 def __ne__(self, other):
63 return self.__dict__ != other.__dict__
64
Conley Owensceea3682011-10-20 10:45:47 -070065 def _resolveFetchUrl(self):
66 url = self.fetchUrl.rstrip('/')
Conley Owensdb728cd2011-09-26 16:34:01 -070067 manifestUrl = self.manifestUrl.rstrip('/')
Shawn Pearcea9f11b32013-01-02 15:40:48 -080068 p = manifestUrl.startswith('persistent-http')
69 if p:
70 manifestUrl = manifestUrl[len('persistent-'):]
71
Conley Owensdb728cd2011-09-26 16:34:01 -070072 # urljoin will get confused if there is no scheme in the base url
73 # ie, if manifestUrl is of the form <hostname:port>
74 if manifestUrl.find(':') != manifestUrl.find('/') - 1:
David Pursehousec1b86a22012-11-14 11:36:51 +090075 manifestUrl = 'gopher://' + manifestUrl
Conley Owensdb728cd2011-09-26 16:34:01 -070076 url = urlparse.urljoin(manifestUrl, url)
Shawn Pearcea9f11b32013-01-02 15:40:48 -080077 url = re.sub(r'^gopher://', '', url)
78 if p:
79 url = 'persistent-' + url
80 return url
Conley Owensceea3682011-10-20 10:45:47 -070081
82 def ToRemoteSpec(self, projectName):
Conley Owens9d8f9142011-10-20 14:36:35 -070083 url = self.resolvedFetchUrl.rstrip('/') + '/' + projectName
Yestin Sunb292b982012-07-02 07:32:50 -070084 remoteName = self.name
85 if self.remoteAlias:
86 remoteName = self.remoteAlias
87 return RemoteSpec(remoteName, url, self.reviewUrl)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070088
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070089class XmlManifest(object):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070090 """manages the repo configuration file"""
91
92 def __init__(self, repodir):
93 self.repodir = os.path.abspath(repodir)
94 self.topdir = os.path.dirname(self.repodir)
95 self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070096 self.globalConfig = GitConfig.ForUser()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070097
98 self.repoProject = MetaProject(self, 'repo',
99 gitdir = os.path.join(repodir, 'repo/.git'),
100 worktree = os.path.join(repodir, 'repo'))
101
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700102 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -0800103 gitdir = os.path.join(repodir, 'manifests.git'),
104 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700105
106 self._Unload()
107
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700108 def Override(self, name):
109 """Use a different manifest, just for the current instantiation.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700110 """
111 path = os.path.join(self.manifestProject.worktree, name)
112 if not os.path.isfile(path):
113 raise ManifestParseError('manifest %s not found' % name)
114
115 old = self.manifestFile
116 try:
117 self.manifestFile = path
118 self._Unload()
119 self._Load()
120 finally:
121 self.manifestFile = old
122
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700123 def Link(self, name):
124 """Update the repo metadata to use a different manifest.
125 """
126 self.Override(name)
127
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700128 try:
129 if os.path.exists(self.manifestFile):
130 os.remove(self.manifestFile)
131 os.symlink('manifests/%s' % name, self.manifestFile)
David Pursehouse8a68ff92012-09-24 12:15:13 +0900132 except OSError:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700133 raise ManifestParseError('cannot link manifest %s' % name)
134
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800135 def _RemoteToXml(self, r, doc, root):
136 e = doc.createElement('remote')
137 root.appendChild(e)
138 e.setAttribute('name', r.name)
139 e.setAttribute('fetch', r.fetchUrl)
140 if r.reviewUrl is not None:
141 e.setAttribute('review', r.reviewUrl)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800142
Brian Harring14a66742012-09-28 20:21:57 -0700143 def Save(self, fd, peg_rev=False, peg_rev_upstream=True):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800144 """Write the current manifest out to the given file descriptor.
145 """
Colin Cross5acde752012-03-28 20:15:45 -0700146 mp = self.manifestProject
147
148 groups = mp.config.GetString('manifest.groups')
Matt Gumbel0c635bb2012-12-21 10:14:53 -0800149 if groups:
150 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Colin Cross5acde752012-03-28 20:15:45 -0700151
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800152 doc = xml.dom.minidom.Document()
153 root = doc.createElement('manifest')
154 doc.appendChild(root)
155
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700156 # Save out the notice. There's a little bit of work here to give it the
157 # right whitespace, which assumes that the notice is automatically indented
158 # by 4 by minidom.
159 if self.notice:
160 notice_element = root.appendChild(doc.createElement('notice'))
161 notice_lines = self.notice.splitlines()
162 indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:]
163 notice_element.appendChild(doc.createTextNode(indented_notice))
164
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800165 d = self.default
166 sort_remotes = list(self.remotes.keys())
167 sort_remotes.sort()
168
169 for r in sort_remotes:
170 self._RemoteToXml(self.remotes[r], doc, root)
171 if self.remotes:
172 root.appendChild(doc.createTextNode(''))
173
174 have_default = False
175 e = doc.createElement('default')
176 if d.remote:
177 have_default = True
178 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700179 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800180 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700181 e.setAttribute('revision', d.revisionExpr)
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700182 if d.sync_j > 1:
183 have_default = True
184 e.setAttribute('sync-j', '%d' % d.sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700185 if d.sync_c:
186 have_default = True
187 e.setAttribute('sync-c', 'true')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800188 if d.sync_s:
189 have_default = True
190 e.setAttribute('sync-s', 'true')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800191 if have_default:
192 root.appendChild(e)
193 root.appendChild(doc.createTextNode(''))
194
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700195 if self._manifest_server:
196 e = doc.createElement('manifest-server')
197 e.setAttribute('url', self._manifest_server)
198 root.appendChild(e)
199 root.appendChild(doc.createTextNode(''))
200
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800201 def output_projects(parent, parent_node, projects):
202 for p in projects:
203 output_project(parent, parent_node, self.projects[p])
Che-Liang Chiou69998b02012-01-11 11:28:42 +0800204
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800205 def output_project(parent, parent_node, p):
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700206 if not p.MatchesGroups(groups):
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800207 return
208
209 name = p.name
210 relpath = p.relpath
211 if parent:
212 name = self._UnjoinName(parent.name, name)
213 relpath = self._UnjoinRelpath(parent.relpath, relpath)
Colin Cross5acde752012-03-28 20:15:45 -0700214
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800215 e = doc.createElement('project')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800216 parent_node.appendChild(e)
217 e.setAttribute('name', name)
218 if relpath != name:
219 e.setAttribute('path', relpath)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800220 if not d.remote or p.remote.name != d.remote.name:
221 e.setAttribute('remote', p.remote.name)
222 if peg_rev:
223 if self.IsMirror:
Brian Harring14a66742012-09-28 20:21:57 -0700224 value = p.bare_git.rev_parse(p.revisionExpr + '^0')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800225 else:
Brian Harring14a66742012-09-28 20:21:57 -0700226 value = p.work_git.rev_parse(HEAD + '^0')
227 e.setAttribute('revision', value)
228 if peg_rev_upstream and value != p.revisionExpr:
229 # Only save the origin if the origin is not a sha1, and the default
230 # isn't our value, and the if the default doesn't already have that
231 # covered.
232 e.setAttribute('upstream', p.revisionExpr)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700233 elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
234 e.setAttribute('revision', p.revisionExpr)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800235
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800236 for c in p.copyfiles:
237 ce = doc.createElement('copyfile')
238 ce.setAttribute('src', c.src)
239 ce.setAttribute('dest', c.dest)
240 e.appendChild(ce)
241
Conley Owensbb1b5f52012-08-13 13:11:18 -0700242 default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath]
Dmitry Fink17f85ea2012-08-06 14:52:29 -0700243 egroups = [g for g in p.groups if g not in default_groups]
Conley Owens971de8e2012-04-16 10:36:08 -0700244 if egroups:
245 e.setAttribute('groups', ','.join(egroups))
Colin Cross5acde752012-03-28 20:15:45 -0700246
James W. Mills24c13082012-04-12 15:04:13 -0500247 for a in p.annotations:
248 if a.keep == "true":
249 ae = doc.createElement('annotation')
250 ae.setAttribute('name', a.name)
251 ae.setAttribute('value', a.value)
252 e.appendChild(ae)
253
Anatol Pomazau79770d22012-04-20 14:41:59 -0700254 if p.sync_c:
255 e.setAttribute('sync-c', 'true')
256
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800257 if p.sync_s:
258 e.setAttribute('sync-s', 'true')
259
260 if p.subprojects:
261 sort_projects = [subp.name for subp in p.subprojects]
262 sort_projects.sort()
263 output_projects(p, e, sort_projects)
264
265 sort_projects = [key for key in self.projects.keys()
266 if not self.projects[key].parent]
267 sort_projects.sort()
268 output_projects(None, root, sort_projects)
269
Doug Anderson37282b42011-03-04 11:54:18 -0800270 if self._repo_hooks_project:
271 root.appendChild(doc.createTextNode(''))
272 e = doc.createElement('repo-hooks')
273 e.setAttribute('in-project', self._repo_hooks_project.name)
274 e.setAttribute('enabled-list',
275 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
276 root.appendChild(e)
277
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800278 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
279
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700280 @property
281 def projects(self):
282 self._Load()
283 return self._projects
284
285 @property
286 def remotes(self):
287 self._Load()
288 return self._remotes
289
290 @property
291 def default(self):
292 self._Load()
293 return self._default
294
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800295 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800296 def repo_hooks_project(self):
297 self._Load()
298 return self._repo_hooks_project
299
300 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700301 def notice(self):
302 self._Load()
303 return self._notice
304
305 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700306 def manifest_server(self):
307 self._Load()
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800308 return self._manifest_server
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700309
310 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800311 def IsMirror(self):
312 return self.manifestProject.config.GetBoolean('repo.mirror')
313
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700314 def _Unload(self):
315 self._loaded = False
316 self._projects = {}
317 self._remotes = {}
318 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800319 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700320 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700321 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700322 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700323
324 def _Load(self):
325 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800326 m = self.manifestProject
327 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700328 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800329 b = b[len(R_HEADS):]
330 self.branch = b
331
Colin Cross23acdd32012-04-21 00:33:54 -0700332 nodes = []
Brian Harring475a47d2012-06-07 20:05:35 -0700333 nodes.append(self._ParseManifestXml(self.manifestFile,
334 self.manifestProject.worktree))
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700335
336 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
337 if os.path.exists(local):
Sarah Owenscecd1d82012-11-01 22:59:27 -0700338 print('warning: %s is deprecated; put local manifests in %s instead'
339 % (LOCAL_MANIFEST_NAME, LOCAL_MANIFESTS_DIR_NAME),
340 file=sys.stderr)
Brian Harring475a47d2012-06-07 20:05:35 -0700341 nodes.append(self._ParseManifestXml(local, self.repodir))
Colin Cross23acdd32012-04-21 00:33:54 -0700342
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900343 local_dir = os.path.abspath(os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME))
344 try:
David Pursehouse52f1e5d2012-11-14 04:53:24 +0900345 for local_file in sorted(os.listdir(local_dir)):
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900346 if local_file.endswith('.xml'):
347 try:
348 nodes.append(self._ParseManifestXml(local_file, self.repodir))
349 except ManifestParseError as e:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700350 print('%s' % str(e), file=sys.stderr)
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900351 except OSError:
352 pass
353
Colin Cross23acdd32012-04-21 00:33:54 -0700354 self._ParseManifest(nodes)
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700355
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800356 if self.IsMirror:
357 self._AddMetaProjectMirror(self.repoProject)
358 self._AddMetaProjectMirror(self.manifestProject)
359
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700360 self._loaded = True
361
Brian Harring475a47d2012-06-07 20:05:35 -0700362 def _ParseManifestXml(self, path, include_root):
David Pursehousef7fc8a92012-11-13 04:00:28 +0900363 try:
364 root = xml.dom.minidom.parse(path)
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900365 except (OSError, xml.parsers.expat.ExpatError) as e:
David Pursehousef7fc8a92012-11-13 04:00:28 +0900366 raise ManifestParseError("error parsing manifest %s: %s" % (path, e))
367
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700368 if not root or not root.childNodes:
Brian Harring26448742011-04-28 05:04:41 -0700369 raise ManifestParseError("no root node in %s" % (path,))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700370
Jooncheol Park34acdd22012-08-27 02:25:59 +0900371 for manifest in root.childNodes:
372 if manifest.nodeName == 'manifest':
373 break
374 else:
Brian Harring26448742011-04-28 05:04:41 -0700375 raise ManifestParseError("no <manifest> in %s" % (path,))
376
Colin Cross23acdd32012-04-21 00:33:54 -0700377 nodes = []
David Pursehouse4f7bdea2012-10-22 12:50:15 +0900378 for node in manifest.childNodes: # pylint:disable=W0631
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900379 # We only get here if manifest is initialised
David Pursehousec1b86a22012-11-14 11:36:51 +0900380 if node.nodeName == 'include':
381 name = self._reqatt(node, 'name')
382 fp = os.path.join(include_root, name)
383 if not os.path.isfile(fp):
384 raise ManifestParseError, \
385 "include %s doesn't exist or isn't a file" % \
386 (name,)
387 try:
388 nodes.extend(self._ParseManifestXml(fp, include_root))
389 # should isolate this to the exact exception, but that's
390 # tricky. actual parsing implementation may vary.
391 except (KeyboardInterrupt, RuntimeError, SystemExit):
392 raise
393 except Exception as e:
394 raise ManifestParseError(
395 "failed parsing included manifest %s: %s", (name, e))
396 else:
397 nodes.append(node)
Colin Cross23acdd32012-04-21 00:33:54 -0700398 return nodes
Shawn O. Pearce03eaf072008-11-20 11:42:22 -0800399
Colin Cross23acdd32012-04-21 00:33:54 -0700400 def _ParseManifest(self, node_list):
401 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700402 if node.nodeName == 'remote':
403 remote = self._ParseRemote(node)
David Pursehouse717ece92012-11-13 08:49:16 +0900404 if remote:
405 if remote.name in self._remotes:
406 if remote != self._remotes[remote.name]:
407 raise ManifestParseError(
408 'remote %s already exists with different attributes' %
409 (remote.name))
410 else:
411 self._remotes[remote.name] = remote
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700412
Colin Cross23acdd32012-04-21 00:33:54 -0700413 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700414 if node.nodeName == 'default':
415 if self._default is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800416 raise ManifestParseError(
417 'duplicate default in %s' %
418 (self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700419 self._default = self._ParseDefault(node)
420 if self._default is None:
421 self._default = _Default()
422
Colin Cross23acdd32012-04-21 00:33:54 -0700423 for node in itertools.chain(*node_list):
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700424 if node.nodeName == 'notice':
425 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800426 raise ManifestParseError(
427 'duplicate notice in %s' %
428 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700429 self._notice = self._ParseNotice(node)
430
Colin Cross23acdd32012-04-21 00:33:54 -0700431 for node in itertools.chain(*node_list):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700432 if node.nodeName == 'manifest-server':
433 url = self._reqatt(node, 'url')
434 if self._manifest_server is not None:
David Pursehousec1b86a22012-11-14 11:36:51 +0900435 raise ManifestParseError(
436 'duplicate manifest-server in %s' %
437 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700438 self._manifest_server = url
439
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800440 def recursively_add_projects(project):
441 if self._projects.get(project.name):
442 raise ManifestParseError(
443 'duplicate project %s in %s' %
444 (project.name, self.manifestFile))
445 self._projects[project.name] = project
446 for subproject in project.subprojects:
447 recursively_add_projects(subproject)
448
Colin Cross23acdd32012-04-21 00:33:54 -0700449 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700450 if node.nodeName == 'project':
451 project = self._ParseProject(node)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800452 recursively_add_projects(project)
Doug Anderson37282b42011-03-04 11:54:18 -0800453 if node.nodeName == 'repo-hooks':
454 # Get the name of the project and the (space-separated) list of enabled.
455 repo_hooks_project = self._reqatt(node, 'in-project')
456 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
457
458 # Only one project can be the hooks project
459 if self._repo_hooks_project is not None:
460 raise ManifestParseError(
461 'duplicate repo-hooks in %s' %
462 (self.manifestFile))
463
464 # Store a reference to the Project.
465 try:
466 self._repo_hooks_project = self._projects[repo_hooks_project]
467 except KeyError:
468 raise ManifestParseError(
469 'project %s not found for repo-hooks' %
470 (repo_hooks_project))
471
472 # Store the enabled hooks in the Project object.
473 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
Colin Cross23acdd32012-04-21 00:33:54 -0700474 if node.nodeName == 'remove-project':
475 name = self._reqatt(node, 'name')
476 try:
477 del self._projects[name]
478 except KeyError:
David Pursehousef9107482012-11-16 19:12:32 +0900479 raise ManifestParseError('remove-project element specifies non-existent '
480 'project: %s' % name)
Colin Cross23acdd32012-04-21 00:33:54 -0700481
482 # If the manifest removes the hooks project, treat it as if it deleted
483 # the repo-hooks element too.
484 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
485 self._repo_hooks_project = None
486
Doug Anderson37282b42011-03-04 11:54:18 -0800487
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800488 def _AddMetaProjectMirror(self, m):
489 name = None
490 m_url = m.GetRemote(m.remote.name).url
491 if m_url.endswith('/.git'):
492 raise ManifestParseError, 'refusing to mirror %s' % m_url
493
494 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700495 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800496 if not url.endswith('/'):
497 url += '/'
498 if m_url.startswith(url):
499 remote = self._default.remote
500 name = m_url[len(url):]
501
502 if name is None:
503 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700504 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Shawn O. Pearcef35b2d92012-08-02 11:46:22 -0700505 remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800506 name = m_url[s:]
507
508 if name.endswith('.git'):
509 name = name[:-4]
510
511 if name not in self._projects:
512 m.PreSync()
513 gitdir = os.path.join(self.topdir, '%s.git' % name)
514 project = Project(manifest = self,
515 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700516 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800517 gitdir = gitdir,
518 worktree = None,
519 relpath = None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700520 revisionExpr = m.revisionExpr,
521 revisionId = None)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800522 self._projects[project.name] = project
523
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700524 def _ParseRemote(self, node):
525 """
526 reads a <remote> element from the manifest file
527 """
528 name = self._reqatt(node, 'name')
Yestin Sunb292b982012-07-02 07:32:50 -0700529 alias = node.getAttribute('alias')
530 if alias == '':
531 alias = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700532 fetch = self._reqatt(node, 'fetch')
533 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800534 if review == '':
535 review = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700536 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Yestin Sunb292b982012-07-02 07:32:50 -0700537 return _XmlRemote(name, alias, fetch, manifestUrl, review)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700538
539 def _ParseDefault(self, node):
540 """
541 reads a <default> element from the manifest file
542 """
543 d = _Default()
544 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700545 d.revisionExpr = node.getAttribute('revision')
546 if d.revisionExpr == '':
547 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700548
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700549 sync_j = node.getAttribute('sync-j')
550 if sync_j == '' or sync_j is None:
551 d.sync_j = 1
552 else:
553 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700554
555 sync_c = node.getAttribute('sync-c')
556 if not sync_c:
557 d.sync_c = False
558 else:
559 d.sync_c = sync_c.lower() in ("yes", "true", "1")
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800560
561 sync_s = node.getAttribute('sync-s')
562 if not sync_s:
563 d.sync_s = False
564 else:
565 d.sync_s = sync_s.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700566 return d
567
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700568 def _ParseNotice(self, node):
569 """
570 reads a <notice> element from the manifest file
571
572 The <notice> element is distinct from other tags in the XML in that the
573 data is conveyed between the start and end tag (it's not an empty-element
574 tag).
575
576 The white space (carriage returns, indentation) for the notice element is
577 relevant and is parsed in a way that is based on how python docstrings work.
578 In fact, the code is remarkably similar to here:
579 http://www.python.org/dev/peps/pep-0257/
580 """
581 # Get the data out of the node...
582 notice = node.childNodes[0].data
583
584 # Figure out minimum indentation, skipping the first line (the same line
585 # as the <notice> tag)...
586 minIndent = sys.maxint
587 lines = notice.splitlines()
588 for line in lines[1:]:
589 lstrippedLine = line.lstrip()
590 if lstrippedLine:
591 indent = len(line) - len(lstrippedLine)
592 minIndent = min(indent, minIndent)
593
594 # Strip leading / trailing blank lines and also indentation.
595 cleanLines = [lines[0].strip()]
596 for line in lines[1:]:
597 cleanLines.append(line[minIndent:].rstrip())
598
599 # Clear completely blank lines from front and back...
600 while cleanLines and not cleanLines[0]:
601 del cleanLines[0]
602 while cleanLines and not cleanLines[-1]:
603 del cleanLines[-1]
604
605 return '\n'.join(cleanLines)
606
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800607 def _JoinName(self, parent_name, name):
608 return os.path.join(parent_name, name)
609
610 def _UnjoinName(self, parent_name, name):
611 return os.path.relpath(name, parent_name)
612
613 def _ParseProject(self, node, parent = None):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700614 """
615 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700616 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700617 name = self._reqatt(node, 'name')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800618 if parent:
619 name = self._JoinName(parent.name, name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700620
621 remote = self._get_remote(node)
622 if remote is None:
623 remote = self._default.remote
624 if remote is None:
625 raise ManifestParseError, \
626 "no remote for project %s within %s" % \
627 (name, self.manifestFile)
628
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700629 revisionExpr = node.getAttribute('revision')
630 if not revisionExpr:
631 revisionExpr = self._default.revisionExpr
632 if not revisionExpr:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700633 raise ManifestParseError, \
634 "no revision for project %s within %s" % \
635 (name, self.manifestFile)
636
637 path = node.getAttribute('path')
638 if not path:
639 path = name
640 if path.startswith('/'):
641 raise ManifestParseError, \
642 "project %s path cannot be absolute in %s" % \
643 (name, self.manifestFile)
644
Mike Pontillod3153822012-02-28 11:53:24 -0800645 rebase = node.getAttribute('rebase')
646 if not rebase:
647 rebase = True
648 else:
649 rebase = rebase.lower() in ("yes", "true", "1")
650
Anatol Pomazau79770d22012-04-20 14:41:59 -0700651 sync_c = node.getAttribute('sync-c')
652 if not sync_c:
653 sync_c = False
654 else:
655 sync_c = sync_c.lower() in ("yes", "true", "1")
656
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800657 sync_s = node.getAttribute('sync-s')
658 if not sync_s:
659 sync_s = self._default.sync_s
660 else:
661 sync_s = sync_s.lower() in ("yes", "true", "1")
662
Brian Harring14a66742012-09-28 20:21:57 -0700663 upstream = node.getAttribute('upstream')
664
Conley Owens971de8e2012-04-16 10:36:08 -0700665 groups = ''
666 if node.hasAttribute('groups'):
667 groups = node.getAttribute('groups')
David Pursehouse1d947b32012-10-25 12:23:11 +0900668 groups = [x for x in re.split(r'[,\s]+', groups) if x]
Brian Harring7da13142012-06-15 02:24:20 -0700669
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800670 if parent is None:
671 relpath, worktree, gitdir = self.GetProjectPaths(name, path)
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700672 else:
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800673 relpath, worktree, gitdir = self.GetSubprojectPaths(parent, path)
674
675 default_groups = ['all', 'name:%s' % name, 'path:%s' % relpath]
676 groups.extend(set(default_groups).difference(groups))
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700677
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700678 project = Project(manifest = self,
679 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700680 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700681 gitdir = gitdir,
682 worktree = worktree,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800683 relpath = relpath,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700684 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800685 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700686 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700687 groups = groups,
Brian Harring14a66742012-09-28 20:21:57 -0700688 sync_c = sync_c,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800689 sync_s = sync_s,
690 upstream = upstream,
691 parent = parent)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700692
693 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700694 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700695 self._ParseCopyFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500696 if n.nodeName == 'annotation':
697 self._ParseAnnotation(project, n)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800698 if n.nodeName == 'project':
699 project.subprojects.append(self._ParseProject(n, parent = project))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700700
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700701 return project
702
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800703 def GetProjectPaths(self, name, path):
704 relpath = path
705 if self.IsMirror:
706 worktree = None
707 gitdir = os.path.join(self.topdir, '%s.git' % name)
708 else:
709 worktree = os.path.join(self.topdir, path).replace('\\', '/')
710 gitdir = os.path.join(self.repodir, 'projects', '%s.git' % path)
711 return relpath, worktree, gitdir
712
713 def GetSubprojectName(self, parent, submodule_path):
714 return os.path.join(parent.name, submodule_path)
715
716 def _JoinRelpath(self, parent_relpath, relpath):
717 return os.path.join(parent_relpath, relpath)
718
719 def _UnjoinRelpath(self, parent_relpath, relpath):
720 return os.path.relpath(relpath, parent_relpath)
721
722 def GetSubprojectPaths(self, parent, path):
723 relpath = self._JoinRelpath(parent.relpath, path)
724 gitdir = os.path.join(parent.gitdir, 'subprojects', '%s.git' % path)
725 if self.IsMirror:
726 worktree = None
727 else:
728 worktree = os.path.join(parent.worktree, path).replace('\\', '/')
729 return relpath, worktree, gitdir
730
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700731 def _ParseCopyFile(self, project, node):
732 src = self._reqatt(node, 'src')
733 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800734 if not self.IsMirror:
735 # src is project relative;
736 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800737 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700738
James W. Mills24c13082012-04-12 15:04:13 -0500739 def _ParseAnnotation(self, project, node):
740 name = self._reqatt(node, 'name')
741 value = self._reqatt(node, 'value')
742 try:
743 keep = self._reqatt(node, 'keep').lower()
744 except ManifestParseError:
745 keep = "true"
746 if keep != "true" and keep != "false":
747 raise ManifestParseError, "optional \"keep\" attribute must be \"true\" or \"false\""
748 project.AddAnnotation(name, value, keep)
749
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700750 def _get_remote(self, node):
751 name = node.getAttribute('remote')
752 if not name:
753 return None
754
755 v = self._remotes.get(name)
756 if not v:
757 raise ManifestParseError, \
758 "remote %s not defined in %s" % \
759 (name, self.manifestFile)
760 return v
761
762 def _reqatt(self, node, attname):
763 """
764 reads a required attribute from the node.
765 """
766 v = node.getAttribute(attname)
767 if not v:
768 raise ManifestParseError, \
769 "no %s in <%s> within %s" % \
770 (attname, node.nodeName, self.manifestFile)
771 return v