hg.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. import errno
  29. from bb import data
  30. from bb.fetch2 import FetchMethod
  31. from bb.fetch2 import FetchError
  32. from bb.fetch2 import MissingParameterError
  33. from bb.fetch2 import runfetchcmd
  34. from bb.fetch2 import logger
  35. class Hg(FetchMethod):
  36. """Class to fetch from mercurial repositories"""
  37. def supports(self, ud, d):
  38. """
  39. Check to see if a given url can be fetched with mercurial.
  40. """
  41. return ud.type in ['hg']
  42. def supports_checksum(self, urldata):
  43. """
  44. Don't require checksums for local archives created from
  45. repository checkouts.
  46. """
  47. return False
  48. def urldata_init(self, ud, d):
  49. """
  50. init hg specific variable within url data
  51. """
  52. if not "module" in ud.parm:
  53. raise MissingParameterError('module', ud.url)
  54. ud.module = ud.parm["module"]
  55. if 'protocol' in ud.parm:
  56. ud.proto = ud.parm['protocol']
  57. elif not ud.host:
  58. ud.proto = 'file'
  59. else:
  60. ud.proto = "hg"
  61. ud.setup_revisons(d)
  62. if 'rev' in ud.parm:
  63. ud.revision = ud.parm['rev']
  64. elif not ud.revision:
  65. ud.revision = self.latest_revision(ud, d)
  66. # Create paths to mercurial checkouts
  67. hgsrcname = '%s_%s_%s' % (ud.module.replace('/', '.'), \
  68. ud.host, ud.path.replace('/', '.'))
  69. ud.mirrortarball = 'hg_%s.tar.gz' % hgsrcname
  70. ud.fullmirror = os.path.join(d.getVar("DL_DIR", True), ud.mirrortarball)
  71. hgdir = d.getVar("HGDIR", True) or (d.getVar("DL_DIR", True) + "/hg/")
  72. ud.pkgdir = os.path.join(hgdir, hgsrcname)
  73. ud.moddir = os.path.join(ud.pkgdir, ud.module)
  74. ud.localfile = ud.moddir
  75. ud.basecmd = data.getVar("FETCHCMD_hg", d, True) or "/usr/bin/env hg"
  76. ud.write_tarballs = d.getVar("BB_GENERATE_MIRROR_TARBALLS", True)
  77. def need_update(self, ud, d):
  78. revTag = ud.parm.get('rev', 'tip')
  79. if revTag == "tip":
  80. return True
  81. if not os.path.exists(ud.localpath):
  82. return True
  83. return False
  84. def try_premirror(self, ud, d):
  85. # If we don't do this, updating an existing checkout with only premirrors
  86. # is not possible
  87. if d.getVar("BB_FETCH_PREMIRRORONLY", True) is not None:
  88. return True
  89. if os.path.exists(ud.moddir):
  90. return False
  91. return True
  92. def _buildhgcommand(self, ud, d, command):
  93. """
  94. Build up an hg commandline based on ud
  95. command is "fetch", "update", "info"
  96. """
  97. proto = ud.parm.get('protocol', 'http')
  98. host = ud.host
  99. if proto == "file":
  100. host = "/"
  101. ud.host = "localhost"
  102. if not ud.user:
  103. hgroot = host + ud.path
  104. else:
  105. if ud.pswd:
  106. hgroot = ud.user + ":" + ud.pswd + "@" + host + ud.path
  107. else:
  108. hgroot = ud.user + "@" + host + ud.path
  109. if command == "info":
  110. return "%s identify -i %s://%s/%s" % (ud.basecmd, proto, hgroot, ud.module)
  111. options = [];
  112. # Don't specify revision for the fetch; clone the entire repo.
  113. # This avoids an issue if the specified revision is a tag, because
  114. # the tag actually exists in the specified revision + 1, so it won't
  115. # be available when used in any successive commands.
  116. if ud.revision and command != "fetch":
  117. options.append("-r %s" % ud.revision)
  118. if command == "fetch":
  119. if ud.user and ud.pswd:
  120. 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)
  121. else:
  122. cmd = "%s clone %s %s://%s/%s %s" % (ud.basecmd, " ".join(options), proto, hgroot, ud.module, ud.module)
  123. elif command == "pull":
  124. # do not pass options list; limiting pull to rev causes the local
  125. # repo not to contain it and immediately following "update" command
  126. # will crash
  127. if ud.user and ud.pswd:
  128. 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)
  129. else:
  130. cmd = "%s pull" % (ud.basecmd)
  131. elif command == "update":
  132. if ud.user and ud.pswd:
  133. 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))
  134. else:
  135. cmd = "%s update -C %s" % (ud.basecmd, " ".join(options))
  136. else:
  137. raise FetchError("Invalid hg command %s" % command, ud.url)
  138. return cmd
  139. def download(self, ud, d):
  140. """Fetch url"""
  141. logger.debug(2, "Fetch: checking for module directory '" + ud.moddir + "'")
  142. # If the checkout doesn't exist and the mirror tarball does, extract it
  143. if not os.path.exists(ud.pkgdir) and os.path.exists(ud.fullmirror):
  144. bb.utils.mkdirhier(ud.pkgdir)
  145. os.chdir(ud.pkgdir)
  146. runfetchcmd("tar -xzf %s" % (ud.fullmirror), d)
  147. if os.access(os.path.join(ud.moddir, '.hg'), os.R_OK):
  148. # Found the source, check whether need pull
  149. updatecmd = self._buildhgcommand(ud, d, "update")
  150. os.chdir(ud.moddir)
  151. logger.debug(1, "Running %s", updatecmd)
  152. try:
  153. runfetchcmd(updatecmd, d)
  154. except bb.fetch2.FetchError:
  155. # Runnning pull in the repo
  156. pullcmd = self._buildhgcommand(ud, d, "pull")
  157. logger.info("Pulling " + ud.url)
  158. # update sources there
  159. os.chdir(ud.moddir)
  160. logger.debug(1, "Running %s", pullcmd)
  161. bb.fetch2.check_network_access(d, pullcmd, ud.url)
  162. runfetchcmd(pullcmd, d)
  163. try:
  164. os.unlink(ud.fullmirror)
  165. except OSError as exc:
  166. if exc.errno != errno.ENOENT:
  167. raise
  168. # No source found, clone it.
  169. if not os.path.exists(ud.moddir):
  170. fetchcmd = self._buildhgcommand(ud, d, "fetch")
  171. logger.info("Fetch " + ud.url)
  172. # check out sources there
  173. bb.utils.mkdirhier(ud.pkgdir)
  174. os.chdir(ud.pkgdir)
  175. logger.debug(1, "Running %s", fetchcmd)
  176. bb.fetch2.check_network_access(d, fetchcmd, ud.url)
  177. runfetchcmd(fetchcmd, d)
  178. # Even when we clone (fetch), we still need to update as hg's clone
  179. # won't checkout the specified revision if its on a branch
  180. updatecmd = self._buildhgcommand(ud, d, "update")
  181. os.chdir(ud.moddir)
  182. logger.debug(1, "Running %s", updatecmd)
  183. runfetchcmd(updatecmd, d)
  184. def clean(self, ud, d):
  185. """ Clean the hg dir """
  186. bb.utils.remove(ud.localpath, True)
  187. bb.utils.remove(ud.fullmirror)
  188. bb.utils.remove(ud.fullmirror + ".done")
  189. def supports_srcrev(self):
  190. return True
  191. def _latest_revision(self, ud, d, name):
  192. """
  193. Compute tip revision for the url
  194. """
  195. bb.fetch2.check_network_access(d, self._buildhgcommand(ud, d, "info"))
  196. output = runfetchcmd(self._buildhgcommand(ud, d, "info"), d)
  197. return output.strip()
  198. def _build_revision(self, ud, d, name):
  199. return ud.revision
  200. def _revision_key(self, ud, d, name):
  201. """
  202. Return a unique key for the url
  203. """
  204. return "hg:" + ud.moddir
  205. def build_mirror_data(self, ud, d):
  206. # Generate a mirror tarball if needed
  207. if ud.write_tarballs == "1" and not os.path.exists(ud.fullmirror):
  208. # it's possible that this symlink points to read-only filesystem with PREMIRROR
  209. if os.path.islink(ud.fullmirror):
  210. os.unlink(ud.fullmirror)
  211. os.chdir(ud.pkgdir)
  212. logger.info("Creating tarball of hg repository")
  213. runfetchcmd("tar -czf %s %s" % (ud.fullmirror, ud.module), d)
  214. runfetchcmd("touch %s.done" % (ud.fullmirror), d)
  215. def localpath(self, ud, d):
  216. return ud.pkgdir
  217. def unpack(self, ud, destdir, d):
  218. """
  219. Make a local clone or export for the url
  220. """
  221. revflag = "-r %s" % ud.revision
  222. subdir = ud.parm.get("destsuffix", ud.module)
  223. codir = "%s/%s" % (destdir, subdir)
  224. scmdata = ud.parm.get("scmdata", "")
  225. if scmdata != "nokeep":
  226. if not os.access(os.path.join(codir, '.hg'), os.R_OK):
  227. logger.debug(2, "Unpack: creating new hg repository in '" + codir + "'")
  228. runfetchcmd("%s init %s" % (ud.basecmd, codir), d)
  229. logger.debug(2, "Unpack: updating source in '" + codir + "'")
  230. os.chdir(codir)
  231. runfetchcmd("%s pull %s" % (ud.basecmd, ud.moddir), d)
  232. runfetchcmd("%s up -C %s" % (ud.basecmd, revflag), d)
  233. else:
  234. logger.debug(2, "Unpack: extracting source to '" + codir + "'")
  235. os.chdir(ud.moddir)
  236. runfetchcmd("%s archive -t files %s %s" % (ud.basecmd, revflag, codir), d)