svn.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. """
  2. BitBake 'Fetch' implementation for svn.
  3. """
  4. # Copyright (C) 2003, 2004 Chris Larson
  5. # Copyright (C) 2004 Marcin Juszkiewicz
  6. #
  7. # SPDX-License-Identifier: GPL-2.0-only
  8. #
  9. # Based on functions from the base bb module, Copyright 2003 Holger Schurig
  10. import os
  11. import bb
  12. import re
  13. from bb.fetch2 import FetchMethod
  14. from bb.fetch2 import FetchError
  15. from bb.fetch2 import MissingParameterError
  16. from bb.fetch2 import runfetchcmd
  17. from bb.fetch2 import logger
  18. class Svn(FetchMethod):
  19. """Class to fetch a module or modules from svn repositories"""
  20. def supports(self, ud, d):
  21. """
  22. Check to see if a given url can be fetched with svn.
  23. """
  24. return ud.type in ['svn']
  25. def urldata_init(self, ud, d):
  26. """
  27. init svn specific variable within url data
  28. """
  29. if not "module" in ud.parm:
  30. raise MissingParameterError('module', ud.url)
  31. ud.basecmd = d.getVar("FETCHCMD_svn") or "/usr/bin/env svn --non-interactive --trust-server-cert"
  32. ud.module = ud.parm["module"]
  33. if not "path_spec" in ud.parm:
  34. ud.path_spec = ud.module
  35. else:
  36. ud.path_spec = ud.parm["path_spec"]
  37. # Create paths to svn checkouts
  38. svndir = d.getVar("SVNDIR") or (d.getVar("DL_DIR") + "/svn")
  39. relpath = self._strip_leading_slashes(ud.path)
  40. ud.pkgdir = os.path.join(svndir, ud.host, relpath)
  41. ud.moddir = os.path.join(ud.pkgdir, ud.path_spec)
  42. # Protects the repository from concurrent updates, e.g. from two
  43. # recipes fetching different revisions at the same time
  44. ud.svnlock = os.path.join(ud.pkgdir, "svn.lock")
  45. ud.setup_revisions(d)
  46. if 'rev' in ud.parm:
  47. ud.revision = ud.parm['rev']
  48. ud.localfile = d.expand('%s_%s_%s_%s_.tar.gz' % (ud.module.replace('/', '.'), ud.host, ud.path.replace('/', '.'), ud.revision))
  49. def _buildsvncommand(self, ud, d, command):
  50. """
  51. Build up an svn commandline based on ud
  52. command is "fetch", "update", "info"
  53. """
  54. proto = ud.parm.get('protocol', 'svn')
  55. svn_ssh = None
  56. if proto == "svn+ssh" and "ssh" in ud.parm:
  57. svn_ssh = ud.parm["ssh"]
  58. svnroot = ud.host + ud.path
  59. options = []
  60. options.append("--no-auth-cache")
  61. if ud.user:
  62. options.append("--username %s" % ud.user)
  63. if ud.pswd:
  64. options.append("--password %s" % ud.pswd)
  65. if command == "info":
  66. svncmd = "%s info %s %s://%s/%s/" % (ud.basecmd, " ".join(options), proto, svnroot, ud.module)
  67. elif command == "log1":
  68. svncmd = "%s log --limit 1 %s %s://%s/%s/" % (ud.basecmd, " ".join(options), proto, svnroot, ud.module)
  69. else:
  70. suffix = ""
  71. # externals may be either 'allowed' or 'nowarn', but not both. Allowed
  72. # will not issue a warning, but will log to the debug buffer what has likely
  73. # been downloaded by SVN.
  74. if not ("externals" in ud.parm and ud.parm["externals"] == "allowed"):
  75. options.append("--ignore-externals")
  76. if ud.revision:
  77. options.append("-r %s" % ud.revision)
  78. suffix = "@%s" % (ud.revision)
  79. if command == "fetch":
  80. transportuser = ud.parm.get("transportuser", "")
  81. svncmd = "%s co %s %s://%s%s/%s%s %s" % (ud.basecmd, " ".join(options), proto, transportuser, svnroot, ud.module, suffix, ud.path_spec)
  82. elif command == "update":
  83. svncmd = "%s update %s" % (ud.basecmd, " ".join(options))
  84. else:
  85. raise FetchError("Invalid svn command %s" % command, ud.url)
  86. if svn_ssh:
  87. svncmd = "SVN_SSH=\"%s\" %s" % (svn_ssh, svncmd)
  88. return svncmd
  89. def download(self, ud, d):
  90. """Fetch url"""
  91. logger.debug(2, "Fetch: checking for module directory '" + ud.moddir + "'")
  92. lf = bb.utils.lockfile(ud.svnlock)
  93. try:
  94. if os.access(os.path.join(ud.moddir, '.svn'), os.R_OK):
  95. svncmd = self._buildsvncommand(ud, d, "update")
  96. logger.info("Update " + ud.url)
  97. # We need to attempt to run svn upgrade first in case its an older working format
  98. try:
  99. runfetchcmd(ud.basecmd + " upgrade", d, workdir=ud.moddir)
  100. except FetchError:
  101. pass
  102. logger.debug(1, "Running %s", svncmd)
  103. bb.fetch2.check_network_access(d, svncmd, ud.url)
  104. runfetchcmd(svncmd, d, workdir=ud.moddir)
  105. else:
  106. svncmd = self._buildsvncommand(ud, d, "fetch")
  107. logger.info("Fetch " + ud.url)
  108. # check out sources there
  109. bb.utils.mkdirhier(ud.pkgdir)
  110. logger.debug(1, "Running %s", svncmd)
  111. bb.fetch2.check_network_access(d, svncmd, ud.url)
  112. runfetchcmd(svncmd, d, workdir=ud.pkgdir)
  113. if not ("externals" in ud.parm and ud.parm["externals"] == "nowarn"):
  114. # Warn the user if this had externals (won't catch them all)
  115. output = runfetchcmd("svn propget svn:externals || true", d, workdir=ud.moddir)
  116. if output:
  117. if "--ignore-externals" in svncmd.split():
  118. bb.warn("%s contains svn:externals." % ud.url)
  119. bb.warn("These should be added to the recipe SRC_URI as necessary.")
  120. bb.warn("svn fetch has ignored externals:\n%s" % output)
  121. bb.warn("To disable this warning add ';externals=nowarn' to the url.")
  122. else:
  123. bb.debug(1, "svn repository has externals:\n%s" % output)
  124. scmdata = ud.parm.get("scmdata", "")
  125. if scmdata == "keep":
  126. tar_flags = ""
  127. else:
  128. tar_flags = "--exclude='.svn'"
  129. # tar them up to a defined filename
  130. runfetchcmd("tar %s -czf %s %s" % (tar_flags, ud.localpath, ud.path_spec), d,
  131. cleanup=[ud.localpath], workdir=ud.pkgdir)
  132. finally:
  133. bb.utils.unlockfile(lf)
  134. def clean(self, ud, d):
  135. """ Clean SVN specific files and dirs """
  136. bb.utils.remove(ud.localpath)
  137. bb.utils.remove(ud.moddir, True)
  138. def supports_srcrev(self):
  139. return True
  140. def _revision_key(self, ud, d, name):
  141. """
  142. Return a unique key for the url
  143. """
  144. return "svn:" + ud.moddir
  145. def _latest_revision(self, ud, d, name):
  146. """
  147. Return the latest upstream revision number
  148. """
  149. bb.fetch2.check_network_access(d, self._buildsvncommand(ud, d, "log1"), ud.url)
  150. output = runfetchcmd("LANG=C LC_ALL=C " + self._buildsvncommand(ud, d, "log1"), d, True)
  151. # skip the first line, as per output of svn log
  152. # then we expect the revision on the 2nd line
  153. revision = re.search('^r([0-9]*)', output.splitlines()[1]).group(1)
  154. return revision
  155. def sortable_revision(self, ud, d, name):
  156. """
  157. Return a sortable revision number which in our case is the revision number
  158. """
  159. return False, self._build_revision(ud, d)
  160. def _build_revision(self, ud, d):
  161. return ud.revision