PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/bitbake/lib/bb/fetch2/hg.py

https://gitlab.com/actawithamana/poky
Python | 270 lines | 261 code | 0 blank | 9 comment | 2 complexity | eec6aed0690d2003c801efd666e6af5d MD5 | raw file
  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. runfetchcmd("tar -xzf %s" % (ud.fullmirror), d, workdir=ud.pkgdir)
  146. if os.access(os.path.join(ud.moddir, '.hg'), os.R_OK):
  147. # Found the source, check whether need pull
  148. updatecmd = self._buildhgcommand(ud, d, "update")
  149. logger.debug(1, "Running %s", updatecmd)
  150. try:
  151. runfetchcmd(updatecmd, d, workdir=ud.moddir)
  152. except bb.fetch2.FetchError:
  153. # Runnning pull in the repo
  154. pullcmd = self._buildhgcommand(ud, d, "pull")
  155. logger.info("Pulling " + ud.url)
  156. # update sources there
  157. logger.debug(1, "Running %s", pullcmd)
  158. bb.fetch2.check_network_access(d, pullcmd, ud.url)
  159. runfetchcmd(pullcmd, d, workdir=ud.moddir)
  160. try:
  161. os.unlink(ud.fullmirror)
  162. except OSError as exc:
  163. if exc.errno != errno.ENOENT:
  164. raise
  165. # No source found, clone it.
  166. if not os.path.exists(ud.moddir):
  167. fetchcmd = self._buildhgcommand(ud, d, "fetch")
  168. logger.info("Fetch " + ud.url)
  169. # check out sources there
  170. bb.utils.mkdirhier(ud.pkgdir)
  171. logger.debug(1, "Running %s", fetchcmd)
  172. bb.fetch2.check_network_access(d, fetchcmd, ud.url)
  173. runfetchcmd(fetchcmd, d, workdir=ud.pkgdir)
  174. # Even when we clone (fetch), we still need to update as hg's clone
  175. # won't checkout the specified revision if its on a branch
  176. updatecmd = self._buildhgcommand(ud, d, "update")
  177. logger.debug(1, "Running %s", updatecmd)
  178. runfetchcmd(updatecmd, d, workdir=ud.moddir)
  179. def clean(self, ud, d):
  180. """ Clean the hg dir """
  181. bb.utils.remove(ud.localpath, True)
  182. bb.utils.remove(ud.fullmirror)
  183. bb.utils.remove(ud.fullmirror + ".done")
  184. def supports_srcrev(self):
  185. return True
  186. def _latest_revision(self, ud, d, name):
  187. """
  188. Compute tip revision for the url
  189. """
  190. bb.fetch2.check_network_access(d, self._buildhgcommand(ud, d, "info"))
  191. output = runfetchcmd(self._buildhgcommand(ud, d, "info"), d)
  192. return output.strip()
  193. def _build_revision(self, ud, d, name):
  194. return ud.revision
  195. def _revision_key(self, ud, d, name):
  196. """
  197. Return a unique key for the url
  198. """
  199. return "hg:" + ud.moddir
  200. def build_mirror_data(self, ud, d):
  201. # Generate a mirror tarball if needed
  202. if ud.write_tarballs == "1" and not os.path.exists(ud.fullmirror):
  203. # it's possible that this symlink points to read-only filesystem with PREMIRROR
  204. if os.path.islink(ud.fullmirror):
  205. os.unlink(ud.fullmirror)
  206. logger.info("Creating tarball of hg repository")
  207. runfetchcmd("tar -czf %s %s" % (ud.fullmirror, ud.module), d, workdir=ud.pkgdir)
  208. runfetchcmd("touch %s.done" % (ud.fullmirror), d, workdir=ud.pkgdir)
  209. def localpath(self, ud, d):
  210. return ud.pkgdir
  211. def unpack(self, ud, destdir, d):
  212. """
  213. Make a local clone or export for the url
  214. """
  215. revflag = "-r %s" % ud.revision
  216. subdir = ud.parm.get("destsuffix", ud.module)
  217. codir = "%s/%s" % (destdir, subdir)
  218. scmdata = ud.parm.get("scmdata", "")
  219. if scmdata != "nokeep":
  220. if not os.access(os.path.join(codir, '.hg'), os.R_OK):
  221. logger.debug(2, "Unpack: creating new hg repository in '" + codir + "'")
  222. runfetchcmd("%s init %s" % (ud.basecmd, codir), d)
  223. logger.debug(2, "Unpack: updating source in '" + codir + "'")
  224. runfetchcmd("%s pull %s" % (ud.basecmd, ud.moddir), d, workdir=codir)
  225. runfetchcmd("%s up -C %s" % (ud.basecmd, revflag), d, workdir=codir)
  226. else:
  227. logger.debug(2, "Unpack: extracting source to '" + codir + "'")
  228. runfetchcmd("%s archive -t files %s %s" % (ud.basecmd, revflag, codir), d, workdir=ud.moddir)