manifest_xml.py 23 KB

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