bzr.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. """
  2. BitBake 'Fetch' implementation for bzr.
  3. """
  4. # Copyright (C) 2007 Ross Burton
  5. # Copyright (C) 2007 Richard Purdie
  6. #
  7. # Classes for obtaining upstream sources for the
  8. # BitBake build tools.
  9. # Copyright (C) 2003, 2004 Chris Larson
  10. #
  11. # SPDX-License-Identifier: GPL-2.0-only
  12. #
  13. # This program is free software; you can redistribute it and/or modify
  14. # it under the terms of the GNU General Public License version 2 as
  15. # published by the Free Software Foundation.
  16. #
  17. # This program is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. # GNU General Public License for more details.
  21. #
  22. # You should have received a copy of the GNU General Public License along
  23. # with this program; if not, write to the Free Software Foundation, Inc.,
  24. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  25. import os
  26. import sys
  27. import logging
  28. import bb
  29. from bb.fetch2 import FetchMethod
  30. from bb.fetch2 import FetchError
  31. from bb.fetch2 import runfetchcmd
  32. from bb.fetch2 import logger
  33. class Bzr(FetchMethod):
  34. def supports(self, ud, d):
  35. return ud.type in ['bzr']
  36. def urldata_init(self, ud, d):
  37. """
  38. init bzr specific variable within url data
  39. """
  40. # Create paths to bzr checkouts
  41. bzrdir = d.getVar("BZRDIR") or (d.getVar("DL_DIR") + "/bzr")
  42. relpath = self._strip_leading_slashes(ud.path)
  43. ud.pkgdir = os.path.join(bzrdir, ud.host, relpath)
  44. ud.setup_revisions(d)
  45. if not ud.revision:
  46. ud.revision = self.latest_revision(ud, d)
  47. ud.localfile = d.expand('bzr_%s_%s_%s.tar.gz' % (ud.host, ud.path.replace('/', '.'), ud.revision))
  48. def _buildbzrcommand(self, ud, d, command):
  49. """
  50. Build up an bzr commandline based on ud
  51. command is "fetch", "update", "revno"
  52. """
  53. basecmd = d.getVar("FETCHCMD_bzr") or "/usr/bin/env bzr"
  54. proto = ud.parm.get('protocol', 'http')
  55. bzrroot = ud.host + ud.path
  56. options = []
  57. if command == "revno":
  58. bzrcmd = "%s revno %s %s://%s" % (basecmd, " ".join(options), proto, bzrroot)
  59. else:
  60. if ud.revision:
  61. options.append("-r %s" % ud.revision)
  62. if command == "fetch":
  63. bzrcmd = "%s branch %s %s://%s" % (basecmd, " ".join(options), proto, bzrroot)
  64. elif command == "update":
  65. bzrcmd = "%s pull %s --overwrite" % (basecmd, " ".join(options))
  66. else:
  67. raise FetchError("Invalid bzr command %s" % command, ud.url)
  68. return bzrcmd
  69. def download(self, ud, d):
  70. """Fetch url"""
  71. if os.access(os.path.join(ud.pkgdir, os.path.basename(ud.pkgdir), '.bzr'), os.R_OK):
  72. bzrcmd = self._buildbzrcommand(ud, d, "update")
  73. logger.debug(1, "BZR Update %s", ud.url)
  74. bb.fetch2.check_network_access(d, bzrcmd, ud.url)
  75. runfetchcmd(bzrcmd, d, workdir=os.path.join(ud.pkgdir, os.path.basename(ud.path)))
  76. else:
  77. bb.utils.remove(os.path.join(ud.pkgdir, os.path.basename(ud.pkgdir)), True)
  78. bzrcmd = self._buildbzrcommand(ud, d, "fetch")
  79. bb.fetch2.check_network_access(d, bzrcmd, ud.url)
  80. logger.debug(1, "BZR Checkout %s", ud.url)
  81. bb.utils.mkdirhier(ud.pkgdir)
  82. logger.debug(1, "Running %s", bzrcmd)
  83. runfetchcmd(bzrcmd, d, workdir=ud.pkgdir)
  84. scmdata = ud.parm.get("scmdata", "")
  85. if scmdata == "keep":
  86. tar_flags = ""
  87. else:
  88. tar_flags = "--exclude='.bzr' --exclude='.bzrtags'"
  89. # tar them up to a defined filename
  90. runfetchcmd("tar %s -czf %s %s" % (tar_flags, ud.localpath, os.path.basename(ud.pkgdir)),
  91. d, cleanup=[ud.localpath], workdir=ud.pkgdir)
  92. def supports_srcrev(self):
  93. return True
  94. def _revision_key(self, ud, d, name):
  95. """
  96. Return a unique key for the url
  97. """
  98. return "bzr:" + ud.pkgdir
  99. def _latest_revision(self, ud, d, name):
  100. """
  101. Return the latest upstream revision number
  102. """
  103. logger.debug(2, "BZR fetcher hitting network for %s", ud.url)
  104. bb.fetch2.check_network_access(d, self._buildbzrcommand(ud, d, "revno"), ud.url)
  105. output = runfetchcmd(self._buildbzrcommand(ud, d, "revno"), d, True)
  106. return output.strip()
  107. def sortable_revision(self, ud, d, name):
  108. """
  109. Return a sortable revision number which in our case is the revision number
  110. """
  111. return False, self._build_revision(ud, d)
  112. def _build_revision(self, ud, d):
  113. return ud.revision