manifest_xml.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. #
  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. from __future__ import print_function
  16. import itertools
  17. import os
  18. import re
  19. import sys
  20. import xml.dom.minidom
  21. from pyversion import is_python3
  22. if is_python3():
  23. import urllib.parse
  24. else:
  25. import imp
  26. import urlparse
  27. urllib = imp.new_module('urllib')
  28. urllib.parse = urlparse
  29. from git_config import GitConfig
  30. from git_refs import R_HEADS, HEAD
  31. from project import RemoteSpec, Project, MetaProject
  32. from error import ManifestParseError
  33. MANIFEST_FILE_NAME = 'manifest.xml'
  34. LOCAL_MANIFEST_NAME = 'local_manifest.xml'
  35. LOCAL_MANIFESTS_DIR_NAME = 'local_manifests'
  36. urllib.parse.uses_relative.extend(['ssh', 'git'])
  37. urllib.parse.uses_netloc.extend(['ssh', 'git'])
  38. class _Default(object):
  39. """Project defaults within the manifest."""
  40. revisionExpr = None
  41. destBranchExpr = None
  42. remote = None
  43. sync_j = 1
  44. sync_c = False
  45. sync_s = False
  46. def __eq__(self, other):
  47. return self.__dict__ == other.__dict__
  48. def __ne__(self, other):
  49. return self.__dict__ != other.__dict__
  50. class _XmlRemote(object):
  51. def __init__(self,
  52. name,
  53. alias=None,
  54. fetch=None,
  55. manifestUrl=None,
  56. review=None):
  57. self.name = name
  58. self.fetchUrl = fetch
  59. self.manifestUrl = manifestUrl
  60. self.remoteAlias = alias
  61. self.reviewUrl = review
  62. self.resolvedFetchUrl = self._resolveFetchUrl()
  63. def __eq__(self, other):
  64. return self.__dict__ == other.__dict__
  65. def __ne__(self, other):
  66. return self.__dict__ != other.__dict__
  67. def _resolveFetchUrl(self):
  68. url = self.fetchUrl.rstrip('/')
  69. manifestUrl = self.manifestUrl.rstrip('/')
  70. p = manifestUrl.startswith('persistent-http')
  71. if p:
  72. manifestUrl = manifestUrl[len('persistent-'):]
  73. # urljoin will get confused if there is no scheme in the base url
  74. # ie, if manifestUrl is of the form <hostname:port>
  75. if manifestUrl.find(':') != manifestUrl.find('/') - 1:
  76. manifestUrl = 'gopher://' + manifestUrl
  77. url = urllib.parse.urljoin(manifestUrl, url)
  78. url = re.sub(r'^gopher://', '', url)
  79. if p:
  80. url = 'persistent-' + url
  81. return url
  82. def ToRemoteSpec(self, projectName):
  83. url = self.resolvedFetchUrl.rstrip('/') + '/' + projectName
  84. remoteName = self.name
  85. if self.remoteAlias:
  86. remoteName = self.remoteAlias
  87. return RemoteSpec(remoteName, url, self.reviewUrl)
  88. class XmlManifest(object):
  89. """manages the repo configuration file"""
  90. def __init__(self, repodir):
  91. self.repodir = os.path.abspath(repodir)
  92. self.topdir = os.path.dirname(self.repodir)
  93. self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME)
  94. self.globalConfig = GitConfig.ForUser()
  95. self.localManifestWarning = False
  96. self.repoProject = MetaProject(self, 'repo',
  97. gitdir = os.path.join(repodir, 'repo/.git'),
  98. worktree = os.path.join(repodir, 'repo'))
  99. self.manifestProject = MetaProject(self, 'manifests',
  100. gitdir = os.path.join(repodir, 'manifests.git'),
  101. worktree = os.path.join(repodir, 'manifests'))
  102. self._Unload()
  103. def Override(self, name):
  104. """Use a different manifest, just for the current instantiation.
  105. """
  106. path = os.path.join(self.manifestProject.worktree, name)
  107. if not os.path.isfile(path):
  108. raise ManifestParseError('manifest %s not found' % name)
  109. old = self.manifestFile
  110. try:
  111. self.manifestFile = path
  112. self._Unload()
  113. self._Load()
  114. finally:
  115. self.manifestFile = old
  116. def Link(self, name):
  117. """Update the repo metadata to use a different manifest.
  118. """
  119. self.Override(name)
  120. try:
  121. if os.path.lexists(self.manifestFile):
  122. os.remove(self.manifestFile)
  123. os.symlink('manifests/%s' % name, self.manifestFile)
  124. except OSError as e:
  125. raise ManifestParseError('cannot link manifest %s: %s' % (name, str(e)))
  126. def _RemoteToXml(self, r, doc, root):
  127. e = doc.createElement('remote')
  128. root.appendChild(e)
  129. e.setAttribute('name', r.name)
  130. e.setAttribute('fetch', r.fetchUrl)
  131. if r.remoteAlias is not None:
  132. e.setAttribute('alias', r.remoteAlias)
  133. if r.reviewUrl is not None:
  134. e.setAttribute('review', r.reviewUrl)
  135. def Save(self, fd, peg_rev=False, peg_rev_upstream=True):
  136. """Write the current manifest out to the given file descriptor.
  137. """
  138. mp = self.manifestProject
  139. groups = mp.config.GetString('manifest.groups')
  140. if groups:
  141. groups = [x for x in re.split(r'[,\s]+', groups) if x]
  142. doc = xml.dom.minidom.Document()
  143. root = doc.createElement('manifest')
  144. doc.appendChild(root)
  145. # Save out the notice. There's a little bit of work here to give it the
  146. # right whitespace, which assumes that the notice is automatically indented
  147. # by 4 by minidom.
  148. if self.notice:
  149. notice_element = root.appendChild(doc.createElement('notice'))
  150. notice_lines = self.notice.splitlines()
  151. indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:]
  152. notice_element.appendChild(doc.createTextNode(indented_notice))
  153. d = self.default
  154. for r in sorted(self.remotes):
  155. self._RemoteToXml(self.remotes[r], doc, root)
  156. if self.remotes:
  157. root.appendChild(doc.createTextNode(''))
  158. have_default = False
  159. e = doc.createElement('default')
  160. if d.remote:
  161. have_default = True
  162. e.setAttribute('remote', d.remote.name)
  163. if d.revisionExpr:
  164. have_default = True
  165. e.setAttribute('revision', d.revisionExpr)
  166. if d.sync_j > 1:
  167. have_default = True
  168. e.setAttribute('sync-j', '%d' % d.sync_j)
  169. if d.sync_c:
  170. have_default = True
  171. e.setAttribute('sync-c', 'true')
  172. if d.sync_s:
  173. have_default = True
  174. e.setAttribute('sync-s', 'true')
  175. if have_default:
  176. root.appendChild(e)
  177. root.appendChild(doc.createTextNode(''))
  178. if self._manifest_server:
  179. e = doc.createElement('manifest-server')
  180. e.setAttribute('url', self._manifest_server)
  181. root.appendChild(e)
  182. root.appendChild(doc.createTextNode(''))
  183. def output_projects(parent, parent_node, projects):
  184. for p in projects:
  185. output_project(parent, parent_node, self.projects[p])
  186. def output_project(parent, parent_node, p):
  187. if not p.MatchesGroups(groups):
  188. return
  189. name = p.name
  190. relpath = p.relpath
  191. if parent:
  192. name = self._UnjoinName(parent.name, name)
  193. relpath = self._UnjoinRelpath(parent.relpath, relpath)
  194. e = doc.createElement('project')
  195. parent_node.appendChild(e)
  196. e.setAttribute('name', name)
  197. if relpath != name:
  198. e.setAttribute('path', relpath)
  199. remoteName = None
  200. if d.remote:
  201. remoteName = d.remote.remoteAlias or d.remote.name
  202. if not d.remote or p.remote.name != remoteName:
  203. e.setAttribute('remote', p.remote.name)
  204. if peg_rev:
  205. if self.IsMirror:
  206. value = p.bare_git.rev_parse(p.revisionExpr + '^0')
  207. else:
  208. value = p.work_git.rev_parse(HEAD + '^0')
  209. e.setAttribute('revision', value)
  210. if peg_rev_upstream and value != p.revisionExpr:
  211. # Only save the origin if the origin is not a sha1, and the default
  212. # isn't our value, and the if the default doesn't already have that
  213. # covered.
  214. e.setAttribute('upstream', p.revisionExpr)
  215. elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
  216. e.setAttribute('revision', p.revisionExpr)
  217. for c in p.copyfiles:
  218. ce = doc.createElement('copyfile')
  219. ce.setAttribute('src', c.src)
  220. ce.setAttribute('dest', c.dest)
  221. e.appendChild(ce)
  222. default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath]
  223. egroups = [g for g in p.groups if g not in default_groups]
  224. if egroups:
  225. e.setAttribute('groups', ','.join(egroups))
  226. for a in p.annotations:
  227. if a.keep == "true":
  228. ae = doc.createElement('annotation')
  229. ae.setAttribute('name', a.name)
  230. ae.setAttribute('value', a.value)
  231. e.appendChild(ae)
  232. if p.sync_c:
  233. e.setAttribute('sync-c', 'true')
  234. if p.sync_s:
  235. e.setAttribute('sync-s', 'true')
  236. if p.subprojects:
  237. sort_projects = list(sorted([subp.name for subp in p.subprojects]))
  238. output_projects(p, e, sort_projects)
  239. sort_projects = list(sorted([key for key, value in self.projects.items()
  240. if not value.parent]))
  241. sort_projects.sort()
  242. output_projects(None, root, sort_projects)
  243. if self._repo_hooks_project:
  244. root.appendChild(doc.createTextNode(''))
  245. e = doc.createElement('repo-hooks')
  246. e.setAttribute('in-project', self._repo_hooks_project.name)
  247. e.setAttribute('enabled-list',
  248. ' '.join(self._repo_hooks_project.enabled_repo_hooks))
  249. root.appendChild(e)
  250. doc.writexml(fd, '', ' ', '\n', 'UTF-8')
  251. @property
  252. def projects(self):
  253. self._Load()
  254. return self._projects
  255. @property
  256. def remotes(self):
  257. self._Load()
  258. return self._remotes
  259. @property
  260. def default(self):
  261. self._Load()
  262. return self._default
  263. @property
  264. def repo_hooks_project(self):
  265. self._Load()
  266. return self._repo_hooks_project
  267. @property
  268. def notice(self):
  269. self._Load()
  270. return self._notice
  271. @property
  272. def manifest_server(self):
  273. self._Load()
  274. return self._manifest_server
  275. @property
  276. def IsMirror(self):
  277. return self.manifestProject.config.GetBoolean('repo.mirror')
  278. @property
  279. def IsArchive(self):
  280. return self.manifestProject.config.GetBoolean('repo.archive')
  281. def _Unload(self):
  282. self._loaded = False
  283. self._projects = {}
  284. self._remotes = {}
  285. self._default = None
  286. self._repo_hooks_project = None
  287. self._notice = None
  288. self.branch = None
  289. self._manifest_server = None
  290. def _Load(self):
  291. if not self._loaded:
  292. m = self.manifestProject
  293. b = m.GetBranch(m.CurrentBranch).merge
  294. if b is not None and b.startswith(R_HEADS):
  295. b = b[len(R_HEADS):]
  296. self.branch = b
  297. nodes = []
  298. nodes.append(self._ParseManifestXml(self.manifestFile,
  299. self.manifestProject.worktree))
  300. local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
  301. if os.path.exists(local):
  302. if not self.localManifestWarning:
  303. self.localManifestWarning = True
  304. print('warning: %s is deprecated; put local manifests in `%s` instead'
  305. % (LOCAL_MANIFEST_NAME, os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME)),
  306. file=sys.stderr)
  307. nodes.append(self._ParseManifestXml(local, self.repodir))
  308. local_dir = os.path.abspath(os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME))
  309. try:
  310. for local_file in sorted(os.listdir(local_dir)):
  311. if local_file.endswith('.xml'):
  312. local = os.path.join(local_dir, local_file)
  313. nodes.append(self._ParseManifestXml(local, self.repodir))
  314. except OSError:
  315. pass
  316. try:
  317. self._ParseManifest(nodes)
  318. except ManifestParseError as e:
  319. # There was a problem parsing, unload ourselves in case they catch
  320. # this error and try again later, we will show the correct error
  321. self._Unload()
  322. raise e
  323. if self.IsMirror:
  324. self._AddMetaProjectMirror(self.repoProject)
  325. self._AddMetaProjectMirror(self.manifestProject)
  326. self._loaded = True
  327. def _ParseManifestXml(self, path, include_root):
  328. try:
  329. root = xml.dom.minidom.parse(path)
  330. except (OSError, xml.parsers.expat.ExpatError) as e:
  331. raise ManifestParseError("error parsing manifest %s: %s" % (path, e))
  332. if not root or not root.childNodes:
  333. raise ManifestParseError("no root node in %s" % (path,))
  334. for manifest in root.childNodes:
  335. if manifest.nodeName == 'manifest':
  336. break
  337. else:
  338. raise ManifestParseError("no <manifest> in %s" % (path,))
  339. nodes = []
  340. for node in manifest.childNodes: # pylint:disable=W0631
  341. # We only get here if manifest is initialised
  342. if node.nodeName == 'include':
  343. name = self._reqatt(node, 'name')
  344. fp = os.path.join(include_root, name)
  345. if not os.path.isfile(fp):
  346. raise ManifestParseError("include %s doesn't exist or isn't a file"
  347. % (name,))
  348. try:
  349. nodes.extend(self._ParseManifestXml(fp, include_root))
  350. # should isolate this to the exact exception, but that's
  351. # tricky. actual parsing implementation may vary.
  352. except (KeyboardInterrupt, RuntimeError, SystemExit):
  353. raise
  354. except Exception as e:
  355. raise ManifestParseError(
  356. "failed parsing included manifest %s: %s", (name, e))
  357. else:
  358. nodes.append(node)
  359. return nodes
  360. def _ParseManifest(self, node_list):
  361. for node in itertools.chain(*node_list):
  362. if node.nodeName == 'remote':
  363. remote = self._ParseRemote(node)
  364. if remote:
  365. if remote.name in self._remotes:
  366. if remote != self._remotes[remote.name]:
  367. raise ManifestParseError(
  368. 'remote %s already exists with different attributes' %
  369. (remote.name))
  370. else:
  371. self._remotes[remote.name] = remote
  372. for node in itertools.chain(*node_list):
  373. if node.nodeName == 'default':
  374. new_default = self._ParseDefault(node)
  375. if self._default is None:
  376. self._default = new_default
  377. elif new_default != self._default:
  378. raise ManifestParseError('duplicate default in %s' %
  379. (self.manifestFile))
  380. if self._default is None:
  381. self._default = _Default()
  382. for node in itertools.chain(*node_list):
  383. if node.nodeName == 'notice':
  384. if self._notice is not None:
  385. raise ManifestParseError(
  386. 'duplicate notice in %s' %
  387. (self.manifestFile))
  388. self._notice = self._ParseNotice(node)
  389. for node in itertools.chain(*node_list):
  390. if node.nodeName == 'manifest-server':
  391. url = self._reqatt(node, 'url')
  392. if self._manifest_server is not None:
  393. raise ManifestParseError(
  394. 'duplicate manifest-server in %s' %
  395. (self.manifestFile))
  396. self._manifest_server = url
  397. def recursively_add_projects(project):
  398. if self._projects.get(project.name):
  399. raise ManifestParseError(
  400. 'duplicate project %s in %s' %
  401. (project.name, self.manifestFile))
  402. self._projects[project.name] = project
  403. for subproject in project.subprojects:
  404. recursively_add_projects(subproject)
  405. for node in itertools.chain(*node_list):
  406. if node.nodeName == 'project':
  407. project = self._ParseProject(node)
  408. recursively_add_projects(project)
  409. if node.nodeName == 'repo-hooks':
  410. # Get the name of the project and the (space-separated) list of enabled.
  411. repo_hooks_project = self._reqatt(node, 'in-project')
  412. enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
  413. # Only one project can be the hooks project
  414. if self._repo_hooks_project is not None:
  415. raise ManifestParseError(
  416. 'duplicate repo-hooks in %s' %
  417. (self.manifestFile))
  418. # Store a reference to the Project.
  419. try:
  420. self._repo_hooks_project = self._projects[repo_hooks_project]
  421. except KeyError:
  422. raise ManifestParseError(
  423. 'project %s not found for repo-hooks' %
  424. (repo_hooks_project))
  425. # Store the enabled hooks in the Project object.
  426. self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
  427. if node.nodeName == 'remove-project':
  428. name = self._reqatt(node, 'name')
  429. try:
  430. del self._projects[name]
  431. except KeyError:
  432. raise ManifestParseError('remove-project element specifies non-existent '
  433. 'project: %s' % name)
  434. # If the manifest removes the hooks project, treat it as if it deleted
  435. # the repo-hooks element too.
  436. if self._repo_hooks_project and (self._repo_hooks_project.name == name):
  437. self._repo_hooks_project = None
  438. def _AddMetaProjectMirror(self, m):
  439. name = None
  440. m_url = m.GetRemote(m.remote.name).url
  441. if m_url.endswith('/.git'):
  442. raise ManifestParseError('refusing to mirror %s' % m_url)
  443. if self._default and self._default.remote:
  444. url = self._default.remote.resolvedFetchUrl
  445. if not url.endswith('/'):
  446. url += '/'
  447. if m_url.startswith(url):
  448. remote = self._default.remote
  449. name = m_url[len(url):]
  450. if name is None:
  451. s = m_url.rindex('/') + 1
  452. manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
  453. remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl)
  454. name = m_url[s:]
  455. if name.endswith('.git'):
  456. name = name[:-4]
  457. if name not in self._projects:
  458. m.PreSync()
  459. gitdir = os.path.join(self.topdir, '%s.git' % name)
  460. project = Project(manifest = self,
  461. name = name,
  462. remote = remote.ToRemoteSpec(name),
  463. gitdir = gitdir,
  464. worktree = None,
  465. relpath = None,
  466. revisionExpr = m.revisionExpr,
  467. revisionId = None)
  468. self._projects[project.name] = project
  469. def _ParseRemote(self, node):
  470. """
  471. reads a <remote> element from the manifest file
  472. """
  473. name = self._reqatt(node, 'name')
  474. alias = node.getAttribute('alias')
  475. if alias == '':
  476. alias = None
  477. fetch = self._reqatt(node, 'fetch')
  478. review = node.getAttribute('review')
  479. if review == '':
  480. review = None
  481. manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
  482. return _XmlRemote(name, alias, fetch, manifestUrl, review)
  483. def _ParseDefault(self, node):
  484. """
  485. reads a <default> element from the manifest file
  486. """
  487. d = _Default()
  488. d.remote = self._get_remote(node)
  489. d.revisionExpr = node.getAttribute('revision')
  490. if d.revisionExpr == '':
  491. d.revisionExpr = None
  492. d.destBranchExpr = node.getAttribute('dest-branch') or None
  493. sync_j = node.getAttribute('sync-j')
  494. if sync_j == '' or sync_j is None:
  495. d.sync_j = 1
  496. else:
  497. d.sync_j = int(sync_j)
  498. sync_c = node.getAttribute('sync-c')
  499. if not sync_c:
  500. d.sync_c = False
  501. else:
  502. d.sync_c = sync_c.lower() in ("yes", "true", "1")
  503. sync_s = node.getAttribute('sync-s')
  504. if not sync_s:
  505. d.sync_s = False
  506. else:
  507. d.sync_s = sync_s.lower() in ("yes", "true", "1")
  508. return d
  509. def _ParseNotice(self, node):
  510. """
  511. reads a <notice> element from the manifest file
  512. The <notice> element is distinct from other tags in the XML in that the
  513. data is conveyed between the start and end tag (it's not an empty-element
  514. tag).
  515. The white space (carriage returns, indentation) for the notice element is
  516. relevant and is parsed in a way that is based on how python docstrings work.
  517. In fact, the code is remarkably similar to here:
  518. http://www.python.org/dev/peps/pep-0257/
  519. """
  520. # Get the data out of the node...
  521. notice = node.childNodes[0].data
  522. # Figure out minimum indentation, skipping the first line (the same line
  523. # as the <notice> tag)...
  524. minIndent = sys.maxsize
  525. lines = notice.splitlines()
  526. for line in lines[1:]:
  527. lstrippedLine = line.lstrip()
  528. if lstrippedLine:
  529. indent = len(line) - len(lstrippedLine)
  530. minIndent = min(indent, minIndent)
  531. # Strip leading / trailing blank lines and also indentation.
  532. cleanLines = [lines[0].strip()]
  533. for line in lines[1:]:
  534. cleanLines.append(line[minIndent:].rstrip())
  535. # Clear completely blank lines from front and back...
  536. while cleanLines and not cleanLines[0]:
  537. del cleanLines[0]
  538. while cleanLines and not cleanLines[-1]:
  539. del cleanLines[-1]
  540. return '\n'.join(cleanLines)
  541. def _JoinName(self, parent_name, name):
  542. return os.path.join(parent_name, name)
  543. def _UnjoinName(self, parent_name, name):
  544. return os.path.relpath(name, parent_name)
  545. def _ParseProject(self, node, parent = None):
  546. """
  547. reads a <project> element from the manifest file
  548. """
  549. name = self._reqatt(node, 'name')
  550. if parent:
  551. name = self._JoinName(parent.name, name)
  552. remote = self._get_remote(node)
  553. if remote is None:
  554. remote = self._default.remote
  555. if remote is None:
  556. raise ManifestParseError("no remote for project %s within %s" %
  557. (name, self.manifestFile))
  558. revisionExpr = node.getAttribute('revision')
  559. if not revisionExpr:
  560. revisionExpr = self._default.revisionExpr
  561. if not revisionExpr:
  562. raise ManifestParseError("no revision for project %s within %s" %
  563. (name, self.manifestFile))
  564. path = node.getAttribute('path')
  565. if not path:
  566. path = name
  567. if path.startswith('/'):
  568. raise ManifestParseError("project %s path cannot be absolute in %s" %
  569. (name, self.manifestFile))
  570. rebase = node.getAttribute('rebase')
  571. if not rebase:
  572. rebase = True
  573. else:
  574. rebase = rebase.lower() in ("yes", "true", "1")
  575. sync_c = node.getAttribute('sync-c')
  576. if not sync_c:
  577. sync_c = False
  578. else:
  579. sync_c = sync_c.lower() in ("yes", "true", "1")
  580. sync_s = node.getAttribute('sync-s')
  581. if not sync_s:
  582. sync_s = self._default.sync_s
  583. else:
  584. sync_s = sync_s.lower() in ("yes", "true", "1")
  585. clone_depth = node.getAttribute('clone-depth')
  586. if clone_depth:
  587. try:
  588. clone_depth = int(clone_depth)
  589. if clone_depth <= 0:
  590. raise ValueError()
  591. except ValueError:
  592. raise ManifestParseError('invalid clone-depth %s in %s' %
  593. (clone_depth, self.manifestFile))
  594. dest_branch = node.getAttribute('dest-branch') or self._default.destBranchExpr
  595. upstream = node.getAttribute('upstream')
  596. groups = ''
  597. if node.hasAttribute('groups'):
  598. groups = node.getAttribute('groups')
  599. groups = [x for x in re.split(r'[,\s]+', groups) if x]
  600. if parent is None:
  601. relpath, worktree, gitdir = self.GetProjectPaths(name, path)
  602. else:
  603. relpath, worktree, gitdir = self.GetSubprojectPaths(parent, path)
  604. default_groups = ['all', 'name:%s' % name, 'path:%s' % relpath]
  605. groups.extend(set(default_groups).difference(groups))
  606. if self.IsMirror and node.hasAttribute('force-path'):
  607. if node.getAttribute('force-path').lower() in ("yes", "true", "1"):
  608. gitdir = os.path.join(self.topdir, '%s.git' % path)
  609. project = Project(manifest = self,
  610. name = name,
  611. remote = remote.ToRemoteSpec(name),
  612. gitdir = gitdir,
  613. worktree = worktree,
  614. relpath = relpath,
  615. revisionExpr = revisionExpr,
  616. revisionId = None,
  617. rebase = rebase,
  618. groups = groups,
  619. sync_c = sync_c,
  620. sync_s = sync_s,
  621. clone_depth = clone_depth,
  622. upstream = upstream,
  623. parent = parent,
  624. dest_branch = dest_branch)
  625. for n in node.childNodes:
  626. if n.nodeName == 'copyfile':
  627. self._ParseCopyFile(project, n)
  628. if n.nodeName == 'annotation':
  629. self._ParseAnnotation(project, n)
  630. if n.nodeName == 'project':
  631. project.subprojects.append(self._ParseProject(n, parent = project))
  632. return project
  633. def GetProjectPaths(self, name, path):
  634. relpath = path
  635. if self.IsMirror:
  636. worktree = None
  637. gitdir = os.path.join(self.topdir, '%s.git' % name)
  638. else:
  639. worktree = os.path.join(self.topdir, path).replace('\\', '/')
  640. gitdir = os.path.join(self.repodir, 'projects', '%s.git' % path)
  641. return relpath, worktree, gitdir
  642. def GetSubprojectName(self, parent, submodule_path):
  643. return os.path.join(parent.name, submodule_path)
  644. def _JoinRelpath(self, parent_relpath, relpath):
  645. return os.path.join(parent_relpath, relpath)
  646. def _UnjoinRelpath(self, parent_relpath, relpath):
  647. return os.path.relpath(relpath, parent_relpath)
  648. def GetSubprojectPaths(self, parent, path):
  649. relpath = self._JoinRelpath(parent.relpath, path)
  650. gitdir = os.path.join(parent.gitdir, 'subprojects', '%s.git' % path)
  651. if self.IsMirror:
  652. worktree = None
  653. else:
  654. worktree = os.path.join(parent.worktree, path).replace('\\', '/')
  655. return relpath, worktree, gitdir
  656. def _ParseCopyFile(self, project, node):
  657. src = self._reqatt(node, 'src')
  658. dest = self._reqatt(node, 'dest')
  659. if not self.IsMirror:
  660. # src is project relative;
  661. # dest is relative to the top of the tree
  662. project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
  663. def _ParseAnnotation(self, project, node):
  664. name = self._reqatt(node, 'name')
  665. value = self._reqatt(node, 'value')
  666. try:
  667. keep = self._reqatt(node, 'keep').lower()
  668. except ManifestParseError:
  669. keep = "true"
  670. if keep != "true" and keep != "false":
  671. raise ManifestParseError('optional "keep" attribute must be '
  672. '"true" or "false"')
  673. project.AddAnnotation(name, value, keep)
  674. def _get_remote(self, node):
  675. name = node.getAttribute('remote')
  676. if not name:
  677. return None
  678. v = self._remotes.get(name)
  679. if not v:
  680. raise ManifestParseError("remote %s not defined in %s" %
  681. (name, self.manifestFile))
  682. return v
  683. def _reqatt(self, node, attname):
  684. """
  685. reads a required attribute from the node.
  686. """
  687. v = node.getAttribute(attname)
  688. if not v:
  689. raise ManifestParseError("no %s in <%s> within %s" %
  690. (attname, node.nodeName, self.manifestFile))
  691. return v