svn.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 svn.
  5. """
  6. # Copyright (C) 2003, 2004 Chris Larson
  7. # Copyright (C) 2004 Marcin Juszkiewicz
  8. #
  9. # SPDX-License-Identifier: GPL-2.0-only
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License version 2 as
  13. # published by the Free Software Foundation.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License along
  21. # with this program; if not, write to the Free Software Foundation, Inc.,
  22. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. #
  24. # Based on functions from the base bb module, Copyright 2003 Holger Schurig
  25. import os
  26. import sys
  27. import logging
  28. import bb
  29. import re
  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 Svn(FetchMethod):
  36. """Class to fetch a module or modules from svn repositories"""
  37. def supports(self, ud, d):
  38. """
  39. Check to see if a given url can be fetched with svn.
  40. """
  41. return ud.type in ['svn']
  42. def urldata_init(self, ud, d):
  43. """
  44. init svn specific variable within url data
  45. """
  46. if not "module" in ud.parm:
  47. raise MissingParameterError('module', ud.url)
  48. ud.basecmd = d.getVar("FETCHCMD_svn") or "/usr/bin/env svn --non-interactive --trust-server-cert"
  49. ud.module = ud.parm["module"]
  50. if not "path_spec" in ud.parm:
  51. ud.path_spec = ud.module
  52. else:
  53. ud.path_spec = ud.parm["path_spec"]
  54. # Create paths to svn checkouts
  55. svndir = d.getVar("SVNDIR") or (d.getVar("DL_DIR") + "/svn")
  56. relpath = self._strip_leading_slashes(ud.path)
  57. ud.pkgdir = os.path.join(svndir, ud.host, relpath)
  58. ud.moddir = os.path.join(ud.pkgdir, ud.module)
  59. # Protects the repository from concurrent updates, e.g. from two
  60. # recipes fetching different revisions at the same time
  61. ud.svnlock = os.path.join(ud.pkgdir, "svn.lock")
  62. ud.setup_revisions(d)
  63. if 'rev' in ud.parm:
  64. ud.revision = ud.parm['rev']
  65. ud.localfile = d.expand('%s_%s_%s_%s_.tar.gz' % (ud.module.replace('/', '.'), ud.host, ud.path.replace('/', '.'), ud.revision))
  66. def _buildsvncommand(self, ud, d, command):
  67. """
  68. Build up an svn commandline based on ud
  69. command is "fetch", "update", "info"
  70. """
  71. proto = ud.parm.get('protocol', 'svn')
  72. svn_ssh = None
  73. if proto == "svn+ssh" and "ssh" in ud.parm:
  74. svn_ssh = ud.parm["ssh"]
  75. svnroot = ud.host + ud.path
  76. options = []
  77. options.append("--no-auth-cache")
  78. if ud.user:
  79. options.append("--username %s" % ud.user)
  80. if ud.pswd:
  81. options.append("--password %s" % ud.pswd)
  82. if command == "info":
  83. svncmd = "%s info %s %s://%s/%s/" % (ud.basecmd, " ".join(options), proto, svnroot, ud.module)
  84. elif command == "log1":
  85. svncmd = "%s log --limit 1 %s %s://%s/%s/" % (ud.basecmd, " ".join(options), proto, svnroot, ud.module)
  86. else:
  87. suffix = ""
  88. if ud.revision:
  89. options.append("-r %s" % ud.revision)
  90. suffix = "@%s" % (ud.revision)
  91. if command == "fetch":
  92. transportuser = ud.parm.get("transportuser", "")
  93. svncmd = "%s co %s %s://%s%s/%s%s %s" % (ud.basecmd, " ".join(options), proto, transportuser, svnroot, ud.module, suffix, ud.path_spec)
  94. elif command == "update":
  95. svncmd = "%s update %s" % (ud.basecmd, " ".join(options))
  96. else:
  97. raise FetchError("Invalid svn command %s" % command, ud.url)
  98. if svn_ssh:
  99. svncmd = "SVN_SSH=\"%s\" %s" % (svn_ssh, svncmd)
  100. return svncmd
  101. def download(self, ud, d):
  102. """Fetch url"""
  103. logger.debug(2, "Fetch: checking for module directory '" + ud.moddir + "'")
  104. lf = bb.utils.lockfile(ud.svnlock)
  105. try:
  106. if os.access(os.path.join(ud.moddir, '.svn'), os.R_OK):
  107. svnupdatecmd = self._buildsvncommand(ud, d, "update")
  108. logger.info("Update " + ud.url)
  109. # We need to attempt to run svn upgrade first in case its an older working format
  110. try:
  111. runfetchcmd(ud.basecmd + " upgrade", d, workdir=ud.moddir)
  112. except FetchError:
  113. pass
  114. logger.debug(1, "Running %s", svnupdatecmd)
  115. bb.fetch2.check_network_access(d, svnupdatecmd, ud.url)
  116. runfetchcmd(svnupdatecmd, d, workdir=ud.moddir)
  117. else:
  118. svnfetchcmd = self._buildsvncommand(ud, d, "fetch")
  119. logger.info("Fetch " + ud.url)
  120. # check out sources there
  121. bb.utils.mkdirhier(ud.pkgdir)
  122. logger.debug(1, "Running %s", svnfetchcmd)
  123. bb.fetch2.check_network_access(d, svnfetchcmd, ud.url)
  124. runfetchcmd(svnfetchcmd, d, workdir=ud.pkgdir)
  125. scmdata = ud.parm.get("scmdata", "")
  126. if scmdata == "keep":
  127. tar_flags = ""
  128. else:
  129. tar_flags = "--exclude='.svn'"
  130. # tar them up to a defined filename
  131. runfetchcmd("tar %s -czf %s %s" % (tar_flags, ud.localpath, ud.path_spec), d,
  132. cleanup=[ud.localpath], workdir=ud.pkgdir)
  133. finally:
  134. bb.utils.unlockfile(lf)
  135. def clean(self, ud, d):
  136. """ Clean SVN specific files and dirs """
  137. bb.utils.remove(ud.localpath)
  138. bb.utils.remove(ud.moddir, True)
  139. def supports_srcrev(self):
  140. return True
  141. def _revision_key(self, ud, d, name):
  142. """
  143. Return a unique key for the url
  144. """
  145. return "svn:" + ud.moddir
  146. def _latest_revision(self, ud, d, name):
  147. """
  148. Return the latest upstream revision number
  149. """
  150. bb.fetch2.check_network_access(d, self._buildsvncommand(ud, d, "log1"), ud.url)
  151. output = runfetchcmd("LANG=C LC_ALL=C " + self._buildsvncommand(ud, d, "log1"), d, True)
  152. # skip the first line, as per output of svn log
  153. # then we expect the revision on the 2nd line
  154. revision = re.search('^r([0-9]*)', output.splitlines()[1]).group(1)
  155. return revision
  156. def sortable_revision(self, ud, d, name):
  157. """
  158. Return a sortable revision number which in our case is the revision number
  159. """
  160. return False, self._build_revision(ud, d)
  161. def _build_revision(self, ud, d):
  162. return ud.revision