blob: 8bcc61637aa8fbea488de91c2b83bb045a1aafcf [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
David Pursehouse59bbb582013-05-17 10:49:33 +090021import xml.dom.minidom
22
23from pyversion import is_python3
24if is_python3():
Chirayu Desai217ea7d2013-03-01 19:14:38 +053025 import urllib.parse
David Pursehouse59bbb582013-05-17 10:49:33 +090026else:
Chirayu Desai217ea7d2013-03-01 19:14:38 +053027 import imp
28 import urlparse
29 urllib = imp.new_module('urllib')
Chirayu Desaidb2ad9d2013-06-11 13:42:25 +053030 urllib.parse = urlparse
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070031
Simran Basib9a1b732015-08-20 12:19:28 -070032import gitc_utils
David Pursehousee15c65a2012-08-22 10:46:11 +090033from git_config import GitConfig
David Pursehousee00aa6b2012-09-11 14:33:51 +090034from git_refs import R_HEADS, HEAD
35from project import RemoteSpec, Project, MetaProject
Julien Camperguedd654222014-01-09 16:21:37 +010036from error import ManifestParseError, ManifestInvalidRevisionError
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070037
38MANIFEST_FILE_NAME = 'manifest.xml'
Shawn O. Pearce5cc66792008-10-23 16:19:27 -070039LOCAL_MANIFEST_NAME = 'local_manifest.xml'
David Pursehouse2d5a0df2012-11-13 02:50:36 +090040LOCAL_MANIFESTS_DIR_NAME = 'local_manifests'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070041
Anthony Kingcb07ba72015-03-28 23:26:04 +000042# urljoin gets confused if the scheme is not known.
43urllib.parse.uses_relative.extend(['ssh', 'git', 'persistent-https', 'rpc'])
44urllib.parse.uses_netloc.extend(['ssh', 'git', 'persistent-https', 'rpc'])
Conley Owensdb728cd2011-09-26 16:34:01 -070045
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070046class _Default(object):
47 """Project defaults within the manifest."""
48
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -070049 revisionExpr = None
Conley Owensb6a16e62013-09-25 15:06:09 -070050 destBranchExpr = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070051 remote = None
Shawn O. Pearce6392c872011-09-22 17:44:31 -070052 sync_j = 1
Anatol Pomazau79770d22012-04-20 14:41:59 -070053 sync_c = False
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +080054 sync_s = False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070055
Julien Campergue74879922013-10-09 14:38:46 +020056 def __eq__(self, other):
57 return self.__dict__ == other.__dict__
58
59 def __ne__(self, other):
60 return self.__dict__ != other.__dict__
61
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070062class _XmlRemote(object):
63 def __init__(self,
64 name,
Yestin Sunb292b982012-07-02 07:32:50 -070065 alias=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070066 fetch=None,
Steve Raed6480452016-08-10 15:00:00 -070067 pushUrl=None,
Conley Owensdb728cd2011-09-26 16:34:01 -070068 manifestUrl=None,
Anthony King36ea2fb2014-05-06 11:54:01 +010069 review=None,
Jonathan Nieder93719792015-03-17 11:29:58 -070070 revision=None):
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070071 self.name = name
72 self.fetchUrl = fetch
Steve Raed6480452016-08-10 15:00:00 -070073 self.pushUrl = pushUrl
Conley Owensdb728cd2011-09-26 16:34:01 -070074 self.manifestUrl = manifestUrl
Yestin Sunb292b982012-07-02 07:32:50 -070075 self.remoteAlias = alias
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070076 self.reviewUrl = review
Anthony King36ea2fb2014-05-06 11:54:01 +010077 self.revision = revision
Conley Owensceea3682011-10-20 10:45:47 -070078 self.resolvedFetchUrl = self._resolveFetchUrl()
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070079
David Pursehouse717ece92012-11-13 08:49:16 +090080 def __eq__(self, other):
81 return self.__dict__ == other.__dict__
82
83 def __ne__(self, other):
84 return self.__dict__ != other.__dict__
85
Conley Owensceea3682011-10-20 10:45:47 -070086 def _resolveFetchUrl(self):
87 url = self.fetchUrl.rstrip('/')
Conley Owensdb728cd2011-09-26 16:34:01 -070088 manifestUrl = self.manifestUrl.rstrip('/')
Conley Owens2d0f5082014-01-31 15:03:51 -080089 # urljoin will gets confused over quite a few things. The ones we care
90 # about here are:
91 # * no scheme in the base url, like <hostname:port>
Anthony Kingcb07ba72015-03-28 23:26:04 +000092 # We handle no scheme by replacing it with an obscure protocol, gopher
93 # and then replacing it with the original when we are done.
94
Conley Owensdb728cd2011-09-26 16:34:01 -070095 if manifestUrl.find(':') != manifestUrl.find('/') - 1:
Conley Owens4ccad752015-04-29 10:45:37 -070096 url = urllib.parse.urljoin('gopher://' + manifestUrl, url)
97 url = re.sub(r'^gopher://', '', url)
Anthony Kingcb07ba72015-03-28 23:26:04 +000098 else:
99 url = urllib.parse.urljoin(manifestUrl, url)
Shawn Pearcea9f11b32013-01-02 15:40:48 -0800100 return url
Conley Owensceea3682011-10-20 10:45:47 -0700101
102 def ToRemoteSpec(self, projectName):
Conley Owens9d8f9142011-10-20 14:36:35 -0700103 url = self.resolvedFetchUrl.rstrip('/') + '/' + projectName
Yestin Sunb292b982012-07-02 07:32:50 -0700104 remoteName = self.name
Conley Owens1e7ab2a2013-10-08 17:26:57 -0700105 if self.remoteAlias:
David Pursehouse37128b62013-10-15 10:48:40 +0900106 remoteName = self.remoteAlias
Dan Willemsen96c2d652016-04-06 16:03:54 -0700107 return RemoteSpec(remoteName,
108 url=url,
Steve Raed6480452016-08-10 15:00:00 -0700109 pushUrl=self.pushUrl,
Dan Willemsen96c2d652016-04-06 16:03:54 -0700110 review=self.reviewUrl,
111 orig_name=self.name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700112
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -0700113class XmlManifest(object):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700114 """manages the repo configuration file"""
115
116 def __init__(self, repodir):
117 self.repodir = os.path.abspath(repodir)
118 self.topdir = os.path.dirname(self.repodir)
119 self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700120 self.globalConfig = GitConfig.ForUser()
David Pursehouse4eb285c2013-02-14 16:28:44 +0900121 self.localManifestWarning = False
Simran Basib9a1b732015-08-20 12:19:28 -0700122 self.isGitcClient = False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700123
124 self.repoProject = MetaProject(self, 'repo',
125 gitdir = os.path.join(repodir, 'repo/.git'),
126 worktree = os.path.join(repodir, 'repo'))
127
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700128 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -0800129 gitdir = os.path.join(repodir, 'manifests.git'),
130 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700131
132 self._Unload()
133
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700134 def Override(self, name):
135 """Use a different manifest, just for the current instantiation.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700136 """
137 path = os.path.join(self.manifestProject.worktree, name)
138 if not os.path.isfile(path):
139 raise ManifestParseError('manifest %s not found' % name)
140
141 old = self.manifestFile
142 try:
143 self.manifestFile = path
144 self._Unload()
145 self._Load()
146 finally:
147 self.manifestFile = old
148
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700149 def Link(self, name):
150 """Update the repo metadata to use a different manifest.
151 """
152 self.Override(name)
153
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700154 try:
Sebastian Frias223bf962012-11-21 19:09:25 +0100155 if os.path.lexists(self.manifestFile):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700156 os.remove(self.manifestFile)
157 os.symlink('manifests/%s' % name, self.manifestFile)
Sebastian Frias223bf962012-11-21 19:09:25 +0100158 except OSError as e:
159 raise ManifestParseError('cannot link manifest %s: %s' % (name, str(e)))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700160
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800161 def _RemoteToXml(self, r, doc, root):
162 e = doc.createElement('remote')
163 root.appendChild(e)
164 e.setAttribute('name', r.name)
165 e.setAttribute('fetch', r.fetchUrl)
Steve Raed6480452016-08-10 15:00:00 -0700166 if r.pushUrl is not None:
167 e.setAttribute('pushurl', r.pushUrl)
Conley Owens1e7ab2a2013-10-08 17:26:57 -0700168 if r.remoteAlias is not None:
169 e.setAttribute('alias', r.remoteAlias)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800170 if r.reviewUrl is not None:
171 e.setAttribute('review', r.reviewUrl)
Anthony King36ea2fb2014-05-06 11:54:01 +0100172 if r.revision is not None:
173 e.setAttribute('revision', r.revision)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800174
Josh Triplett884a3872014-06-12 14:57:29 -0700175 def _ParseGroups(self, groups):
176 return [x for x in re.split(r'[,\s]+', groups) if x]
177
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700178 def Save(self, fd, peg_rev=False, peg_rev_upstream=True, groups=None):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800179 """Write the current manifest out to the given file descriptor.
180 """
Colin Cross5acde752012-03-28 20:15:45 -0700181 mp = self.manifestProject
182
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700183 if groups is None:
184 groups = mp.config.GetString('manifest.groups')
Matt Gumbel0c635bb2012-12-21 10:14:53 -0800185 if groups:
Josh Triplett884a3872014-06-12 14:57:29 -0700186 groups = self._ParseGroups(groups)
Colin Cross5acde752012-03-28 20:15:45 -0700187
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800188 doc = xml.dom.minidom.Document()
189 root = doc.createElement('manifest')
190 doc.appendChild(root)
191
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700192 # Save out the notice. There's a little bit of work here to give it the
193 # right whitespace, which assumes that the notice is automatically indented
194 # by 4 by minidom.
195 if self.notice:
196 notice_element = root.appendChild(doc.createElement('notice'))
197 notice_lines = self.notice.splitlines()
198 indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:]
199 notice_element.appendChild(doc.createTextNode(indented_notice))
200
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800201 d = self.default
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800202
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530203 for r in sorted(self.remotes):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800204 self._RemoteToXml(self.remotes[r], doc, root)
205 if self.remotes:
206 root.appendChild(doc.createTextNode(''))
207
208 have_default = False
209 e = doc.createElement('default')
210 if d.remote:
211 have_default = True
212 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700213 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800214 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700215 e.setAttribute('revision', d.revisionExpr)
Simon Ruggier7e59de22015-07-24 12:50:06 +0200216 if d.destBranchExpr:
217 have_default = True
218 e.setAttribute('dest-branch', d.destBranchExpr)
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700219 if d.sync_j > 1:
220 have_default = True
221 e.setAttribute('sync-j', '%d' % d.sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700222 if d.sync_c:
223 have_default = True
224 e.setAttribute('sync-c', 'true')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800225 if d.sync_s:
226 have_default = True
227 e.setAttribute('sync-s', 'true')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800228 if have_default:
229 root.appendChild(e)
230 root.appendChild(doc.createTextNode(''))
231
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700232 if self._manifest_server:
233 e = doc.createElement('manifest-server')
234 e.setAttribute('url', self._manifest_server)
235 root.appendChild(e)
236 root.appendChild(doc.createTextNode(''))
237
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800238 def output_projects(parent, parent_node, projects):
David James8d201162013-10-11 17:03:19 -0700239 for project_name in projects:
240 for project in self._projects[project_name]:
241 output_project(parent, parent_node, project)
Che-Liang Chiou69998b02012-01-11 11:28:42 +0800242
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800243 def output_project(parent, parent_node, p):
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700244 if not p.MatchesGroups(groups):
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800245 return
246
247 name = p.name
248 relpath = p.relpath
249 if parent:
250 name = self._UnjoinName(parent.name, name)
251 relpath = self._UnjoinRelpath(parent.relpath, relpath)
Colin Cross5acde752012-03-28 20:15:45 -0700252
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800253 e = doc.createElement('project')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800254 parent_node.appendChild(e)
255 e.setAttribute('name', name)
256 if relpath != name:
257 e.setAttribute('path', relpath)
Conley Owensa17d7af2013-10-16 14:38:09 -0700258 remoteName = None
259 if d.remote:
Dan Willemsen96c2d652016-04-06 16:03:54 -0700260 remoteName = d.remote.name
261 if not d.remote or p.remote.orig_name != remoteName:
262 remoteName = p.remote.orig_name
Anthony King36ea2fb2014-05-06 11:54:01 +0100263 e.setAttribute('remote', remoteName)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800264 if peg_rev:
265 if self.IsMirror:
Brian Harring14a66742012-09-28 20:21:57 -0700266 value = p.bare_git.rev_parse(p.revisionExpr + '^0')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800267 else:
Brian Harring14a66742012-09-28 20:21:57 -0700268 value = p.work_git.rev_parse(HEAD + '^0')
269 e.setAttribute('revision', value)
Conley Owens551dfec2015-07-10 14:54:54 -0700270 if peg_rev_upstream:
271 if p.upstream:
272 e.setAttribute('upstream', p.upstream)
273 elif value != p.revisionExpr:
274 # Only save the origin if the origin is not a sha1, and the default
275 # isn't our value
276 e.setAttribute('upstream', p.revisionExpr)
Anthony King36ea2fb2014-05-06 11:54:01 +0100277 else:
Dan Willemsen96c2d652016-04-06 16:03:54 -0700278 revision = self.remotes[p.remote.orig_name].revision or d.revisionExpr
Anthony King36ea2fb2014-05-06 11:54:01 +0100279 if not revision or revision != p.revisionExpr:
280 e.setAttribute('revision', p.revisionExpr)
Mani Chandel7a91d512014-07-24 16:27:08 +0530281 if p.upstream and p.upstream != p.revisionExpr:
282 e.setAttribute('upstream', p.upstream)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800283
Simon Ruggier7e59de22015-07-24 12:50:06 +0200284 if p.dest_branch and p.dest_branch != d.destBranchExpr:
285 e.setAttribute('dest-branch', p.dest_branch)
286
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800287 for c in p.copyfiles:
288 ce = doc.createElement('copyfile')
289 ce.setAttribute('src', c.src)
290 ce.setAttribute('dest', c.dest)
291 e.appendChild(ce)
292
Jeff Hamiltone0df2322014-04-21 17:10:59 -0500293 for l in p.linkfiles:
294 le = doc.createElement('linkfile')
295 le.setAttribute('src', l.src)
296 le.setAttribute('dest', l.dest)
297 e.appendChild(le)
298
Conley Owensbb1b5f52012-08-13 13:11:18 -0700299 default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath]
Dmitry Fink17f85ea2012-08-06 14:52:29 -0700300 egroups = [g for g in p.groups if g not in default_groups]
Conley Owens971de8e2012-04-16 10:36:08 -0700301 if egroups:
302 e.setAttribute('groups', ','.join(egroups))
Colin Cross5acde752012-03-28 20:15:45 -0700303
James W. Mills24c13082012-04-12 15:04:13 -0500304 for a in p.annotations:
305 if a.keep == "true":
306 ae = doc.createElement('annotation')
307 ae.setAttribute('name', a.name)
308 ae.setAttribute('value', a.value)
309 e.appendChild(ae)
310
Anatol Pomazau79770d22012-04-20 14:41:59 -0700311 if p.sync_c:
312 e.setAttribute('sync-c', 'true')
313
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800314 if p.sync_s:
315 e.setAttribute('sync-s', 'true')
316
andy.chengd3db3ba2017-12-07 15:04:55 +0800317 if p.lfs_fetch:
318 e.setAttribute('lfs-fetch', 'true')
319
Dan Willemsen88409222015-08-17 15:29:10 -0700320 if p.clone_depth:
321 e.setAttribute('clone-depth', str(p.clone_depth))
322
Simran Basib9a1b732015-08-20 12:19:28 -0700323 self._output_manifest_project_extras(p, e)
324
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800325 if p.subprojects:
David James8d201162013-10-11 17:03:19 -0700326 subprojects = set(subp.name for subp in p.subprojects)
327 output_projects(p, e, list(sorted(subprojects)))
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800328
David James8d201162013-10-11 17:03:19 -0700329 projects = set(p.name for p in self._paths.values() if not p.parent)
330 output_projects(None, root, list(sorted(projects)))
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800331
Doug Anderson37282b42011-03-04 11:54:18 -0800332 if self._repo_hooks_project:
333 root.appendChild(doc.createTextNode(''))
334 e = doc.createElement('repo-hooks')
335 e.setAttribute('in-project', self._repo_hooks_project.name)
336 e.setAttribute('enabled-list',
337 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
338 root.appendChild(e)
339
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800340 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
341
Simran Basib9a1b732015-08-20 12:19:28 -0700342 def _output_manifest_project_extras(self, p, e):
343 """Manifests can modify e if they support extra project attributes."""
344 pass
345
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700346 @property
David James8d201162013-10-11 17:03:19 -0700347 def paths(self):
348 self._Load()
349 return self._paths
350
351 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700352 def projects(self):
353 self._Load()
Anthony Kingd58bfe52014-05-05 23:30:49 +0100354 return list(self._paths.values())
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700355
356 @property
357 def remotes(self):
358 self._Load()
359 return self._remotes
360
361 @property
362 def default(self):
363 self._Load()
364 return self._default
365
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800366 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800367 def repo_hooks_project(self):
368 self._Load()
369 return self._repo_hooks_project
370
371 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700372 def notice(self):
373 self._Load()
374 return self._notice
375
376 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700377 def manifest_server(self):
378 self._Load()
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800379 return self._manifest_server
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700380
381 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800382 def IsMirror(self):
383 return self.manifestProject.config.GetBoolean('repo.mirror')
384
Julien Campergue335f5ef2013-10-16 11:02:35 +0200385 @property
386 def IsArchive(self):
387 return self.manifestProject.config.GetBoolean('repo.archive')
388
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700389 def _Unload(self):
390 self._loaded = False
391 self._projects = {}
David James8d201162013-10-11 17:03:19 -0700392 self._paths = {}
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700393 self._remotes = {}
394 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800395 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700396 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700397 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700398 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700399
400 def _Load(self):
401 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800402 m = self.manifestProject
403 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700404 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800405 b = b[len(R_HEADS):]
406 self.branch = b
407
Colin Cross23acdd32012-04-21 00:33:54 -0700408 nodes = []
Brian Harring475a47d2012-06-07 20:05:35 -0700409 nodes.append(self._ParseManifestXml(self.manifestFile,
410 self.manifestProject.worktree))
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700411
412 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
413 if os.path.exists(local):
David Pursehouse4eb285c2013-02-14 16:28:44 +0900414 if not self.localManifestWarning:
415 self.localManifestWarning = True
416 print('warning: %s is deprecated; put local manifests in `%s` instead'
417 % (LOCAL_MANIFEST_NAME, os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME)),
418 file=sys.stderr)
Brian Harring475a47d2012-06-07 20:05:35 -0700419 nodes.append(self._ParseManifestXml(local, self.repodir))
Colin Cross23acdd32012-04-21 00:33:54 -0700420
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900421 local_dir = os.path.abspath(os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME))
422 try:
David Pursehouse52f1e5d2012-11-14 04:53:24 +0900423 for local_file in sorted(os.listdir(local_dir)):
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900424 if local_file.endswith('.xml'):
David Pursehouse5f434ed2012-11-22 13:48:10 +0900425 local = os.path.join(local_dir, local_file)
426 nodes.append(self._ParseManifestXml(local, self.repodir))
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900427 except OSError:
428 pass
429
Joe Onorato26e24752013-01-11 12:35:53 -0800430 try:
431 self._ParseManifest(nodes)
432 except ManifestParseError as e:
433 # There was a problem parsing, unload ourselves in case they catch
434 # this error and try again later, we will show the correct error
435 self._Unload()
436 raise e
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700437
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800438 if self.IsMirror:
439 self._AddMetaProjectMirror(self.repoProject)
440 self._AddMetaProjectMirror(self.manifestProject)
441
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700442 self._loaded = True
443
Brian Harring475a47d2012-06-07 20:05:35 -0700444 def _ParseManifestXml(self, path, include_root):
David Pursehousef7fc8a92012-11-13 04:00:28 +0900445 try:
446 root = xml.dom.minidom.parse(path)
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900447 except (OSError, xml.parsers.expat.ExpatError) as e:
David Pursehousef7fc8a92012-11-13 04:00:28 +0900448 raise ManifestParseError("error parsing manifest %s: %s" % (path, e))
449
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700450 if not root or not root.childNodes:
Brian Harring26448742011-04-28 05:04:41 -0700451 raise ManifestParseError("no root node in %s" % (path,))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700452
Jooncheol Park34acdd22012-08-27 02:25:59 +0900453 for manifest in root.childNodes:
454 if manifest.nodeName == 'manifest':
455 break
456 else:
Brian Harring26448742011-04-28 05:04:41 -0700457 raise ManifestParseError("no <manifest> in %s" % (path,))
458
Colin Cross23acdd32012-04-21 00:33:54 -0700459 nodes = []
David Pursehouse4f7bdea2012-10-22 12:50:15 +0900460 for node in manifest.childNodes: # pylint:disable=W0631
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900461 # We only get here if manifest is initialised
David Pursehousec1b86a22012-11-14 11:36:51 +0900462 if node.nodeName == 'include':
463 name = self._reqatt(node, 'name')
464 fp = os.path.join(include_root, name)
465 if not os.path.isfile(fp):
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530466 raise ManifestParseError("include %s doesn't exist or isn't a file"
467 % (name,))
David Pursehousec1b86a22012-11-14 11:36:51 +0900468 try:
469 nodes.extend(self._ParseManifestXml(fp, include_root))
470 # should isolate this to the exact exception, but that's
471 # tricky. actual parsing implementation may vary.
472 except (KeyboardInterrupt, RuntimeError, SystemExit):
473 raise
474 except Exception as e:
475 raise ManifestParseError(
476 "failed parsing included manifest %s: %s", (name, e))
477 else:
478 nodes.append(node)
Colin Cross23acdd32012-04-21 00:33:54 -0700479 return nodes
Shawn O. Pearce03eaf072008-11-20 11:42:22 -0800480
Colin Cross23acdd32012-04-21 00:33:54 -0700481 def _ParseManifest(self, node_list):
482 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700483 if node.nodeName == 'remote':
484 remote = self._ParseRemote(node)
David Pursehouse717ece92012-11-13 08:49:16 +0900485 if remote:
486 if remote.name in self._remotes:
487 if remote != self._remotes[remote.name]:
488 raise ManifestParseError(
489 'remote %s already exists with different attributes' %
490 (remote.name))
491 else:
492 self._remotes[remote.name] = remote
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700493
Colin Cross23acdd32012-04-21 00:33:54 -0700494 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700495 if node.nodeName == 'default':
Julien Campergue74879922013-10-09 14:38:46 +0200496 new_default = self._ParseDefault(node)
497 if self._default is None:
498 self._default = new_default
499 elif new_default != self._default:
David Pursehouse37128b62013-10-15 10:48:40 +0900500 raise ManifestParseError('duplicate default in %s' %
501 (self.manifestFile))
Julien Campergue74879922013-10-09 14:38:46 +0200502
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700503 if self._default is None:
504 self._default = _Default()
505
Colin Cross23acdd32012-04-21 00:33:54 -0700506 for node in itertools.chain(*node_list):
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700507 if node.nodeName == 'notice':
508 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800509 raise ManifestParseError(
510 'duplicate notice in %s' %
511 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700512 self._notice = self._ParseNotice(node)
513
Colin Cross23acdd32012-04-21 00:33:54 -0700514 for node in itertools.chain(*node_list):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700515 if node.nodeName == 'manifest-server':
516 url = self._reqatt(node, 'url')
517 if self._manifest_server is not None:
David Pursehousec1b86a22012-11-14 11:36:51 +0900518 raise ManifestParseError(
519 'duplicate manifest-server in %s' %
520 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700521 self._manifest_server = url
522
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800523 def recursively_add_projects(project):
David James8d201162013-10-11 17:03:19 -0700524 projects = self._projects.setdefault(project.name, [])
525 if project.relpath is None:
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800526 raise ManifestParseError(
David James8d201162013-10-11 17:03:19 -0700527 'missing path for %s in %s' %
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800528 (project.name, self.manifestFile))
David James8d201162013-10-11 17:03:19 -0700529 if project.relpath in self._paths:
530 raise ManifestParseError(
531 'duplicate path %s in %s' %
532 (project.relpath, self.manifestFile))
533 self._paths[project.relpath] = project
534 projects.append(project)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800535 for subproject in project.subprojects:
536 recursively_add_projects(subproject)
537
Colin Cross23acdd32012-04-21 00:33:54 -0700538 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700539 if node.nodeName == 'project':
540 project = self._ParseProject(node)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800541 recursively_add_projects(project)
Josh Triplett884a3872014-06-12 14:57:29 -0700542 if node.nodeName == 'extend-project':
543 name = self._reqatt(node, 'name')
544
545 if name not in self._projects:
546 raise ManifestParseError('extend-project element specifies non-existent '
547 'project: %s' % name)
548
549 path = node.getAttribute('path')
550 groups = node.getAttribute('groups')
551 if groups:
552 groups = self._ParseGroups(groups)
553
554 for p in self._projects[name]:
555 if path and p.relpath != path:
556 continue
557 if groups:
558 p.groups.extend(groups)
Doug Anderson37282b42011-03-04 11:54:18 -0800559 if node.nodeName == 'repo-hooks':
560 # Get the name of the project and the (space-separated) list of enabled.
561 repo_hooks_project = self._reqatt(node, 'in-project')
562 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
563
564 # Only one project can be the hooks project
565 if self._repo_hooks_project is not None:
566 raise ManifestParseError(
567 'duplicate repo-hooks in %s' %
568 (self.manifestFile))
569
570 # Store a reference to the Project.
571 try:
David James8d201162013-10-11 17:03:19 -0700572 repo_hooks_projects = self._projects[repo_hooks_project]
Doug Anderson37282b42011-03-04 11:54:18 -0800573 except KeyError:
574 raise ManifestParseError(
575 'project %s not found for repo-hooks' %
576 (repo_hooks_project))
577
David James8d201162013-10-11 17:03:19 -0700578 if len(repo_hooks_projects) != 1:
579 raise ManifestParseError(
580 'internal error parsing repo-hooks in %s' %
581 (self.manifestFile))
582 self._repo_hooks_project = repo_hooks_projects[0]
583
Doug Anderson37282b42011-03-04 11:54:18 -0800584 # Store the enabled hooks in the Project object.
585 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
Colin Cross23acdd32012-04-21 00:33:54 -0700586 if node.nodeName == 'remove-project':
587 name = self._reqatt(node, 'name')
David Jamesb8433df2014-01-30 10:11:17 -0800588
589 if name not in self._projects:
David Pursehousef9107482012-11-16 19:12:32 +0900590 raise ManifestParseError('remove-project element specifies non-existent '
591 'project: %s' % name)
Colin Cross23acdd32012-04-21 00:33:54 -0700592
David Jamesb8433df2014-01-30 10:11:17 -0800593 for p in self._projects[name]:
594 del self._paths[p.relpath]
595 del self._projects[name]
596
Colin Cross23acdd32012-04-21 00:33:54 -0700597 # If the manifest removes the hooks project, treat it as if it deleted
598 # the repo-hooks element too.
599 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
600 self._repo_hooks_project = None
601
Doug Anderson37282b42011-03-04 11:54:18 -0800602
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800603 def _AddMetaProjectMirror(self, m):
604 name = None
605 m_url = m.GetRemote(m.remote.name).url
606 if m_url.endswith('/.git'):
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530607 raise ManifestParseError('refusing to mirror %s' % m_url)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800608
609 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700610 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800611 if not url.endswith('/'):
612 url += '/'
613 if m_url.startswith(url):
614 remote = self._default.remote
615 name = m_url[len(url):]
616
617 if name is None:
618 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700619 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Shawn O. Pearcef35b2d92012-08-02 11:46:22 -0700620 remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800621 name = m_url[s:]
622
623 if name.endswith('.git'):
624 name = name[:-4]
625
626 if name not in self._projects:
627 m.PreSync()
628 gitdir = os.path.join(self.topdir, '%s.git' % name)
629 project = Project(manifest = self,
630 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700631 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800632 gitdir = gitdir,
David James8d201162013-10-11 17:03:19 -0700633 objdir = gitdir,
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800634 worktree = None,
Kwanhong Leeccd218c2014-02-17 13:07:32 +0900635 relpath = name or None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700636 revisionExpr = m.revisionExpr,
637 revisionId = None)
David James8d201162013-10-11 17:03:19 -0700638 self._projects[project.name] = [project]
Kwanhong Leeccd218c2014-02-17 13:07:32 +0900639 self._paths[project.relpath] = project
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800640
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700641 def _ParseRemote(self, node):
642 """
643 reads a <remote> element from the manifest file
644 """
645 name = self._reqatt(node, 'name')
Yestin Sunb292b982012-07-02 07:32:50 -0700646 alias = node.getAttribute('alias')
647 if alias == '':
648 alias = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700649 fetch = self._reqatt(node, 'fetch')
Steve Raed6480452016-08-10 15:00:00 -0700650 pushUrl = node.getAttribute('pushurl')
651 if pushUrl == '':
652 pushUrl = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700653 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800654 if review == '':
655 review = None
Anthony King36ea2fb2014-05-06 11:54:01 +0100656 revision = node.getAttribute('revision')
657 if revision == '':
658 revision = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700659 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Steve Raed6480452016-08-10 15:00:00 -0700660 return _XmlRemote(name, alias, fetch, pushUrl, manifestUrl, review, revision)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700661
662 def _ParseDefault(self, node):
663 """
664 reads a <default> element from the manifest file
665 """
666 d = _Default()
667 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700668 d.revisionExpr = node.getAttribute('revision')
669 if d.revisionExpr == '':
670 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700671
Bryan Jacobsf609f912013-05-06 13:36:24 -0400672 d.destBranchExpr = node.getAttribute('dest-branch') or None
673
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700674 sync_j = node.getAttribute('sync-j')
675 if sync_j == '' or sync_j is None:
676 d.sync_j = 1
677 else:
678 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700679
680 sync_c = node.getAttribute('sync-c')
681 if not sync_c:
682 d.sync_c = False
683 else:
684 d.sync_c = sync_c.lower() in ("yes", "true", "1")
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800685
686 sync_s = node.getAttribute('sync-s')
687 if not sync_s:
688 d.sync_s = False
689 else:
690 d.sync_s = sync_s.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700691 return d
692
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700693 def _ParseNotice(self, node):
694 """
695 reads a <notice> element from the manifest file
696
697 The <notice> element is distinct from other tags in the XML in that the
698 data is conveyed between the start and end tag (it's not an empty-element
699 tag).
700
701 The white space (carriage returns, indentation) for the notice element is
702 relevant and is parsed in a way that is based on how python docstrings work.
703 In fact, the code is remarkably similar to here:
704 http://www.python.org/dev/peps/pep-0257/
705 """
706 # Get the data out of the node...
707 notice = node.childNodes[0].data
708
709 # Figure out minimum indentation, skipping the first line (the same line
710 # as the <notice> tag)...
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530711 minIndent = sys.maxsize
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700712 lines = notice.splitlines()
713 for line in lines[1:]:
714 lstrippedLine = line.lstrip()
715 if lstrippedLine:
716 indent = len(line) - len(lstrippedLine)
717 minIndent = min(indent, minIndent)
718
719 # Strip leading / trailing blank lines and also indentation.
720 cleanLines = [lines[0].strip()]
721 for line in lines[1:]:
722 cleanLines.append(line[minIndent:].rstrip())
723
724 # Clear completely blank lines from front and back...
725 while cleanLines and not cleanLines[0]:
726 del cleanLines[0]
727 while cleanLines and not cleanLines[-1]:
728 del cleanLines[-1]
729
730 return '\n'.join(cleanLines)
731
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800732 def _JoinName(self, parent_name, name):
733 return os.path.join(parent_name, name)
734
735 def _UnjoinName(self, parent_name, name):
736 return os.path.relpath(name, parent_name)
737
Simran Basib9a1b732015-08-20 12:19:28 -0700738 def _ParseProject(self, node, parent = None, **extra_proj_attrs):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700739 """
740 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700741 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700742 name = self._reqatt(node, 'name')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800743 if parent:
744 name = self._JoinName(parent.name, name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700745
746 remote = self._get_remote(node)
747 if remote is None:
748 remote = self._default.remote
749 if remote is None:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530750 raise ManifestParseError("no remote for project %s within %s" %
751 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700752
Anthony King36ea2fb2014-05-06 11:54:01 +0100753 revisionExpr = node.getAttribute('revision') or remote.revision
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700754 if not revisionExpr:
755 revisionExpr = self._default.revisionExpr
756 if not revisionExpr:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530757 raise ManifestParseError("no revision for project %s within %s" %
758 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700759
760 path = node.getAttribute('path')
761 if not path:
762 path = name
763 if path.startswith('/'):
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530764 raise ManifestParseError("project %s path cannot be absolute in %s" %
765 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700766
Mike Pontillod3153822012-02-28 11:53:24 -0800767 rebase = node.getAttribute('rebase')
768 if not rebase:
769 rebase = True
770 else:
771 rebase = rebase.lower() in ("yes", "true", "1")
772
Anatol Pomazau79770d22012-04-20 14:41:59 -0700773 sync_c = node.getAttribute('sync-c')
774 if not sync_c:
775 sync_c = False
776 else:
777 sync_c = sync_c.lower() in ("yes", "true", "1")
778
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800779 sync_s = node.getAttribute('sync-s')
780 if not sync_s:
781 sync_s = self._default.sync_s
782 else:
783 sync_s = sync_s.lower() in ("yes", "true", "1")
784
David Pursehouseede7f122012-11-27 22:25:30 +0900785 clone_depth = node.getAttribute('clone-depth')
786 if clone_depth:
787 try:
788 clone_depth = int(clone_depth)
789 if clone_depth <= 0:
790 raise ValueError()
791 except ValueError:
792 raise ManifestParseError('invalid clone-depth %s in %s' %
793 (clone_depth, self.manifestFile))
794
Bryan Jacobsf609f912013-05-06 13:36:24 -0400795 dest_branch = node.getAttribute('dest-branch') or self._default.destBranchExpr
796
Brian Harring14a66742012-09-28 20:21:57 -0700797 upstream = node.getAttribute('upstream')
798
Conley Owens971de8e2012-04-16 10:36:08 -0700799 groups = ''
800 if node.hasAttribute('groups'):
801 groups = node.getAttribute('groups')
Josh Triplett884a3872014-06-12 14:57:29 -0700802 groups = self._ParseGroups(groups)
Brian Harring7da13142012-06-15 02:24:20 -0700803
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800804 if parent is None:
David James8d201162013-10-11 17:03:19 -0700805 relpath, worktree, gitdir, objdir = self.GetProjectPaths(name, path)
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700806 else:
David James8d201162013-10-11 17:03:19 -0700807 relpath, worktree, gitdir, objdir = \
808 self.GetSubprojectPaths(parent, name, path)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800809
810 default_groups = ['all', 'name:%s' % name, 'path:%s' % relpath]
811 groups.extend(set(default_groups).difference(groups))
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700812
Scott Fandb83b1b2013-02-28 09:34:14 +0800813 if self.IsMirror and node.hasAttribute('force-path'):
814 if node.getAttribute('force-path').lower() in ("yes", "true", "1"):
815 gitdir = os.path.join(self.topdir, '%s.git' % path)
816
natalie.chene8996f92015-12-29 10:53:30 +0800817 lfs_fetch = node.getAttribute('lfs-fetch')
818 if not lfs_fetch:
819 lfe_fetch = False
820 else:
821 lfs_fetch = lfs_fetch.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700822 project = Project(manifest = self,
823 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700824 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700825 gitdir = gitdir,
David James8d201162013-10-11 17:03:19 -0700826 objdir = objdir,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700827 worktree = worktree,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800828 relpath = relpath,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700829 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800830 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700831 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700832 groups = groups,
Brian Harring14a66742012-09-28 20:21:57 -0700833 sync_c = sync_c,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800834 sync_s = sync_s,
David Pursehouseede7f122012-11-27 22:25:30 +0900835 clone_depth = clone_depth,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800836 upstream = upstream,
Bryan Jacobsf609f912013-05-06 13:36:24 -0400837 parent = parent,
Simran Basib9a1b732015-08-20 12:19:28 -0700838 dest_branch = dest_branch,
natalie.chene8996f92015-12-29 10:53:30 +0800839 lfs_fetch = lfs_fetch,
Simran Basib9a1b732015-08-20 12:19:28 -0700840 **extra_proj_attrs)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700841
842 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700843 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700844 self._ParseCopyFile(project, n)
Jeff Hamiltone0df2322014-04-21 17:10:59 -0500845 if n.nodeName == 'linkfile':
846 self._ParseLinkFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500847 if n.nodeName == 'annotation':
848 self._ParseAnnotation(project, n)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800849 if n.nodeName == 'project':
850 project.subprojects.append(self._ParseProject(n, parent = project))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700851
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700852 return project
853
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800854 def GetProjectPaths(self, name, path):
855 relpath = path
856 if self.IsMirror:
857 worktree = None
858 gitdir = os.path.join(self.topdir, '%s.git' % name)
David James8d201162013-10-11 17:03:19 -0700859 objdir = gitdir
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800860 else:
861 worktree = os.path.join(self.topdir, path).replace('\\', '/')
862 gitdir = os.path.join(self.repodir, 'projects', '%s.git' % path)
David James8d201162013-10-11 17:03:19 -0700863 objdir = os.path.join(self.repodir, 'project-objects', '%s.git' % name)
864 return relpath, worktree, gitdir, objdir
865
866 def GetProjectsWithName(self, name):
867 return self._projects.get(name, [])
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800868
869 def GetSubprojectName(self, parent, submodule_path):
870 return os.path.join(parent.name, submodule_path)
871
872 def _JoinRelpath(self, parent_relpath, relpath):
873 return os.path.join(parent_relpath, relpath)
874
875 def _UnjoinRelpath(self, parent_relpath, relpath):
876 return os.path.relpath(relpath, parent_relpath)
877
David James8d201162013-10-11 17:03:19 -0700878 def GetSubprojectPaths(self, parent, name, path):
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800879 relpath = self._JoinRelpath(parent.relpath, path)
880 gitdir = os.path.join(parent.gitdir, 'subprojects', '%s.git' % path)
David James8d201162013-10-11 17:03:19 -0700881 objdir = os.path.join(parent.gitdir, 'subproject-objects', '%s.git' % name)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800882 if self.IsMirror:
883 worktree = None
884 else:
885 worktree = os.path.join(parent.worktree, path).replace('\\', '/')
David James8d201162013-10-11 17:03:19 -0700886 return relpath, worktree, gitdir, objdir
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800887
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700888 def _ParseCopyFile(self, project, node):
889 src = self._reqatt(node, 'src')
890 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800891 if not self.IsMirror:
892 # src is project relative;
893 # dest is relative to the top of the tree
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800894 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700895
Jeff Hamiltone0df2322014-04-21 17:10:59 -0500896 def _ParseLinkFile(self, project, node):
897 src = self._reqatt(node, 'src')
898 dest = self._reqatt(node, 'dest')
899 if not self.IsMirror:
900 # src is project relative;
901 # dest is relative to the top of the tree
902 project.AddLinkFile(src, dest, os.path.join(self.topdir, dest))
903
James W. Mills24c13082012-04-12 15:04:13 -0500904 def _ParseAnnotation(self, project, node):
905 name = self._reqatt(node, 'name')
906 value = self._reqatt(node, 'value')
907 try:
908 keep = self._reqatt(node, 'keep').lower()
909 except ManifestParseError:
910 keep = "true"
911 if keep != "true" and keep != "false":
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530912 raise ManifestParseError('optional "keep" attribute must be '
913 '"true" or "false"')
James W. Mills24c13082012-04-12 15:04:13 -0500914 project.AddAnnotation(name, value, keep)
915
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700916 def _get_remote(self, node):
917 name = node.getAttribute('remote')
918 if not name:
919 return None
920
921 v = self._remotes.get(name)
922 if not v:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530923 raise ManifestParseError("remote %s not defined in %s" %
924 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700925 return v
926
927 def _reqatt(self, node, attname):
928 """
929 reads a required attribute from the node.
930 """
931 v = node.getAttribute(attname)
932 if not v:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530933 raise ManifestParseError("no %s in <%s> within %s" %
934 (attname, node.nodeName, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700935 return v
Julien Camperguedd654222014-01-09 16:21:37 +0100936
937 def projectsDiff(self, manifest):
938 """return the projects differences between two manifests.
939
940 The diff will be from self to given manifest.
941
942 """
943 fromProjects = self.paths
944 toProjects = manifest.paths
945
Anthony King7446c592014-05-06 09:19:39 +0100946 fromKeys = sorted(fromProjects.keys())
947 toKeys = sorted(toProjects.keys())
Julien Camperguedd654222014-01-09 16:21:37 +0100948
949 diff = {'added': [], 'removed': [], 'changed': [], 'unreachable': []}
950
951 for proj in fromKeys:
952 if not proj in toKeys:
953 diff['removed'].append(fromProjects[proj])
954 else:
955 fromProj = fromProjects[proj]
956 toProj = toProjects[proj]
957 try:
958 fromRevId = fromProj.GetCommitRevisionId()
959 toRevId = toProj.GetCommitRevisionId()
960 except ManifestInvalidRevisionError:
961 diff['unreachable'].append((fromProj, toProj))
962 else:
963 if fromRevId != toRevId:
964 diff['changed'].append((fromProj, toProj))
965 toKeys.remove(proj)
966
967 for proj in toKeys:
968 diff['added'].append(toProjects[proj])
969
970 return diff
Simran Basib9a1b732015-08-20 12:19:28 -0700971
972
973class GitcManifest(XmlManifest):
974
975 def __init__(self, repodir, gitc_client_name):
976 """Initialize the GitcManifest object."""
977 super(GitcManifest, self).__init__(repodir)
978 self.isGitcClient = True
979 self.gitc_client_name = gitc_client_name
Simran Basi8ce50412015-08-28 14:25:44 -0700980 self.gitc_client_dir = os.path.join(gitc_utils.get_gitc_manifest_dir(),
Simran Basib9a1b732015-08-20 12:19:28 -0700981 gitc_client_name)
982 self.manifestFile = os.path.join(self.gitc_client_dir, '.manifest')
983
984 def _ParseProject(self, node, parent = None):
985 """Override _ParseProject and add support for GITC specific attributes."""
986 return super(GitcManifest, self)._ParseProject(
987 node, parent=parent, old_revision=node.getAttribute('old-revision'))
988
989 def _output_manifest_project_extras(self, p, e):
990 """Output GITC Specific Project attributes"""
991 if p.old_revision:
Stefan Beller66851062016-06-17 16:40:08 -0700992 e.setAttribute('old-revision', str(p.old_revision))
Simran Basib9a1b732015-08-20 12:19:28 -0700993