hg.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 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("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. elif not ud.revision:
  55. ud.revision = self.latest_revision(ud.url, ud, d)
  56. ud.localfile = data.expand('%s_%s_%s_%s.tar.gz' % (ud.module.replace('/', '.'), ud.host, ud.path.replace('/', '.'), ud.revision), d)
  57. def forcefetch(self, url, ud, d):
  58. revTag = ud.parm.get('rev', 'tip')
  59. return revTag == "tip"
  60. def localpath(self, url, ud, d):
  61. return os.path.join(data.getVar("DL_DIR", d, True), ud.localfile)
  62. def _buildhgcommand(self, ud, d, command):
  63. """
  64. Build up an hg commandline based on ud
  65. command is "fetch", "update", "info"
  66. """
  67. basecmd = data.expand('${FETCHCMD_hg}', d)
  68. proto = ud.parm.get('proto', 'http')
  69. host = ud.host
  70. if proto == "file":
  71. host = "/"
  72. ud.host = "localhost"
  73. if not ud.user:
  74. hgroot = host + ud.path
  75. else:
  76. hgroot = ud.user + "@" + host + ud.path
  77. if command is "info":
  78. return "%s identify -i %s://%s/%s" % (basecmd, proto, hgroot, ud.module)
  79. options = [];
  80. if ud.revision:
  81. options.append("-r %s" % ud.revision)
  82. if command is "fetch":
  83. cmd = "%s clone %s %s://%s/%s %s" % (basecmd, " ".join(options), proto, hgroot, ud.module, ud.module)
  84. elif command is "pull":
  85. # do not pass options list; limiting pull to rev causes the local
  86. # repo not to contain it and immediately following "update" command
  87. # will crash
  88. cmd = "%s pull" % (basecmd)
  89. elif command is "update":
  90. cmd = "%s update -C %s" % (basecmd, " ".join(options))
  91. else:
  92. raise FetchError("Invalid hg command %s" % command)
  93. return cmd
  94. def download(self, loc, ud, d):
  95. """Fetch url"""
  96. logger.debug(2, "Fetch: checking for module directory '" + ud.moddir + "'")
  97. if os.access(os.path.join(ud.moddir, '.hg'), os.R_OK):
  98. updatecmd = self._buildhgcommand(ud, d, "pull")
  99. logger.info("Update " + loc)
  100. # update sources there
  101. os.chdir(ud.moddir)
  102. logger.debug(1, "Running %s", updatecmd)
  103. bb.fetch2.check_network_access(d, updatecmd)
  104. runfetchcmd(updatecmd, d)
  105. else:
  106. fetchcmd = self._buildhgcommand(ud, d, "fetch")
  107. logger.info("Fetch " + loc)
  108. # check out sources there
  109. bb.mkdirhier(ud.pkgdir)
  110. os.chdir(ud.pkgdir)
  111. logger.debug(1, "Running %s", fetchcmd)
  112. bb.fetch2.check_network_access(d, 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, name):
  138. """
  139. Compute tip revision for the url
  140. """
  141. bb.fetch2.check_network_access(d, self._buildhgcommand(ud, d, "info"))
  142. output = runfetchcmd(self._buildhgcommand(ud, d, "info"), d)
  143. return output.strip()
  144. def _build_revision(self, url, ud, d):
  145. return ud.revision
  146. def _revision_key(self, url, ud, d, name):
  147. """
  148. Return a unique key for the url
  149. """
  150. return "hg:" + ud.moddir