hg.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 supports_checksum(self, urldata):
  42. """
  43. Don't require checksums for local archives created from
  44. repository checkouts.
  45. """
  46. return False
  47. def urldata_init(self, ud, d):
  48. """
  49. init hg specific variable within url data
  50. """
  51. if not "module" in ud.parm:
  52. raise MissingParameterError('module', ud.url)
  53. ud.module = ud.parm["module"]
  54. # Create paths to mercurial checkouts
  55. relpath = self._strip_leading_slashes(ud.path)
  56. ud.pkgdir = os.path.join(data.expand('${HGDIR}', d), ud.host, relpath)
  57. ud.moddir = os.path.join(ud.pkgdir, ud.module)
  58. ud.setup_revisons(d)
  59. if 'rev' in ud.parm:
  60. ud.revision = ud.parm['rev']
  61. elif not ud.revision:
  62. ud.revision = self.latest_revision(ud, d)
  63. ud.localfile = ud.moddir
  64. ud.basecmd = data.getVar("FETCHCMD_hg", d, True) or "/usr/bin/env hg"
  65. def need_update(self, ud, d):
  66. revTag = ud.parm.get('rev', 'tip')
  67. if revTag == "tip":
  68. return True
  69. if not os.path.exists(ud.localpath):
  70. return True
  71. return False
  72. def _buildhgcommand(self, ud, d, command):
  73. """
  74. Build up an hg commandline based on ud
  75. command is "fetch", "update", "info"
  76. """
  77. proto = ud.parm.get('protocol', 'http')
  78. host = ud.host
  79. if proto == "file":
  80. host = "/"
  81. ud.host = "localhost"
  82. if not ud.user:
  83. hgroot = host + ud.path
  84. else:
  85. if ud.pswd:
  86. hgroot = ud.user + ":" + ud.pswd + "@" + host + ud.path
  87. else:
  88. hgroot = ud.user + "@" + host + ud.path
  89. if command == "info":
  90. return "%s identify -i %s://%s/%s" % (ud.basecmd, proto, hgroot, ud.module)
  91. options = [];
  92. # Don't specify revision for the fetch; clone the entire repo.
  93. # This avoids an issue if the specified revision is a tag, because
  94. # the tag actually exists in the specified revision + 1, so it won't
  95. # be available when used in any successive commands.
  96. if ud.revision and command != "fetch":
  97. options.append("-r %s" % ud.revision)
  98. if command == "fetch":
  99. if ud.user and ud.pswd:
  100. cmd = "%s --config auth.default.prefix=* --config auth.default.username=%s --config auth.default.password=%s --config \"auth.default.schemes=%s\" clone %s %s://%s/%s %s" % (ud.basecmd, ud.user, ud.pswd, proto, " ".join(options), proto, hgroot, ud.module, ud.module)
  101. else:
  102. cmd = "%s clone %s %s://%s/%s %s" % (ud.basecmd, " ".join(options), proto, hgroot, ud.module, ud.module)
  103. elif command == "pull":
  104. # do not pass options list; limiting pull to rev causes the local
  105. # repo not to contain it and immediately following "update" command
  106. # will crash
  107. if ud.user and ud.pswd:
  108. cmd = "%s --config auth.default.prefix=* --config auth.default.username=%s --config auth.default.password=%s --config \"auth.default.schemes=%s\" pull" % (ud.basecmd, ud.user, ud.pswd, proto)
  109. else:
  110. cmd = "%s pull" % (ud.basecmd)
  111. elif command == "update":
  112. if ud.user and ud.pswd:
  113. cmd = "%s --config auth.default.prefix=* --config auth.default.username=%s --config auth.default.password=%s --config \"auth.default.schemes=%s\" update -C %s" % (ud.basecmd, ud.user, ud.pswd, proto, " ".join(options))
  114. else:
  115. cmd = "%s update -C %s" % (ud.basecmd, " ".join(options))
  116. else:
  117. raise FetchError("Invalid hg command %s" % command, ud.url)
  118. return cmd
  119. def download(self, ud, d):
  120. """Fetch url"""
  121. logger.debug(2, "Fetch: checking for module directory '" + ud.moddir + "'")
  122. if os.access(os.path.join(ud.moddir, '.hg'), os.R_OK):
  123. updatecmd = self._buildhgcommand(ud, d, "pull")
  124. logger.info("Update " + ud.url)
  125. # update sources there
  126. os.chdir(ud.moddir)
  127. logger.debug(1, "Running %s", updatecmd)
  128. bb.fetch2.check_network_access(d, updatecmd, ud.url)
  129. runfetchcmd(updatecmd, d)
  130. else:
  131. fetchcmd = self._buildhgcommand(ud, d, "fetch")
  132. logger.info("Fetch " + ud.url)
  133. # check out sources there
  134. bb.utils.mkdirhier(ud.pkgdir)
  135. os.chdir(ud.pkgdir)
  136. logger.debug(1, "Running %s", fetchcmd)
  137. bb.fetch2.check_network_access(d, fetchcmd, ud.url)
  138. runfetchcmd(fetchcmd, d)
  139. # Even when we clone (fetch), we still need to update as hg's clone
  140. # won't checkout the specified revision if its on a branch
  141. updatecmd = self._buildhgcommand(ud, d, "update")
  142. os.chdir(ud.moddir)
  143. logger.debug(1, "Running %s", updatecmd)
  144. runfetchcmd(updatecmd, d)
  145. def clean(self, ud, d):
  146. """ Clean the hg dir """
  147. bb.utils.remove(ud.localpath, True)
  148. def supports_srcrev(self):
  149. return True
  150. def _latest_revision(self, ud, d, name):
  151. """
  152. Compute tip revision for the url
  153. """
  154. bb.fetch2.check_network_access(d, self._buildhgcommand(ud, d, "info"))
  155. output = runfetchcmd(self._buildhgcommand(ud, d, "info"), d)
  156. return output.strip()
  157. def _build_revision(self, ud, d, name):
  158. return ud.revision
  159. def _revision_key(self, ud, d, name):
  160. """
  161. Return a unique key for the url
  162. """
  163. return "hg:" + ud.moddir
  164. def localpath(self, ud, d):
  165. return ud.moddir
  166. def unpack(self, ud, destdir, d):
  167. """
  168. Make a local clone or export for the url
  169. """
  170. revflag = "-r %s" % ud.revision
  171. subdir = ud.parm.get("destsuffix", ud.module)
  172. codir = "%s/%s" % (destdir, subdir)
  173. scmdata = ud.parm.get("scmdata", "")
  174. if scmdata != "nokeep":
  175. if not os.access(os.path.join(codir, '.hg'), os.R_OK):
  176. logger.debug(2, "Unpack: creating new hg repository in '" + codir + "'")
  177. runfetchcmd("%s init %s" % (ud.basecmd, codir), d)
  178. logger.debug(2, "Unpack: updating source in '" + codir + "'")
  179. os.chdir(codir)
  180. runfetchcmd("%s pull %s" % (ud.basecmd, ud.moddir), d)
  181. runfetchcmd("%s up -C %s" % (ud.basecmd, revflag), d)
  182. else:
  183. logger.debug(2, "Unpack: extracting source to '" + codir + "'")
  184. os.chdir(ud.moddir)
  185. runfetchcmd("%s archive -t files %s %s" % (ud.basecmd, revflag, codir), d)