hg.py 10 KB

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