bzr.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. import os
  14. import sys
  15. import logging
  16. import bb
  17. from bb.fetch2 import FetchMethod
  18. from bb.fetch2 import FetchError
  19. from bb.fetch2 import runfetchcmd
  20. from bb.fetch2 import logger
  21. class Bzr(FetchMethod):
  22. def supports(self, ud, d):
  23. return ud.type in ['bzr']
  24. def urldata_init(self, ud, d):
  25. """
  26. init bzr specific variable within url data
  27. """
  28. # Create paths to bzr checkouts
  29. bzrdir = d.getVar("BZRDIR") or (d.getVar("DL_DIR") + "/bzr")
  30. relpath = self._strip_leading_slashes(ud.path)
  31. ud.pkgdir = os.path.join(bzrdir, ud.host, relpath)
  32. ud.setup_revisions(d)
  33. if not ud.revision:
  34. ud.revision = self.latest_revision(ud, d)
  35. ud.localfile = d.expand('bzr_%s_%s_%s.tar.gz' % (ud.host, ud.path.replace('/', '.'), ud.revision))
  36. def _buildbzrcommand(self, ud, d, command):
  37. """
  38. Build up an bzr commandline based on ud
  39. command is "fetch", "update", "revno"
  40. """
  41. basecmd = d.getVar("FETCHCMD_bzr") or "/usr/bin/env bzr"
  42. proto = ud.parm.get('protocol', 'http')
  43. bzrroot = ud.host + ud.path
  44. options = []
  45. if command == "revno":
  46. bzrcmd = "%s revno %s %s://%s" % (basecmd, " ".join(options), proto, bzrroot)
  47. else:
  48. if ud.revision:
  49. options.append("-r %s" % ud.revision)
  50. if command == "fetch":
  51. bzrcmd = "%s branch %s %s://%s" % (basecmd, " ".join(options), proto, bzrroot)
  52. elif command == "update":
  53. bzrcmd = "%s pull %s --overwrite" % (basecmd, " ".join(options))
  54. else:
  55. raise FetchError("Invalid bzr command %s" % command, ud.url)
  56. return bzrcmd
  57. def download(self, ud, d):
  58. """Fetch url"""
  59. if os.access(os.path.join(ud.pkgdir, os.path.basename(ud.pkgdir), '.bzr'), os.R_OK):
  60. bzrcmd = self._buildbzrcommand(ud, d, "update")
  61. logger.debug(1, "BZR Update %s", ud.url)
  62. bb.fetch2.check_network_access(d, bzrcmd, ud.url)
  63. runfetchcmd(bzrcmd, d, workdir=os.path.join(ud.pkgdir, os.path.basename(ud.path)))
  64. else:
  65. bb.utils.remove(os.path.join(ud.pkgdir, os.path.basename(ud.pkgdir)), True)
  66. bzrcmd = self._buildbzrcommand(ud, d, "fetch")
  67. bb.fetch2.check_network_access(d, bzrcmd, ud.url)
  68. logger.debug(1, "BZR Checkout %s", ud.url)
  69. bb.utils.mkdirhier(ud.pkgdir)
  70. logger.debug(1, "Running %s", bzrcmd)
  71. runfetchcmd(bzrcmd, d, workdir=ud.pkgdir)
  72. scmdata = ud.parm.get("scmdata", "")
  73. if scmdata == "keep":
  74. tar_flags = ""
  75. else:
  76. tar_flags = "--exclude='.bzr' --exclude='.bzrtags'"
  77. # tar them up to a defined filename
  78. runfetchcmd("tar %s -czf %s %s" % (tar_flags, ud.localpath, os.path.basename(ud.pkgdir)),
  79. d, cleanup=[ud.localpath], workdir=ud.pkgdir)
  80. def supports_srcrev(self):
  81. return True
  82. def _revision_key(self, ud, d, name):
  83. """
  84. Return a unique key for the url
  85. """
  86. return "bzr:" + ud.pkgdir
  87. def _latest_revision(self, ud, d, name):
  88. """
  89. Return the latest upstream revision number
  90. """
  91. logger.debug(2, "BZR fetcher hitting network for %s", ud.url)
  92. bb.fetch2.check_network_access(d, self._buildbzrcommand(ud, d, "revno"), ud.url)
  93. output = runfetchcmd(self._buildbzrcommand(ud, d, "revno"), d, True)
  94. return output.strip()
  95. def sortable_revision(self, ud, d, name):
  96. """
  97. Return a sortable revision number which in our case is the revision number
  98. """
  99. return False, self._build_revision(ud, d)
  100. def _build_revision(self, ud, d):
  101. return ud.revision