hg.py 10 KB

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