hg.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # ex:ts=4:sw=4:sts=4:et
  2. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  3. """
  4. BitBake 'Fetch' implementation for mercurial DRCS (hg).
  5. """
  6. # Copyright (C) 2003, 2004 Chris Larson
  7. # Copyright (C) 2004 Marcin Juszkiewicz
  8. # Copyright (C) 2007 Robert Schuster
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License version 2 as
  12. # published by the Free Software Foundation.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. #
  23. # Based on functions from the base bb module, Copyright 2003 Holger Schurig
  24. import os
  25. import sys
  26. import logging
  27. import bb
  28. from bb import data
  29. from bb.fetch2 import FetchMethod
  30. from bb.fetch2 import FetchError
  31. from bb.fetch2 import MissingParameterError
  32. from bb.fetch2 import runfetchcmd
  33. from bb.fetch2 import logger
  34. class Hg(FetchMethod):
  35. """Class to fetch from mercurial repositories"""
  36. def supports(self, ud, d):
  37. """
  38. Check to see if a given url can be fetched with mercurial.
  39. """
  40. return ud.type in ['hg']
  41. def urldata_init(self, ud, d):
  42. """
  43. init hg specific variable within url data
  44. """
  45. if not "module" in ud.parm:
  46. raise MissingParameterError('module', ud.url)
  47. ud.module = ud.parm["module"]
  48. # Create paths to mercurial checkouts
  49. relpath = self._strip_leading_slashes(ud.path)
  50. ud.pkgdir = os.path.join(data.expand('${HGDIR}', d), ud.host, relpath)
  51. ud.moddir = os.path.join(ud.pkgdir, ud.module)
  52. ud.setup_revisons(d)
  53. if 'rev' in ud.parm:
  54. ud.revision = ud.parm['rev']
  55. elif not ud.revision:
  56. ud.revision = self.latest_revision(ud.url, ud, d)
  57. ud.localfile = data.expand('%s_%s_%s_%s.tar.gz' % (ud.module.replace('/', '.'), ud.host, ud.path.replace('/', '.'), ud.revision), d)
  58. def need_update(self, ud, d):
  59. revTag = ud.parm.get('rev', 'tip')
  60. if revTag == "tip":
  61. return True
  62. if not os.path.exists(ud.localpath):
  63. return True
  64. return False
  65. def _buildhgcommand(self, ud, d, command):
  66. """
  67. Build up an hg commandline based on ud
  68. command is "fetch", "update", "info"
  69. """
  70. basecmd = data.expand('${FETCHCMD_hg}', d)
  71. proto = ud.parm.get('protocol', 'http')
  72. host = ud.host
  73. if proto == "file":
  74. host = "/"
  75. ud.host = "localhost"
  76. if not ud.user:
  77. hgroot = host + ud.path
  78. else:
  79. if ud.pswd:
  80. hgroot = ud.user + ":" + ud.pswd + "@" + host + ud.path
  81. else:
  82. hgroot = ud.user + "@" + host + ud.path
  83. if command == "info":
  84. return "%s identify -i %s://%s/%s" % (basecmd, proto, hgroot, ud.module)
  85. options = [];
  86. # Don't specify revision for the fetch; clone the entire repo.
  87. # This avoids an issue if the specified revision is a tag, because
  88. # the tag actually exists in the specified revision + 1, so it won't
  89. # be available when used in any successive commands.
  90. if ud.revision and command != "fetch":
  91. options.append("-r %s" % ud.revision)
  92. if command == "fetch":
  93. cmd = "%s clone %s %s://%s/%s %s" % (basecmd, " ".join(options), proto, hgroot, ud.module, ud.module)
  94. elif command == "pull":
  95. # do not pass options list; limiting pull to rev causes the local
  96. # repo not to contain it and immediately following "update" command
  97. # will crash
  98. if ud.user and ud.pswd:
  99. cmd = "%s --config auth.default.prefix=* --config auth.default.username=%s --config auth.default.password=%s --config \"auth.default.schemes=%s\" pull" % (basecmd, ud.user, ud.pswd, proto)
  100. else:
  101. cmd = "%s pull" % (basecmd)
  102. elif command == "update":
  103. cmd = "%s update -C %s" % (basecmd, " ".join(options))
  104. else:
  105. raise FetchError("Invalid hg command %s" % command, ud.url)
  106. return cmd
  107. def download(self, ud, d):
  108. """Fetch url"""
  109. logger.debug(2, "Fetch: checking for module directory '" + ud.moddir + "'")
  110. if os.access(os.path.join(ud.moddir, '.hg'), os.R_OK):
  111. updatecmd = self._buildhgcommand(ud, d, "pull")
  112. logger.info("Update " + ud.url)
  113. # update sources there
  114. os.chdir(ud.moddir)
  115. logger.debug(1, "Running %s", updatecmd)
  116. bb.fetch2.check_network_access(d, updatecmd, ud.url)
  117. runfetchcmd(updatecmd, d)
  118. else:
  119. fetchcmd = self._buildhgcommand(ud, d, "fetch")
  120. logger.info("Fetch " + ud.url)
  121. # check out sources there
  122. bb.utils.mkdirhier(ud.pkgdir)
  123. os.chdir(ud.pkgdir)
  124. logger.debug(1, "Running %s", fetchcmd)
  125. bb.fetch2.check_network_access(d, fetchcmd, ud.url)
  126. runfetchcmd(fetchcmd, d)
  127. # Even when we clone (fetch), we still need to update as hg's clone
  128. # won't checkout the specified revision if its on a branch
  129. updatecmd = self._buildhgcommand(ud, d, "update")
  130. os.chdir(ud.moddir)
  131. logger.debug(1, "Running %s", updatecmd)
  132. runfetchcmd(updatecmd, d)
  133. scmdata = ud.parm.get("scmdata", "")
  134. if scmdata == "keep":
  135. tar_flags = ""
  136. else:
  137. tar_flags = "--exclude '.hg' --exclude '.hgrags'"
  138. os.chdir(ud.pkgdir)
  139. runfetchcmd("tar %s -czf %s %s" % (tar_flags, ud.localpath, ud.module), d, cleanup = [ud.localpath])
  140. def supports_srcrev(self):
  141. return True
  142. def _latest_revision(self, ud, d, name):
  143. """
  144. Compute tip revision for the url
  145. """
  146. bb.fetch2.check_network_access(d, self._buildhgcommand(ud, d, "info"))
  147. output = runfetchcmd(self._buildhgcommand(ud, d, "info"), d)
  148. return output.strip()
  149. def _build_revision(self, ud, d, name):
  150. return ud.revision
  151. def _revision_key(self, ud, d, name):
  152. """
  153. Return a unique key for the url
  154. """
  155. return "hg:" + ud.moddir