hg.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 Fetch
  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(Fetch):
  35. """Class to fetch from mercurial repositories"""
  36. def supports(self, url, 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 forcefetch(self, url, ud, d):
  42. revTag = ud.parm.get('rev', 'tip')
  43. return revTag == "tip"
  44. def localpath(self, url, ud, d):
  45. if not "module" in ud.parm:
  46. raise MissingParameterError("hg method needs a 'module' parameter")
  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. if 'rev' in ud.parm:
  53. ud.revision = ud.parm['rev']
  54. else:
  55. tag = Fetch.srcrev_internal_helper(ud, d)
  56. if tag is True:
  57. ud.revision = self.latest_revision(url, ud, d)
  58. elif tag:
  59. ud.revision = tag
  60. else:
  61. ud.revision = self.latest_revision(url, ud, d)
  62. ud.localfile = data.expand('%s_%s_%s_%s.tar.gz' % (ud.module.replace('/', '.'), ud.host, ud.path.replace('/', '.'), ud.revision), d)
  63. return os.path.join(data.getVar("DL_DIR", d, True), ud.localfile)
  64. def _buildhgcommand(self, ud, d, command):
  65. """
  66. Build up an hg commandline based on ud
  67. command is "fetch", "update", "info"
  68. """
  69. basecmd = data.expand('${FETCHCMD_hg}', d)
  70. proto = ud.parm.get('proto', 'http')
  71. host = ud.host
  72. if proto == "file":
  73. host = "/"
  74. ud.host = "localhost"
  75. if not ud.user:
  76. hgroot = host + ud.path
  77. else:
  78. hgroot = ud.user + "@" + host + ud.path
  79. if command is "info":
  80. return "%s identify -i %s://%s/%s" % (basecmd, proto, hgroot, ud.module)
  81. options = [];
  82. if ud.revision:
  83. options.append("-r %s" % ud.revision)
  84. if command is "fetch":
  85. cmd = "%s clone %s %s://%s/%s %s" % (basecmd, " ".join(options), proto, hgroot, ud.module, ud.module)
  86. elif command is "pull":
  87. # do not pass options list; limiting pull to rev causes the local
  88. # repo not to contain it and immediately following "update" command
  89. # will crash
  90. cmd = "%s pull" % (basecmd)
  91. elif command is "update":
  92. cmd = "%s update -C %s" % (basecmd, " ".join(options))
  93. else:
  94. raise FetchError("Invalid hg command %s" % command)
  95. return cmd
  96. def go(self, loc, ud, d):
  97. """Fetch url"""
  98. logger.debug(2, "Fetch: checking for module directory '" + ud.moddir + "'")
  99. if os.access(os.path.join(ud.moddir, '.hg'), os.R_OK):
  100. updatecmd = self._buildhgcommand(ud, d, "pull")
  101. logger.info("Update " + loc)
  102. # update sources there
  103. os.chdir(ud.moddir)
  104. logger.debug(1, "Running %s", updatecmd)
  105. runfetchcmd(updatecmd, d)
  106. else:
  107. fetchcmd = self._buildhgcommand(ud, d, "fetch")
  108. logger.info("Fetch " + loc)
  109. # check out sources there
  110. bb.mkdirhier(ud.pkgdir)
  111. os.chdir(ud.pkgdir)
  112. logger.debug(1, "Running %s", fetchcmd)
  113. runfetchcmd(fetchcmd, d)
  114. # Even when we clone (fetch), we still need to update as hg's clone
  115. # won't checkout the specified revision if its on a branch
  116. updatecmd = self._buildhgcommand(ud, d, "update")
  117. os.chdir(ud.moddir)
  118. logger.debug(1, "Running %s", updatecmd)
  119. runfetchcmd(updatecmd, d)
  120. scmdata = ud.parm.get("scmdata", "")
  121. if scmdata == "keep":
  122. tar_flags = ""
  123. else:
  124. tar_flags = "--exclude '.hg' --exclude '.hgrags'"
  125. os.chdir(ud.pkgdir)
  126. try:
  127. runfetchcmd("tar %s -czf %s %s" % (tar_flags, ud.localpath, ud.module), d)
  128. except:
  129. t, v, tb = sys.exc_info()
  130. try:
  131. os.unlink(ud.localpath)
  132. except OSError:
  133. pass
  134. raise t, v, tb
  135. def supports_srcrev(self):
  136. return True
  137. def _latest_revision(self, url, ud, d):
  138. """
  139. Compute tip revision for the url
  140. """
  141. output = runfetchcmd(self._buildhgcommand(ud, d, "info"), d)
  142. return output.strip()
  143. def _build_revision(self, url, ud, d):
  144. return ud.revision
  145. def _revision_key(self, url, ud, d):
  146. """
  147. Return a unique key for the url
  148. """
  149. return "hg:" + ud.moddir