hg.py 9.7 KB

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