hg.py 9.6 KB

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